| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 29 |
|
| 30 |
|
| 31 |
public class SystemProperty extends SystemFunction implements Callable { |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 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 |
|
| 64 |
} |
| 65 |
} |
| 66 |
return null; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 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 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 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 |
|
| 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 |
|
| 184 |
} else { |
| 185 |
return ""; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
} |
| 190 |
|