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 19 20 public Expression preEvaluate(StaticContext env) { 21 return this; 22 } 23 24 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 55 56 public int getIntrinsicDependencies() { 57 return StaticProperty.DEPENDS_ON_CONTEXT_ITEM; 58 } 59 60 65 66 private boolean isLang(String arglang, NodeInfo target) { 67 68 69 String 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 | Popular Tags |