Debian Codegraph @

net/sf/saxon/functions/SystemProperty.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.functions;
9
10 import com.saxonica.functions.extfn.EXPathArchive.Archive;
11 import com.saxonica.functions.extfn.EXPathBinary;
12 import com.saxonica.functions.extfn.EXPathFile;
13 import net.sf.saxon.Configuration;
14 import net.sf.saxon.Version;
15 import net.sf.saxon.expr.*;
16 import net.sf.saxon.expr.parser.ContextItemStaticInfo;
17 import net.sf.saxon.expr.parser.ExpressionVisitor;
18 import net.sf.saxon.expr.parser.RetainedStaticContext;
19 import net.sf.saxon.lib.Feature;
20 import net.sf.saxon.lib.NamespaceConstant;
21 import net.sf.saxon.om.Sequence;
22 import net.sf.saxon.om.StructuredQName;
23 import net.sf.saxon.trans.XPathException;
24 import net.sf.saxon.value.StringValue;
25
26
27 /**
28 * Implementation of the XSLT system-property() function
29 */
30
31 public class SystemProperty extends SystemFunction implements Callable {
32
33 /**
34 * Allow the function to create an optimized call based on the values of the actual arguments
35 *
36 * @param visitor the expression visitor
37 * @param contextInfo information about the context item
38 * @param arguments the supplied arguments to the function call. Note: modifying the contents
39 * of this array should not be attempted, it is likely to have no effect.
40 * @return either a function call on this function, or an expression that delivers
41 * the same result, or null indicating that no optimization has taken place
42 * @throws XPathException if an error is detected
43 */
44 @Override
45 public Expression makeOptimizedFunctionCall(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo, Expression... arguments) throws XPathException {
46 if (arguments[0] instanceof Literal) {
47 try {
48 StringValue name = (StringValue) ((Literal) arguments[0]).getValue();
49 StructuredQName qName = StructuredQName.fromLexicalQName(name.getStringValue(),
50 false, true,
51 getRetainedStaticContext());
52 String uri = qName.getURI();
53 String local = qName.getLocalPart();
54 if (uri.equals(NamespaceConstant.XSLT) &&
55 (local.equals("version") || local.equals("vendor") ||
56 local.equals("vendor-url") || local.equals("product-name") ||
57 local.equals("product-version") || local.equals("supports-backwards-compatibility") ||
58 local.equals("xpath-version") || local.equals("xsd-version"))) {
59 String result = getProperty(uri, local, getRetainedStaticContext());
60 return new StringLiteral(result);
61 }
62 } catch (XPathException e) {
63 // no action
64 }
65 }
66 return null;
67 }
68
69 /**
70 * Evaluate the function call
71 *
72 * @param context the dynamic evaluation context
73 * @param arguments the values of the arguments, supplied as SequenceIterators
74 * @return the result of the evaluation, in the form of a SequenceIterator
75 * @throws net.sf.saxon.trans.XPathException
76 * if a dynamic error occurs during the evaluation of the expression
77 */
78 public StringValue call(XPathContext context, Sequence[] arguments) throws XPathException {
79
80 StringValue name = (StringValue) arguments[0].head();
81 try {
82 StructuredQName qName = StructuredQName.fromLexicalQName(name.getStringValue(),
83 false, true,
84 getRetainedStaticContext());
85
86 return new StringValue(getProperty(
87 qName.getURI(), qName.getLocalPart(), getRetainedStaticContext()));
88
89 } catch (XPathException err) {
90 throw new XPathException("Invalid system property name. " + err.getMessage(), "XTDE1390", context);
91 }
92 }
93
94 private boolean allowsEarlyEvaluation(Sequence[] arguments, XPathContext context) throws XPathException {
95 StringValue name = (StringValue) arguments[0].head();
96 try {
97 StructuredQName qName = StructuredQName.fromLexicalQName(name.getStringValue(),
98 false, true,
99 getRetainedStaticContext());
100 String uri = qName.getURI();
101 String local = qName.getLocalPart();
102 return uri.equals(NamespaceConstant.XSLT) &&
103 (local.equals("version") || local.equals("vendor") ||
104 local.equals("vendor-url") || local.equals("product-name") ||
105 local.equals("product-version") || local.equals("supports-backwards-compatibility") ||
106 local.equals("xpath-version") || local.equals("xsd-version"));
107
108 } catch (XPathException err) {
109 throw new XPathException("Invalid system property name. " + err.getMessage(), "XTDE1390", context);
110 }
111
112 }
113
114 public static String yesOrNo(boolean whatever) {
115 return whatever ? "yes" : "no";
116 }
117
118 /**
119 * Here's the real code:
120 *
121 * @param uri the namespace URI of the system property name
122 * @param local the local part of the system property name
123 * @param rsc context information
124 * @return the value of the corresponding system property
125 */
126
127 public static String getProperty(String uri, String local, RetainedStaticContext rsc) {
128 Configuration config = rsc.getConfiguration();
129 String edition = rsc.getPackageData().getTargetEdition();
130 if (uri.equals(NamespaceConstant.XSLT)) {
131 switch (local) {
132 case "version":
133 return "3.0";
134 case "vendor":
135 return Version.getProductVendor();
136 case "vendor-url":
137 return Version.getWebSiteAddress();
138 case "product-name":
139 return Version.getProductName();
140 case "product-version":
141 return Version.getProductVariantAndVersion(edition);
142 case "is-schema-aware":
143 boolean schemaAware = rsc.getPackageData().isSchemaAware();
144 return yesOrNo(schemaAware);
145 case "supports-serialization":
146 return yesOrNo(!"JS".equals(edition));
147 case "supports-backwards-compatibility":
148 return "yes";
149 case "supports-namespace-axis":
150 return "yes";
151 case "supports-streaming":
152 return yesOrNo(
153 "EE".equals(edition) &&
154 config.isLicensedFeature(Configuration.LicenseFeature.ENTERPRISE_XSLT) &&
155 !config.getConfigurationProperty(Feature.STREAMABILITY).equals("off"));
156 case "supports-dynamic-evaluation":
157 return yesOrNo(
158 !"JS".equals(edition) &&
159 config.isLicensedFeature(Configuration.LicenseFeature.PROFESSIONAL_EDITION) &&
160 !config.getBooleanProperty(Feature.DISABLE_XSL_EVALUATE));
161 case "supports-higher-order-functions":
162 return yesOrNo(
163 !"JS".equals(edition) &&
164 !"HE".equals(edition) &&
165 config.isLicensedFeature(Configuration.LicenseFeature.PROFESSIONAL_EDITION));
166 case "xpath-version":
167 return "3.1";
168 case "xsd-version":
169 return rsc.getConfiguration().getXsdVersion() == Configuration.XSD10 ? "1.0" : "1.1";
170 }
171 return "";
172
173 } else if (uri.isEmpty() && config.getBooleanProperty(Feature.ALLOW_EXTERNAL_FUNCTIONS)) {
174 String val = System.getProperty(local);
175 return val == null ? "" : val;
176 //#if EE==true || PE==true
177 } else if (uri.equals(EXPathBinary.NAMESPACE) && local.equals("version")) {
178 return Double.toString(EXPathBinary.VERSION);
179 } else if (uri.equals(EXPathFile.NAMESPACE) && local.equals("version")) {
180 return EXPathFile.VERSION.toString();
181 } else if (uri.equals(Archive.NAMESPACE) && local.equals("version")) {
182 return Archive.VERSION.toString();
183 //#endif
184 } else {
185 return "";
186 }
187 }
188
189 }
190