Removed code duplication. Added tests.
diff --git a/com.google.eclipse.protobuf.integration.test/src/com/google/eclipse/protobuf/model/util/Imports_uriAsEnteredByUser_Test.java b/com.google.eclipse.protobuf.integration.test/src/com/google/eclipse/protobuf/model/util/Imports_uriAsEnteredByUser_Test.java
new file mode 100644
index 0000000..0b31fc6
--- /dev/null
+++ b/com.google.eclipse.protobuf.integration.test/src/com/google/eclipse/protobuf/model/util/Imports_uriAsEnteredByUser_Test.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2012 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.model.util;
+
+import static com.google.eclipse.protobuf.junit.core.IntegrationTestModule.integrationTestModule;
+import static com.google.eclipse.protobuf.junit.core.XtextRule.overrideRuntimeModuleWith;
+import static org.eclipse.xtext.EcoreUtil2.getAllContentsOfType;
+import static org.hamcrest.core.IsEqual.equalTo;
+import static org.junit.Assert.assertThat;
+
+import java.util.List;
+
+import org.junit.*;
+
+import com.google.eclipse.protobuf.junit.core.XtextRule;
+import com.google.eclipse.protobuf.protobuf.*;
+
+/**
+ * Tests for <code>{@link Imports#uriAsEnteredByUser(Import)}</code>
+ *
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+public class Imports_uriAsEnteredByUser_Test {
+ @Rule public XtextRule xtext = overrideRuntimeModuleWith(integrationTestModule());
+
+ private Imports imports;
+
+ @Before public void setUp() {
+ imports = xtext.getInstanceOf(Imports.class);
+ }
+
+ // syntax = "proto2";
+ // import "types.proto";
+ @Test public void should_return_import_URI_as_entered_by_user() {
+ Protobuf root = xtext.root();
+ Import anImport = firstImportOf(root);
+ anImport.setImportURI("file://test-protos/types.proto"); // simulate the URI is resolved
+ assertThat(imports.uriAsEnteredByUser(anImport), equalTo("types.proto"));
+ }
+
+ private Import firstImportOf(Protobuf root) {
+ List<Import> allImports = getAllContentsOfType(root, Import.class);
+ return allImports.get(0);
+ }
+}
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/editor/hyperlinking/ProtobufHyperlinkDetector.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/editor/hyperlinking/ProtobufHyperlinkDetector.java
index ea34dc1..5e0429e 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/editor/hyperlinking/ProtobufHyperlinkDetector.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/editor/hyperlinking/ProtobufHyperlinkDetector.java
@@ -19,9 +19,9 @@
import org.eclipse.xtext.ui.editor.model.IXtextDocument;
import org.eclipse.xtext.util.concurrent.IUnitOfWork;
+import com.google.eclipse.protobuf.model.util.Imports;
import com.google.eclipse.protobuf.protobuf.Import;
import com.google.eclipse.protobuf.ui.editor.FileOpener;
-import com.google.eclipse.protobuf.ui.util.Imports;
import com.google.inject.Inject;
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Imports.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Imports.java
deleted file mode 100644
index 0debb18..0000000
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/util/Imports.java
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * Copyright (c) 2012 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.ui.util;
-
-import static com.google.eclipse.protobuf.protobuf.ProtobufPackage.Literals.IMPORT__IMPORT_URI;
-
-import org.eclipse.xtext.nodemodel.INode;
-
-import com.google.eclipse.protobuf.conversion.STRINGValueConverter;
-import com.google.eclipse.protobuf.model.util.INodes;
-import com.google.eclipse.protobuf.protobuf.Import;
-import com.google.inject.Inject;
-
-/**
- * Utility methods related to <code>{@link Import}</code>s.
- *
- * @author alruiz@google.com (Alex Ruiz)
- */
-public class Imports {
- @Inject private INodes nodes;
- @Inject private STRINGValueConverter stringValueConverter;
-
- public String uriAsEnteredByUser(Import anImport) {
- INode node = nodes.firstNodeForFeature(anImport, IMPORT__IMPORT_URI);
- if (node == null) {
- return null;
- }
- String text = node.getText();
- if (text == null) {
- return null;
- }
- return stringValueConverter.toValue(text, node);
-
- }
-}
diff --git a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/validation/ProtobufValidation.java b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/validation/ProtobufValidation.java
index 74a81c8..179d527 100644
--- a/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/validation/ProtobufValidation.java
+++ b/com.google.eclipse.protobuf.ui/src/com/google/eclipse/protobuf/ui/validation/ProtobufValidation.java
@@ -21,9 +21,9 @@
import org.eclipse.xtext.ui.editor.model.*;
import org.eclipse.xtext.util.concurrent.IUnitOfWork;
+import com.google.eclipse.protobuf.model.util.Imports;
import com.google.eclipse.protobuf.protobuf.Import;
import com.google.eclipse.protobuf.ui.ProtobufPlugIn;
-import com.google.eclipse.protobuf.ui.util.Imports;
/**
* @author alruiz@google.com (Alex Ruiz)
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 13a3093..a1a64a2 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,10 +8,8 @@
*/
package com.google.eclipse.protobuf.conversion;
-import static java.util.regex.Pattern.compile;
-import static org.eclipse.xtext.util.Strings.*;
-
-import java.util.regex.Pattern;
+import static com.google.eclipse.protobuf.util.Strings.*;
+import static org.eclipse.xtext.util.Strings.convertToJavaString;
import org.eclipse.xtext.conversion.ValueConverterException;
import org.eclipse.xtext.conversion.impl.AbstractLexerBasedConverter;
@@ -23,17 +21,16 @@
* @author alruiz@google.com (Alex Ruiz)
*/
public class STRINGValueConverter extends AbstractLexerBasedConverter<String> {
- private static final Pattern LINE_BREAK = compile("\"[\t\r\n]+\"|'[\t\r\n]+'");
-
@Override protected String toEscapedString(String value) {
if (value == null) {
return null;
}
+ // TODO check if we really need to quote
return '"' + convertToJavaString(removeLineBreaksFrom(value), false) + '"';
}
/**
- * Creates a {@code String} from the given input, if the given input represents a multi-line string.
+ * Creates a {@code String} from the given input, if the given input represents a multiple-line string.
* @param string the given input.
* @param node the parsed node including hidden parts.
* @return the new integer.
@@ -44,20 +41,12 @@
return null;
}
try {
- String clean = removeLineBreaksFrom(string).trim();
- return convertToJavaString(clean.substring(1, clean.length() - 1), true);
+ return convertToJavaString(unquote(removeLineBreaksFrom(string).trim()), true);
} catch (IllegalArgumentException e) {
throw parsingError(string, node, e);
}
}
- private static String removeLineBreaksFrom(String s) {
- if (isEmpty(s)) {
- return s;
- }
- return LINE_BREAK.matcher(s).replaceAll("");
- }
-
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/model/util/Imports.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/util/Imports.java
index bd08c14..deaef4c 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/util/Imports.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/util/Imports.java
@@ -8,25 +8,31 @@
*/
package com.google.eclipse.protobuf.model.util;
+import static com.google.eclipse.protobuf.protobuf.ProtobufPackage.Literals.IMPORT__IMPORT_URI;
+import static com.google.eclipse.protobuf.util.Strings.*;
+import static org.eclipse.xtext.util.Strings.convertToJavaString;
+
import org.eclipse.emf.common.util.URI;
+import org.eclipse.xtext.nodemodel.INode;
import com.google.eclipse.protobuf.protobuf.Import;
import com.google.eclipse.protobuf.scoping.ProtoDescriptorProvider;
-import com.google.inject.*;
+import com.google.inject.Inject;
/**
* Utility methods related to imports.
*
* @author alruiz@google.com (Alex Ruiz)
*/
-@Singleton public class Imports {
+public class Imports {
@Inject private ProtoDescriptorProvider descriptorProvider;
+ @Inject private INodes nodes;
/**
- * Indicates whether the URI of the given import is equal to the path of descriptor.proto
- * ("google/protobuf/descriptor.proto").
- * @param anImport the import to check.
- * @return {@code true} if the URI of the given import is equal to the path of descriptor.proto, {@code false}
+ * Indicates whether the URI of the given {@code Import} is equal to the path of the file "descriptor.proto."
+ * @param anImport the {@code Import} to check.
+ * @return {@code true} if the URI of the given {@code Import} is equal to the path of the file "descriptor.proto,"
+ * {@code false}
* otherwise.
*/
public boolean hasUnresolvedDescriptorUri(Import anImport) {
@@ -37,9 +43,9 @@
}
/**
- * Indicates whether the given <code>{@link Import}</code> is pointing to descriptor.proto.
- * @param anImport the given import to check.
- * @return {@code true} if the given import is pointing to descriptor.proto, {@code false} otherwise.
+ * Indicates whether the given {@code Import} is pointing to descriptor.proto.
+ * @param anImport the given {@code Import} to check.
+ * @return {@code true} if the given {@code Import} is pointing to descriptor.proto, {@code false} otherwise.
*/
public boolean isImportingDescriptor(Import anImport) {
if (hasUnresolvedDescriptorUri(anImport)) {
@@ -57,4 +63,21 @@
}
return false;
}
-}
+
+ /**
+ * Returns the URI of the given {@code Import} as it looks in the editor (i.e. before it is resolved.)
+ * @param anImport the given {@code Import}.
+ * @return the URI of the given {@code Import} as it looks in the editor.
+ */
+ public String uriAsEnteredByUser(Import anImport) {
+ INode node = nodes.firstNodeForFeature(anImport, IMPORT__IMPORT_URI);
+ if (node == null) {
+ return null;
+ }
+ String text = node.getText();
+ if (text == null) {
+ return null;
+ }
+ return convertToJavaString(unquote(removeLineBreaksFrom(text).trim()), 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 59867db..bcaf5fa 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
@@ -8,16 +8,24 @@
*/
package com.google.eclipse.protobuf.util;
+import static java.util.regex.Pattern.compile;
+import static org.eclipse.xtext.util.Strings.isEmpty;
+
+import java.util.regex.Pattern;
+
/**
* Utility methods related to {@code String}.s
*
* @author alruiz@google.com (Alex Ruiz)
*/
public final class Strings {
+ private static final Pattern LINE_BREAK = compile("\"[\t\r\n]+\"|'[\t\r\n]+'");
+
/**
- * Returns the given {@code String} in double quotes.
+ * Returns a {@code String} containing the given one in double quotes.
* @param s the given {@code String}, may be {@code null}.
- * @return the given {@code String} in double quotes, or {@code null} if the given {@code String} is {@code null}.
+ * @return a {@code String} containing the given one in double quotes, or {@code null} if the given {@code String} is
+ * {@code null}.
*/
public static String quote(String s) {
if (s == null) {
@@ -26,5 +34,37 @@
return "\"" + s + "\"";
}
+ /**
+ * Removes surrounding quotes from the given {@code String}.
+ * @param s the given {@code String}, may be {@code null}.
+ * @return a {@code String} containing the given one without surrounding quotes, or {@code null} if the given
+ * {@code String} is {@code null}.
+ */
+ public static String unquote(String s) {
+ if (!isQuoted(s)) {
+ return s;
+ }
+ return s.substring(1, s.length() - 1);
+ }
+
+ private static boolean isQuoted(String s) {
+ if (isEmpty(s)) {
+ return false;
+ }
+ return (s.startsWith("\"") && s.endsWith("\"")) || (s.startsWith("'") && s.endsWith("'"));
+ }
+
+ /**
+ * Returns a {@code String} containing the given one without line breaks.
+ * @param s 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 removeLineBreaksFrom(String s) {
+ if (isEmpty(s)) {
+ return s;
+ }
+ return LINE_BREAK.matcher(s).replaceAll("");
+ }
private Strings() {}
}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/ImportValidator.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/ImportValidator.java
index 4f06f51..916a911 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/ImportValidator.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/validation/ImportValidator.java
@@ -41,8 +41,8 @@
@Override public void register(EValidatorRegistrar registrar) {}
/**
- * Verifies that "imports" in the given root only refer to "proto2" files. If non-proto2 "imports" are found, this
- * validator creates warning markers for such "imports".
+ * Verifies that {@code Import}s in the given root only refer to "proto2" files. If non-proto2 {@code Import}s are
+ * found, this validator will create warning markers for such "imports".
* @param root the root containing the imports to check.
*/
@Check public void checkNonProto2Imports(Protobuf root) {
@@ -109,9 +109,9 @@
}
/**
- * Verifies that the URI of the given "import" has been resolved. If the URI has not been resolved, the validator
- * creates an error marker for the given "import."
- * @param anImport the given "import."
+ * Verifies that the URI of the given {@code Import} has been resolved. If the URI has not been resolved, this
+ * validator will create an error marker for the given {@code Import}.
+ * @param anImport the given {@code Import}.
*/
@Check public void checkUriIsResolved(Import anImport) {
if (isResolved(anImport)) {