| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 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 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 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 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 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 |
|