KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xsl > grammar > ResultNode


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xsl.grammar;
21
22 import org.w3c.dom.*;
23
24 /**
25  * Node that can be used as HintContext. It's used for nested grammars.
26  * It can narrow context to given namespace etc.
27  *
28  * @author asgeir@dimonsoftware.com
29  */

30 public class ResultNode implements Node {
31
32     protected Node peer;
33
34     protected String JavaDoc ignorePrefix;
35
36     protected String JavaDoc onlyUsePrefix;
37
38     /** Creates a new instance of ResultNode
39      * If onlyUsePrefix is non-null, the result node hirarchy will only have elements
40      * with this prefix.
41      * If ignorePrefix is non-null and onlyUsePrefix is null, the node hirarchy will
42      * not include nodes with this prefix.
43      * If both ignorePrefix and onlyUsePrefix are null, the node hirarchy will only
44      * include nodes with no prefixes.
45      * @peer the peer which this object contains
46      * @ignorePrefix a prefix (typically ending with ":") which should be ignored in
47      * this node hirarchy
48      * @onlyUsePrefix the prefix which all the nodes in the node hirarchy should have.
49      */

50     public ResultNode(Node peer, String JavaDoc ignorePrefix, String JavaDoc onlyUsePrefix) {
51         this.peer = peer;
52         this.ignorePrefix = ignorePrefix;
53         this.onlyUsePrefix = onlyUsePrefix;
54     }
55     
56     public Node appendChild(Node newChild) throws DOMException {
57         return createNode(peer.appendChild(newChild));
58     }
59     
60     public Node cloneNode(boolean deep) {
61         return createNode(peer.cloneNode(deep));
62     }
63     
64     public NamedNodeMap getAttributes() {
65         return peer.getAttributes();
66     }
67     
68     public NodeList getChildNodes() {
69         return new ResultNodeList(peer.getChildNodes());
70     }
71     
72     public Node getFirstChild() {
73         NodeList childNodes = getChildNodes();
74         if (childNodes.getLength() == 0) {
75             return null;
76         } else {
77             return childNodes.item(0);
78         }
79     }
80     
81     public Node getLastChild() {
82         NodeList childNodes = new ResultNodeList(peer.getChildNodes());
83         if (childNodes.getLength() == 0) {
84             return null;
85         } else {
86             return childNodes.item(childNodes.getLength()-1);
87         }
88     }
89     
90     public String JavaDoc getLocalName() {
91         return peer.getLocalName();
92     }
93     
94     public String JavaDoc getNamespaceURI() {
95         return peer.getNamespaceURI();
96     }
97     
98     public Node getNextSibling() {
99         Node node = peer.getNextSibling();
100         while (node != null && node.getNodeName() != null && !hasAllowedPrefix(node.getNodeName())) {
101             node = node.getNextSibling();
102         }
103         
104         if (node == null) {
105             return null;
106         } else {
107             return createNode(node);
108         }
109     }
110     
111     public String JavaDoc getNodeName() {
112         return peer.getNodeName();
113     }
114     
115     public short getNodeType() {
116         return peer.getNodeType();
117     }
118     
119     public String JavaDoc getNodeValue() throws DOMException {
120         return peer.getNodeValue();
121     }
122     
123     public Document getOwnerDocument() {
124         return peer.getOwnerDocument();
125     }
126     
127     public Node getParentNode() {
128         Node node = peer.getParentNode();
129         while (node != null && node.getNodeName() != null && !hasAllowedPrefix(node.getNodeName())) {
130             node = node.getParentNode();
131         }
132         
133         if (node == null) {
134             return null;
135         } else {
136             return createNode(node);
137         }
138     }
139     
140     public String JavaDoc getPrefix() {
141         return peer.getPrefix();
142     }
143     
144     public Node getPreviousSibling() {
145         Node node = peer.getPreviousSibling();
146         while (node != null && node.getNodeName() != null && !hasAllowedPrefix(node.getNodeName())) {
147             node = node.getPreviousSibling();
148         }
149         
150         if (node == null) {
151             return null;
152         } else {
153             return createNode(node);
154         }
155     }
156     
157     public boolean hasAttributes() {
158         return peer.hasAttributes();
159     }
160     
161     public boolean hasChildNodes() {
162         return getChildNodes().getLength() > 0;
163     }
164     
165     public Node insertBefore(Node newChild, Node refChild) throws DOMException {
166         return createNode(peer.insertBefore(newChild, refChild));
167     }
168     
169     public boolean isSupported(String JavaDoc feature, String JavaDoc version) {
170         return peer.isSupported(feature, version);
171     }
172     
173     public void normalize() {
174         peer.normalize();
175     }
176     
177     public Node removeChild(Node oldChild) throws DOMException {
178         return createNode(peer.removeChild(oldChild));
179     }
180     
181     public Node replaceChild(Node newChild, Node oldChild) throws DOMException {
182         return createNode(peer.replaceChild(newChild, oldChild));
183     }
184     
185     public void setNodeValue(String JavaDoc nodeValue) throws DOMException {
186         peer.setNodeValue(nodeValue);
187     }
188     
189     public void setPrefix(String JavaDoc prefix) throws DOMException {
190         peer.setPrefix(prefix);
191     }
192
193     /**
194      * Create narrowing result node from given node.
195      */

196     protected Node createNode(Node orig) {
197         if (orig.getNodeType() == Node.ELEMENT_NODE) {
198             return new ResultElement((Element)orig, ignorePrefix, onlyUsePrefix);
199         } else if (orig.getNodeType() == Node.DOCUMENT_NODE) {
200             return new ResultDocument((Document)orig, ignorePrefix, onlyUsePrefix);
201         } else if (orig.getNodeType() == Node.ATTRIBUTE_NODE) {
202             return new ResultAttr((Attr)orig, ignorePrefix, onlyUsePrefix);
203         } else {
204             return orig;
205         }
206     }
207     
208     /**
209      * Returns true if the prefix rules described in the constructor javadocs
210      * are fulfilled, otherwise returns false.
211      */

212     protected boolean hasAllowedPrefix(String JavaDoc name) {
213         if (onlyUsePrefix != null) {
214             return name.startsWith(onlyUsePrefix);
215         } else if (ignorePrefix != null){
216             return !name.startsWith(ignorePrefix);
217         } else {
218             return name.indexOf(':') == -1;
219         }
220     }
221     
222     public class ResultNodeList implements NodeList{
223         java.util.Vector JavaDoc nodeVector;
224         
225         public ResultNodeList(NodeList list) {
226             nodeVector = new java.util.Vector JavaDoc(list.getLength());
227             for (int ind = 0; ind < list.getLength(); ind++) {
228                 Node node = list.item(ind);
229                 if (node.getNodeName() != null && hasAllowedPrefix(node.getNodeName())) {
230                     nodeVector.add(createNode(node));
231                 }
232             }
233         }
234         
235         public int getLength() {
236             return nodeVector.size();
237         }
238         
239         public Node item(int index) {
240             return (Node)nodeVector.elementAt(index);
241         }
242     }
243     
244     //
245
// Implementation of DOM Level 3 methods
246
//
247

248     public short compareDocumentPosition (Node a) {
249         throw new UOException();
250     }
251     
252     public String JavaDoc getBaseURI() {
253         throw new UOException();
254     }
255     public Object JavaDoc getFeature(String JavaDoc a, String JavaDoc b) {
256         throw new UOException();
257     }
258     public String JavaDoc getTextContent () {
259         throw new UOException();
260     }
261     public Object JavaDoc getUserData(String JavaDoc a) {
262         throw new UOException();
263     }
264     public boolean isDefaultNamespace (String JavaDoc a) {
265         throw new UOException();
266     }
267     public boolean isEqualNode(Node a) {
268         throw new UOException();
269     }
270     public boolean isSameNode(Node a) {
271         throw new UOException();
272     }
273     public String JavaDoc lookupNamespaceURI(String JavaDoc a) {
274         throw new UOException();
275     }
276     public String JavaDoc lookupPrefix(String JavaDoc a) {
277         throw new UOException();
278     }
279     public void setTextContent(String JavaDoc a) {
280         throw new UOException();
281     }
282     public Object JavaDoc setUserData(String JavaDoc a, Object JavaDoc b, UserDataHandler c) {
283         throw new UOException();
284     }
285     
286     // Implementation of DOM Level 3 methods for Element
287
public TypeInfo getSchemaTypeInfo() {
288         throw new UOException ();
289     }
290     public void setIdAttribute(String JavaDoc a, boolean b) {
291         throw new UOException ();
292     }
293     public void setIdAttributeNS(String JavaDoc a, String JavaDoc b, boolean c) {
294         throw new UOException ();
295     }
296     public void setIdAttributeNode(Attr a, boolean b) {
297         throw new UOException ();
298     }
299     // Implementation of DOM Level 3 methods for Attr
300

301     public boolean isId () {
302         throw new UOException ();
303     }
304     // Implementation of DOM Level 3 methods for Text
305
public Text replaceWholeText (String JavaDoc a) {
306         throw new UOException ();
307     }
308     public String JavaDoc getWholeText() {
309         throw new UOException ();
310     }
311     public boolean isElementContentWhitespace() {
312         throw new UOException ();
313     }
314     
315     // Dom Level 3 methods for Document
316
public Node adoptNode (Node a) {
317         throw new UOException ();
318     }
319     public String JavaDoc getDocumentURI () {
320         throw new UOException ();
321     }
322     public DOMConfiguration getDomConfig() {
323         throw new UOException ();
324     }
325     public String JavaDoc getInputEncoding() {
326         throw new UOException ();
327     }
328     public boolean getStrictErrorChecking() {
329         throw new UOException ();
330     }
331     public String JavaDoc getXmlEncoding () {
332         throw new UOException ();
333     }
334     public boolean getXmlStandalone() {
335         throw new UOException ();
336     }
337     public String JavaDoc getXmlVersion() {
338         throw new UOException ();
339     }
340     public void normalizeDocument() {
341         throw new UOException ();
342     }
343     public Node renameNode(Node a, String JavaDoc nb, String JavaDoc c) {
344         throw new UOException ();
345     }
346     public void setDocumentURI(String JavaDoc a) {
347         throw new UOException ();
348     }
349     public void setStrictErrorChecking(boolean a) {
350         throw new UOException ();
351     }
352     public void setXmlStandalone(boolean a) {
353         throw new UOException ();
354     }
355     public void setXmlVersion(String JavaDoc a) {
356         throw new UOException ();
357     }
358     
359     private static final class UOException extends IllegalStateException JavaDoc {
360         
361     }
362 }
363
Popular Tags