KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.icl.saxon.functions;
2 import com.icl.saxon.*;
3 import com.icl.saxon.expr.*;
4 import com.icl.saxon.om.NodeInfo;
5
6 import java.util.*;
7 import java.text.*;
8
9
10
11 public class NormalizeSpace extends Function {
12
13     /**
14     * Function name (for diagnostics)
15     */

16
17     public String JavaDoc getName() {
18         return "normalize-space";
19     };
20
21     /**
22     * Determine the data type of the expression
23     * @return Value.STRING
24     */

25
26     public int getDataType() {
27         return Value.STRING;
28     }
29
30     /**
31     * Simplify and validate.
32     */

33
34     public Expression simplify() throws XPathException {
35         int numArgs = checkArgumentCount(0, 1);
36         if (numArgs==1) {
37             argument[0] = argument[0].simplify();
38             if (argument[0] instanceof Value) {
39                 return evaluate(null);
40             }
41         }
42         return this;
43     }
44
45     /**
46     * Evaluate the function in a string context
47     */

48
49     public String JavaDoc evaluateAsString(Context c) throws XPathException {
50         if (getNumberOfArguments()==1) {
51             return normalize(argument[0].evaluateAsString(c));
52         } else {
53             return normalize((c.getContextNodeInfo()).getStringValue());
54         }
55     }
56
57     /**
58     * Evaluate in a general context
59     */

60
61     public Value evaluate(Context c) throws XPathException {
62         return new StringValue(evaluateAsString(c));
63     }
64
65     /**
66     * Determine the dependencies
67     */

68
69     public int getDependencies() {
70         if (getNumberOfArguments()==1) {
71             return argument[0].getDependencies();
72         } else {
73             return Context.CONTEXT_NODE;
74         }
75     }
76
77     /**
78     * Reduce the dependencies
79     */

80
81     public Expression reduce(int dep, Context c) throws XPathException {
82         if (getNumberOfArguments()==1) {
83             NormalizeSpace f = new NormalizeSpace();
84             f.addArgument(argument[0].reduce(dep, c));
85             f.setStaticContext(getStaticContext());
86             return f.simplify();
87         } else {
88             if ((dep & Context.CONTEXT_NODE)!=0) {
89                 return evaluate(c);
90             } else {
91                 return this;
92             }
93         }
94     }
95
96     /**
97     * The algorithm that does the work
98     */

99
100     private static String JavaDoc normalize(String JavaDoc s) {
101         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
102         StringTokenizer st = new StringTokenizer(s);
103         while (st.hasMoreTokens()) {
104             sb.append(st.nextToken());
105             if (st.hasMoreTokens()) sb.append(" ");
106         }
107         return sb.toString();
108     }
109
110 }
111
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
127
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
128
//
129
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
130
//
131
// Contributor(s): none.
132
//
133
Popular Tags