KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.icl.saxon.functions;
2 import com.icl.saxon.*;
3 //import com.icl.saxon.om.Name;
4
import com.icl.saxon.om.Namespace;
5 import com.icl.saxon.om.NodeInfo;
6 import com.icl.saxon.expr.*;
7
8 import java.util.*;
9 //import java.lang.Math;
10
//import java.text.*;
11

12
13
14 public class Lang extends Function {
15
16     /**
17     * Function name (for diagnostics)
18     */

19
20     public String JavaDoc getName() {
21         return "lang";
22     };
23
24     /**
25     * Determine the data type of the expression
26     * @return Value.BOOLEAN
27     */

28
29     public int getDataType() {
30         return Value.BOOLEAN;
31     }
32
33     /**
34     * Simplify and validate.
35     */

36
37     public Expression simplify() throws XPathException {
38         checkArgumentCount(1, 1);
39         argument[0] = argument[0].simplify();
40         return this;
41     }
42
43     /**
44     * Evaluate the function in a boolean context
45     */

46
47     public boolean evaluateAsBoolean(Context c) throws XPathException {
48         return isLang(argument[0].evaluateAsString(c), c);
49     }
50
51     /**
52     * Evaluate in a general context
53     */

54
55     public Value evaluate(Context c) throws XPathException {
56         return new BooleanValue(evaluateAsBoolean(c));
57     }
58
59     /**
60     * Determine the dependencies
61     */

62
63     public int getDependencies() {
64         return Context.CONTEXT_NODE | argument[0].getDependencies();
65     }
66
67     /**
68     * Reduce the dependencies
69     */

70
71     public Expression reduce(int dep, Context c) throws XPathException {
72         Lang f = new Lang();
73         f.addArgument(argument[0].reduce(dep, c));
74         f.setStaticContext(getStaticContext());
75         return f.simplify();
76     }
77
78     /**
79     * Test whether the context node has the given language attribute
80     * @param arglang the language being tested
81     * @param context the context, to identify the context node
82     */

83     
84     private static boolean isLang(String JavaDoc arglang, Context context) throws XPathException {
85
86         NodeInfo node = context.getContextNodeInfo();
87        
88         String JavaDoc doclang=null;
89         
90         while(node!=null) {
91             doclang = node.getAttributeValue(Namespace.XML, "lang");
92             if (doclang!=null) break;
93             node=(NodeInfo)node.getParent();
94         }
95
96         if (doclang==null) return false;
97         
98         if (arglang.equalsIgnoreCase(doclang)) return true;
99         int hyphen = doclang.indexOf("-");
100         if (hyphen<0) return false;
101         doclang = doclang.substring(0, hyphen);
102         if (arglang.equalsIgnoreCase(doclang)) return true;
103         return false;
104     }
105
106
107 }
108
109
110 //
111
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
112
// you may not use this file except in compliance with the License. You may obtain a copy of the
113
// License at http://www.mozilla.org/MPL/
114
//
115
// Software distributed under the License is distributed on an "AS IS" basis,
116
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
117
// See the License for the specific language governing rights and limitations under the License.
118
//
119
// The Original Code is: all this file.
120
//
121
// The Initial Developer of the Original Code is
122
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
123
//
124
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
125
//
126
// Contributor(s): none.
127
//
128
Popular Tags