1 package com.icl.saxon.functions; 2 import com.icl.saxon.*; 3 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 12 13 14 public class Lang extends Function { 15 16 19 20 public String getName() { 21 return "lang"; 22 }; 23 24 28 29 public int getDataType() { 30 return Value.BOOLEAN; 31 } 32 33 36 37 public Expression simplify() throws XPathException { 38 checkArgumentCount(1, 1); 39 argument[0] = argument[0].simplify(); 40 return this; 41 } 42 43 46 47 public boolean evaluateAsBoolean(Context c) throws XPathException { 48 return isLang(argument[0].evaluateAsString(c), c); 49 } 50 51 54 55 public Value evaluate(Context c) throws XPathException { 56 return new BooleanValue(evaluateAsBoolean(c)); 57 } 58 59 62 63 public int getDependencies() { 64 return Context.CONTEXT_NODE | argument[0].getDependencies(); 65 } 66 67 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 83 84 private static boolean isLang(String arglang, Context context) throws XPathException { 85 86 NodeInfo node = context.getContextNodeInfo(); 87 88 String 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 | Popular Tags |