KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.icl.saxon.functions;
2 import com.icl.saxon.*;
3 import com.icl.saxon.expr.*;
4
5 import java.util.*;
6 //import java.lang.Math;
7
//import java.text.*;
8

9 public class Concat extends Function {
10
11     /**
12     * Function name (for diagnostics)
13     */

14
15     public String JavaDoc getName() {
16         return "concat";
17     };
18
19     /**
20     * Determine the data type of the expression
21     * @return Value.BOOLEAN
22     */

23
24     public int getDataType() {
25         return Value.STRING;
26     }
27
28     /**
29     * Simplify and validate.
30     * This is a pure function so it can be simplified in advance if the arguments are known
31     */

32
33     public Expression simplify() throws XPathException {
34         int numArgs = checkArgumentCount(2, Integer.MAX_VALUE);
35         boolean allKnown = true;
36         for (int i=0; i<numArgs; i++) {
37             argument[i] = argument[i].simplify();
38             if (!(argument[i] instanceof Value)) {
39                 allKnown = false;
40             }
41         }
42         if (allKnown) {
43             return evaluate(null);
44         }
45         return this;
46     }
47
48     /**
49     * Evaluate the function in a string context
50     */

51
52     public String JavaDoc evaluateAsString(Context c) throws XPathException {
53         int numArgs = getNumberOfArguments();
54
55         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
56         for (int i=0; i<numArgs; i++) {
57             sb.append(argument[i].evaluateAsString(c));
58         }
59         
60         return sb.toString();
61     }
62
63     /**
64     * Evaluate in a general context
65     */

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

74
75     public int getDependencies() {
76         int numArgs = getNumberOfArguments();
77         int dep = 0;
78         for (int i=0; i<numArgs; i++) {
79             dep |= argument[i].getDependencies();
80         }
81         return dep;
82     }
83
84     /**
85     * Reduce the dependencies
86     */

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