@Test public void expandWindowsStyleSystemVariables() throws Exception { Assert.assertEquals("$PATH", EnvironmentUtil.expandWindowsStyleSystemVariables("$PATH")); Assert.assertEquals(System.getenv("PATH"), EnvironmentUtil.expandWindowsStyleSystemVariables("%PATH%")); Assert.assertEquals( "abc" + System.getenv("PATH") + "def", EnvironmentUtil.expandWindowsStyleSystemVariables("abc%PATH%def") ); }
@Nonnull static String expandWindowsStyleSystemVariables(@Nonnull String value) { StringBuilder expandedValue = new StringBuilder(); Matcher systemVariableMatcher = WINDOWS_SYSTEM_VARIABLE_PATTERN.matcher(value); int previousEnd = 0; while (systemVariableMatcher.find()) { int start = systemVariableMatcher.start(); int end = systemVariableMatcher.end(); if (start > previousEnd) { expandedValue.append(value.substring(previousEnd, start)); } String variableName = value.substring(start + 1, end - 1); String variableValue = StringUtil.trimToNull(System.getenv(variableName)); if (variableValue == null) { expandedValue.append('%').append(variableName).append('%'); } else { expandedValue.append(variableValue); } previousEnd = end; } return expandedValue.append(value.substring(previousEnd)).toString(); }
EnvironmentUtil { @Nonnull static String expandWindowsStyleSystemVariables(@Nonnull String value) { StringBuilder expandedValue = new StringBuilder(); Matcher systemVariableMatcher = WINDOWS_SYSTEM_VARIABLE_PATTERN.matcher(value); int previousEnd = 0; while (systemVariableMatcher.find()) { int start = systemVariableMatcher.start(); int end = systemVariableMatcher.end(); if (start > previousEnd) { expandedValue.append(value.substring(previousEnd, start)); } String variableName = value.substring(start + 1, end - 1); String variableValue = StringUtil.trimToNull(System.getenv(variableName)); if (variableValue == null) { expandedValue.append('%').append(variableName).append('%'); } else { expandedValue.append(variableValue); } previousEnd = end; } return expandedValue.append(value.substring(previousEnd)).toString(); } }
EnvironmentUtil { @Nonnull static String expandWindowsStyleSystemVariables(@Nonnull String value) { StringBuilder expandedValue = new StringBuilder(); Matcher systemVariableMatcher = WINDOWS_SYSTEM_VARIABLE_PATTERN.matcher(value); int previousEnd = 0; while (systemVariableMatcher.find()) { int start = systemVariableMatcher.start(); int end = systemVariableMatcher.end(); if (start > previousEnd) { expandedValue.append(value.substring(previousEnd, start)); } String variableName = value.substring(start + 1, end - 1); String variableValue = StringUtil.trimToNull(System.getenv(variableName)); if (variableValue == null) { expandedValue.append('%').append(variableName).append('%'); } else { expandedValue.append(variableValue); } previousEnd = end; } return expandedValue.append(value.substring(previousEnd)).toString(); } private  EnvironmentUtil(); }
EnvironmentUtil { @Nonnull static String expandWindowsStyleSystemVariables(@Nonnull String value) { StringBuilder expandedValue = new StringBuilder(); Matcher systemVariableMatcher = WINDOWS_SYSTEM_VARIABLE_PATTERN.matcher(value); int previousEnd = 0; while (systemVariableMatcher.find()) { int start = systemVariableMatcher.start(); int end = systemVariableMatcher.end(); if (start > previousEnd) { expandedValue.append(value.substring(previousEnd, start)); } String variableName = value.substring(start + 1, end - 1); String variableValue = StringUtil.trimToNull(System.getenv(variableName)); if (variableValue == null) { expandedValue.append('%').append(variableName).append('%'); } else { expandedValue.append(variableValue); } previousEnd = end; } return expandedValue.append(value.substring(previousEnd)).toString(); } private  EnvironmentUtil(); @Contract("null -> null; !null -> !null") @Nullable static String expandSystemVariablesQuietly(@Nullable String value); @Contract("null -> null; !null -> !null") @Nullable static String expandSystemVariables(@Nullable String value); }
EnvironmentUtil { @Nonnull static String expandWindowsStyleSystemVariables(@Nonnull String value) { StringBuilder expandedValue = new StringBuilder(); Matcher systemVariableMatcher = WINDOWS_SYSTEM_VARIABLE_PATTERN.matcher(value); int previousEnd = 0; while (systemVariableMatcher.find()) { int start = systemVariableMatcher.start(); int end = systemVariableMatcher.end(); if (start > previousEnd) { expandedValue.append(value.substring(previousEnd, start)); } String variableName = value.substring(start + 1, end - 1); String variableValue = StringUtil.trimToNull(System.getenv(variableName)); if (variableValue == null) { expandedValue.append('%').append(variableName).append('%'); } else { expandedValue.append(variableValue); } previousEnd = end; } return expandedValue.append(value.substring(previousEnd)).toString(); } private  EnvironmentUtil(); @Contract("null -> null; !null -> !null") @Nullable static String expandSystemVariablesQuietly(@Nullable String value); @Contract("null -> null; !null -> !null") @Nullable static String expandSystemVariables(@Nullable String value);  }