mirror of
https://github.com/sigmasternchen/xtext-core
synced 2025-03-16 16:58:56 +00:00
Solved superGrammar problem by implying that XtextMetmodelResource is always within an XtextResourceSet and thereby has a classloader context
Heiko's patch https://bugs.eclipse.org/bugs/attachment.cgi?id=114089
This commit is contained in:
parent
3d393de36b
commit
9ab63c2ff1
2 changed files with 39 additions and 27 deletions
|
@ -10,17 +10,19 @@ package org.eclipse.xtext.resource.metamodel;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.eclipse.emf.common.util.EList;
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.emf.ecore.EPackage;
|
||||
import org.eclipse.emf.ecore.resource.Resource;
|
||||
import org.eclipse.emf.ecore.resource.impl.ResourceImpl;
|
||||
import org.eclipse.xtext.Grammar;
|
||||
import org.eclipse.xtext.IGrammarAccess;
|
||||
import org.eclipse.xtext.service.IServiceScope;
|
||||
import org.eclipse.xtext.service.ServiceRegistry;
|
||||
import org.eclipse.xtext.service.ServiceScopeFactory;
|
||||
import org.eclipse.xtext.resource.XtextResourceSet;
|
||||
import org.eclipse.xtext.services.XtextResourceFactory;
|
||||
import org.eclipse.xtext.util.Strings;
|
||||
|
||||
/**
|
||||
* @author Jan Köhnlein - Initial contribution and API
|
||||
|
@ -32,11 +34,6 @@ public class XtextMetamodelResource extends ResourceImpl {
|
|||
super(uri);
|
||||
}
|
||||
|
||||
private Grammar getGrammar(String languageID) {
|
||||
IServiceScope serviceScope = ServiceScopeFactory.get(languageID);
|
||||
IGrammarAccess service = ServiceRegistry.getService(serviceScope, IGrammarAccess.class);
|
||||
return service.getGrammar();
|
||||
}
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
|
@ -46,23 +43,35 @@ public class XtextMetamodelResource extends ResourceImpl {
|
|||
*/
|
||||
@Override
|
||||
protected void doLoad(InputStream inputStream, Map<?, ?> options) throws IOException {
|
||||
XtextResourceFactory xtextResourceFactory = new XtextResourceFactory();
|
||||
Resource xtextResource = xtextResourceFactory.createResource(getURI());
|
||||
// xtextResource.load(inputStream, options);
|
||||
// EList<EObject> grammarResourceContents = xtextResource.getContents();
|
||||
// if (grammarResourceContents.size() != 1) {
|
||||
// throw new IllegalStateException("XtextResource " + Strings.notNull(getURI().toString())
|
||||
// + " contains other than 1 elements");
|
||||
// }
|
||||
// EObject firstObject = grammarResourceContents.get(0);
|
||||
// if (!(firstObject instanceof Grammar)) {
|
||||
// throw new IllegalStateException("XtextResource " + Strings.notNull(getURI().toString())
|
||||
// + " does not contain a root element of type Grammar");
|
||||
// }
|
||||
// Grammar grammar = (Grammar) firstObject;
|
||||
// Xtext2EcoreTransformer xtext2EcoreTransformer = new Xtext2EcoreTransformer();
|
||||
// List<EPackage> ePackages = xtext2EcoreTransformer.transform(grammar);
|
||||
// getContents().addAll(ePackages);
|
||||
if (getResourceSet() == null || !(getResourceSet() instanceof XtextResourceSet))
|
||||
throw new IllegalStateException("XtextResource " + Strings.notNull(getURI().toString())
|
||||
+ " must must be contained in a XtextResourceSet");
|
||||
|
||||
XtextResourceSet containerResourceSet = (XtextResourceSet) this.getResourceSet();
|
||||
XtextResourceSet rs = new XtextResourceSet();
|
||||
rs.setClasspathURIContext(containerResourceSet.getClasspathURIContext());
|
||||
rs.getResourceFactoryRegistry().getExtensionToFactoryMap().put(getURI().fileExtension(),
|
||||
new XtextResourceFactory());
|
||||
|
||||
Resource xtextResource = rs.getResource(getURI(), true);
|
||||
xtextResource.load(inputStream, options);
|
||||
|
||||
EList<EObject> grammarResourceContents = xtextResource.getContents();
|
||||
if (grammarResourceContents.size() != 1) {
|
||||
throw new IllegalStateException("XtextResource " + Strings.notNull(getURI().toString())
|
||||
+ " contains other than 1 element");
|
||||
}
|
||||
EObject firstObject = grammarResourceContents.get(0);
|
||||
if (!(firstObject instanceof Grammar)) {
|
||||
throw new IllegalStateException("XtextResource " + Strings.notNull(getURI().toString())
|
||||
+ " does not contain a root element of type Grammar");
|
||||
}
|
||||
Grammar grammar = (Grammar) firstObject;
|
||||
Xtext2EcoreTransformer xtext2EcoreTransformer = new Xtext2EcoreTransformer();
|
||||
// TODO bind ErrorAcceptor
|
||||
|
||||
List<EPackage> ePackages = xtext2EcoreTransformer.transform(grammar);
|
||||
getContents().addAll(ePackages);
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -12,6 +12,7 @@ import junit.framework.TestCase;
|
|||
import org.eclipse.emf.common.util.EList;
|
||||
import org.eclipse.emf.common.util.URI;
|
||||
import org.eclipse.emf.ecore.EObject;
|
||||
import org.eclipse.emf.ecore.EPackage;
|
||||
import org.eclipse.emf.ecore.resource.Resource;
|
||||
import org.eclipse.xtext.XtextStandaloneSetup;
|
||||
import org.eclipse.xtext.resource.XtextResourceSet;
|
||||
|
@ -31,6 +32,8 @@ public class XtextMetamodelResourceTest extends TestCase {
|
|||
Resource resource = rs.getResource(URI.createURI("classpath:/org/eclipse/xtext/testlanguages/TestLanguage.xtext"), true);
|
||||
EList<EObject> contents = resource.getContents();
|
||||
assertEquals(1, contents.size());
|
||||
fail();
|
||||
assertTrue(contents.get(0) instanceof EPackage);
|
||||
EPackage ePackage = (EPackage) contents.get(0);
|
||||
assertEquals(6, ePackage.getEClassifiers().size());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue