diff --git a/plugins/org.eclipse.xtext/plugin.xml b/plugins/org.eclipse.xtext/plugin.xml index f14e36da7..ecf77e643 100644 --- a/plugins/org.eclipse.xtext/plugin.xml +++ b/plugins/org.eclipse.xtext/plugin.xml @@ -5,7 +5,7 @@ - $Id: plugin.xml,v 1.3 2008/05/23 08:53:11 jkohnlein Exp $ + $Id: plugin.xml,v 1.4 2008/10/01 13:40:11 jkohnlein Exp $ --> @@ -30,5 +30,13 @@ class = "org.eclipse.xtext.xtextutil.XtextutilPackage" genModel = "model/parsetree.genmodel" /> - + diff --git a/plugins/org.eclipse.xtext/src/org/eclipse/xtext/resource/metamodel/XtextMetamodelResource.java b/plugins/org.eclipse.xtext/src/org/eclipse/xtext/resource/metamodel/XtextMetamodelResource.java index 94e02361c..c56182f46 100644 --- a/plugins/org.eclipse.xtext/src/org/eclipse/xtext/resource/metamodel/XtextMetamodelResource.java +++ b/plugins/org.eclipse.xtext/src/org/eclipse/xtext/resource/metamodel/XtextMetamodelResource.java @@ -12,20 +12,67 @@ import java.io.IOException; import java.io.InputStream; import java.util.Map; +import org.eclipse.emf.common.util.URI; +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.services.XtextResourceFactory; /** * @author Jan Köhnlein - Initial contribution and API - * + * */ public class XtextMetamodelResource extends ResourceImpl { - /* (non-Javadoc) - * @see org.eclipse.emf.ecore.resource.impl.ResourceImpl#doLoad(java.io.InputStream, java.util.Map) + public XtextMetamodelResource(URI uri) { + super(uri); + } + + private Grammar getGrammar(String languageID) { + IServiceScope serviceScope = ServiceScopeFactory.get(languageID); + IGrammarAccess service = ServiceRegistry.getService(serviceScope, IGrammarAccess.class); + return service.getGrammar(); + } + /* + * (non-Javadoc) + * + * @see + * org.eclipse.emf.ecore.resource.impl.ResourceImpl#doLoad(java.io.InputStream + * , java.util.Map) */ @Override protected void doLoad(InputStream inputStream, Map options) throws IOException { - // TODO: implement - super.doLoad(inputStream, options); + XtextResourceFactory xtextResourceFactory = new XtextResourceFactory(); + Resource xtextResource = xtextResourceFactory.createResource(getURI()); +// xtextResource.load(inputStream, options); +// EList 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 ePackages = xtext2EcoreTransformer.transform(grammar); +// getContents().addAll(ePackages); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.emf.ecore.resource.impl.ResourceImpl#doUnload() + */ + @Override + protected void doUnload() { + // TODO Auto-generated method stub + super.doUnload(); } } diff --git a/plugins/org.eclipse.xtext/src/org/eclipse/xtext/resource/metamodel/XtextMetamodelResourceFactory.java b/plugins/org.eclipse.xtext/src/org/eclipse/xtext/resource/metamodel/XtextMetamodelResourceFactory.java new file mode 100644 index 000000000..ffa0dd10f --- /dev/null +++ b/plugins/org.eclipse.xtext/src/org/eclipse/xtext/resource/metamodel/XtextMetamodelResourceFactory.java @@ -0,0 +1,27 @@ +/******************************************************************************* + * Copyright (c) 2008 itemis AG (http://www.itemis.eu) and others. + * 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 org.eclipse.xtext.resource.metamodel; + +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.resource.impl.ResourceFactoryImpl; + +/** + * @author Jan Köhnlein - Initial contribution and API + */ +public class XtextMetamodelResourceFactory extends ResourceFactoryImpl { + + /* (non-Javadoc) + * @see org.eclipse.emf.ecore.resource.impl.ResourceFactoryImpl#createResource(org.eclipse.emf.common.util.URI) + */ + @Override + public Resource createResource(URI uri) { + // TODO Auto-generated method stub + return new XtextMetamodelResource(uri); + } +} diff --git a/tests/org.eclipse.xtext.generator.tests/.project b/tests/org.eclipse.xtext.generator.tests/.project index 4507b2ec6..2b87c6ab4 100644 --- a/tests/org.eclipse.xtext.generator.tests/.project +++ b/tests/org.eclipse.xtext.generator.tests/.project @@ -1,6 +1,6 @@ - org.eclipse.xtext.reference.ui_gen + org.eclipse.xtext.generator.tests diff --git a/tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/resource/metamodel/XtextMetamodelResourceTest.java b/tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/resource/metamodel/XtextMetamodelResourceTest.java new file mode 100644 index 000000000..d696456bb --- /dev/null +++ b/tests/org.eclipse.xtext.generator.tests/src/org/eclipse/xtext/resource/metamodel/XtextMetamodelResourceTest.java @@ -0,0 +1,36 @@ +/******************************************************************************* + * Copyright (c) 2008 itemis AG (http://www.itemis.eu) and others. + * 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 org.eclipse.xtext.resource.metamodel; + +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.resource.Resource; +import org.eclipse.xtext.XtextStandaloneSetup; +import org.eclipse.xtext.resource.XtextResourceSet; +import org.eclipse.xtext.testlanguages.TestLanguageStandaloneSetup; + +/** + * @author Jan Köhnlein - Initial contribution and API + */ +public class XtextMetamodelResourceTest extends TestCase { + + public void testResourceLoad() throws Exception { + XtextStandaloneSetup.doSetup(); + TestLanguageStandaloneSetup.doSetup(); + XtextResourceSet rs = new XtextResourceSet(); + rs.setClasspathURIContext(XtextMetamodelResourceTest.class.getClassLoader()); + rs.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xtext", new XtextMetamodelResourceFactory()); + Resource resource = rs.getResource(URI.createURI("classpath:/org/eclipse/xtext/testlanguages/TestLanguage.xtext"), true); + EList contents = resource.getContents(); + assertEquals(1, contents.size()); + fail(); + } +}