1 package net.sf.saxon.functions; 2 import net.sf.saxon.expr.Expression; 3 import net.sf.saxon.expr.StaticContext; 4 import net.sf.saxon.expr.StaticProperty; 5 import net.sf.saxon.expr.XPathContext; 6 import net.sf.saxon.om.Item; 7 import net.sf.saxon.om.NodeInfo; 8 import net.sf.saxon.trans.XPathException; 9 import net.sf.saxon.value.QNameValue; 10 import net.sf.saxon.value.StringValue; 11 import net.sf.saxon.value.AnyURIValue; 12 13 17 18 public class NamePart extends SystemFunction { 19 20 public static final int NAME = 0; 21 public static final int LOCAL_NAME = 1; 22 public static final int NAMESPACE_URI = 2; 23 public static final int GENERATE_ID = 3; 24 public static final int DOCUMENT_URI = 4; 25 public static final int NODE_NAME = 6; 26 27 30 31 public Expression simplify(StaticContext env) throws XPathException { 32 useContextItemAsDefault(); 33 return simplifyArguments(env); 34 } 35 36 41 42 public int computeSpecialProperties() { 43 int p = super.computeSpecialProperties(); 44 if (operation == GENERATE_ID) { 45 return p & ~StaticProperty.NON_CREATIVE; 46 } else { 47 return p; 48 } 49 } 50 51 54 55 public Item evaluateItem(XPathContext c) throws XPathException { 56 NodeInfo node = (NodeInfo)argument[0].evaluateItem(c); 57 if (node==null) { 58 if (operation == NODE_NAME) { 61 return null; 62 } else if (operation == DOCUMENT_URI || operation == NAMESPACE_URI) { 63 return AnyURIValue.EMPTY_URI; 64 } else { 65 return StringValue.EMPTY_STRING; 66 } 67 } 68 69 String s; 70 switch (operation) { 71 case NAME: 72 s = node.getDisplayName(); 73 break; 74 case LOCAL_NAME: 75 s = node.getLocalPart(); 76 break; 77 case NAMESPACE_URI: 78 String uri = node.getURI(); 79 s = (uri==null ? "" : uri); 80 return new AnyURIValue(s); 83 case GENERATE_ID: 84 s = node.generateId(); 85 break; 86 case DOCUMENT_URI: 87 s = node.getSystemId(); 91 return new AnyURIValue(s); 92 case NODE_NAME: 93 int nc = node.getNameCode(); 94 if (nc == -1) { 95 return null; 96 } 97 return new QNameValue(node.getNamePool(), nc); 98 default: 99 throw new UnsupportedOperationException ("Unknown name operation"); 100 } 101 return new StringValue(s); 102 } 103 104 109 110 public static boolean isGenerateIdFunction(Expression exp) { 111 return ((exp instanceof NamePart) && ((NamePart)exp).operation == GENERATE_ID); 112 } 113 } 114 115 | Popular Tags |