|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
PruningElementStack.java | 0% | 0% | 0% | 0% |
|
1 | /* | |
2 | * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved. | |
3 | * | |
4 | * This software is open source. | |
5 | * See the bottom of this file for the licence. | |
6 | * | |
7 | * $Id: PruningElementStack.java,v 1.9 2004/06/25 08:03:37 maartenc Exp $ | |
8 | */ | |
9 | ||
10 | package org.dom4j.io; | |
11 | ||
12 | import org.dom4j.Element; | |
13 | import org.dom4j.ElementHandler; | |
14 | ||
15 | /** <p><code>PruningElementStack</code> is a stack of {@link Element} | |
16 | * instances which will prune the tree when a path expression is reached. | |
17 | * This is useful for parsing very large documents where children of the | |
18 | * root element can be processed individually rather than keeping them all | |
19 | * in memory at the same time.</p> | |
20 | * | |
21 | * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a> | |
22 | * @version $Revision: 1.9 $ | |
23 | */ | |
24 | class PruningElementStack extends ElementStack { | |
25 | ||
26 | /** ElementHandler to call when pruning occurs */ | |
27 | private ElementHandler elementHandler; | |
28 | ||
29 | /** the element name path which denotes the node to remove from its parent | |
30 | * when it is complete (i.e. when it is popped from the stack). | |
31 | * The first entry in the path will be a child of the root node | |
32 | */ | |
33 | private String[] path; | |
34 | ||
35 | /** The level at which a path match can occur. | |
36 | * We match when we have popped the selected node so the | |
37 | * and the lastElementIndex points to its parent so this | |
38 | * value should be path.length - 2 | |
39 | */ | |
40 | private int matchingElementIndex; | |
41 | ||
42 | ||
43 | ||
44 | 0 | public PruningElementStack(String[] path, ElementHandler elementHandler) { |
45 | 0 | this.path = path; |
46 | 0 | this.elementHandler = elementHandler; |
47 | 0 | checkPath(); |
48 | } | |
49 | ||
50 | 0 | public PruningElementStack(String[] path, ElementHandler elementHandler, int defaultCapacity) { |
51 | 0 | super(defaultCapacity); |
52 | 0 | this.path = path; |
53 | 0 | this.elementHandler = elementHandler; |
54 | 0 | checkPath(); |
55 | } | |
56 | ||
57 | 0 | public Element popElement() { |
58 | 0 | Element answer = super.popElement(); |
59 | ||
60 | 0 | if ( lastElementIndex == matchingElementIndex && lastElementIndex >= 0 ) { |
61 | // we are popping the correct level in the tree | |
62 | // lets check if the path fits | |
63 | // | |
64 | // NOTE: this is an inefficient way of doing it - we could | |
65 | // maintain a history of which parts matched? | |
66 | 0 | if ( validElement( answer, lastElementIndex + 1 ) ) { |
67 | 0 | Element parent = null; |
68 | 0 | for ( int i = 0; i <= lastElementIndex; i++ ) { |
69 | 0 | parent = stack[i]; |
70 | 0 | if ( ! validElement( parent, i ) ) { |
71 | 0 | parent = null; |
72 | 0 | break; |
73 | } | |
74 | } | |
75 | 0 | if ( parent != null ) { |
76 | 0 | pathMatches(parent, answer); |
77 | } | |
78 | } | |
79 | } | |
80 | 0 | return answer; |
81 | } | |
82 | ||
83 | 0 | protected void pathMatches(Element parent, Element selectedNode) { |
84 | //System.out.println( "Matched: " + selectedNode + " about to call handler" ); | |
85 | ||
86 | //elementHandler.handle( selectedNode ); | |
87 | 0 | elementHandler.onEnd(this); |
88 | ||
89 | //System.out.println( "Pruning: removing " + selectedNode + " from parent: " + parent ); | |
90 | 0 | parent.remove( selectedNode ); |
91 | } | |
92 | ||
93 | 0 | protected boolean validElement(Element element, int index) { |
94 | 0 | String requiredName = path[index]; |
95 | 0 | String name = element.getName(); |
96 | 0 | if (requiredName == name) { |
97 | 0 | return true; |
98 | } | |
99 | 0 | if (requiredName != null && name != null ) { |
100 | 0 | return requiredName.equals( name ); |
101 | } | |
102 | 0 | return false; |
103 | } | |
104 | ||
105 | ||
106 | 0 | private void checkPath() { |
107 | 0 | if ( path.length < 2 ) { |
108 | 0 | throw new RuntimeException( "Invalid path of length: " + path.length + " it must be greater than 2" ); |
109 | } | |
110 | 0 | matchingElementIndex = path.length - 2; |
111 | } | |
112 | } | |
113 | ||
114 | ||
115 | ||
116 | ||
117 | /* | |
118 | * Redistribution and use of this software and associated documentation | |
119 | * ("Software"), with or without modification, are permitted provided | |
120 | * that the following conditions are met: | |
121 | * | |
122 | * 1. Redistributions of source code must retain copyright | |
123 | * statements and notices. Redistributions must also contain a | |
124 | * copy of this document. | |
125 | * | |
126 | * 2. Redistributions in binary form must reproduce the | |
127 | * above copyright notice, this list of conditions and the | |
128 | * following disclaimer in the documentation and/or other | |
129 | * materials provided with the distribution. | |
130 | * | |
131 | * 3. The name "DOM4J" must not be used to endorse or promote | |
132 | * products derived from this Software without prior written | |
133 | * permission of MetaStuff, Ltd. For written permission, | |
134 | * please contact dom4j-info@metastuff.com. | |
135 | * | |
136 | * 4. Products derived from this Software may not be called "DOM4J" | |
137 | * nor may "DOM4J" appear in their names without prior written | |
138 | * permission of MetaStuff, Ltd. DOM4J is a registered | |
139 | * trademark of MetaStuff, Ltd. | |
140 | * | |
141 | * 5. Due credit should be given to the DOM4J Project - | |
142 | * http://www.dom4j.org | |
143 | * | |
144 | * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS | |
145 | * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT | |
146 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
147 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
148 | * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
149 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
150 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
151 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
152 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
153 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
154 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
155 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
156 | * | |
157 | * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved. | |
158 | * | |
159 | * $Id: PruningElementStack.java,v 1.9 2004/06/25 08:03:37 maartenc Exp $ | |
160 | */ |
|