2
* Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
4
* This software is open source.
5
* See the bottom of this file for the licence.
10
import org.dom4j.Element;
11
import org.dom4j.ElementHandler;
12
import org.dom4j.ElementPath;
16
* <code>ElementStack</code> is used internally inside the {@link
17
* SAXContentHandler} to maintain a stack of {@link Element}instances. It opens
18
* an integration possibility allowing derivations to prune the tree when a node
22
* @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
23
* @version $Revision: 1.14 $
25
class ElementStack implements ElementPath {
26
/** stack of <code>Element</code> objects */
27
protected Element[] stack;
29
/** index of the item at the top of the stack or -1 if the stack is empty */
30
protected int lastElementIndex = -1;
32
private DispatchHandler handler = null;
34
public ElementStack() {
38
public ElementStack(int defaultCapacity) {
39
stack = new Element[defaultCapacity];
42
public void setDispatchHandler(DispatchHandler dispatchHandler) {
43
this.handler = dispatchHandler;
46
public DispatchHandler getDispatchHandler() {
51
* Peeks at the top element on the stack without changing the contents of
55
lastElementIndex = -1;
59
* Peeks at the top element on the stack without changing the contents of
62
* @return the current element on the stack
64
public Element peekElement() {
65
if (lastElementIndex < 0) {
69
return stack[lastElementIndex];
73
* Pops the element off the stack
75
* @return the element that has just been popped off the stack
77
public Element popElement() {
78
if (lastElementIndex < 0) {
82
return stack[lastElementIndex--];
86
* Pushes a new element onto the stack
91
public void pushElement(Element element) {
92
int length = stack.length;
94
if (++lastElementIndex >= length) {
95
reallocate(length * 2);
98
stack[lastElementIndex] = element;
102
* Reallocates the stack to the given size
107
protected void reallocate(int size) {
108
Element[] oldStack = stack;
109
stack = new Element[size];
110
System.arraycopy(oldStack, 0, stack, 0, oldStack.length);
113
// The ElementPath Interface
116
return lastElementIndex + 1;
119
public Element getElement(int depth) {
123
element = (Element) stack[depth];
124
} catch (ArrayIndexOutOfBoundsException e) {
131
public String getPath() {
132
if (handler == null) {
133
setDispatchHandler(new DispatchHandler());
136
return handler.getPath();
139
public Element getCurrent() {
140
return peekElement();
143
public void addHandler(String path, ElementHandler elementHandler) {
144
this.handler.addHandler(getHandlerPath(path), elementHandler);
147
public void removeHandler(String path) {
148
this.handler.removeHandler(getHandlerPath(path));
157
* @return true when an <code>ElementHandler</code> is registered for the
160
public boolean containsHandler(String path) {
161
return this.handler.containsHandler(path);
164
private String getHandlerPath(String path) {
167
if (this.handler == null) {
168
setDispatchHandler(new DispatchHandler());
171
if (path.startsWith("/")) {
173
} else if (getPath().equals("/")) {
174
handlerPath = getPath() + path;
176
handlerPath = getPath() + "/" + path;
184
* Redistribution and use of this software and associated documentation
185
* ("Software"), with or without modification, are permitted provided that the
186
* following conditions are met:
188
* 1. Redistributions of source code must retain copyright statements and
189
* notices. Redistributions must also contain a copy of this document.
191
* 2. Redistributions in binary form must reproduce the above copyright notice,
192
* this list of conditions and the following disclaimer in the documentation
193
* and/or other materials provided with the distribution.
195
* 3. The name "DOM4J" must not be used to endorse or promote products derived
196
* from this Software without prior written permission of MetaStuff, Ltd. For
197
* written permission, please contact dom4j-info@metastuff.com.
199
* 4. Products derived from this Software may not be called "DOM4J" nor may
200
* "DOM4J" appear in their names without prior written permission of MetaStuff,
201
* Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
203
* 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
205
* THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
206
* ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
207
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
208
* ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
209
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
210
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
211
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
212
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
213
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
214
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
215
* POSSIBILITY OF SUCH DAMAGE.
217
* Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.