DocumentNode.java
01 package net.sourceforge.pmd.lang.ast.xpath.saxon;
02 
03 import java.util.HashMap;
04 import java.util.Iterator;
05 import java.util.Map;
06 
07 import net.sf.saxon.om.Axis;
08 import net.sf.saxon.om.AxisIterator;
09 import net.sf.saxon.om.DocumentInfo;
10 import net.sf.saxon.om.Navigator;
11 import net.sf.saxon.om.NodeInfo;
12 import net.sf.saxon.om.SingleNodeIterator;
13 import net.sf.saxon.type.Type;
14 import net.sourceforge.pmd.lang.ast.Node;
15 
16 /**
17  * A Saxon OM Document node for an AST Node.
18  */
19 public class DocumentNode extends AbstractNodeInfo implements DocumentInfo {
20 
21     /**
22      * The root ElementNode of the DocumentNode.
23      */
24     protected final ElementNode rootNode;
25 
26     /**
27      * Mapping from AST Node to corresponding ElementNode.
28      */
29     public final Map<Node, ElementNode> nodeToElementNode = new HashMap<Node, ElementNode>();
30 
31     /**
32      * Construct a DocumentNode, with the given AST Node serving as the root
33      * ElementNode.
34      
35      @param node The root AST Node.
36      
37      @see ElementNode
38      */
39     public DocumentNode(Node node) {
40   this.rootNode = new ElementNode(this, new IdGenerator(), null, node, -1);
41     }
42 
43     /**
44      * {@inheritDoc}
45      */
46     public String[] getUnparsedEntity(String name) {
47   throw createUnsupportedOperationException("DocumentInfo.getUnparsedEntity(String)");
48     }
49 
50     /**
51      * {@inheritDoc}
52      */
53     public Iterator getUnparsedEntityNames() {
54   throw createUnsupportedOperationException("DocumentInfo.getUnparsedEntityNames()");
55     }
56 
57     /**
58      * {@inheritDoc}
59      */
60     public NodeInfo selectID(String id) {
61   throw createUnsupportedOperationException("DocumentInfo.selectID(String)");
62     }
63 
64     @Override
65     public int getNodeKind() {
66   return Type.DOCUMENT;
67     }
68 
69     @Override
70     public DocumentInfo getDocumentRoot() {
71   return this;
72     }
73 
74     @Override
75     public boolean hasChildNodes() {
76   return true;
77     }
78 
79     @Override
80     public AxisIterator iterateAxis(byte axisNumber) {
81   switch (axisNumber) {
82   case Axis.DESCENDANT:
83       return new Navigator.DescendantEnumeration(this, false, true);
84   case Axis.DESCENDANT_OR_SELF:
85       return new Navigator.DescendantEnumeration(this, true, true);
86   case Axis.CHILD:
87       return SingleNodeIterator.makeIterator(rootNode);
88   default:
89       return super.iterateAxis(axisNumber);
90   }
91     }
92 }