/
SystemProperty.java
160 lines (140 loc) · 6.37 KB
/
SystemProperty.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.xmlcalabash.functions;
import com.xmlcalabash.core.XProcException;
import com.xmlcalabash.runtime.XCompoundStep;
import com.xmlcalabash.runtime.XStep;
import net.sf.saxon.expr.Expression;
import net.sf.saxon.expr.StaticContext;
import net.sf.saxon.expr.XPathContext;
import net.sf.saxon.lib.ExtensionFunctionCall;
import net.sf.saxon.lib.ExtensionFunctionDefinition;
import net.sf.saxon.om.SequenceIterator;
import net.sf.saxon.om.StructuredQName;
import net.sf.saxon.trans.XPathException;
import net.sf.saxon.tree.iter.SingletonIterator;
import net.sf.saxon.value.StringValue;
import net.sf.saxon.value.SequenceType;
import com.xmlcalabash.core.XProcConstants;
import com.xmlcalabash.core.XProcRuntime;
/**
* Implementation of the XSLT system-property() function
*/
public class SystemProperty extends ExtensionFunctionDefinition {
private static StructuredQName funcname = new StructuredQName("p", XProcConstants.NS_XPROC, "system-property");
private ThreadLocal tl_runtime = new ThreadLocal() {
protected synchronized Object initialValue() {
return null;
}
};
protected SystemProperty() {
// you can't call this one
}
public SystemProperty(XProcRuntime runtime) {
tl_runtime.set(runtime);
}
public StructuredQName getFunctionQName() {
return funcname;
}
public int getMinimumNumberOfArguments() {
return 1;
}
public int getMaximumNumberOfArguments() {
return 1;
}
public SequenceType[] getArgumentTypes() {
return new SequenceType[]{SequenceType.SINGLE_STRING};
}
public SequenceType getResultType(SequenceType[] suppliedArgumentTypes) {
return SequenceType.SINGLE_STRING;
}
public ExtensionFunctionCall makeCallExpression() {
return new SystemPropertyCall();
}
private class SystemPropertyCall extends ExtensionFunctionCall {
private StaticContext staticContext = null;
public void supplyStaticContext(StaticContext context, int locationId, Expression[] arguments) throws XPathException {
staticContext = context;
}
public SequenceIterator call(SequenceIterator[] arguments, XPathContext context) throws XPathException {
StructuredQName propertyName = null;
XProcRuntime runtime = (XProcRuntime) tl_runtime.get();
XStep step = runtime.getXProcData().getStep();
// FIXME: this can't be the best way to do this...
// FIXME: And what, exactly, is this even supposed to be doing!?
if (step != null && !(step instanceof XCompoundStep)) {
throw XProcException.dynamicError(23);
}
try {
SequenceIterator iter = arguments[0];
String lexicalQName = iter.next().getStringValue();
propertyName = StructuredQName.fromLexicalQName(
lexicalQName,
false,
context.getConfiguration().getNameChecker(),
staticContext.getNamespaceResolver());
} catch (XPathException e) {
if (e.getErrorCodeLocalPart()==null || e.getErrorCodeLocalPart().equals("FOCA0002")
|| e.getErrorCodeLocalPart().equals("FONS0004")) {
e.setErrorCode("XTDE1390");
}
throw e;
}
String uri = propertyName.getNamespaceURI();
String local = propertyName.getLocalName();
String value = "";
if (uri.equals(XProcConstants.NS_XPROC)) {
if ("episode".equals(local)) {
value = runtime.getEpisode();
} else if ("language".equals(local)) {
value = runtime.getLanguage();
} else if ("product-name".equals(local)) {
value = runtime.getProductName();
} else if ("product-version".equals(local)) {
value = runtime.getProductVersion();
} else if ("vendor".equals(local)) {
value = runtime.getVendor();
} else if ("vendor-uri".equals(local)) {
value = runtime.getVendorURI();
} else if ("version".equals(local)) {
value = runtime.getXProcVersion();
} else if ("xpath-version".equals(local)) {
value = runtime.getXPathVersion();
} else if ("psvi-supported".equals(local)) {
value = runtime.getPSVISupported() ? "true" : "false";
}
} else if (uri.equals(XProcConstants.NS_CALABASH_EX)) {
if ("transparent-json".equals(local)) {
value = runtime.transparentJSON() ? "true" : "false";
} else if ("json-flavor".equals(local)) {
value = runtime.jsonFlavor();
} else if ("general-values".equals(local)) {
value = runtime.getAllowGeneralExpressions() ? "true" : "false";
} else if ("xpointer-on-text".equals(local)) {
value = runtime.getAllowXPointerOnText() ? "true" : "false";
} else if ("saxon-version".equals(local)) {
value = runtime.getConfiguration().getProcessor().getSaxonProductVersion();
} else if ("saxon-edition".equals(local)) {
value = runtime.getConfiguration().saxonProcessor;
}
}
return SingletonIterator.makeIterator(
new StringValue(value));
}
}
}
//
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
// you may not use this file except in compliance with the License. You may obtain a copy of the
// License at http://www.mozilla.org/MPL/
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
// See the License for the specific language governing rights and limitations under the License.
//
// The Original Code is: all this file.
//
// The Initial Developer of the Original Code is Michael H. Kay.
//
// Portions created by Norman Walsh are Copyright (C) Mark Logic Corporation. All Rights Reserved.
//
// Contributor(s): Norman Walsh.
//