KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.functions;
2 import net.sf.saxon.expr.*;
3 import net.sf.saxon.om.Item;
4 import net.sf.saxon.om.FastStringBuffer;
5 import net.sf.saxon.trans.XPathException;
6 import net.sf.saxon.value.AtomicValue;
7 import net.sf.saxon.value.StringValue;
8
9 import java.util.StringTokenizer JavaDoc;
10
11 /**
12  * Implement the XPath normalize-space() function
13  */

14
15 public class NormalizeSpace extends SystemFunction {
16
17     /**
18     * Simplify and validate.
19     */

20
21      public Expression simplify(StaticContext env) throws XPathException {
22         return simplifyArguments(env);
23     }
24     /**
25      * Determine the intrinsic dependencies of an expression, that is, those which are not derived
26      * from the dependencies of its subexpressions. For example, position() has an intrinsic dependency
27      * on the context position, while (position()+1) does not. The default implementation
28      * of the method returns 0, indicating "no dependencies".
29      *
30      * @return a set of bit-significant flags identifying the "intrinsic"
31      * dependencies. The flags are documented in class net.sf.saxon.value.StaticProperty
32      */

33
34     public int getIntrinsicDependencies() {
35         int d = super.getIntrinsicDependencies();
36         if (argument.length == 0) {
37             d |= StaticProperty.DEPENDS_ON_CONTEXT_ITEM;
38         }
39         return d;
40     }
41
42     /**
43     * Pre-evaluate a function at compile time. Functions that do not allow
44     * pre-evaluation, or that need access to context information, can override this method.
45     */

46
47     public Expression preEvaluate(StaticContext env) throws XPathException {
48         if (argument.length == 0) {
49             return this;
50         } else {
51             return ExpressionTool.eagerEvaluate(this, null);
52         }
53     }
54
55     /**
56     * Evaluate in a general context
57     */

58
59     public Item evaluateItem(XPathContext c) throws XPathException {
60         if (argument.length == 0) {
61             return StringValue.makeStringValue(normalize(c.getContextItem().getStringValueCS()));
62         } else {
63             AtomicValue sv = (AtomicValue)argument[0].evaluateItem(c);
64             if (sv==null) return StringValue.EMPTY_STRING;
65             return StringValue.makeStringValue(normalize(sv.getStringValueCS()));
66         }
67     }
68
69     /**
70     * The algorithm that does the work
71     */

72
73     public static CharSequence JavaDoc normalize(CharSequence JavaDoc s) {
74         FastStringBuffer sb = new FastStringBuffer(s.length());
75         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(s.toString());
76         while (st.hasMoreTokens()) {
77             sb.append(st.nextToken());
78             if (st.hasMoreTokens()) {
79                 sb.append(' ');
80             }
81         }
82         return sb.condense();
83     }
84
85 }
86
87
88
89
90 //
91
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
92
// you may not use this file except in compliance with the License. You may obtain a copy of the
93
// License at http://www.mozilla.org/MPL/
94
//
95
// Software distributed under the License is distributed on an "AS IS" basis,
96
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
97
// See the License for the specific language governing rights and limitations under the License.
98
//
99
// The Original Code is: all this file.
100
//
101
// The Initial Developer of the Original Code is Michael H. Kay.
102
//
103
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
104
//
105
// Contributor(s): none.
106
//
107
Popular Tags