KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > functions > NamePart


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 /**
14 * This class supports the name(), local-name(), and namespace-uri() functions
15 * from XPath 1.0, and also the XSLT generate-id() function
16 */

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     /**
28     * Simplify and validate.
29     */

30
31      public Expression simplify(StaticContext env) throws XPathException {
32         useContextItemAsDefault();
33         return simplifyArguments(env);
34     }
35
36     /**
37      * Determine the special properties of this expression. The generate-id()
38      * function is a special case: it is considered creative if its operand
39      * is creative, so that generate-id(f()) is not taken out of a loop
40      */

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     /**
52     * Evaluate the function in a string context
53     */

54
55     public Item evaluateItem(XPathContext c) throws XPathException {
56         NodeInfo node = (NodeInfo)argument[0].evaluateItem(c);
57         if (node==null) {
58             // All the functions that allow the argument to be an empty sequence
59
// return an empty string, except for node-name()
60
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 JavaDoc 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 JavaDoc uri = node.getURI();
79                 s = (uri==null ? "" : uri);
80                         // null should no longer be returned, but the spec has changed, so it's
81
// better to be defensive
82
return new AnyURIValue(s);
83             case GENERATE_ID:
84                 s = node.generateId();
85                 break;
86             case DOCUMENT_URI:
87                 // TODO: the function should never return a relative URI; and it shouldn't return a value
88
// in the case of temporary trees, or any other document that can't be retrieved using the
89
// system ID.
90
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 JavaDoc("Unknown name operation");
100         }
101         return new StringValue(s);
102     }
103
104     /**
105      * Test whether an expression is a call on the generate-id() function
106      * @param exp the expression to be tested
107      * @return true if exp is a call on generate-id(), else false
108      */

109
110     public static boolean isGenerateIdFunction(Expression exp) {
111         return ((exp instanceof NamePart) && ((NamePart)exp).operation == GENERATE_ID);
112     }
113 }
114
115 //
116
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
117
// you may not use this file except in compliance with the License. You may obtain a copy of the
118
// License at http://www.mozilla.org/MPL/
119
//
120
// Software distributed under the License is distributed on an "AS IS" basis,
121
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
122
// See the License for the specific language governing rights and limitations under the License.
123
//
124
// The Original Code is: all this file.
125
//
126
// The Initial Developer of the Original Code is Michael H. Kay.
127
//
128
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
129
//
130
// Contributor(s): none.
131
//
132
Popular Tags