KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > functions > GenerateId


1 package com.icl.saxon.functions;
2 import com.icl.saxon.Context;
3 import com.icl.saxon.om.NodeInfo;
4 import com.icl.saxon.om.NodeEnumeration;
5 import com.icl.saxon.expr.*;
6
7
8 public class GenerateId extends Function {
9
10     /**
11     * Function name (for diagnostics)
12     */

13
14     public String getName() {
15         return "generate-id";
16     };
17
18     /**
19     * Determine the data type of the expression
20     * @return Value.STRING
21     */

22
23     public int getDataType() {
24         return Value.STRING;
25     }
26
27     /**
28     * Simplify and validate.
29     */

30
31     public Expression simplify() throws XPathException {
32         checkArgumentCount(0, 1);
33         return this;
34     }
35
36     /**
37     * Evaluate the function in a string context
38     */

39
40     public String evaluateAsString(Context c) throws XPathException {
41         int numArgs = getNumberOfArguments();
42         
43         if (numArgs==0) {
44             NodeInfo node = c.getContextNodeInfo();
45             String localId = node.generateId();
46             return "d" +
47                    c.getController().getDocumentPool().
48                             getDocumentNumber(node.getDocumentRoot()) +
49                    localId;
50         }
51
52         NodeEnumeration enum = argument[0].enumerate(c, true);
53         if (enum.hasMoreElements()) {
54             NodeInfo node = enum.nextElement();
55             String localId = node.generateId();
56             return "d" +
57                    c.getController().getDocumentPool().
58                             getDocumentNumber(node.getDocumentRoot()) +
59                    localId;
60         } else {
61             return "";
62         }
63     }
64
65     /**
66     * Evaluate in a general context
67     */

68
69     public Value evaluate(Context c) throws XPathException {
70         return new StringValue(evaluateAsString(c));
71     }
72
73     /**
74     * Determine the dependencies
75     */

76
77     public int getDependencies() {
78         if (getNumberOfArguments()==0) {
79             return Context.CONTEXT_NODE;
80         } else {
81             return argument[0].getDependencies();
82         }
83     }
84
85     /**
86     * Reduce the dependencies
87     */

88
89     public Expression reduce(int dep, Context c) throws XPathException {
90         GenerateId f = new GenerateId();
91         if (getNumberOfArguments()==1) {
92             f.addArgument(argument[0].reduce(dep, c));
93             f.setStaticContext(getStaticContext());
94             return f;
95         } else {
96             if ((dep & Context.CONTEXT_NODE)!=0) {
97                 return evaluate(c);
98             } else {
99                 return this;
100             }
101         }
102
103     }
104
105 }
106
107
108
109 //
110
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
111
// you may not use this file except in compliance with the License. You may obtain a copy of the
112
// License at http://www.mozilla.org/MPL/
113
//
114
// Software distributed under the License is distributed on an "AS IS" basis,
115
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
116
// See the License for the specific language governing rights and limitations under the License.
117
//
118
// The Original Code is: all this file.
119
//
120
// The Initial Developer of the Original Code is
121
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
122
//
123
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
124
//
125
// Contributor(s): none.
126
//
127
Popular Tags