@Test public void expandNxStyleSystemVariables() throws Exception { Assert.assertEquals("%PATH%", EnvironmentUtil.expandNxStyleSystemVariables("%PATH%")); Assert.assertEquals(System.getenv("PATH"), EnvironmentUtil.expandNxStyleSystemVariables("$PATH")); Assert.assertEquals( "abc" + System.getenv("PATH") + " def", EnvironmentUtil.expandNxStyleSystemVariables("abc$PATH def") ); Assert.assertEquals( "a", EnvironmentUtil.expandNxStyleSystemVariables("a$PATH" + RandomUtil.getRandomToken()) ); Assert.assertEquals( "", EnvironmentUtil.expandNxStyleSystemVariables("$PATH" + RandomUtil.getRandomToken()) ); Assert.assertEquals("\\$PATHcabaca", EnvironmentUtil.expandNxStyleSystemVariables("\\$PATHcabaca")); Assert.assertEquals("a\\$PATHcabaca", EnvironmentUtil.expandNxStyleSystemVariables("a\\$PATHcabaca")); Assert.assertEquals("'$PATH'", EnvironmentUtil.expandNxStyleSystemVariables("'$PATH'")); Assert.assertEquals("a'$PATH'b", EnvironmentUtil.expandNxStyleSystemVariables("a'$PATH'b")); Assert.assertEquals("'\\$PATH'", EnvironmentUtil.expandNxStyleSystemVariables("'\\$PATH'")); }
@Nonnull static String expandNxStyleSystemVariables(@Nonnull String value) { StringBuilder expandedValue = new StringBuilder(); Matcher systemVariableMatcher = NX_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); String variableValue = StringUtil.trimToNull(System.getenv(variableName)); if (start > 0 && ( end < value.length() && value.charAt(start - 1) == '\'' && value.charAt(end) == '\'' || value.charAt(start - 1) == '\\' )) { expandedValue.append('$').append(variableName); } else if (variableValue == null) { } else { expandedValue.append(variableValue); } previousEnd = end; } return expandedValue.append(value.substring(previousEnd)).toString(); }
EnvironmentUtil { @Nonnull static String expandNxStyleSystemVariables(@Nonnull String value) { StringBuilder expandedValue = new StringBuilder(); Matcher systemVariableMatcher = NX_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); String variableValue = StringUtil.trimToNull(System.getenv(variableName)); if (start > 0 && ( end < value.length() && value.charAt(start - 1) == '\'' && value.charAt(end) == '\'' || value.charAt(start - 1) == '\\' )) { expandedValue.append('$').append(variableName); } else if (variableValue == null) { } else { expandedValue.append(variableValue); } previousEnd = end; } return expandedValue.append(value.substring(previousEnd)).toString(); } }
EnvironmentUtil { @Nonnull static String expandNxStyleSystemVariables(@Nonnull String value) { StringBuilder expandedValue = new StringBuilder(); Matcher systemVariableMatcher = NX_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); String variableValue = StringUtil.trimToNull(System.getenv(variableName)); if (start > 0 && ( end < value.length() && value.charAt(start - 1) == '\'' && value.charAt(end) == '\'' || value.charAt(start - 1) == '\\' )) { expandedValue.append('$').append(variableName); } else if (variableValue == null) { } else { expandedValue.append(variableValue); } previousEnd = end; } return expandedValue.append(value.substring(previousEnd)).toString(); } private  EnvironmentUtil(); }
EnvironmentUtil { @Nonnull static String expandNxStyleSystemVariables(@Nonnull String value) { StringBuilder expandedValue = new StringBuilder(); Matcher systemVariableMatcher = NX_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); String variableValue = StringUtil.trimToNull(System.getenv(variableName)); if (start > 0 && ( end < value.length() && value.charAt(start - 1) == '\'' && value.charAt(end) == '\'' || value.charAt(start - 1) == '\\' )) { expandedValue.append('$').append(variableName); } else if (variableValue == null) { } 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 expandNxStyleSystemVariables(@Nonnull String value) { StringBuilder expandedValue = new StringBuilder(); Matcher systemVariableMatcher = NX_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); String variableValue = StringUtil.trimToNull(System.getenv(variableName)); if (start > 0 && ( end < value.length() && value.charAt(start - 1) == '\'' && value.charAt(end) == '\'' || value.charAt(start - 1) == '\\' )) { expandedValue.append('$').append(variableName); } else if (variableValue == null) { } 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);  }