Skip to content

Commit

Permalink
minor: to squash
Browse files Browse the repository at this point in the history
  • Loading branch information
rnveach committed Dec 2, 2018
1 parent 351cc3f commit 953d645
Showing 1 changed file with 128 additions and 128 deletions.
256 changes: 128 additions & 128 deletions src/main/java/com/puppycrawl/tools/checkstyle/ConfigurationLoader.java
Expand Up @@ -453,6 +453,134 @@ public static Configuration loadConfiguration(InputSource configSource,
}
}

/**
* Replaces {@code ${xxx}} style constructions in the given value
* with the string value of the corresponding data types.
*
* <p>Code copied from ant -
* http://cvs.apache.org/viewcvs/jakarta-ant/src/main/org/apache/tools/ant/ProjectHelper.java
*
* @param value The string to be scanned for property references.
* May be {@code null}, in which case this
* method returns immediately with no effect.
* @param props Mapping (String to String) of property names to their
* values. Must not be {@code null}.
* @param defaultValue default to use if one of the properties in value
* cannot be resolved from props.
*
* @return the original string with the properties replaced, or
* {@code null} if the original string is {@code null}.
* @throws CheckstyleException if the string contains an opening
* {@code ${} without a closing
* {@code }}
* @noinspection MethodWithMultipleReturnPoints, MethodOnlyUsedFromInnerClass
*/
private static String replaceProperties(
String value, PropertyResolver props, String defaultValue)
throws CheckstyleException {
if (value == null) {
return null;
}

final List<String> fragments = new ArrayList<>();
final List<String> propertyRefs = new ArrayList<>();
parsePropertyString(value, fragments, propertyRefs);

final StringBuilder sb = new StringBuilder(256);
final Iterator<String> fragmentsIterator = fragments.iterator();
final Iterator<String> propertyRefsIterator = propertyRefs.iterator();
while (fragmentsIterator.hasNext()) {
String fragment = fragmentsIterator.next();
if (fragment == null) {
final String propertyName = propertyRefsIterator.next();
fragment = props.resolve(propertyName);
if (fragment == null) {
if (defaultValue != null) {
sb.replace(0, sb.length(), defaultValue);
break;
}
throw new CheckstyleException(
"Property ${" + propertyName + "} has not been set");
}
}
sb.append(fragment);
}

return sb.toString();
}

/**
* Parses a string containing {@code ${xxx}} style property
* references into two lists. The first list is a collection
* of text fragments, while the other is a set of string property names.
* {@code null} entries in the first list indicate a property
* reference from the second list.
*
* <p>Code copied from ant -
* http://cvs.apache.org/viewcvs/jakarta-ant/src/main/org/apache/tools/ant/ProjectHelper.java
*
* @param value Text to parse. Must not be {@code null}.
* @param fragments List to add text fragments to.
* Must not be {@code null}.
* @param propertyRefs List to add property names to.
* Must not be {@code null}.
*
* @throws CheckstyleException if the string contains an opening
* {@code ${} without a closing
* {@code }}
*/
private static void parsePropertyString(String value,
List<String> fragments,
List<String> propertyRefs)
throws CheckstyleException {
int prev = 0;
//search for the next instance of $ from the 'prev' position
int pos = value.indexOf(DOLLAR_SIGN, prev);
while (pos >= 0) {
//if there was any text before this, add it as a fragment
if (pos > 0) {
fragments.add(value.substring(prev, pos));
}
//if we are at the end of the string, we tack on a $
//then move past it
if (pos == value.length() - 1) {
fragments.add(String.valueOf(DOLLAR_SIGN));
prev = pos + 1;
}
else if (value.charAt(pos + 1) == '{') {
//property found, extract its name or bail on a typo
final int endName = value.indexOf('}', pos);
if (endName == -1) {
throw new CheckstyleException("Syntax error in property: "
+ value);
}
final String propertyName = value.substring(pos + 2, endName);
fragments.add(null);
propertyRefs.add(propertyName);
prev = endName + 1;
}
else {
if (value.charAt(pos + 1) == DOLLAR_SIGN) {
//backwards compatibility two $ map to one mode
fragments.add(String.valueOf(DOLLAR_SIGN));
}
else {
//new behaviour: $X maps to $X for all values of X!='$'
fragments.add(value.substring(pos, pos + 2));
}
prev = pos + 2;
}

//search for the next instance of $ from the 'prev' position
pos = value.indexOf(DOLLAR_SIGN, prev);
}
//no more $ signs found
//if there is any tail to the file, append it
if (prev < value.length()) {
fragments.add(value.substring(prev));
}
}

