001: package net.sf.saxon.functions;
002:
003: import net.sf.saxon.Configuration;
004: import net.sf.saxon.Version;
005: import net.sf.saxon.expr.Expression;
006: import net.sf.saxon.expr.StaticContext;
007: import net.sf.saxon.expr.XPathContext;
008: import net.sf.saxon.om.*;
009: import net.sf.saxon.trans.StaticError;
010: import net.sf.saxon.trans.XPathException;
011: import net.sf.saxon.value.StringValue;
012:
013: public class SystemProperty extends SystemFunction implements
014: XSLTFunction {
015:
016: private NamespaceResolver nsContext;
017: private transient boolean checked = false;
018:
019: // the second time checkArguments is called, it's a global check so the static context is inaccurate
020:
021: public void checkArguments(StaticContext env) throws XPathException {
022: if (checked)
023: return;
024: checked = true;
025: super .checkArguments(env);
026: if (!(argument[0] instanceof StringValue)) {
027: // we need to save the namespace context
028: nsContext = env.getNamespaceResolver();
029: }
030: }
031:
032: /**
033: * preEvaluate: this method performs compile-time evaluation
034: */
035:
036: public Expression preEvaluate(StaticContext env)
037: throws XPathException {
038: CharSequence name = ((StringValue) argument[0])
039: .getStringValueCS();
040:
041: try {
042: String[] parts = env.getConfiguration().getNameChecker()
043: .getQNameParts(name);
044: String prefix = parts[0];
045: String lname = parts[1];
046: String uri;
047: if (prefix.equals("")) {
048: uri = "";
049: } else {
050: uri = env.getURIForPrefix(prefix);
051: }
052: return new StringValue(getProperty(uri, lname, env
053: .getConfiguration()));
054: } catch (QNameException e) {
055: throw new StaticError("Invalid system property name. "
056: + e.getMessage());
057: }
058: }
059:
060: /**
061: * Evaluate the function at run-time
062: */
063:
064: public Item evaluateItem(XPathContext context)
065: throws XPathException {
066:
067: CharSequence name = argument[0].evaluateItem(context)
068: .getStringValueCS();
069:
070: try {
071: String[] parts = context.getConfiguration()
072: .getNameChecker().getQNameParts(name);
073: String prefix = parts[0];
074: String lname = parts[1];
075: String uri;
076: if (prefix.equals("")) {
077: uri = "";
078: } else {
079: uri = nsContext.getURIForPrefix(prefix, false);
080: }
081: return new StringValue(getProperty(uri, lname, context
082: .getConfiguration()));
083: } catch (QNameException e) {
084: dynamicError("Invalid system property name. "
085: + e.getMessage(), "XTDE1390", context);
086: return null;
087: }
088: }
089:
090: /**
091: * Here's the real code:
092: */
093:
094: public static String getProperty(String uri, String local,
095: Configuration config) {
096: if (uri.equals(NamespaceConstant.XSLT)) {
097: if (local.equals("version"))
098: return Version.getXSLVersionString();
099: if (local.equals("vendor"))
100: return Version.getProductTitle();
101: if (local.equals("vendor-url"))
102: return Version.getWebSiteAddress();
103: if (local.equals("product-name"))
104: return Version.getProductName();
105: if (local.equals("product-version"))
106: return config.isSchemaAware(Configuration.XSLT) ? Version
107: .getSchemaAwareProductVersion()
108: : Version.getProductVersion();
109: if (local.equals("is-schema-aware"))
110: return config.isSchemaAware(Configuration.XSLT) ? "yes"
111: : "no";
112: if (local.equals("supports-serialization"))
113: return "yes";
114: if (local.equals("supports-backwards-compatibility"))
115: return "yes";
116: return "";
117:
118: } else if (uri.equals("")) {
119: String val = System.getProperty(local);
120: if (val == null)
121: val = "";
122: return val;
123: } else {
124: return "";
125: }
126: }
127:
128: }
129:
130: //
131: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
132: // you may not use this file except in compliance with the License. You may obtain a copy of the
133: // License at http://www.mozilla.org/MPL/
134: //
135: // Software distributed under the License is distributed on an "AS IS" basis,
136: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
137: // See the License for the specific language governing rights and limitations under the License.
138: //
139: // The Original Code is: all this file.
140: //
141: // The Initial Developer of the Original Code is Michael H. Kay.
142: //
143: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
144: //
145: // Contributor(s): none.
146: //
|