Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » static model from xml(generate java class from xml description)
static model from xml [message #1709967] Fri, 02 October 2015 09:52 Go to next message
Alex Gor is currently offline Alex GorFriend
Messages: 159
Registered: November 2014
Location: Russia
Senior Member
Hello

I work on xtext based application. Right now we use dynamic implementaion of model. It means : we have xml file that describe object in model and xsd file that is used for verification xml file. Metamodel is loaded from xml file using code like this
metamodel = new XSDMetaModel();
xsdFileName = FileSystem.getTypesXsdPath();
metamodel.addSchemaFile(xsdFileName);
XMLReaderImpl reader = new XMLReaderImpl(new ResourceSetImpl(), etamodel);
xmlFileName = FileSystemRegistry.getAtomFileSystem().getTypesXmlPath();
reader.setUri(xmlFileName);
EObject e = reader.readXML();
return e;


then we create actual object of semantic model via


public static EObject createEObject(final String name, final EPackage metamodel) {
EClass meta = (EClass) metamodel.getEClassifier(name);
if (meta != null) {
return metamodel.getEFactoryInstance().create(meta);
} else {
throw new RuntimeException("Metamodelelement " + name + " cannot be created. Probably a misstyping?");
}
}
[/code]

So we use dynamic implmentaion. Are there way to generate Java class from XML descriptionin compile time and used static implementation?
<type name="XXXT" schema="SHEMA" table="TABLE">
		<attribute name="N1" type="String" />
		<attribute name="N2" type="Integer"/>
	</type>


I would like to create ecore file from this descprition like
  <eClassifiers xsi:type="ecore:EClass" name="C1" eSuperTypes="//GENERICTYPE">
    <eStructuralFeatures xsi:type="ecore:EAttribute" name="C1">
      <eType xsi:type="ecore:EDataType" href="http://www.eclipse.org/emf/2002/Ecore#//EString"/>
    </eStructuralFeatures>
    <eStructuralFeatures xsi:type="ecore:EAttribute" name="C2">
      <eType xsi:type="ecore:EDataType" href="http://www.eclipse.org/emf/2002/Ecore#//EString"/>
    </eStructuralFeatures>
   

[Updated on: Fri, 02 October 2015 10:57]

Report message to a moderator

Re: static model from xml [message #1710091 is a reply to message #1709967] Sun, 04 October 2015 07:43 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Alex,

Comments below.

On 02/10/2015 11:52 AM, Alex Gor wrote:
> Hello
>
> I work on xtext based application. Right now we use dynamic
> implementaion of model. It means : we have xml file that describe
> object in model and xsd file that is used for verification xml file.
> Metamodel is loaded from xml file using code like this
>
> metamodel = new XSDMetaModel();
> xsdFileName = FileSystem.getTypesXsdPath();
> metamodel.addSchemaFile(xsdFileName);
> XMLReaderImpl reader = new XMLReaderImpl(new ResourceSetImpl(),
> etamodel);
> xmlFileName = FileSystemRegistry.getAtomFileSystem().getTypesXmlPath();
> reader.setUri(xmlFileName);
> EObject e = reader.readXML();
> return e;
I don't recognize these class names. They're not provided by EMF itself...
>
>
> then we create actual object of semantic model via
>
> public static EObject createEObject(final String name, final EPackage
> metamodel) {
> EClass meta = (EClass) metamodel.getEClassifier(name);
> if (meta != null) {
> return metamodel.getEFactoryInstance().create(meta);
> } else {
> throw new RuntimeException("Metamodelelement " + name + "
> cannot be created. Probably a misstyping?");
> }
> }
>
>
> So we use dynamic implmentaion. Are there way to generate Java class
> from XML descriptionin compile time and used static implementation?
This old tutorial shows how:
https://www.eclipse.org/modeling/emf/docs/2.x/tutorials/xlibmod/xlibmod_emf2.0.html
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: static model from xml [message #1710215 is a reply to message #1710091] Mon, 05 October 2015 11:57 Go to previous messageGo to next message
Alex Gor is currently offline Alex GorFriend
Messages: 159
Registered: November 2014
Location: Russia
Senior Member
Thanks for the link. I generated corresponded editor. To my regret is unclear how to imported the xml description to generated editor. I can do it programmatically using code
metamodel = new XSDMetaModel();
xsdFileName = FileSystem.getTypesXsdPath();
metamodel.addSchemaFile(xsdFileName);
XMLReaderImpl reader = new XMLReaderImpl(new ResourceSetImpl(), etamodel);
xmlFileName = FileSystemRegistry.getTypesXmlPath();
reader.setUri(xmlFileName);
EObject e = reader.readXML();
return e


Can I do it in compile time and then using generated class instead of dynamic implementation?

[Updated on: Mon, 05 October 2015 11:57]

Report message to a moderator

Re: static model from xml [message #1710256 is a reply to message #1710215] Mon, 05 October 2015 14:40 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Alex,

As I mentioned, I don't recognize these APIs. If you've followed a
tutorial, you can invoke Generate Test Code and look at the
XyzExample.Java to see to load XML purely with EMF using the generated
model in a stand alone program...

On 05/10/2015 1:57 PM, Alex Gor wrote:
> Thanks for the link. I generated corresponded editor. To my regret is
> unclear how to imported the xml description to generated editor. I can
> do it programmatically using code
> metamodel = new XSDMetaModel();
> xsdFileName = FileSystem.getTypesXsdPath();
> metamodel.addSchemaFile(xsdFileName);
> XMLReaderImpl reader = new XMLReaderImpl(new ResourceSetImpl(),
> etamodel);
> xmlFileName = FileSystemRegistry.getAtomFileSystem().getTypesXmlPath();
> reader.setUri(xmlFileName);
> EObject e = reader.readXML();
> return e
>
>
> Can I do it in compile time and then using generated class instead of
> dynamic implementation?
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Renaming a Resource opened in Editor
Next Topic:Adding Drop Support To Ecore Editor
Goto Forum:
  


Current Time: Fri Apr 26 23:39:07 GMT 2024

Powered by FUDForum. Page generated in 0.03127 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top