| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.XPathContext; |
| 11 |
import net.sf.saxon.lib.EnvironmentVariableResolver; |
| 12 |
import net.sf.saxon.lib.Feature; |
| 13 |
import net.sf.saxon.om.Sequence; |
| 14 |
import net.sf.saxon.om.ZeroOrOne; |
| 15 |
import net.sf.saxon.trans.XPathException; |
| 16 |
import net.sf.saxon.value.StringValue; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
public class EnvironmentVariable extends SystemFunction { |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public ZeroOrOne<StringValue> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 34 |
|
| 35 |
return new ZeroOrOne<>( |
| 36 |
getVariable((StringValue) arguments[0].head(), context)); |
| 37 |
} |
| 38 |
|
| 39 |
private static StringValue getVariable(StringValue environVar, XPathContext context) { |
| 40 |
EnvironmentVariableResolver resolver = context.getConfiguration().getConfigurationProperty( |
| 41 |
Feature.ENVIRONMENT_VARIABLE_RESOLVER); |
| 42 |
String environVarName = environVar.getStringValue(); |
| 43 |
String environValue = ""; |
| 44 |
if (context.getConfiguration().getBooleanProperty(Feature.ALLOW_EXTERNAL_FUNCTIONS)) { |
| 45 |
try { |
| 46 |
environValue = resolver.getEnvironmentVariable(environVarName); |
| 47 |
if (environValue == null) { |
| 48 |
return null; |
| 49 |
} |
| 50 |
} catch (SecurityException | NullPointerException e) { |
| 51 |
|
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
return new StringValue(environValue); |
| 56 |
} |
| 57 |
|
| 58 |
} |
| 59 |
|
| 60 |
|