KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > functions > SystemProperty


1 package com.icl.saxon.functions;
2 import com.icl.saxon.*;
3 import com.icl.saxon.om.Name;
4 import com.icl.saxon.om.Namespace;
5 import com.icl.saxon.expr.*;
6
7 import java.util.*;
8 import java.lang.Math JavaDoc;
9 import java.text.*;
10
11
12
13 public class SystemProperty extends Function {
14
15     public String JavaDoc getName() {
16         return "system-property";
17     };
18
19     /**
20     * Determine the data type of the expression
21     * @return Value.ANY (meaning not known in advance)
22     */

23
24     public int getDataType() {
25         return Value.ANY;
26     }
27
28     /**
29     * Validate and simplify
30     */

31
32     public Expression simplify() throws XPathException {
33         checkArgumentCount(1, 1);
34         argument[0] = argument[0].simplify();
35         if (argument[0] instanceof Value) {
36             return evaluate(null);
37         }
38         return this;
39     }
40
41     /**
42     * Evaluate the function
43     */

44     
45     public Value evaluate(Context context) throws XPathException {
46         String JavaDoc name = argument[0].evaluateAsString(context);
47         if (!Name.isQName(name)) {
48             throw new XPathException("Argument " + name + " is not a valid QName");
49         }
50         String JavaDoc prefix = Name.getPrefix(name);
51         String JavaDoc lname = Name.getLocalName(name);
52         String JavaDoc uri;
53         if (prefix.equals("")) {
54             uri = "";
55         } else {
56             uri = getStaticContext().getURIForPrefix(prefix);
57         }
58         return getProperty(uri, lname);
59     }
60
61     /**
62     * Here's the real code:
63     */

64
65     public static Value getProperty(String JavaDoc uri, String JavaDoc local) {
66         if (uri.equals(Namespace.XSLT)) {
67             if (local.equals("version"))
68                 return new NumericValue(Version.getXSLVersion());
69             if (local.equals("vendor"))
70                 return new StringValue(Version.getProductName());
71             if (local.equals("vendor-url"))
72                 return new StringValue(Version.getWebSiteAddress());
73             return new StringValue("");
74             
75         } else if (uri.equals("")) {
76             String JavaDoc val = System.getProperty(local);
77             if (val==null) val="";
78             return new StringValue(val);
79         } else {
80             return new StringValue("");
81         }
82     }
83
84     /**
85     * Get dependencies
86     */

87
88     public int getDependencies() {
89         return argument[0].getDependencies();
90     }
91
92     /**
93     * Remove dependencies
94     */

95
96     public Expression reduce(int dep, Context c) throws XPathException {
97         SystemProperty f = new SystemProperty();
98         f.addArgument(argument[0].reduce(dep, c));
99         f.setStaticContext(getStaticContext());
100         return f;
101     }
102
103
104 }
105
106
107 //
108
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
109
// you may not use this file except in compliance with the License. You may obtain a copy of the
110
// License at http://www.mozilla.org/MPL/
111
//
112
// Software distributed under the License is distributed on an "AS IS" basis,
113
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
114
// See the License for the specific language governing rights and limitations under the License.
115
//
116
// The Original Code is: all this file.
117
//
118
// The Initial Developer of the Original Code is
119
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
120
//
121
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
122
//
123
// Contributor(s): none.
124
//
125
Popular Tags