Home » Modeling » EMF » Loading and XML String into a model when there is no file?
| Loading and XML String into a model when there is no file? [message #387554] |
Tue, 07 September 2004 23:38  |
Fred Clewis Messages: 31 Registered: July 2009 |
Member |
|
|
I do not see how to load an XML String into a model created by the
associated xsd. How can you create a Resource without a URI or how can
I create a URI for a String in memory?
thanks for any ideas, I really did look to see if this newbie question
had been asked...
|
|
| | | | | |
| Re: Loading and XML String into a model when there is no file? [message #387564 is a reply to message #387561] |
Wed, 08 September 2004 11:27   |
David Steinberg Messages: 489 Registered: July 2009 |
Senior Member |
|
|
Fred Clewis wrote:
> Dave, unlike my previous question that you helped with, I now do not
> want to have EMF open a connection and obtain the message input. I have
> the message String already sitting around in memory. It had to be
> obtained with non-EMF means. So how would I get it into the model? Do
> you agree with the approach Eike had? Any specifics or examples would
> be great.
Hi Fred,
Whoops, I didn't even notice that you were the same person who had asked
about loading a remote resource before. If I had, I wouldn't have been
so explicit in restating that answer. ;)
What I'm suggesting is that you don't need to use a specialized
URIConverter. You can simply use the load(InputStream, Map) method on
Resource, and directly specify the input stream to read from.
On the other hand, you *will* need to use Eike's approach if you might
need the resource to be demand-loaded while resolving proxies, because
other resources may contain references to objects within it. This is
because the resource set performs demand loading simply by calling
Resource.load(Map).
If, however, you know that you'll never have cross-resource references
or if you can load the resource in advance, yourself, then you can
simply use the two argument form of load().
Cheers,
Dave
|
|
|
| Re: Loading and XML String into a model when there is no file? [message #387565 is a reply to message #387563] |
Wed, 08 September 2004 11:57   |
David Steinberg Messages: 489 Registered: July 2009 |
Senior Member |
|
|
Eike Stepper wrote:
> i think the version dave suggests will also lead to implementing
> your own Stream classes, only they are retrieved/used differently.
>
> making your own Stream classes should not be hard.
Hi Eike,
Yes, I wasn't saying anything about the Stream classes, themselves. I
was just pointing out that Fred might not need a specialized
URIConverter in order to use them.
As for the Stream classes, it seems Fred is most interested in reading,
not writing, the model. So, there is StringBufferInputStream, but as
the Javadoc suggests, it doesn't do character encoding properly. I
understand that he is retrieving the model externally from some remote
source. If he didn't decode it into a string, but just kept it as a
byte array, then he could just use a ByteArrayInputStream.
Cheers,
Dave
|
|
|
| Re: Loading and XML String into a model when there is no file? [message #387584 is a reply to message #387565] |
Wed, 08 September 2004 20:22   |
Fred Clewis Messages: 31 Registered: July 2009 |
Member |
|
|
Dave, Eike, thanks again,
I still don't get the solution too well and want to do what I should
have done in the begining and give you a specific problem:
1. you have used the Eclipse 3.0 and EMF 2.0 (with SDO and XSD) to
create the 3 projects corresponding to Model code, Edit Code, and Editor
Code from an XSD schema. All the methods below are exactly as they were
generated.
2. you want to use this pattern as a base for an application that will:
A. have a different custom MultiPageEditorPart
B. editor receives input of XML instances conforming to the schema from
a memory byte[] instead of a file
3. the challenge can be focused on two ares:
A. the MultiPageEditorPart .init method
B. the MultiPageEditorPart .createModel method
4. the .init method of the editor: (solved, I think)
public void init(IEditorSite site, IEditorInput editorInput)
will be called by code in another view rather than the workbench.
Since it it not file based , it will not use the IFileEditorInput
subclass of IEditorInput, but rather the IStorageEditorInput subclass.
This is read only, but that is OK, i think, because I do not need
conventional editor update interaction with this input, I just want to
snapshot load the EMF model which will then be hooked up properly for
update/dirty situations.
5. Now for the hard part, the .createModel method of the editor:
public void createModel() {
// I assume that the input is a file object.
//
IFileEditorInput modelFile = (IFileEditorInput)getEditorInput();
try {
// Load the resource through the editing domain.
//
Resource resource =
editingDomain.loadResource
(URI.createPlatformResourceURI(modelFile.getFile().getFullPa th().toString()).toString());
}
As you can see by the comment, the developer assumed file input. What I
have been pestering yall about all this time is how to change the code
above. My editorInput in my case is a IStorageEditorInput and I can do a:
getEditorInput().getStorage().getContents() and I can get an
InputStream of the message contents.
Can I alter the code above somehow to use an InputStream. Does the use
of the editingDomain (which is used elsewhere in the code) complicate
the surgery?
thanks, sorry for life story here...
Fred
|
|
| |
| Re: Loading and XML String into a model when there is no file? [message #387590 is a reply to message #387584] |
Thu, 09 September 2004 09:03  |
David Steinberg Messages: 489 Registered: July 2009 |
Senior Member |
|
|
Fred Clewis wrote:
> 5. Now for the hard part, the .createModel method of the editor:
>
> public void createModel() {
> // I assume that the input is a file object.
> //
> IFileEditorInput modelFile = (IFileEditorInput)getEditorInput();
>
> try {
> // Load the resource through the editing domain.
> //
> Resource resource =
> editingDomain.loadResource
>
> (URI.createPlatformResourceURI(modelFile.getFile().getFullPa th().toString()).toString());
>
> }
>
> As you can see by the comment, the developer assumed file input. What I
> have been pestering yall about all this time is how to change the code
> above. My editorInput in my case is a IStorageEditorInput and I can do a:
>
> getEditorInput().getStorage().getContents() and I can get an
> InputStream of the message contents.
>
> Can I alter the code above somehow to use an InputStream. Does the use
> of the editingDomain (which is used elsewhere in the code) complicate
> the surgery?
Hi Fred,
It shouldn't be too difficult. EditingDomain.loadResource() is just a
convenience. You can call getResourceSet() instead, and deal with the
resource set directly. So, you could replace this line:
Resource resource =
editingDomain.loadResource(URI.createPlatformResourceURI(mod elFile.getFile().getFullPath().toString()).toString());
(Sorry about the line wrapping.)
By this:
ResourceSet set = editingDomain.getResourceSet();
Resource resource = set.createResource(URI.createURI("foo:bar"));
resource.load(myInputStream, null);
The silly looking URI shouldn't matter if you don't have any
cross-document references. If you do, this approach won't work, and you
should use a more appropriate URI, and use the URIConverter to return
the input stream, as Eike suggested.
Cheers,
Dave
|
|
|
Goto Forum:
Current Time: Tue Oct 08 08:07:23 EDT 2013
Powered by FUDForum. Page generated in 0.02294 seconds
|