Debian Codegraph @

net/sf/saxon/functions/EnvironmentVariable.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 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 * Implement the XPath 3.0 fn:environment-variable() function
20 */
21
22 public class EnvironmentVariable extends SystemFunction {
23
24 /**
25 * Evaluate the expression
26 *
27 * @param context the dynamic evaluation context
28 * @param arguments the values of the arguments, supplied as SequenceIterators
29 * @return the result of the evaluation, in the form of a SequenceIterator
30 * @throws net.sf.saxon.trans.XPathException
31 * if a dynamic error occurs during the evaluation of the expression
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 // no action;
52 }
53 }
54
55 return new StringValue(environValue);
56 }
57
58 }
59
60 // Copyright (c) 2010-2017 Saxonica Limited.