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