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 net.sf.saxon.om.NodeInfo;
7   import net.sf.saxon.om.SequenceIterator;
8   import net.sf.saxon.trans.XPathException;
9   import net.sf.saxon.type.Type;
10  import net.sf.saxon.value.BooleanValue;
11  import net.sf.saxon.value.Int64Value;
12  import net.sf.saxon.value.StringValue;
13  import net.sf.saxon.value.Value;
14  import net.sourceforge.pmd.lang.ast.xpath.Attribute;
15  
16  /**
17   * A Saxon OM Attribute node for an AST Node Attribute.
18   */
19  public class AttributeNode extends AbstractNodeInfo {
20      protected final Attribute attribute;
21      protected final int id;
22      protected Value value;
23  
24      public AttributeNode(Attribute attribute, int id) {
25  	this.attribute = attribute;
26  	this.id = id;
27      }
28  
29      @Override
30      public int getNodeKind() {
31  	return Type.ATTRIBUTE;
32      }
33  
34      @Override
35      public String getLocalPart() {
36  	return attribute.getName();
37      }
38  
39      @Override
40      public String getURI() {
41  	return "";
42      }
43  
44      @Override
45      public Value atomize() throws XPathException {
46  	if (value == null) {
47  	    Object v = attribute.getValue();
48  	    // TODO Need to handle the full range of types, is there something Saxon can do to help?
49  	    if (v instanceof String) {
50  		value = new StringValue((String) v);
51  	    } else if (v instanceof Boolean) {
52  		value = BooleanValue.get(((Boolean) v).booleanValue());
53  	    } else if (v instanceof Integer) {
54  		value = Int64Value.makeIntegerValue((Integer) v);
55  	    } else if (v == null) {
56  		// Ok
57  	    } else {
58  		throw new RuntimeException("Unable to create ValueRepresentaton for attribute value: " + v
59  			+ " of type " + v.getClass());
60  	    }
61  	}
62  	return value;
63      }
64  
65      @Override
66      public CharSequence getStringValueCS() {
67  	return attribute.getStringValue();
68      }
69  
70      @Override
71      public SequenceIterator getTypedValue() throws XPathException {
72  	return atomize().iterate();
73      }
74  
75      @Override
76      public int compareOrder(NodeInfo other) {
77  	return Integer.signum(this.id - ((AttributeNode) other).id);
78      }
79  }