KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.functions;
2 import net.sf.saxon.expr.Expression;
3 import net.sf.saxon.expr.StaticContext;
4 import net.sf.saxon.expr.StaticProperty;
5 import net.sf.saxon.expr.XPathContext;
6 import net.sf.saxon.om.Item;
7 import net.sf.saxon.om.NodeInfo;
8 import net.sf.saxon.style.StandardNames;
9 import net.sf.saxon.trans.DynamicError;
10 import net.sf.saxon.trans.XPathException;
11 import net.sf.saxon.value.BooleanValue;
12
13
14 public class Lang extends SystemFunction {
15
16     /**
17     * preEvaluate: this method suppresses compile-time evaluation by doing nothing
18     */

19
20     public Expression preEvaluate(StaticContext env) {
21         return this;
22     }
23
24     /**
25     * Evaluate in a general context
26     */

27
28     public Item evaluateItem(XPathContext c) throws XPathException {
29         NodeInfo target;
30         if (argument.length > 1) {
31             target = (NodeInfo)argument[1].evaluateItem(c);
32         } else {
33             Item current = c.getContextItem();
34             if (current==null) {
35                 DynamicError err = new DynamicError("The context item is undefined");
36                 err.setErrorCode("FONC0001");
37                 err.setXPathContext(c);
38                 throw err;
39             }
40             if (!(current instanceof NodeInfo)) {
41                 DynamicError err = new DynamicError("The context item is not a node");
42                 err.setErrorCode("FOTY0011");
43                 err.setXPathContext(c);
44                 throw err;
45             }
46             target = (NodeInfo)current;
47         }
48         boolean b = isLang(argument[0].evaluateItem(c).getStringValue(), target);
49         return BooleanValue.get(b);
50     }
51
52     /**
53     * Determine the dependencies
54     */

55
56     public int getIntrinsicDependencies() {
57         return StaticProperty.DEPENDS_ON_CONTEXT_ITEM;
58     }
59
60     /**
61     * Test whether the context node has the given language attribute
62     * @param arglang the language being tested
63     * @param target the target node
64     */

65
66     private boolean isLang(String JavaDoc arglang, NodeInfo target) {
67
68
69         String JavaDoc doclang = null;
70         NodeInfo node = target;
71
72         while(node!=null) {
73             doclang = node.getAttributeValue(StandardNames.XML_LANG);
74             if (doclang!=null) break;
75             node = node.getParent();
76             if (node==null) return false;
77         }
78
79         if (doclang==null) return false;
80
81         if (arglang.equalsIgnoreCase(doclang)) return true;
82         int hyphen = doclang.indexOf("-");
83         if (hyphen<0) return false;
84         doclang = doclang.substring(0, hyphen);
85         if (arglang.equalsIgnoreCase(doclang)) return true;
86         return false;
87     }
88
89
90 }
91
92
93 //
94
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
95
// you may not use this file except in compliance with the License. You may obtain a copy of the
96
// License at http://www.mozilla.org/MPL/
97
//
98
// Software distributed under the License is distributed on an "AS IS" basis,
99
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
100
// See the License for the specific language governing rights and limitations under the License.
101
//
102
// The Original Code is: all this file.
103
//
104
// The Initial Developer of the Original Code is Michael H. Kay.
105
//
106
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
107
//
108
// Contributor(s): none.
109
//
110
Popular Tags