Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF "Technology" (Ecore Tools, EMFatic, etc)  » using part of EMF model in Preferences
using part of EMF model in Preferences [message #1735726] Wed, 22 June 2016 09:22 Go to next message
Martin Jacob is currently offline Martin JacobFriend
Messages: 191
Registered: July 2009
Senior Member
Hi,

I have the task to make a part (one EObject with children) of an EMF
model editable in Eclipse preferences.

The idea is to save an XML snipped to the preference store and also load
the snipped into EMF. An existing Form shall be used to edit the model
in the preferences page.

I believe I could use
http://wiki.eclipse.org/EMF/FAQ#How_do_I_serialize_a_resource_to_a_String_instead_of_a_file.3F
to serialize the model to a string (XML snippet) and store this string
into the preferences.

The problems I have is how to read a XML snipped from a string?

Is it possible to create an EMF model but not having the root element?
e.g.:
complete XML
<A>
<B>
<C/>
<C/>
</B>
</A>

XML snipped
<B>
<C/>
<C/>
</B>

Does any one know a link covering this topic?

Thanks, Martin
Re: using part of EMF model in Preferences [message #1735738 is a reply to message #1735726] Wed, 22 June 2016 11:19 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
Martin,

Probably this option will do the trick:

org.eclipse.emf.ecore.xmi.XMLResource.OPTION_ROOT_OBJECTS

Using org.eclipse.emf.ecore.xmi.XMLResource.load(InputSource, Map<?, ?>)
you should be able to load from an InputSource that wraps a StringReader
that wraps your String representation.


On 22.06.2016 05:22, Martin Jacob wrote:
> Hi,
>
> I have the task to make a part (one EObject with children) of an EMF
> model editable in Eclipse preferences.
>
> The idea is to save an XML snipped to the preference store and also
> load the snipped into EMF. An existing Form shall be used to edit the
> model in the preferences page.
>
> I believe I could use
> http://wiki.eclipse.org/EMF/FAQ#How_do_I_serialize_a_resource_to_a_String_instead_of_a_file.3F
> to serialize the model to a string (XML snippet) and store this string
> into the preferences.
>
> The problems I have is how to read a XML snipped from a string?
>
> Is it possible to create an EMF model but not having the root element?
> e.g.:
> complete XML
> <A>
> <B>
> <C/>
> <C/>
> </B>
> </A>
>
> XML snipped
> <B>
> <C/>
> <C/>
> </B>
>
> Does any one know a link covering this topic?
>
> Thanks, Martin


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: using part of EMF model in Preferences [message #1735769 is a reply to message #1735738] Wed, 22 June 2016 14:41 Go to previous message
Martin Jacob is currently offline Martin JacobFriend
Messages: 191
Registered: July 2009
Senior Member
Thanks Ed,

Note:
- mVehicleSelection it's the root EObject of the later XML snipped


my solution reading:
		ComposedAdapterFactory adapterFactory = new 
ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE);

		BasicCommandStack commandStack = new BasicCommandStack();

		mEditingDomain = new AdapterFactoryEditingDomain(adapterFactory, 
commandStack, new HashMap<Resource, Boolean>());

		IPreferenceStore lPreferenceStore = 
Activator.getDefault().getPreferenceStore();
		XMLResource lXMLResource = new XMLResourceImpl();

		String lPref = 
lPreferenceStore.getString(PreferenceConstants.P_VEHICLE_CHART_TYPE_ENGINE_ALL);
		StringReader lStringReader = new StringReader(lPref);
		InputSource lInputSource = new InputSource(lStringReader);
		Map<Object, Object> lOptions = new HashMap<Object, Object>();
		try {
			lXMLResource.load(lInputSource, lOptions);
		} catch (IOException e) {
			e.printStackTrace();
		}
		mVehicleSelection = EcoreUtil.copy((VehicleSelection) 
lXMLResource.getContents().get(0));



Note:
- Its important to create a copy of the XMLResource.getContent().get(0)
as the content is read only.


solution writing:

		IPreferenceStore lPreferenceStore = 
Activator.getDefault().getPreferenceStore();

		XMLResource lXMLResource = new XMLResourceImpl();
		Map<Object, Object> lOptions = new HashMap<Object, Object>();
		List<EObject> lRoot = new ArrayList<EObject>();
		lRoot.add(mVehicleSelection);
		lOptions.put(XMLResource.OPTION_ROOT_OBJECTS, lRoot);

		StringWriter lStringWriter = new StringWriter();
		URIConverter.WriteableOutputStream lUws = new 
URIConverter.WriteableOutputStream(lStringWriter, "UTF-8");
		try {
			lXMLResource.save(lUws, lOptions);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String lPref = lStringWriter.toString();
	 
lPreferenceStore.setValue(PreferenceConstants.P_VEHICLE_CHART_TYPE_ENGINE_ALL, 
lPref);



Martin
Previous Topic:[Teneo] Problem setting a primary composite key that is also a foreign key
Next Topic: [EMF Forms] Viewmodel migration to 1.9.0 fails
Goto Forum:
  


Current Time: Thu Apr 18 09:20:09 GMT 2024

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

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

Back to the top