using part of EMF model in Preferences [message #1735726] |
Wed, 22 June 2016 05:22  |
Eclipse User |
|
|
|
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 #1735769 is a reply to message #1735738] |
Wed, 22 June 2016 10:41  |
Eclipse User |
|
|
|
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
|
|
|
Powered by
FUDForum. Page generated in 0.04131 seconds