/**
* Implements the SAX document handler interfaces, so they do not
* appear in the public API of the ConfigurationLoader.
Expand Down Expand Up @@ -586,134 +714,6 @@ public void endElement(String uri,
}
}

/**
* Replaces {@code ${xxx}} style constructions in the given value
* with the string value of the corresponding data types.
*
* <p>Code copied from ant -
* http://cvs.apache.org/viewcvs/jakarta-ant/src/main/org/apache/tools/ant/ProjectHelper.java
*
* @param value The string to be scanned for property references.
* May be {@code null}, in which case this
* method returns immediately with no effect.
* @param props Mapping (String to String) of property names to their
* values. Must not be {@code null}.
* @param defaultValue default to use if one of the properties in value
* cannot be resolved from props.
*
* @return the original string with the properties replaced, or
* {@code null} if the original string is {@code null}.
* @throws CheckstyleException if the string contains an opening
* {@code ${} without a closing
* {@code }}
* @noinspection MethodWithMultipleReturnPoints
*/
private String replaceProperties(
String value, PropertyResolver props, String defaultValue)
throws CheckstyleException {
if (value == null) {
return null;
}

final List<String> fragments = new ArrayList<>();
final List<String> propertyRefs = new ArrayList<>();
parsePropertyString(value, fragments, propertyRefs);

final StringBuilder sb = new StringBuilder(256);
final Iterator<String> fragmentsIterator = fragments.iterator();
final Iterator<String> propertyRefsIterator = propertyRefs.iterator();
while (fragmentsIterator.hasNext()) {
String fragment = fragmentsIterator.next();
if (fragment == null) {
final String propertyName = propertyRefsIterator.next();
fragment = props.resolve(propertyName);
if (fragment == null) {
if (defaultValue != null) {
sb.replace(0, sb.length(), defaultValue);
break;
}
throw new CheckstyleException(
"Property ${" + propertyName + "} has not been set");
}
}
sb.append(fragment);
}

return sb.toString();
}

/**
* Parses a string containing {@code ${xxx}} style property
* references into two lists. The first list is a collection
* of text fragments, while the other is a set of string property names.
* {@code null} entries in the first list indicate a property
* reference from the second list.
*
* <p>Code copied from ant -
* http://cvs.apache.org/viewcvs/jakarta-ant/src/main/org/apache/tools/ant/ProjectHelper.java
*
* @param value Text to parse. Must not be {@code null}.
* @param fragments List to add text fragments to.
* Must not be {@code null}.
* @param propertyRefs List to add property names to.
* Must not be {@code null}.
*
* @throws CheckstyleException if the string contains an opening
* {@code ${} without a closing
* {@code }}
*/
private void parsePropertyString(String value,
List<String> fragments,
List<String> propertyRefs)
throws CheckstyleException {
int prev = 0;
//search for the next instance of $ from the 'prev' position
int pos = value.indexOf(DOLLAR_SIGN, prev);
while (pos >= 0) {
//if there was any text before this, add it as a fragment
if (pos > 0) {
fragments.add(value.substring(prev, pos));
}
//if we are at the end of the string, we tack on a $
//then move past it
if (pos == value.length() - 1) {
fragments.add(String.valueOf(DOLLAR_SIGN));
prev = pos + 1;
}
else if (value.charAt(pos + 1) == '{') {
//property found, extract its name or bail on a typo
final int endName = value.indexOf('}', pos);
if (endName == -1) {
throw new CheckstyleException("Syntax error in property: "
+ value);
}
final String propertyName = value.substring(pos + 2, endName);
fragments.add(null);
propertyRefs.add(propertyName);
prev = endName + 1;
}
else {
if (value.charAt(pos + 1) == DOLLAR_SIGN) {
//backwards compatibility two $ map to one mode
fragments.add(String.valueOf(DOLLAR_SIGN));
}
else {
//new behaviour: $X maps to $X for all values of X!='$'
fragments.add(value.substring(pos, pos + 2));
}
prev = pos + 2;
}

//search for the next instance of $ from the 'prev' position
pos = value.indexOf(DOLLAR_SIGN, prev);
}
//no more $ signs found
//if there is any tail to the file, append it
if (prev < value.length()) {
fragments.add(value.substring(prev));
}
}

/**
* Util method to recheck attribute in module.
* @param module module to check
Expand Down

0 comments on commit 953d645

Please sign in to comment.