Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Sapphire » Unmarshall XML in Model
Unmarshall XML in Model [message #720612] Tue, 30 August 2011 23:27 Go to next message
Nepomuk Seiler is currently offline Nepomuk Seiler
Messages: 88
Registered: December 2010
Member
Hi,

I want to build an editor with Sapphire for my XML-Model. Right now I'm using JAXB to annotate my class and (un)marshall it.

First of all, is that the best-practices to retrieve my model from a XML file?

XmlResourceStore store = new XmlResourceStore(new FileInputStream("/path/to/xmlFile.xml"));
RootXmlResource xmlResource = new RootXmlResource(store);
IDataProcessingUnit dpu = IDataProcessingUnit.TYPE.instantiate(xmlResource);



Second, which bundles do I have to import, so other bundles which don't provide any UI functions, can unmarshal this model?


  • *.sapphire.modeling
  • *.sapphire.modeling.xml


is that enough?
Re: Unmarshall XML in Model [message #720637 is a reply to message #720612] Wed, 31 August 2011 01:22 Go to previous messageGo to next message
Konstantin Komissarchik is currently offline Konstantin Komissarchik
Messages: 940
Registered: July 2009
Senior Member
It would be better to replace your first line with the following:

XmlResourceStore store = new XmlResourceStore(new FileResourceStore(new File("/path/to/xmlFile.xml")));

That will give you read/write support as opposed to just read support in your posted snippet. The other thing to be aware of when using XmlResourceStore(InputStream) constractor is that the the input stream isn't going to be closed by Sapphire. You will need to close it yourself (in a finally block). Don't need to worry about that if you use FileResourceStore.

Regarding which bundles to import, those two should be fine for many XML-bound models.

- Konstantin
Re: Marshall XML in Model [message #721277 is a reply to message #720637] Thu, 01 September 2011 10:17 Go to previous messageGo to next message
Nepomuk Seiler is currently offline Nepomuk Seiler
Messages: 88
Registered: December 2010
Member
Hi,

I get it back the other way round. Since I found no "New Wizard", like the Eclipse default, I tried to create my model.xml programmatic. With no success yet Sad

My first attempt
XmlResourceStore store = new XmlResourceStore(new File("/home/muki/sapphire.dpu.xml"));
RootXmlResource rootSource = new RootXmlResource(store);
rootSource.init(dpu);
rootSource.save();


My second one
dpu.resource().save();


No exceptions. The model isn't empty. The output file is empty. What am I missing?

thx,
Muki
Re: Marshall XML in Model [message #721363 is a reply to message #721277] Thu, 01 September 2011 15:09 Go to previous messageGo to next message
Konstantin Komissarchik is currently offline Konstantin Komissarchik
Messages: 940
Registered: July 2009
Senior Member
Your original attempt was more correct. You must instantiate your model around the resource you intend to use. You cannot hook it up after the fact.

Sapphire model reads/writes to the resource as you manipulate the model. For instance, XmlResource is implemented on a live XML DOM. As you manipulate the model, the XML DOM is manipulated. When you save, all that happens is that DOM is serialized. This is different from how JAXB operates, where serialization to XML happens on save.

XmlResourceStore store = new XmlResourceStore(new File("/path/to/xmlFile.xml"));
RootXmlResource xmlResource = new RootXmlResource(store);
IDataProcessingUnit dpu = IDataProcessingUnit.TYPE.instantiate(xmlResource);

// Manipulate the model.

dpu.resource.save()


Hope this clarifies.

- Konstantin

[Updated on: Thu, 01 September 2011 15:10]

Report message to a moderator

Re: Marshall XML in Model [message #721437 is a reply to message #721363] Thu, 01 September 2011 18:55 Go to previous messageGo to next message
Nepomuk Seiler is currently offline Nepomuk Seiler
Messages: 88
Registered: December 2010
Member
Hi,

Okay. My wizard is based on the Ezbug samples. I have a class (similar to IFileBugReport)

public interface IFileDataProcessingUnit extends IExecutableModelElement

    // *** Method: execute ***
    
    @DelegateImplementation( IDataProcessingUnitOp.class )
    
    Status execute( ProgressMonitor monitor );


IDataProcessingUnitOp.class is executed after finish the wizard:

public static final Status execute(final IFileDataProcessingUnit context, final ProgressMonitor monitor) {
		// Do something here.
		IDataProcessingUnit dpu = context.getDataProcessingUnit();
		try {
			XmlResourceStore store = new XmlResourceStore(new File("/home/muki/sapphire.dpu.xml"));
			RootXmlResource rootSource = new RootXmlResource(store);
			rootSource.init(dpu);
			rootSource.save();

		} catch (ResourceStoreException e) {
			e.printStackTrace();
		}

		return Status.createOkStatus();
	}


However this, doesn't work as you explained, because I need the XMLResourceStore before I create the model. Normally I would have add a new page, which generates the XMLResourceStore(WorkspaceResourceStore) and passes the value to the next wizard page. I read this thread http://www.eclipse.org/forums/index.php/t/238061/, which didn't really help me Sad

thx in advance,
Muki
Re: Marshall XML in Model [message #721450 is a reply to message #721437] Thu, 01 September 2011 19:31 Go to previous messageGo to next message
Konstantin Komissarchik is currently offline Konstantin Komissarchik
Messages: 940
Registered: July 2009
Senior Member
It sounds like you are trying to make a wizard that will create a new XML file after asking user for file name and other details.

Do this before calling SapphireWizard constructor:

IFileDataProcessingUnit dpu = IFileDataProcessingUnit.TYPE.instantiate( new RootXmlResource() );


The above will instantiate your model such that it's backed by XML, but it's not going to be linked to a file.

In execute method, you can then retrieve DOM like so:

( (RootXmlResource) context.resource() ).store().getDomDocument()


Once you have DOM, you can write it out to file, etc.

Another approach is to not back your in-wizard model instance by XML at all. Gather user input and then in the execute method, instantiate an XML/file backed model and copy the data to it.

- Konstantin
Re: Marshall XML in Model [message #721490 is a reply to message #721450] Thu, 01 September 2011 22:10 Go to previous messageGo to next message
Nepomuk Seiler is currently offline Nepomuk Seiler
Messages: 88
Registered: December 2010
Member
Hi,

Okay that works. Another idea came up to me. Would it be possible to instantiate your own "SapphireWizardPage" backed by a UI-Layout in your xyz.sdef?
So I can add the page in a custom wizard?

cheers,
Muki
Re: Marshall XML in Model [message #721511 is a reply to message #721490] Fri, 02 September 2011 00:22 Go to previous message
Konstantin Komissarchik is currently offline Konstantin Komissarchik
Messages: 940
Registered: July 2009
Senior Member
You can certainly do that. Just see how SapphireWizard class is implemented. You can also implement your own wizard page and then use SapphireControl inside WizardPage.createControl(). It all depends on how much work you want to do.

- Konstantin
Previous Topic:XMLAttribute binding
Next Topic:Code Generation
Goto Forum:
  


Current Time: Sun Sep 21 12:06:16 GMT 2014

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

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