| Home » Modeling » TMF (Xtext) » Serialize a single element to a String
 Goto Forum:| 
| Serialize a single element to a String [message #56838] | Thu, 09 July 2009 13:50  |  | 
| Eclipse User  |  |  |  |  | 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 09:25   |  | 
| Eclipse User  |  |  |  |  | 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 10:24   |  | 
| Eclipse User  |  |  |  |  | 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 11:23   |  | 
| Eclipse User  |  |  |  |  | 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 11:43  |  | 
| Eclipse User  |  |  |  |  | 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.
 >
 >
 |  |  |  | 
 
 
 Current Time: Fri Oct 31 00:05:17 EDT 2025 
 Powered by FUDForum . Page generated in 0.26324 seconds |