Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Serialize a single element to a String
Serialize a single element to a String [message #56838] Thu, 09 July 2009 17:50 Go to next message
drew frantz is currently offline drew frantzFriend
Messages: 340
Registered: July 2009
Senior Member
Would like to serialize a single element (an emf xtext element) from an
XtextResource into a string. I can do the entire XtextResource using
resource.save (...) , but just want to serialize one element so I can
display it as read-only text.
Re: Serialize a single element to a String [message #57259 is a reply to message #56838] Mon, 13 July 2009 07:03 Go to previous messageGo to next message
Sven Efftinge is currently offline Sven EfftingeFriend
Messages: 1823
Registered: July 2009
Senior Member
As long as the EObject comes from an XtextResource and wasn't mnodified
in-memory, you can use the attached node model (which is basically a
parse tree) to get the textual representation.

The nodes are stored in a NodeAdapter.
Use org.eclipse.xtext.parsetree.NodeUtil.getNodeAdapter(EObject) to
obtain it and on the adapter you can get the node and serialize it.

So the code is:

NodeAdapter nodeAdapter = NodeUtil.getNodeAdapter(model);
CompositeNode node = nodeAdapter.getParserNode();
String textRepresantation = node.serialize();

Regards,
Sven

drew schrieb:
> Would like to serialize a single element (an emf xtext element) from an
> XtextResource into a string. I can do the entire XtextResource using
> resource.save (...) , but just want to serialize one element so I can
> display it as read-only text.
>
>
Re: Serialize a single element to a String [message #57640 is a reply to message #57259] Mon, 13 July 2009 15:34 Go to previous messageGo to next message
drew frantz is currently offline drew frantzFriend
Messages: 340
Registered: July 2009
Senior Member
I need to be able to get the representation even if the resources was
modified in memory. How can I reparse the node model to get an updated
in-memory representation?


\"Sven Efftinge" <sven.efftinge@itemis.de> wrote in message
news:h3em8p$pml$1@build.eclipse.org...
> As long as the EObject comes from an XtextResource and wasn't mnodified
> in-memory, you can use the attached node model (which is basically a parse
> tree) to get the textual representation.
>
> The nodes are stored in a NodeAdapter.
> Use org.eclipse.xtext.parsetree.NodeUtil.getNodeAdapter(EObject) to obtain
> it and on the adapter you can get the node and serialize it.
>
> So the code is:
>
> NodeAdapter nodeAdapter = NodeUtil.getNodeAdapter(model);
> CompositeNode node = nodeAdapter.getParserNode();
> String textRepresantation = node.serialize();
>
> Regards,
> Sven
>
> drew schrieb:
>> Would like to serialize a single element (an emf xtext element) from an
>> XtextResource into a string. I can do the entire XtextResource using
>> resource.save (...) , but just want to serialize one element so I can
>> display it as read-only text.
Re: Serialize a single element to a String [message #57716 is a reply to message #57640] Mon, 13 July 2009 18:32 Go to previous messageGo to next message
Sven Efftinge is currently offline Sven EfftingeFriend
Messages: 1823
Registered: July 2009
Senior Member
You need to use an org.eclipse.xtext.parsetree.reconstr.SerializerUtil.
You can let Guice inject it into any component by just adding

@Inject
private SerializerUtil serializer;

If you want to get it from outside (i.e. your code is not Guice aware),
you can use the generated Setup class to get an injector and obtain the
serializer:

Injector injector = new
MyLanguageStandaloneSetup().createInjectorAndDoEMFRegistrati on();
SerializerUtil serializer = injector.getInstance(SerializerUtil.class);

Having the serializer, you can serialize your models like this:

String textualRepresentation = serializer.serialize(myObj);

Note, that SerializeUtil makes use of the formatter (IFormatter).

Cheers,
Sven

drew schrieb:
> I need to be able to get the representation even if the resources was
> modified in memory. How can I reparse the node model to get an updated
> in-memory representation?
>
>
> \"Sven Efftinge" <sven.efftinge@itemis.de> wrote in message
> news:h3em8p$pml$1@build.eclipse.org...
>> As long as the EObject comes from an XtextResource and wasn't mnodified
>> in-memory, you can use the attached node model (which is basically a parse
>> tree) to get the textual representation.
>>
>> The nodes are stored in a NodeAdapter.
>> Use org.eclipse.xtext.parsetree.NodeUtil.getNodeAdapter(EObject) to obtain
>> it and on the adapter you can get the node and serialize it.
>>
>> So the code is:
>>
>> NodeAdapter nodeAdapter = NodeUtil.getNodeAdapter(model);
>> CompositeNode node = nodeAdapter.getParserNode();
>> String textRepresantation = node.serialize();
>>
>> Regards,
>> Sven
>>
>> drew schrieb:
>>> Would like to serialize a single element (an emf xtext element) from an
>>> XtextResource into a string. I can do the entire XtextResource using
>>> resource.save (...) , but just want to serialize one element so I can
>>> display it as read-only text.
>
>
Re: Serialize a single element to a String [message #59198 is a reply to message #57716] Wed, 15 July 2009 13:25 Go to previous messageGo to next message
drew frantz is currently offline drew frantzFriend
Messages: 340
Registered: July 2009
Senior Member
Works great.

Now how about the inverse. I have an EObject and I want to pass in a String,
parse it and use the parsed result to replace the existing EObject.

void replaceExistingUsingStatement(EObject eo, String newStatement)
{
.....
}

"Sven Efftinge" <sven.efftinge@itemis.de> wrote in message
news:h3fukh$nrr$1@build.eclipse.org...
> You need to use an org.eclipse.xtext.parsetree.reconstr.SerializerUtil.
> You can let Guice inject it into any component by just adding
>
> @Inject
> private SerializerUtil serializer;
>
> If you want to get it from outside (i.e. your code is not Guice aware),
> you can use the generated Setup class to get an injector and obtain the
> serializer:
>
> Injector injector = new
> MyLanguageStandaloneSetup().createInjectorAndDoEMFRegistrati on();
> SerializerUtil serializer = injector.getInstance(SerializerUtil.class);
>
> Having the serializer, you can serialize your models like this:
>
> String textualRepresentation = serializer.serialize(myObj);
>
> Note, that SerializeUtil makes use of the formatter (IFormatter).
>
> Cheers,
> Sven
>
> drew schrieb:
>> I need to be able to get the representation even if the resources was
>> modified in memory. How can I reparse the node model to get an updated
>> in-memory representation?
>>
>>
>> \"Sven Efftinge" <sven.efftinge@itemis.de> wrote in message
>> news:h3em8p$pml$1@build.eclipse.org...
>>> As long as the EObject comes from an XtextResource and wasn't mnodified
>>> in-memory, you can use the attached node model (which is basically a
>>> parse tree) to get the textual representation.
>>>
>>> The nodes are stored in a NodeAdapter.
>>> Use org.eclipse.xtext.parsetree.NodeUtil.getNodeAdapter(EObject) to
>>> obtain it and on the adapter you can get the node and serialize it.
>>>
>>> So the code is:
>>>
>>> NodeAdapter nodeAdapter = NodeUtil.getNodeAdapter(model);
>>> CompositeNode node = nodeAdapter.getParserNode();
>>> String textRepresantation = node.serialize();
>>>
>>> Regards,
>>> Sven
>>>
>>> drew schrieb:
>>>> Would like to serialize a single element (an emf xtext element) from an
>>>> XtextResource into a string. I can do the entire XtextResource using
>>>> resource.save (...) , but just want to serialize one element so I can
>>>> display it as read-only text.
>>
Re: Serialize a single element to a String [message #59272 is a reply to message #59198] Wed, 15 July 2009 14:24 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi drew,

please have a look at the IParser or even IAntlrParser interface(s).
Note, that it is most likely not so easy to reparse an arbitrary EObject
this with IParser.reparse(CompositeNode, int, int, String) because the
parser may have made decisions based on look ahead information. In this
case, it will propably try to parse more text then you passed. I think
IAntlrParser.parse(..) is your friend.

Generally speaking it is save to modify an XtextDocument via
TextOperations and thereby delegate the complicated stuff to the
framework. Maybe you want to describe your use case in a few words?

Regards,
Sebastian

Am 15.07.2009 15:25 Uhr, schrieb drew:
> Works great.
>
> Now how about the inverse. I have an EObject and I want to pass in a String,
> parse it and use the parsed result to replace the existing EObject.
>
> void replaceExistingUsingStatement(EObject eo, String newStatement)
> {
> .....
> }
>
> "Sven Efftinge"<sven.efftinge@itemis.de> wrote in message
> news:h3fukh$nrr$1@build.eclipse.org...
>> You need to use an org.eclipse.xtext.parsetree.reconstr.SerializerUtil.
>> You can let Guice inject it into any component by just adding
>>
>> @Inject
>> private SerializerUtil serializer;
>>
>> If you want to get it from outside (i.e. your code is not Guice aware),
>> you can use the generated Setup class to get an injector and obtain the
>> serializer:
>>
>> Injector injector = new
>> MyLanguageStandaloneSetup().createInjectorAndDoEMFRegistrati on();
>> SerializerUtil serializer = injector.getInstance(SerializerUtil.class);
>>
>> Having the serializer, you can serialize your models like this:
>>
>> String textualRepresentation = serializer.serialize(myObj);
>>
>> Note, that SerializeUtil makes use of the formatter (IFormatter).
>>
>> Cheers,
>> Sven
>>
>> drew schrieb:
>>> I need to be able to get the representation even if the resources was
>>> modified in memory. How can I reparse the node model to get an updated
>>> in-memory representation?
>>>
>>>
>>> \"Sven Efftinge"<sven.efftinge@itemis.de> wrote in message
>>> news:h3em8p$pml$1@build.eclipse.org...
>>>> As long as the EObject comes from an XtextResource and wasn't mnodified
>>>> in-memory, you can use the attached node model (which is basically a
>>>> parse tree) to get the textual representation.
>>>>
>>>> The nodes are stored in a NodeAdapter.
>>>> Use org.eclipse.xtext.parsetree.NodeUtil.getNodeAdapter(EObject) to
>>>> obtain it and on the adapter you can get the node and serialize it.
>>>>
>>>> So the code is:
>>>>
>>>> NodeAdapter nodeAdapter = NodeUtil.getNodeAdapter(model);
>>>> CompositeNode node = nodeAdapter.getParserNode();
>>>> String textRepresantation = node.serialize();
>>>>
>>>> Regards,
>>>> Sven
>>>>
>>>> drew schrieb:
>>>>> Would like to serialize a single element (an emf xtext element) from an
>>>>> XtextResource into a string. I can do the entire XtextResource using
>>>>> resource.save (...) , but just want to serialize one element so I can
>>>>> display it as read-only text.
>
Re: Serialize a single element to a String [message #59297 is a reply to message #59272] Wed, 15 July 2009 15:23 Go to previous messageGo to next message
drew frantz is currently offline drew frantzFriend
Messages: 340
Registered: July 2009
Senior Member
I am building a graphical editor and a text editor, I would like to add a
conevenice feature to the graphical editor to replace an existing eObject
using text fragment based on the grammar (as opposed to setting a bunch of
the EObject properties using seprate calls).

The following code almost gets me there, just not sure how to replace an
existing eobject with once that was created from the parser.

Here is some code that almost does this, the find replace is what I am
struggling with.
public void saveParsed(EObject existingObj, String newStatement)
{
Injector injector =
Activator.getDefault().getInjector(Activator.PLUGIN_ID);

IParser ip = injector.getInstance(IAntlrParser.class);

byte[] bArray = newStatement.getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(bArray);
IParseResult ipr = ip.parse(bais);
EObject eRoot = ipr.getRootASTElement();
// This returns the root element for the model
// the actual item I need to replace is contained beneath
the root node
// lookup the new EObject called eFound
// remove the existingObj from its eContainer
// add eFound item back into same collection from
existingObj.eContainer
// is there a better way to perform this replacement?

}

"Sebastian Zarnekow" <Sebastian.Zarnekow@itemis.de> wrote in message
news:h3korg$k6v$1@build.eclipse.org...
> Hi drew,
>
> please have a look at the IParser or even IAntlrParser interface(s).
> Note, that it is most likely not so easy to reparse an arbitrary EObject
> this with IParser.reparse(CompositeNode, int, int, String) because the
> parser may have made decisions based on look ahead information. In this
> case, it will propably try to parse more text then you passed. I think
> IAntlrParser.parse(..) is your friend.
>
> Generally speaking it is save to modify an XtextDocument via
> TextOperations and thereby delegate the complicated stuff to the
> framework. Maybe you want to describe your use case in a few words?
>
> Regards,
> Sebastian
>
> Am 15.07.2009 15:25 Uhr, schrieb drew:
>> Works great.
>>
>> Now how about the inverse. I have an EObject and I want to pass in a
>> String,
>> parse it and use the parsed result to replace the existing EObject.
>>
>> void replaceExistingUsingStatement(EObject eo, String newStatement)
>> {
>> .....
>> }
>>
>> "Sven Efftinge"<sven.efftinge@itemis.de> wrote in message
>> news:h3fukh$nrr$1@build.eclipse.org...
>>> You need to use an org.eclipse.xtext.parsetree.reconstr.SerializerUtil.
>>> You can let Guice inject it into any component by just adding
>>>
>>> @Inject
>>> private SerializerUtil serializer;
>>>
>>> If you want to get it from outside (i.e. your code is not Guice aware),
>>> you can use the generated Setup class to get an injector and obtain the
>>> serializer:
>>>
>>> Injector injector = new
>>> MyLanguageStandaloneSetup().createInjectorAndDoEMFRegistrati on();
>>> SerializerUtil serializer = injector.getInstance(SerializerUtil.class);
>>>
>>> Having the serializer, you can serialize your models like this:
>>>
>>> String textualRepresentation = serializer.serialize(myObj);
>>>
>>> Note, that SerializeUtil makes use of the formatter (IFormatter).
>>>
>>> Cheers,
>>> Sven
>>>
>>> drew schrieb:
>>>> I need to be able to get the representation even if the resources was
>>>> modified in memory. How can I reparse the node model to get an updated
>>>> in-memory representation?
>>>>
>>>>
>>>> \"Sven Efftinge"<sven.efftinge@itemis.de> wrote in message
>>>> news:h3em8p$pml$1@build.eclipse.org...
>>>>> As long as the EObject comes from an XtextResource and wasn't
>>>>> mnodified
>>>>> in-memory, you can use the attached node model (which is basically a
>>>>> parse tree) to get the textual representation.
>>>>>
>>>>> The nodes are stored in a NodeAdapter.
>>>>> Use org.eclipse.xtext.parsetree.NodeUtil.getNodeAdapter(EObject) to
>>>>> obtain it and on the adapter you can get the node and serialize it.
>>>>>
>>>>> So the code is:
>>>>>
>>>>> NodeAdapter nodeAdapter = NodeUtil.getNodeAdapter(model);
>>>>> CompositeNode node = nodeAdapter.getParserNode();
>>>>> String textRepresantation = node.serialize();
>>>>>
>>>>> Regards,
>>>>> Sven
>>>>>
>>>>> drew schrieb:
>>>>>> Would like to serialize a single element (an emf xtext element) from
>>>>>> an
>>>>>> XtextResource into a string. I can do the entire XtextResource using
>>>>>> resource.save (...) , but just want to serialize one element so I can
>>>>>> display it as read-only text.
>>
>
Re: Serialize a single element to a String [message #59320 is a reply to message #59297] Wed, 15 July 2009 15:43 Go to previous message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi drew,

I'm afraid that there is no better way to do the replacement with your
approach.
Please be aware of the fact, that the method #parse(InputStream) expects
the content of the stream to be a valid root model.
Furthermore, please note that your parsed instance may contain syntax
errors.

Regards,
Sebastian

Am 15.07.2009 17:23 Uhr, schrieb drew:
> I am building a graphical editor and a text editor, I would like to add a
> conevenice feature to the graphical editor to replace an existing eObject
> using text fragment based on the grammar (as opposed to setting a bunch of
> the EObject properties using seprate calls).
>
> The following code almost gets me there, just not sure how to replace an
> existing eobject with once that was created from the parser.
>
> Here is some code that almost does this, the find replace is what I am
> struggling with.
> public void saveParsed(EObject existingObj, String newStatement)
> {
> Injector injector =
> Activator.getDefault().getInjector(Activator.PLUGIN_ID);
>
> IParser ip = injector.getInstance(IAntlrParser.class);
>
> byte[] bArray = newStatement.getBytes();
> ByteArrayInputStream bais = new ByteArrayInputStream(bArray);
> IParseResult ipr = ip.parse(bais);
> EObject eRoot = ipr.getRootASTElement();
> // This returns the root element for the model
> // the actual item I need to replace is contained beneath
> the root node
> // lookup the new EObject called eFound
> // remove the existingObj from its eContainer
> // add eFound item back into same collection from
> existingObj.eContainer
> // is there a better way to perform this replacement?
>
> }
>
> "Sebastian Zarnekow"<Sebastian.Zarnekow@itemis.de> wrote in message
> news:h3korg$k6v$1@build.eclipse.org...
>> Hi drew,
>>
>> please have a look at the IParser or even IAntlrParser interface(s).
>> Note, that it is most likely not so easy to reparse an arbitrary EObject
>> this with IParser.reparse(CompositeNode, int, int, String) because the
>> parser may have made decisions based on look ahead information. In this
>> case, it will propably try to parse more text then you passed. I think
>> IAntlrParser.parse(..) is your friend.
>>
>> Generally speaking it is save to modify an XtextDocument via
>> TextOperations and thereby delegate the complicated stuff to the
>> framework. Maybe you want to describe your use case in a few words?
>>
>> Regards,
>> Sebastian
>>
>> Am 15.07.2009 15:25 Uhr, schrieb drew:
>>> Works great.
>>>
>>> Now how about the inverse. I have an EObject and I want to pass in a
>>> String,
>>> parse it and use the parsed result to replace the existing EObject.
>>>
>>> void replaceExistingUsingStatement(EObject eo, String newStatement)
>>> {
>>> .....
>>> }
>>>
>>> "Sven Efftinge"<sven.efftinge@itemis.de> wrote in message
>>> news:h3fukh$nrr$1@build.eclipse.org...
>>>> You need to use an org.eclipse.xtext.parsetree.reconstr.SerializerUtil.
>>>> You can let Guice inject it into any component by just adding
>>>>
>>>> @Inject
>>>> private SerializerUtil serializer;
>>>>
>>>> If you want to get it from outside (i.e. your code is not Guice aware),
>>>> you can use the generated Setup class to get an injector and obtain the
>>>> serializer:
>>>>
>>>> Injector injector = new
>>>> MyLanguageStandaloneSetup().createInjectorAndDoEMFRegistrati on();
>>>> SerializerUtil serializer = injector.getInstance(SerializerUtil.class);
>>>>
>>>> Having the serializer, you can serialize your models like this:
>>>>
>>>> String textualRepresentation = serializer.serialize(myObj);
>>>>
>>>> Note, that SerializeUtil makes use of the formatter (IFormatter).
>>>>
>>>> Cheers,
>>>> Sven
>>>>
>>>> drew schrieb:
>>>>> I need to be able to get the representation even if the resources was
>>>>> modified in memory. How can I reparse the node model to get an updated
>>>>> in-memory representation?
>>>>>
>>>>>
>>>>> \"Sven Efftinge"<sven.efftinge@itemis.de> wrote in message
>>>>> news:h3em8p$pml$1@build.eclipse.org...
>>>>>> As long as the EObject comes from an XtextResource and wasn't
>>>>>> mnodified
>>>>>> in-memory, you can use the attached node model (which is basically a
>>>>>> parse tree) to get the textual representation.
>>>>>>
>>>>>> The nodes are stored in a NodeAdapter.
>>>>>> Use org.eclipse.xtext.parsetree.NodeUtil.getNodeAdapter(EObject) to
>>>>>> obtain it and on the adapter you can get the node and serialize it.
>>>>>>
>>>>>> So the code is:
>>>>>>
>>>>>> NodeAdapter nodeAdapter = NodeUtil.getNodeAdapter(model);
>>>>>> CompositeNode node = nodeAdapter.getParserNode();
>>>>>> String textRepresantation = node.serialize();
>>>>>>
>>>>>> Regards,
>>>>>> Sven
>>>>>>
>>>>>> drew schrieb:
>>>>>>> Would like to serialize a single element (an emf xtext element) from
>>>>>>> an
>>>>>>> XtextResource into a string. I can do the entire XtextResource using
>>>>>>> resource.save (...) , but just want to serialize one element so I can
>>>>>>> display it as read-only text.
>
>
Previous Topic:Invoking antlr - path to grammar files
Next Topic:[XText] Cross Reference - Custom
Goto Forum:
  


Current Time: Sat Nov 09 03:54:35 GMT 2024

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

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

Back to the top