View Javadoc

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