| 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.lib; |
| 9 | |
| 10 | import java.util.Map; |
| 11 | import java.util.Set; |
| 12 | |
| 13 | /** |
| 14 | * Default implementation of the {@link EnvironmentVariableResolver}. This implementation |
| 15 | * maps "environment variables" as defined in the XPath context to environment variables |
| 16 | * delivered by Java using the {@link System#getenv()} method. |
| 17 | */ |
| 18 | |
| 19 | public class StandardEnvironmentVariableResolver implements EnvironmentVariableResolver { |
| 20 | |
| 21 | /** |
| 22 | * Get the list of available environment variables. |
| 23 | * |
| 24 | * @return a set of strings; each such string should be an acceptable argument to the |
| 25 | * method {@link #getEnvironmentVariable(String)} |
| 26 | */ |
| 27 | public Set<String> getAvailableEnvironmentVariables() { |
| 28 | Map<String, String> vars = System.getenv(); |
| 29 | return vars.keySet(); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Get the value of a specific environment variable |
| 34 | * |
| 35 | * @param name the name of the required environment variable |
| 36 | * @return the value of the named environment variable, or null if the variable is |
| 37 | * not defined. The method must not return null if the name is in the list of variables |
| 38 | * returned by the method {@link #getAvailableEnvironmentVariables()} |
| 39 | */ |
| 40 | public String getEnvironmentVariable(String name) { |
| 41 | return System.getenv(name); |
| 42 | } |
| 43 | } |
| 44 |