Code cleanup.
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 3d24d80..43af49e 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
@@ -12,13 +12,14 @@
import static com.google.eclipse.protobuf.util.Strings.*;
import static org.eclipse.xtext.util.Strings.*;
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.xtext.nodemodel.INode;
+import org.eclipse.xtext.scoping.impl.ImportUriResolver;
+
import com.google.eclipse.protobuf.protobuf.Import;
import com.google.eclipse.protobuf.scoping.ProtoDescriptorProvider;
import com.google.inject.Inject;
-import org.eclipse.emf.common.util.URI;
-import org.eclipse.xtext.nodemodel.INode;
-
/**
* Utility methods related to imports.
*
@@ -27,6 +28,7 @@
public class Imports {
@Inject private ProtoDescriptorProvider descriptorProvider;
@Inject private INodes nodes;
+ @Inject private ImportUriResolver uriResolver;
/**
* Indicates whether the URI of the given {@code Import} is equal to the path of the file "descriptor.proto."
@@ -94,4 +96,14 @@
}
return false;
}
+
+ /**
+ * Returns the resolved URI of the given {@code Import}.
+ * @param anImport the the given {@code Import}.
+ * @return the resolved URI of the given {@code Import}, or {@code null} if the URI was not successfully resolved.
+ */
+ public URI resolvedUriOf(Import anImport) {
+ String resolvedUri = uriResolver.apply(anImport);
+ return (isEmpty(resolvedUri)) ? null : URI.createURI(resolvedUri);
+ }
}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/util/Resources.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/util/Resources.java
index 1627d2c..b159fb2 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/util/Resources.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/model/util/Resources.java
@@ -8,44 +8,23 @@
*/
package com.google.eclipse.protobuf.model.util;
-import static org.eclipse.emf.common.util.URI.createURI;
import static org.eclipse.emf.ecore.util.EcoreUtil.getAllContents;
-import org.eclipse.emf.common.util.*;
+import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
-import org.eclipse.emf.ecore.resource.*;
+import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.parser.IParseResult;
import org.eclipse.xtext.resource.XtextResource;
-import org.eclipse.xtext.scoping.impl.ImportUriResolver;
-import com.google.eclipse.protobuf.protobuf.*;
-import com.google.inject.Inject;
+import com.google.eclipse.protobuf.protobuf.Protobuf;
+import com.google.inject.Singleton;
/**
* Utility methods related to <code>{@link Resource}</code>
*
* @author alruiz@google.com (Alex Ruiz)
*/
-public class Resources {
- @Inject private ImportUriResolver uriResolver;
-
- /**
- * Finds in the given <code>{@link ResourceSet}</code> the resource referred by the URI of the given import.
- * @param anImport the given import.
- * @param resourceSet a collection of resources.
- * @return the resource referred by the URI of the given import, or {@code null} is the given {@code ResourceSet} does
- * not contain the resource.
- */
- // TODO move to class ResourceSets
- public Resource importedResource(Import anImport, ResourceSet resourceSet) {
- try {
- URI importUri = createURI(uriResolver.apply(anImport));
- return resourceSet.getResource(importUri, true);
- } catch (Throwable t) {
- return null;
- }
- }
-
+@Singleton public class Resources {
/**
* Returns the root element of the given resource.
* @param resource the given resource.
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/resource/ResourceSets.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/resource/ResourceSets.java
new file mode 100644
index 0000000..cb0c78f
--- /dev/null
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/resource/ResourceSets.java
@@ -0,0 +1,37 @@
+/*
+ * 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.resource;
+
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.resource.*;
+
+import com.google.inject.Singleton;
+
+/**
+ * Utility methods related to <code>{@link ResourceSet}</code>s.
+ *
+ * @author alruiz@google.com (Alex Ruiz)
+ */
+@Singleton public class ResourceSets {
+
+ /**
+ * Finds in the given <code>{@link ResourceSet}</code> the resource referred by the given URI.
+ * @param resourceSet a collection of resources.
+ * @param uri the given URI.
+ * @return the resource referred by the given URI, or {@code null} is the given {@code ResourceSet} does
+ * not contain the resource.
+ */
+ public Resource findResource(ResourceSet resourceSet, URI uri) {
+ try {
+ return resourceSet.getResource(uri, true);
+ } catch (Throwable t) {
+ return null;
+ }
+ }
+}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ModelElementFinder.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ModelElementFinder.java
index 19c53b3..53fa8bc 100644
--- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ModelElementFinder.java
+++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ModelElementFinder.java
@@ -14,7 +14,7 @@
import java.util.*;
-import org.eclipse.emf.common.util.TreeIterator;
+import org.eclipse.emf.common.util.*;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.*;
import org.eclipse.xtext.resource.IEObjectDescription;
@@ -22,6 +22,7 @@
import com.google.eclipse.protobuf.model.util.*;
import com.google.eclipse.protobuf.protobuf.*;
import com.google.eclipse.protobuf.protobuf.Package;
+import com.google.eclipse.protobuf.resource.ResourceSets;
import com.google.inject.Inject;
/**
@@ -33,6 +34,7 @@
@Inject private Packages packages;
@Inject private Protobufs protobufs;
@Inject private Resources resources;
+ @Inject private ResourceSets resourceSets;
Collection<IEObjectDescription> find(EObject start, FinderStrategy finderStrategy, Object criteria) {
Set<IEObjectDescription> descriptions = newHashSet();
@@ -87,7 +89,11 @@
descriptions.addAll(finderStrategy.inDescriptor(anImport, criteria));
continue;
}
- Resource imported = resources.importedResource(anImport, resourceSet);
+ URI resolvedUri = imports.resolvedUriOf(anImport);
+ if (resolvedUri == null) {
+ continue;
+ }
+ Resource imported = resourceSets.findResource(resourceSet, resolvedUri);
if (imported == null) {
continue;
}
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 b71b73e..9f978ac 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
@@ -15,9 +15,7 @@
import static java.lang.String.format;
import static org.eclipse.xtext.util.Tuples.pair;
-import com.google.eclipse.protobuf.model.util.*;
-import com.google.eclipse.protobuf.protobuf.*;
-import com.google.inject.Inject;
+import java.util.*;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.*;
@@ -25,7 +23,10 @@
import org.eclipse.xtext.util.Pair;
import org.eclipse.xtext.validation.*;
-import java.util.*;
+import com.google.eclipse.protobuf.model.util.*;
+import com.google.eclipse.protobuf.protobuf.*;
+import com.google.eclipse.protobuf.resource.ResourceSets;
+import com.google.inject.Inject;
/**
* Verifies that "imports" contain correct values.
@@ -36,6 +37,7 @@
@Inject private Imports imports;
@Inject private Protobufs protobufs;
@Inject private Resources resources;
+ @Inject private ResourceSets resourceSets;
@Inject private ImportUriResolver uriResolver;
@Override public void register(EValidatorRegistrar registrar) {}
@@ -60,7 +62,7 @@
Set<URI> checked = newHashSet();
checked.add(resource.getURI());
for (Import anImport : protobufs.importsIn(root)) {
- Resource imported = resources.importedResource(anImport, resourceSet);
+ Resource imported = importedResource(resourceSet, anImport);
checked.add(imported.getURI());
if (!protobufs.isProto2(resources.rootOf(imported))) {
hasNonProto2 = true;
@@ -87,7 +89,7 @@
}
List<Pair<Import, Resource>> resourcesToCheck = newArrayList();
for (Import anImport : protobufs.importsIn(root)) {
- Resource imported = resources.importedResource(anImport, resourceSet);
+ Resource imported = importedResource(resourceSet, anImport);
if (alreadyChecked.contains(imported.getURI())) {
continue;
}
@@ -104,6 +106,14 @@
return false;
}
+ private Resource importedResource(ResourceSet resourceSet, Import anImport) {
+ URI resolvedUri = imports.resolvedUriOf(anImport);
+ if (resolvedUri != null) {
+ return resourceSets.findResource(resourceSet, resolvedUri);
+ }
+ return null;
+ }
+
private void warnNonProto2ImportFoundIn(Import anImport) {
acceptWarning(importingNonProto2, anImport, IMPORT__IMPORT_URI, INSIGNIFICANT_INDEX, null);
}