Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
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 Go to next message
Fred Clewis is currently offline 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 #387558 is a reply to message #387554] Wed, 08 September 2004 07:00 Go to previous messageGo to next message
Eike Stepper is currently offline Eike Stepper
Messages: 5252
Registered: July 2009
Senior Member
fred,

in general you should have a ResourceSet associated with your resources.

you could install a URIConverter in the ResourceSet that provides your
own implementations of InputStream and OutputStream.

in java.io there is already a StringBufferInputStream. for some strange reason
i can't find a StringBufferOutputStream. make your own and delegate to a
StringWriter.

cheers
/eike



On Tue, 07 Sep 2004 23:38:47 -0400, Fred Clewis <clewisf@us.ibm.com> wrote:

>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 #387559 is a reply to message #387558] Wed, 08 September 2004 09:18 Go to previous messageGo to next message
Fred Clewis is currently offline Fred Clewis
Messages: 31
Registered: July 2009
Member
Am I missing something here? I am surprized that this is such a rare
situation that I would have to extend the EMF classes. Do network
clients typically write incoming XML instances to a temp file to enable
model loading?

I am an EMF newbie, any example code for this would be greatly appreciated,

Fred Clewis
Re: Loading and XML String into a model when there is no file? [message #387560 is a reply to message #387559] Wed, 08 September 2004 10:02 Go to previous messageGo to next message
David Steinberg is currently offline David Steinberg
Messages: 489
Registered: July 2009
Senior Member
Fred Clewis wrote:
> Am I missing something here? I am surprized that this is such a rare
> situation that I would have to extend the EMF classes. Do network
> clients typically write incoming XML instances to a temp file to enable
> model loading?

Hi Fred,

No, I don't think you should need to extend any EMF classes. Resource
provides alternate forms of load() and save() which take an InputStream
and OutputStream, respectively. Instead of using the URIConverter to
obtain a stream, these methods read from and write to the explicitly
specified streams.

Network clients can simply specify, for example, an http-scheme URI for
the resource. The default URIConverter implementation will turn that
into a java.net.URL, and use that to open a connection and obtain an
input stream from it.

Cheers,
Dave
Re: Loading and XML String into a model when there is no file? [message #387561 is a reply to message #387560] Wed, 08 September 2004 10:22 Go to previous messageGo to next message
Fred Clewis is currently offline Fred Clewis
Messages: 31
Registered: July 2009
Member
thanks Dave and Eike,

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.

thanks,
Fred
Re: Loading and XML String into a model when there is no file? [message #387563 is a reply to message #387561] Wed, 08 September 2004 11:17 Go to previous messageGo to next message
Eike Stepper is currently offline Eike Stepper
Messages: 5252
Registered: July 2009
Senior Member
fred,

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.

cheers
/eike



On Wed, 08 Sep 2004 10:22:14 -0400, Fred Clewis <clewisf@us.ibm.com> wrote:

>
>thanks Dave and Eike,
>
>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.
>
>thanks,
>Fred
>
>
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 Go to previous messageGo to next message
David Steinberg is currently offline 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 Go to previous messageGo to next message
David Steinberg is currently offline 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 Go to previous messageGo to next message
Fred Clewis is currently offline 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 #387588 is a reply to message #387584] Thu, 09 September 2004 06:21 Go to previous messageGo to next message
Eclipse User
Originally posted by: inouem.jp.ibm.com

Hi Fred :-)

I think Dave's suggestion is ;

--- snip ---
String xmlString = "<?xml ......>";
ByteArrayInputStream in =
new ByteArrayInputStream(xmlString.getBytes());
Map options = ....;
resource.load(in, options);
--- snip ---

--
INOUE, Motoharu inouem@jp.ibm.com
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 Go to previous message
David Steinberg is currently offline 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
Previous Topic:property source provider
Next Topic:EMF generates code for enums within Eclipse, but not from command line
Goto Forum:
  


Current Time: Tue Oct 08 08:07:23 EDT 2013

Powered by FUDForum. Page generated in 0.02294 seconds