Removed code duplication.
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/STRINGValueConverter.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/STRINGValueConverter.java
index 8176a2d..1960b87 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/STRINGValueConverter.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/STRINGValueConverter.java
@@ -8,15 +8,15 @@
*/
package com.google.eclipse.protobuf.conversion;
-import static com.google.eclipse.protobuf.util.Strings.unquote;
+import static com.google.eclipse.protobuf.util.Strings.*;
import static org.eclipse.xtext.util.Strings.convertToJavaString;
-import java.util.Scanner;
-
import org.eclipse.xtext.conversion.ValueConverterException;
import org.eclipse.xtext.conversion.impl.AbstractLexerBasedConverter;
import org.eclipse.xtext.nodemodel.INode;
+import com.google.common.base.Function;
+
/**
* Converts multi-line strings to {@code String}s.
*
@@ -50,16 +50,18 @@
}
private String toValue(String string) {
- StringBuilder valueBuilder = new StringBuilder();
- Scanner scanner = new Scanner(string);
- while (scanner.hasNextLine()) {
- String line = scanner.nextLine();
- valueBuilder.append(convertToJavaString(unquote(line.trim()), true));
- }
- return valueBuilder.toString();
+ return removeLineBreaks(string, LineTransformation.INSTANCE);
}
private ValueConverterException parsingError(String string, INode node, Exception cause) {
return new ValueConverterException("Couldn't convert '" + string + "' to String.", node, cause);
}
+
+ private static class LineTransformation implements Function<String, String> {
+ private static final LineTransformation INSTANCE = new LineTransformation();
+
+ @Override public String apply(String input) {
+ return convertToJavaString(unquote(input), true);
+ }
+ }
}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/util/Strings.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/util/Strings.java
index e29605e..ffbc9df 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/util/Strings.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/util/Strings.java
@@ -12,6 +12,8 @@
import java.util.Scanner;
+import com.google.common.base.Function;
+
/**
* Utility methods related to {@code String}.s
*
@@ -58,16 +60,32 @@
* is {@code null}.
*/
public static String removeLineBreaksFrom(String s) {
+ return removeLineBreaks(s, null);
+ }
+
+ /**
+ * Returns a {@code String} containing the given one without line breaks.
+ * @param s the given {@code String}, may be {@code null}.
+ * @param transformation any modifications to apply to each line in the given {@code String}, may be {@code null}.
+ * @return a {@code String} containing the given one without line breaks, or {@code null} if the given {@code String}
+ * is {@code null}.
+ */
+ public static String removeLineBreaks(String s, Function<String, String> transformation) {
if (isEmpty(s)) {
return s;
}
StringBuilder valueBuilder = new StringBuilder();
Scanner scanner = new Scanner(s);
while (scanner.hasNextLine()) {
- String line = scanner.nextLine();
- valueBuilder.append(line.trim());
+ String line = scanner.nextLine().trim();
+ if (transformation != null) {
+ line = transformation.apply(line);
+ }
+ valueBuilder.append(line);
}
+ scanner.close();
return valueBuilder.toString();
}
+
private Strings() {}
}