001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.xsl.grammar;
043:
044: import org.w3c.dom.*;
045:
046: /**
047: * Node that can be used as HintContext. It's used for nested grammars.
048: * It can narrow context to given namespace etc.
049: *
050: * @author asgeir@dimonsoftware.com
051: */
052: public class ResultNode implements Node {
053:
054: protected Node peer;
055:
056: protected String ignorePrefix;
057:
058: protected String onlyUsePrefix;
059:
060: /** Creates a new instance of ResultNode
061: * If onlyUsePrefix is non-null, the result node hirarchy will only have elements
062: * with this prefix.
063: * If ignorePrefix is non-null and onlyUsePrefix is null, the node hirarchy will
064: * not include nodes with this prefix.
065: * If both ignorePrefix and onlyUsePrefix are null, the node hirarchy will only
066: * include nodes with no prefixes.
067: * @peer the peer which this object contains
068: * @ignorePrefix a prefix (typically ending with ":") which should be ignored in
069: * this node hirarchy
070: * @onlyUsePrefix the prefix which all the nodes in the node hirarchy should have.
071: */
072: public ResultNode(Node peer, String ignorePrefix,
073: String onlyUsePrefix) {
074: this .peer = peer;
075: this .ignorePrefix = ignorePrefix;
076: this .onlyUsePrefix = onlyUsePrefix;
077: }
078:
079: public Node appendChild(Node newChild) throws DOMException {
080: return createNode(peer.appendChild(newChild));
081: }
082:
083: public Node cloneNode(boolean deep) {
084: return createNode(peer.cloneNode(deep));
085: }
086:
087: public NamedNodeMap getAttributes() {
088: return peer.getAttributes();
089: }
090:
091: public NodeList getChildNodes() {
092: return new ResultNodeList(peer.getChildNodes());
093: }
094:
095: public Node getFirstChild() {
096: NodeList childNodes = getChildNodes();
097: if (childNodes.getLength() == 0) {
098: return null;
099: } else {
100: return childNodes.item(0);
101: }
102: }
103:
104: public Node getLastChild() {
105: NodeList childNodes = new ResultNodeList(peer.getChildNodes());
106: if (childNodes.getLength() == 0) {
107: return null;
108: } else {
109: return childNodes.item(childNodes.getLength() - 1);
110: }
111: }
112:
113: public String getLocalName() {
114: return peer.getLocalName();
115: }
116:
117: public String getNamespaceURI() {
118: return peer.getNamespaceURI();
119: }
120:
121: public Node getNextSibling() {
122: Node node = peer.getNextSibling();
123: while (node != null && node.getNodeName() != null
124: && !hasAllowedPrefix(node.getNodeName())) {
125: node = node.getNextSibling();
126: }
127:
128: if (node == null) {
129: return null;
130: } else {
131: return createNode(node);
132: }
133: }
134:
135: public String getNodeName() {
136: return peer.getNodeName();
137: }
138:
139: public short getNodeType() {
140: return peer.getNodeType();
141: }
142:
143: public String getNodeValue() throws DOMException {
144: return peer.getNodeValue();
145: }
146:
147: public Document getOwnerDocument() {
148: return peer.getOwnerDocument();
149: }
150:
151: public Node getParentNode() {
152: Node node = peer.getParentNode();
153: while (node != null && node.getNodeName() != null
154: && !hasAllowedPrefix(node.getNodeName())) {
155: node = node.getParentNode();
156: }
157:
158: if (node == null) {
159: return null;
160: } else {
161: return createNode(node);
162: }
163: }
164:
165: public String getPrefix() {
166: return peer.getPrefix();
167: }
168:
169: public Node getPreviousSibling() {
170: Node node = peer.getPreviousSibling();
171: while (node != null && node.getNodeName() != null
172: && !hasAllowedPrefix(node.getNodeName())) {
173: node = node.getPreviousSibling();
174: }
175:
176: if (node == null) {
177: return null;
178: } else {
179: return createNode(node);
180: }
181: }
182:
183: public boolean hasAttributes() {
184: return peer.hasAttributes();
185: }
186:
187: public boolean hasChildNodes() {
188: return getChildNodes().getLength() > 0;
189: }
190:
191: public Node insertBefore(Node newChild, Node refChild)
192: throws DOMException {
193: return createNode(peer.insertBefore(newChild, refChild));
194: }
195:
196: public boolean isSupported(String feature, String version) {
197: return peer.isSupported(feature, version);
198: }
199:
200: public void normalize() {
201: peer.normalize();
202: }
203:
204: public Node removeChild(Node oldChild) throws DOMException {
205: return createNode(peer.removeChild(oldChild));
206: }
207:
208: public Node replaceChild(Node newChild, Node oldChild)
209: throws DOMException {
210: return createNode(peer.replaceChild(newChild, oldChild));
211: }
212:
213: public void setNodeValue(String nodeValue) throws DOMException {
214: peer.setNodeValue(nodeValue);
215: }
216:
217: public void setPrefix(String prefix) throws DOMException {
218: peer.setPrefix(prefix);
219: }
220:
221: /**
222: * Create narrowing result node from given node.
223: */
224: protected Node createNode(Node orig) {
225: if (orig.getNodeType() == Node.ELEMENT_NODE) {
226: return new ResultElement((Element) orig, ignorePrefix,
227: onlyUsePrefix);
228: } else if (orig.getNodeType() == Node.DOCUMENT_NODE) {
229: return new ResultDocument((Document) orig, ignorePrefix,
230: onlyUsePrefix);
231: } else if (orig.getNodeType() == Node.ATTRIBUTE_NODE) {
232: return new ResultAttr((Attr) orig, ignorePrefix,
233: onlyUsePrefix);
234: } else {
235: return orig;
236: }
237: }
238:
239: /**
240: * Returns true if the prefix rules described in the constructor javadocs
241: * are fulfilled, otherwise returns false.
242: */
243: protected boolean hasAllowedPrefix(String name) {
244: if (onlyUsePrefix != null) {
245: return name.startsWith(onlyUsePrefix);
246: } else if (ignorePrefix != null) {
247: return !name.startsWith(ignorePrefix);
248: } else {
249: return name.indexOf(':') == -1;
250: }
251: }
252:
253: public class ResultNodeList implements NodeList {
254: java.util.Vector nodeVector;
255:
256: public ResultNodeList(NodeList list) {
257: nodeVector = new java.util.Vector(list.getLength());
258: for (int ind = 0; ind < list.getLength(); ind++) {
259: Node node = list.item(ind);
260: if (node.getNodeName() != null
261: && hasAllowedPrefix(node.getNodeName())) {
262: nodeVector.add(createNode(node));
263: }
264: }
265: }
266:
267: public int getLength() {
268: return nodeVector.size();
269: }
270:
271: public Node item(int index) {
272: return (Node) nodeVector.elementAt(index);
273: }
274: }
275:
276: //
277: // Implementation of DOM Level 3 methods
278: //
279:
280: public short compareDocumentPosition(Node a) {
281: throw new UOException();
282: }
283:
284: public String getBaseURI() {
285: throw new UOException();
286: }
287:
288: public Object getFeature(String a, String b) {
289: throw new UOException();
290: }
291:
292: public String getTextContent() {
293: throw new UOException();
294: }
295:
296: public Object getUserData(String a) {
297: throw new UOException();
298: }
299:
300: public boolean isDefaultNamespace(String a) {
301: throw new UOException();
302: }
303:
304: public boolean isEqualNode(Node a) {
305: throw new UOException();
306: }
307:
308: public boolean isSameNode(Node a) {
309: throw new UOException();
310: }
311:
312: public String lookupNamespaceURI(String a) {
313: throw new UOException();
314: }
315:
316: public String lookupPrefix(String a) {
317: throw new UOException();
318: }
319:
320: public void setTextContent(String a) {
321: throw new UOException();
322: }
323:
324: public Object setUserData(String a, Object b, UserDataHandler c) {
325: throw new UOException();
326: }
327:
328: // Implementation of DOM Level 3 methods for Element
329: public TypeInfo getSchemaTypeInfo() {
330: throw new UOException();
331: }
332:
333: public void setIdAttribute(String a, boolean b) {
334: throw new UOException();
335: }
336:
337: public void setIdAttributeNS(String a, String b, boolean c) {
338: throw new UOException();
339: }
340:
341: public void setIdAttributeNode(Attr a, boolean b) {
342: throw new UOException();
343: }
344:
345: // Implementation of DOM Level 3 methods for Attr
346:
347: public boolean isId() {
348: throw new UOException();
349: }
350:
351: // Implementation of DOM Level 3 methods for Text
352: public Text replaceWholeText(String a) {
353: throw new UOException();
354: }
355:
356: public String getWholeText() {
357: throw new UOException();
358: }
359:
360: public boolean isElementContentWhitespace() {
361: throw new UOException();
362: }
363:
364: // Dom Level 3 methods for Document
365: public Node adoptNode(Node a) {
366: throw new UOException();
367: }
368:
369: public String getDocumentURI() {
370: throw new UOException();
371: }
372:
373: public DOMConfiguration getDomConfig() {
374: throw new UOException();
375: }
376:
377: public String getInputEncoding() {
378: throw new UOException();
379: }
380:
381: public boolean getStrictErrorChecking() {
382: throw new UOException();
383: }
384:
385: public String getXmlEncoding() {
386: throw new UOException();
387: }
388:
389: public boolean getXmlStandalone() {
390: throw new UOException();
391: }
392:
393: public String getXmlVersion() {
394: throw new UOException();
395: }
396:
397: public void normalizeDocument() {
398: throw new UOException();
399: }
400:
401: public Node renameNode(Node a, String nb, String c) {
402: throw new UOException();
403: }
404:
405: public void setDocumentURI(String a) {
406: throw new UOException();
407: }
408:
409: public void setStrictErrorChecking(boolean a) {
410: throw new UOException();
411: }
412:
413: public void setXmlStandalone(boolean a) {
414: throw new UOException();
415: }
416:
417: public void setXmlVersion(String a) {
418: throw new UOException();
419: }
420:
421: private static final class UOException extends
422: IllegalStateException {
423:
424: }
425: }
|