1 package net.sf.saxon.functions; 2 import net.sf.saxon.Configuration; 3 import net.sf.saxon.Version; 4 import net.sf.saxon.expr.Expression; 5 import net.sf.saxon.expr.StaticContext; 6 import net.sf.saxon.expr.XPathContext; 7 import net.sf.saxon.om.*; 8 import net.sf.saxon.trans.StaticError; 9 import net.sf.saxon.trans.XPathException; 10 import net.sf.saxon.value.StringValue; 11 12 13 public class SystemProperty extends SystemFunction implements XSLTFunction { 14 15 private NamespaceResolver nsContext; 16 private transient boolean checked = false; 17 19 public void checkArguments(StaticContext env) throws XPathException { 20 if (checked) return; 21 checked = true; 22 super.checkArguments(env); 23 if (!(argument[0] instanceof StringValue)) { 24 nsContext = env.getNamespaceResolver(); 26 } 27 } 28 29 32 33 public Expression preEvaluate(StaticContext env) throws XPathException { 34 CharSequence name = ((StringValue)argument[0]).getStringValueCS(); 35 36 try { 37 String [] parts = Name.getQNameParts(name); 38 String prefix = parts[0]; 39 String lname = parts[1]; 40 String uri; 41 if (prefix.equals("")) { 42 uri = ""; 43 } else { 44 uri = env.getURIForPrefix(prefix); 45 } 46 return new StringValue(getProperty(uri, lname, env.getConfiguration())); 47 } catch (QNameException e) { 48 throw new StaticError("Invalid system property name. " + e.getMessage()); 49 } 50 } 51 52 53 56 57 public Item evaluateItem(XPathContext context) throws XPathException { 58 59 CharSequence name = argument[0].evaluateItem(context).getStringValueCS(); 60 61 try { 62 String [] parts = Name.getQNameParts(name); 63 String prefix = parts[0]; 64 String lname = parts[1]; 65 String uri; 66 if (prefix.equals("")) { 67 uri = ""; 68 } else { 69 uri = nsContext.getURIForPrefix(prefix, false); 70 } 71 return new StringValue(getProperty(uri, lname, context.getController().getConfiguration())); 72 } catch (QNameException e) { 73 dynamicError("Invalid system property name. " + e.getMessage(), "XTDE1390", context); 74 return null; 75 } 76 } 77 78 81 82 public static String getProperty(String uri, String local, Configuration config) { 83 if (uri.equals(NamespaceConstant.XSLT)) { 84 if (local.equals("version")) 85 return Version.getXSLVersionString(); 86 if (local.equals("vendor")) 87 return Version.getProductTitle(); 88 if (local.equals("vendor-url")) 89 return Version.getWebSiteAddress(); 90 if (local.equals("product-name")) 91 return Version.getProductName(); 92 if (local.equals("product-version")) 93 return config.isSchemaAware(Configuration.XSLT) ? 94 Version.getSchemaAwareProductVersion() : 95 Version.getProductVersion(); 96 if (local.equals("is-schema-aware")) 97 return config.isSchemaAware(Configuration.XSLT) ? "yes" : "no"; 98 if (local.equals("supports-serialization")) 99 return "yes"; 100 if (local.equals("supports-backwards-compatibility")) 101 return "yes"; 102 return ""; 103 104 } else if (uri.equals("")) { 105 String val = System.getProperty(local); 106 if (val==null) val=""; 107 return val; 108 } else { 109 return ""; 110 } 111 } 112 113 } 114 115 116 | Popular Tags |