Simplified extension point for obtaining descriptor.proto.
diff --git a/com.google.eclipse.protobuf/schema/descriptor-source.exsd b/com.google.eclipse.protobuf/schema/descriptor-source.exsd index 98b9724..94685c8 100644 --- a/com.google.eclipse.protobuf/schema/descriptor-source.exsd +++ b/com.google.eclipse.protobuf/schema/descriptor-source.exsd
@@ -49,13 +49,13 @@ <element name="source"> <complexType> - <attribute name="class" type="string"> + <attribute name="path" type="string" use="required"> <annotation> <documentation> Provides the contents of a descriptor.proto file. </documentation> <appInfo> - <meta.attribute kind="java" basedOn=":com.google.eclipse.protobuf.scoping.IProtoDescriptorSource"/> + <meta.attribute kind="resource"/> </appInfo> </annotation> </attribute>
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/IProtoDescriptorSource.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/IProtoDescriptorSource.java deleted file mode 100644 index 319e45b..0000000 --- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/IProtoDescriptorSource.java +++ /dev/null
@@ -1,37 +0,0 @@ -/* - * 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.scoping; - -import com.google.inject.ImplementedBy; - -import org.eclipse.emf.common.util.URI; - -import java.io.*; - -/** - * Provides the contents of <code>{@link ProtoDescriptor}</code>. - * - * @author alruiz@google.com (Alex Ruiz) - */ -@ImplementedBy(OssProtoDescriptorSource.class) -public interface IProtoDescriptorSource { - - /** - * Returns the URI of the resource containing the descriptor. - * @return the URI of the resource containing the descriptor. - */ - URI uri(); - - /** - * Returns the contents of the descriptor. - * @return the contents of the descriptor. - * @throws IOException if something goes wrong. - */ - InputStream contents() throws IOException; -}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/OssProtoDescriptorSource.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/OssProtoDescriptorSource.java deleted file mode 100644 index 01492b4..0000000 --- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/OssProtoDescriptorSource.java +++ /dev/null
@@ -1,37 +0,0 @@ -/* - * 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.scoping; - -import com.google.inject.Singleton; - -import org.eclipse.emf.common.util.URI; - -import java.io.*; -import java.net.URL; - -/** - * @author alruiz@google.com (Alex Ruiz) - */ -@Singleton -class OssProtoDescriptorSource implements IProtoDescriptorSource { - - private static final String DESCRIPTOR_URL = "platform:/plugin/com.google.eclipse.protobuf/descriptor.proto"; - - private static final URI DESCRIPTOR_URI = URI.createURI(DESCRIPTOR_URL); - - public URI uri() { - return DESCRIPTOR_URI; - } - - public InputStream contents() throws IOException { - URL url = new URL(DESCRIPTOR_URL); - return url.openConnection().getInputStream(); - } - -}
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtoDescriptor.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtoDescriptor.java index 31c4cf5..37706cf 100644 --- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtoDescriptor.java +++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtoDescriptor.java
@@ -21,11 +21,13 @@ import com.google.eclipse.protobuf.util.ModelNodes; import com.google.inject.Inject; +import org.eclipse.emf.common.util.URI; import org.eclipse.xtext.nodemodel.INode; import org.eclipse.xtext.parser.*; import org.eclipse.xtext.resource.XtextResource; -import java.io.InputStreamReader; +import java.io.*; +import java.net.URL; import java.util.*; /** @@ -51,13 +53,13 @@ private final ModelNodes nodes; - @Inject public ProtoDescriptor(IParser parser, IProtoDescriptorSource source, ModelNodes nodes) { + @Inject public ProtoDescriptor(IParser parser, URI descriptorLocation, ModelNodes nodes) { this.nodes = nodes; addOptionTypes(); InputStreamReader reader = null; try { - XtextResource resource = new XtextResource(source.uri()); - reader = new InputStreamReader(source.contents(), "UTF-8"); + XtextResource resource = new XtextResource(descriptorLocation); + reader = new InputStreamReader(contents(descriptorLocation), "UTF-8"); IParseResult result = parser.parse(reader); root = (Protobuf) result.getRootASTElement(); resource.getContents().add(root); @@ -71,6 +73,17 @@ } } + /** + * Returns the contents of the descriptor file at the given location. + * @param descriptorLocation the location of the descriptor file. + * @return the contents of the descriptor file. + * @throws IOException if something goes wrong. + */ + protected InputStream contents(URI descriptorLocation) throws IOException { + URL url = new URL(descriptorLocation.toString()); + return url.openConnection().getInputStream(); + } + private void addOptionTypes() { for (OptionType type : OptionType.values()) optionsByType.put(type, new LinkedHashMap<String, Property>());
diff --git a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtoDescriptorProvider.java b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtoDescriptorProvider.java index f1d8ca5..8cc6b2b 100644 --- a/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtoDescriptorProvider.java +++ b/com.google.eclipse.protobuf/src/com/google/eclipse/protobuf/scoping/ProtoDescriptorProvider.java
@@ -8,11 +8,13 @@ */ package com.google.eclipse.protobuf.scoping; +import static org.eclipse.xtext.util.Strings.isEmpty; + import com.google.eclipse.protobuf.util.ModelNodes; import com.google.inject.*; -import org.apache.log4j.Logger; import org.eclipse.core.runtime.*; +import org.eclipse.emf.common.util.URI; import org.eclipse.xtext.parser.IParser; /** @@ -25,10 +27,7 @@ private static final String EXTENSION_ID = "com.google.eclipse.protobuf.descriptorSource"; - private static Logger logger = Logger.getLogger(ProtoDescriptorProvider.class); - @Inject private IParser parser; - @Inject private IProtoDescriptorSource source; @Inject private ModelNodes nodes; @Inject private IExtensionRegistry registry; @@ -39,24 +38,19 @@ public ProtoDescriptor get() { synchronized (lock) { if (descriptor == null) { - IProtoDescriptorSource actualSource = sourceFromPlugin(); - if (actualSource == null) actualSource = source; - descriptor = new ProtoDescriptor(parser, actualSource, nodes); + descriptor = new ProtoDescriptor(parser, descriptorLocation(), nodes); } return descriptor; } } - private IProtoDescriptorSource sourceFromPlugin() { + private URI descriptorLocation() { IConfigurationElement[] config = registry.getConfigurationElementsFor(EXTENSION_ID); - try { - for (IConfigurationElement e : config) { - Object extension = e.createExecutableExtension("class"); - if (extension instanceof IProtoDescriptorSource) return (IProtoDescriptorSource) extension; - } - } catch (CoreException t) { - logger.fatal("Unable to instantiate extension elements", t); + for (IConfigurationElement e : config) { + String path = e.getAttribute("path"); + if (isEmpty(path)) continue; + return URI.createURI("platform:/plugin/" + e.getContributor().getName() + "/" + path); } - return null; + return URI.createURI("platform:/plugin/com.google.eclipse.protobuf/descriptor.proto"); } }