Debian Codegraph @

net/sf/saxon/functions/Lang.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.functions;
9
10 import net.sf.saxon.expr.XPathContext;
11 import net.sf.saxon.lib.NamespaceConstant;
12 import net.sf.saxon.om.Item;
13 import net.sf.saxon.om.NodeInfo;
14 import net.sf.saxon.om.Sequence;
15 import net.sf.saxon.trans.XPathException;
16 import net.sf.saxon.value.BooleanValue;
17
18
19 public class Lang extends SystemFunction {
20
21 /**
22 * Test whether the context node has the given language attribute
23 *
24 * @param arglang the language being tested
25 * @param target the target node
26 * @return true if the node is tagged with this language code
27 */
28
29 public static boolean isLang(String arglang, NodeInfo target) {
30 String doclang = null;
31 NodeInfo node = target;
32
33 while (node != null) {
34 doclang = node.getAttributeValue(NamespaceConstant.XML, "lang");
35 if (doclang != null) {
36 break;
37 }
38 node = node.getParent();
39 if (node == null) {
40 return false;
41 }
42 }
43
44 if (doclang == null) {
45 return false;
46 }
47
48 while (true) {
49 if (arglang.equalsIgnoreCase(doclang)) {
50 return true;
51 }
52 int hyphen = doclang.lastIndexOf("-");
53 if (hyphen < 0) {
54 return false;
55 }
56 doclang = doclang.substring(0, hyphen);
57 }
58 }
59
60 /**
61 * Evaluate the expression
62 *
63 * @param context the dynamic evaluation context
64 * @param arguments the values of the arguments, supplied as SequenceIterators
65 * @return the result of the evaluation, in the form of a SequenceIterator
66 * @throws net.sf.saxon.trans.XPathException
67 * if a dynamic error occurs during the evaluation of the expression
68 */
69 public BooleanValue call(XPathContext context, Sequence[] arguments) throws XPathException {
70 NodeInfo target;
71 if (arguments.length > 1) {
72 target = (NodeInfo) arguments[1].head();
73 } else {
74 target = getAndCheckContextItem(context);
75 }
76 final Item arg0Val = arguments[0].head();
77 final String testLang = arg0Val == null ? "" : arg0Val.getStringValue();
78 return BooleanValue.get(isLang(testLang, target));
79 }
80
81 /**
82 * Get the context item, checking that it exists and is a node
83 *
84 * @param context the XPath dynamic context
85 * @return the context node
86 * @throws XPathException if there is no context item or if the context item is not a node
87 */
88
89 private NodeInfo getAndCheckContextItem(XPathContext context) throws XPathException {
90 NodeInfo target;
91 Item current = context.getContextItem();
92 if (current == null) {
93 XPathException err = new XPathException("The context item for lang() is absent");
94 err.setErrorCode("XPDY0002");
95 err.setXPathContext(context);
96 throw err;
97 }
98 if (!(current instanceof NodeInfo)) {
99 XPathException err = new XPathException("The context item for lang() is not a node");
100 err.setErrorCode("XPTY0004");
101 err.setXPathContext(context);
102 throw err;
103 }
104 target = (NodeInfo) current;
105 return target;
106 }
107 }
108