Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Create xmi file in runtime through saving (Xtext 2.5.1)
Create xmi file in runtime through saving [message #1720777] Wed, 20 January 2016 22:39 Go to next message
Parisa Moosavi is currently offline Parisa MoosaviFriend
Messages: 81
Registered: June 2015
Member
Hi,

I want to save xmi file from the resource in doGenerate. Actually I want to save xmi file when the xtext dsl is saved.

I have changed doGenerate like this
	override void doGenerate(Resource resource, IFileSystemAccess fsa) {
		Helper.saveRecourceAsXmi(resource , resource.URI.path.toString().replace('gcorell','gcore'));
	}


and I have developed an helper class
public class Helper {
	public static void saveRecourceAsXmi( Resource resource , String strURI) {

		try {
			Map<String, String> saveOptions = new HashMap<String, String>();
			Resource xmiResource = new XMIResourceImpl(	URI.createFileURI("filename.myextension"));
			xmiResource.getContents().add(resource.getContents().get(0));
			saveOptions.put(org.eclipse.emf.ecore.xmi.XMLResource.OPTION_ENCODING, "UTF-8");
			xmiResource.save(saveOptions);
			System.out.println("Xmi file filename.myextension saved.");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("Durind saving Xmi file filename.myextension error has been occued.");
			e.printStackTrace();
		}		
	}
}


But it's some how strage I can't find the file, although it is run without exception. indeed I want to save it in the same folder as the resource.URI.Path.
Re: Create xmi file in runtime through saving [message #1720784 is a reply to message #1720777] Thu, 21 January 2016 00:42 Go to previous messageGo to next message
Parisa Moosavi is currently offline Parisa MoosaviFriend
Messages: 81
Registered: June 2015
Member
It was my mistake Smile
public static void saveRecourceAsXmi(Resource resource) {
		try {
			Map<String, String> saveOptions = new HashMap<String, String>();
			Resource xmiResource = new XMIResourceImpl(URI.createURI(resource.getURI().toString().replace("gcorell", "gcore")));
			xmiResource.getContents().add(resource.getContents().get(0));
			saveOptions.put(org.eclipse.emf.ecore.xmi.XMLResource.OPTION_ENCODING,"UTF-8");
			xmiResource.save(saveOptions);
			System.out.println("Xmi file created.");
		} catch (IOException e) {
			System.out.println("Error during creating Xmi.");
			e.printStackTrace();
		}
	}
Re: Create xmi file in runtime through saving [message #1725674 is a reply to message #1720784] Sun, 06 March 2016 15:54 Go to previous messageGo to next message
Parisa Moosavi is currently offline Parisa MoosaviFriend
Messages: 81
Registered: June 2015
Member
Hi,

I use the following code to save a dsl with extension *.wcorell as *.wcore and *.wcore as an xmi format. The code generates the file but document cross reference doesn't work.
	try {
			
			
			ResourceSet localResourceSet= new ResourceSetImpl();
			localResourceSet.getResource(resource.getURI().trimFileExtension().appendFileExtension("ecore"), true);
			localResourceSet.getResource(resource.getURI().trimFileExtension().appendFileExtension("gcore"), true);
			localResourceSet.getResources().add(resource);
			
			EcoreUtil.resolveAll(localResourceSet);			
			
			Map<String, String> saveOptions = new HashMap<String, String>();
			Resource xmiResource = 
					new XMIResourceImpl(resource.getURI().trimFileExtension().appendFileExtension("wcore"));
			xmiResource.getContents().add(localResourceSet.getResources().get(2).getContents().get(0));
			saveOptions.put(org.eclipse.emf.ecore.xmi.XMLResource.OPTION_ENCODING,"UTF-8");
			xmiResource.save(saveOptions);
			
			
//			for (Resource res : localResourceSet.getResources()) {
//				System.out.println("res : " + res.getURI().toString());
//			}

			
			System.out.println(":) Xmi file created for wcore.");
		} catch (IOException e) {
			System.out.println(":( Error during creating Xmi for wcore.");
			e.printStackTrace();
		}


after saving xmi file the part wich contains document cross reference is like following
    <startTask xsi:type="wcore:Sequence" name="seq1">
      <body xsi:type="wcore:Create" name="CreatePerson" return="//CreatePerson/NewPerson">
        <eClass href="[b]PhoneBook.xcore#/EPackage/Person[/b]"/>
        <anchor href="[b]PhoneBook.xcore#/EPackage/PhoneBook/AllPersons[/b]"/>
      </body>
      <follower xsi:type="wcore:Sequence" name="seq2">
        <body xsi:type="wcore:InvokeGUI" name="EnterNewPerson" outputData="//CreatePerson/NewPerson" input="//CreatePerson/NewPerson">
          <page xsi:type="gcore:EditPage" href="[b]PhoneBook.gcorell#//EditPerson[/b]"/>
        </body>
        <follower xsi:type="wcore:InvokeGUI" name="ViewNewPerson" outputData="//CreatePerson/NewPerson" input="//CreatePerson/NewPerson">
          <page xsi:type="gcore:EditPage" href="[b]PhoneBook.gcorell#//EditPerson[/b]"/>
        </follower>
      </follower>
    </startTask>


but I want to have this output.
    <startTask xsi:type="wcore:Sequence" name="seq1">
      <body xsi:type="wcore:Create" name="CreatePerson" return="//CreatePerson/NewPerson">
        <eClass href="[b]PhoneBook.ecore#/EPackage/Person[/b]"/>
        <anchor href="[b]PhoneBook.ecore#/EPackage/PhoneBook/AllPersons[/b]"/>
      </body>
      <follower xsi:type="wcore:Sequence" name="seq2">
        <body xsi:type="wcore:InvokeGUI" name="EnterNewPerson" outputData="//CreatePerson/NewPerson" input="//CreatePerson/NewPerson">
          <page xsi:type="gcore:EditPage" href="[b]PhoneBook.gcore#//EditPerson[/b]"/>
        </body>
        <follower xsi:type="wcore:InvokeGUI" name="ViewNewPerson" outputData="//CreatePerson/NewPerson" input="//CreatePerson/NewPerson">
          <page xsi:type="gcore:EditPage" href="[b]PhoneBook.gcore#//EditPerson[/b]"/>
        </follower>
      </follower>
    </startTask>


What should I add to my code?

Thanks for helping

Re: Create xmi file in runtime through saving [message #1725694 is a reply to message #1725674] Mon, 07 March 2016 04:08 Go to previous message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 14083
Registered: July 2009
Senior Member
I have no idea what you are doing but

If you create the xmi resource in the same resource set and if you move all stuff to xmi resources it should work as you want it


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/it-services/methods-and-tools/xtext
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Using XPath for AST querying
Next Topic:Call functions from other files
Goto Forum:
  


Current Time: Thu Oct 28 12:32:23 GMT 2021

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

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

Back to the top