Fixed: [Issue 99] Allow multi-line import URIs.
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/conversion/HEXValueConverter_toValue_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/conversion/HEXValueConverter_toValue_Test.java
index 50b4256..e70c158 100644
--- a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/conversion/HEXValueConverter_toValue_Test.java
+++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/conversion/HEXValueConverter_toValue_Test.java
@@ -33,7 +33,7 @@
@Rule public XtextRule xtext = new XtextRule();
- private final String hexadecimal;
+ private final String input;
private final Integer expected;
@Parameters
@@ -45,8 +45,8 @@
});
}
- public HEXValueConverter_toValue_Test(String hexadecimal, Integer expected) {
- this.hexadecimal = hexadecimal;
+ public HEXValueConverter_toValue_Test(String input, Integer expected) {
+ this.input = input;
this.expected = expected;
}
@@ -59,7 +59,7 @@
}
@Test public void should_parse_hexadecimal_number() {
- Integer value = converter.toValue(hexadecimal, node);
+ Integer value = converter.toValue(input, node);
assertThat(value, equalTo(expected));
}
}
diff --git a/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/conversion/STRINGValueConverter_toValue_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/conversion/STRINGValueConverter_toValue_Test.java
new file mode 100644
index 0000000..b8b470d
--- /dev/null
+++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/conversion/STRINGValueConverter_toValue_Test.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2011 Google Inc.
+ *
+ * All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse
+ * Public License v1.0 which accompanies this distribution, and is available at
+ *
+ * http://www.eclipse.org/legal/epl-v10.html
+ */
+package com.google.eclipse.protobuf.conversion;
+
+import static java.util.Arrays.asList;
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+
+import java.util.Collection;
+
+import org.eclipse.xtext.nodemodel.INode;
+import org.junit.*;
+import org.junit.runner.RunWith;
+import org.junit.runners.*;
+import org.junit.runners.Parameterized.Parameters;
+
+import com.google.eclipse.protobuf.junit.core.XtextRule;
+
+/**
+ * Tests for <code>{@link STRINGValueConverter#toValue(String, INode)}</code>.
+ *
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+@RunWith(Parameterized.class)
+public class STRINGValueConverter_toValue_Test {
+
+ @Rule public XtextRule xtext = new XtextRule();
+
+ private final String input;
+ private final String expected;
+
+ @Parameters
+ public static Collection<Object[]> parameters() {
+ return asList(new Object[][] {
+ { null, null },
+ { "\"Hello World!\"", "Hello World!" },
+ { "\"Hello\"\n\" World!\"", "Hello World!" },
+ { "\"Hello\"\r\" World!\"", "Hello World!" },
+ { "\"Hello\"\n\n\" World!\"", "Hello World!" },
+ { "\"Hello\"\n\r\" World!\"", "Hello World!" }
+ });
+ }
+
+ public STRINGValueConverter_toValue_Test(String input, String expected) {
+ this.input = input;
+ this.expected = expected;
+ }
+
+ private STRINGValueConverter converter;
+ private INode node;
+
+ @Before public void setUp() {
+ node = mock(INode.class);
+ converter = xtext.injector().getInstance(STRINGValueConverter.class);
+ }
+
+ @Test public void should_parse_multi_line_string() {
+ String value = converter.toValue(input, node);
+ assertThat(value, equalTo(expected));
+ }
+}
diff --git a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Strings_firstCharToLowerCase_Test.java b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/util/Strings_firstCharToLowerCase_Test.java
similarity index 91%
rename from com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Strings_firstCharToLowerCase_Test.java
rename to com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/util/Strings_firstCharToLowerCase_Test.java
index 47fd9b1..8229fe3 100644
--- a/com.google.eclipse.protobuf.ui.test/src/com/google/eclipse/protobuf/ui/util/Strings_firstCharToLowerCase_Test.java
+++ b/com.google.eclipse.protobuf.test/src/com/google/eclipse/protobuf/util/Strings_firstCharToLowerCase_Test.java
@@ -6,7 +6,7 @@
*
* http://www.eclipse.org/legal/epl-v10.html
*/
-package com.google.eclipse.protobuf.ui.util;
+package com.google.eclipse.protobuf.util;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsNull.nullValue;
@@ -14,6 +14,8 @@
import org.junit.Test;
+import com.google.eclipse.protobuf.util.Strings;
+
/**
* Tests for <code>{@link Strings#firstCharToLowerCase(String)}</code>
*
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/contentassist/ProtobufProposalProvider.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/contentassist/ProtobufProposalProvider.java
index 95a4b49..0dfce9e 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/contentassist/ProtobufProposalProvider.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/contentassist/ProtobufProposalProvider.java
@@ -11,8 +11,8 @@
import static com.google.eclipse.protobuf.grammar.CommonKeyword.*;
import static com.google.eclipse.protobuf.protobuf.Modifier.*;
import static com.google.eclipse.protobuf.ui.grammar.CompoundElement.*;
-import static com.google.eclipse.protobuf.ui.util.Strings.firstCharToLowerCase;
import static com.google.eclipse.protobuf.util.CommonWords.space;
+import static com.google.eclipse.protobuf.util.Strings.firstCharToLowerCase;
import static java.lang.String.valueOf;
import static java.util.Collections.emptyList;
import static org.eclipse.xtext.EcoreUtil2.getAllContentsOfType;
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/pages/paths/PathsPreferences.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/pages/paths/PathsPreferences.java
index 7575d39..cb0127f 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/pages/paths/PathsPreferences.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/preferences/pages/paths/PathsPreferences.java
@@ -9,7 +9,7 @@
package com.google.eclipse.protobuf.ui.preferences.pages.paths;
import static com.google.eclipse.protobuf.ui.preferences.pages.paths.PathResolutionType.*;
-import static com.google.eclipse.protobuf.ui.util.Strings.CSV_PATTERN;
+import static com.google.eclipse.protobuf.util.Strings.CSV_PATTERN;
import static java.util.Collections.*;
import java.util.*;
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/HEXValueConverter.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/HEXValueConverter.java
index d731b46..5901541 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/HEXValueConverter.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/HEXValueConverter.java
@@ -22,7 +22,7 @@
public class HEXValueConverter extends AbstractLexerBasedConverter<Integer> {
/**
- * Creates am {@code int} from the given input, if the given input represents an hexadecimal number.
+ * Creates an {@code int} from the given input, if the given input represents an hexadecimal number.
* @param string the given input.
* @param node the parsed node including hidden parts.
* @return the new integer.
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/ProtobufTerminalConverters.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/ProtobufTerminalConverters.java
index 965f141..c56b41b 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/ProtobufTerminalConverters.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/ProtobufTerminalConverters.java
@@ -8,20 +8,26 @@
*/
package com.google.eclipse.protobuf.conversion;
-import com.google.inject.Inject;
-
import org.eclipse.xtext.common.services.DefaultTerminalConverters;
import org.eclipse.xtext.conversion.*;
+import com.google.inject.Inject;
+
/**
* @author alruiz@google.com (Alex Ruiz)
*/
public class ProtobufTerminalConverters extends DefaultTerminalConverters {
@Inject private HEXValueConverter hexValueConverter;
-
+ @Inject private STRINGValueConverter stringValueConverter;
+
@ValueConverter(rule = "HEX")
public IValueConverter<Integer> HEX() {
return hexValueConverter;
}
+
+ @ValueConverter(rule = "STRING")
+ public IValueConverter<String> STRING() {
+ return stringValueConverter;
+ }
}
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
new file mode 100644
index 0000000..ac50185
--- /dev/null
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/conversion/STRINGValueConverter.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2011 Google Inc.
+ *
+ * All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse
+ * Public License v1.0 which accompanies this distribution, and is available at
+ *
+ * http://www.eclipse.org/legal/epl-v10.html
+ */
+package com.google.eclipse.protobuf.conversion;
+
+import static com.google.eclipse.protobuf.util.Strings.removeLineBreaksFrom;
+import static org.eclipse.xtext.util.Strings.*;
+
+import org.eclipse.xtext.conversion.ValueConverterException;
+import org.eclipse.xtext.conversion.impl.AbstractLexerBasedConverter;
+import org.eclipse.xtext.nodemodel.INode;
+
+/**
+ * Converts multi-line strings to {@code String}s.
+ *
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+public class STRINGValueConverter extends AbstractLexerBasedConverter<String> {
+
+ @Override
+ protected String toEscapedString(String value) {
+ if (value == null) return null;
+ return '"' + convertToJavaString(removeLineBreaksFrom(value), false) + '"';
+ }
+
+ /**
+ * Creates a {@code String} from the given input, if the given input represents a multi-line string.
+ * @param string the given input.
+ * @param node the parsed node including hidden parts.
+ * @return the new integer.
+ * @throws ValueConverterException if the given input has illegal characters.
+ */
+ public String toValue(String string, INode node) throws ValueConverterException {
+ if (string == null) return null;
+ try {
+ String clean = removeLineBreaksFrom(string);
+ return convertFromJavaString(clean.substring(1, clean.length() - 1), true);
+ } catch (IllegalArgumentException e) {
+ throw parsingError(string, node, e);
+ }
+ }
+
+ private ValueConverterException parsingError(String string, INode node, Exception cause) {
+ return new ValueConverterException("Couldn't convert '" + string + "' to String.", node, cause);
+ }
+}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtobufImportUriResolver.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtobufImportUriResolver.java
index 2f9aae5..e1cfbc8 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtobufImportUriResolver.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtobufImportUriResolver.java
@@ -8,6 +8,8 @@
*/
package com.google.eclipse.protobuf.scoping;
+import static com.google.eclipse.protobuf.util.Strings.removeLineBreaksFrom;
+
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.scoping.impl.ImportUriResolver;
@@ -39,12 +41,12 @@
if (from instanceof Import) {
Import anImport = (Import) from;
anImport.setImportURI(resolveImportUri(anImport));
- return super.apply(from);
}
return super.apply(from);
}
private String resolveImportUri(Import anImport) {
- return delegate.resolveUri(anImport.getImportURI(), anImport.eResource());
+ String importURI = removeLineBreaksFrom(anImport.getImportURI());
+ return delegate.resolveUri(importURI, anImport.eResource());
}
}
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Strings.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/util/Strings.java
similarity index 74%
rename from com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Strings.java
rename to com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/util/Strings.java
index af8b091..1d4e8eb 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Strings.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/util/Strings.java
@@ -6,7 +6,7 @@
*
* http://www.eclipse.org/legal/epl-v10.html
*/
-package com.google.eclipse.protobuf.ui.util;
+package com.google.eclipse.protobuf.util;
import static java.lang.Character.toLowerCase;
@@ -32,6 +32,16 @@
chars[0] = toLowerCase(chars[0]);
return new String(chars);
}
-
+
+ /**
+ * Removes any line breaks from the given {@code String}.
+ * @param s the given {@code String}.
+ * @return the given {@code String} with all line breaks removed.
+ */
+ public static String removeLineBreaksFrom(String s) {
+ if (s == null) return s;
+ return s.replaceAll("\"[\t\r\n]+\"|'[\t\r\n]+'", "");
+ }
+
private Strings() {}
}