KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > functions > SystemProperty


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         // the second time checkArguments is called, it's a global check so the static context is inaccurate
18

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             // we need to save the namespace context
25
nsContext = env.getNamespaceResolver();
26         }
27     }
28
29     /**
30     * preEvaluate: this method performs compile-time evaluation
31     */

32
33     public Expression preEvaluate(StaticContext env) throws XPathException {
34         CharSequence JavaDoc name = ((StringValue)argument[0]).getStringValueCS();
35
36         try {
37             String JavaDoc[] parts = Name.getQNameParts(name);
38             String JavaDoc prefix = parts[0];
39             String JavaDoc lname = parts[1];
40             String JavaDoc 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     /**
54     * Evaluate the function at run-time
55     */

56
57     public Item evaluateItem(XPathContext context) throws XPathException {
58
59         CharSequence JavaDoc name = argument[0].evaluateItem(context).getStringValueCS();
60
61         try {
62             String JavaDoc[] parts = Name.getQNameParts(name);
63             String JavaDoc prefix = parts[0];
64             String JavaDoc lname = parts[1];
65             String JavaDoc 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     /**
79     * Here's the real code:
80     */

81
82     public static String JavaDoc getProperty(String JavaDoc uri, String JavaDoc 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 JavaDoc val = System.getProperty(local);
106             if (val==null) val="";
107             return val;
108         } else {
109             return "";
110         }
111     }
112
113 }
114
115
116 //
117
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
118
// you may not use this file except in compliance with the License. You may obtain a copy of the
119
// License at http://www.mozilla.org/MPL/
120
//
121
// Software distributed under the License is distributed on an "AS IS" basis,
122
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
123
// See the License for the specific language governing rights and limitations under the License.
124
//
125
// The Original Code is: all this file.
126
//
127
// The Initial Developer of the Original Code is Michael H. Kay.
128
//
129
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
130
//
131
// Contributor(s): none.
132
//
133
Popular Tags