Home » Modeling » EMF » Marshalling (XML) / Unresolved Proxy(Interlinked objects)
Marshalling (XML) / Unresolved Proxy [message #1072171] |
Mon, 22 July 2013 04:24  |
Eclipse User |
|
|
|
Hi guys,
I'm quite new with EMF and I'm dealing with an issue when marshalling|unmarshalling EObject.
NOTE : I replaced all '/' characters by '|' character due to limitation I have about posting messages (it's my first post here).
Model
My ecore is generated from a XSD schema (I simplified it) :
<complexType name="SystemConfiguration">
<sequence>
<element name="hardConfigurationList" minOccurs="1" maxOccurs="1" type="systconf:HardConfigList" |>
<element name="softConfigurationList" minOccurs="1" maxOccurs="1" type="systconf:SoftConfigList" |>
<|sequence>
<|complexType>
<complexType name="HardConfigList">
<sequence>
<element name="hardwares" minOccurs="0" maxOccurs="unbounded" type="systconf:HardConfiguration" |>
<|sequence>
<|complexType>
<complexType name="SoftConfigList">
<sequence>
<element name="softwares" minOccurs="0" maxOccurs="unbounded" type="systconf:SoftConfiguration" |>
<|sequence>
<|complexType>
<complexType name="HardConfiguration">
<sequence>
<element name="softConfList" minOccurs="0" maxOccurs="unbounded" type="anyURI" ecore:reference="systconf:SoftConfiguration" ecore:containment="false" |>
<|sequence>
<attribute name="name" type="string" use="required" |>
<|complexType>
<complexType name="SoftConfiguration">
<sequence>
<element name="hardConfList" minOccurs="0" maxOccurs="unbounded" type="anyURI" ecore:reference="systconf:HardConfiguration" ecore:containment="false" |>
<|sequence>
<attribute name="name" type="string" use="required" |>
<|complexType>
Conversion to XML File
To save my EObject as a XML file, I use the following code :
public void write(String fileName, EObject eObject) throws IOException {
|| creation of the document root
EObject obj = getDocumentRoot(eObject);
|| creation of the necessary resources for the transformation
Resource.Factory factory = new XMLResourceFactoryImpl();
Resource rs = factory.createResource(URI.createFileURI(fileName));
rs.getContents().add(obj);
|| save the resource
rs.save(saveOptions);
}
private EObject getDocumentRoot(EObject eObject) {
EObject obj = eObject;
|| retrieving ePackage of class
EPackage ePackage = (EPackage) eObject.eClass().eContainer();
|| getting eClass of docroot
EClass docRootClass = ExtendedMetaData.INSTANCE.getDocumentRoot(ePackage);
if (docRootClass != null) {
|| retrieve features of docroot
List<EStructuralFeature> features = ExtendedMetaData.INSTANCE.getAllElements(ExtendedMetaData.INSTANCE
.getDocumentRoot(ePackage));
|| looking for the feature of the element to persist
EStructuralFeature featureDocRoot = getFeatureDocRoot(features, eObject);
|| if feature not found, then the element should not be persisted...
if (featureDocRoot == null) {
|| throw exception;
}
|| adding the object to the document root
EClass eClass = ExtendedMetaData.INSTANCE.getDocumentRoot(ePackage);
obj = EcoreUtil.create(eClass);
obj.eSet(featureDocRoot, eObject);
}
return obj;
}
where my save options are :
OPTION_KEEP_DEFAULT_CONTENT = true
OPTION_EXTENDED_META_DATA = true
OPTION_SCHEMA_LOCATION = true
OPTION_USE_ENCODED_ATTRIBUTE_STYLE = true
OPTION_USE_LEXICAL_HANDLER = true
OPTION_DEFER_IDREF_RESOLUTION = true
OPTION_DEFER_ATTACHMENT = true
Here is the kind of XML file I get :
<?xml version="1.0" encoding="ASCII"?>
<systconf:SystConfigurationElement xmlns:systconf="http:||my|systconf|" >
<systconf:hardConfigurationList>
<systconf:hardwares name="name">
<systconf:softConfList>target|configuration.xml#||@systConfigurationElement|@softConfigurationList|@softwares.0<|systconf:softConfList>
<|systconf:hardwares>
<|systconf:hardConfigurationList>
<systconf:softConfigurationList>
<systconf:softwares name="name">
<systconf:hardConfList>target|configuration.xml#||@systConfigurationElement|@hardConfigurationList|@hardwares.0<|systconf:hardConfList>
<|systconf:softwares>
<|systconf:softConfigurationList>
<|systconf:SystConfigurationElement>
I perfectly understand there is a need to create such reference in XML file to prevent infinite loops.
But the problem is I can't get back my object from this XML file.
Convert XML to EObject
Here is my code to read XML file to convert it in EObject :
public EObject read(InputStream inputStream) throws IOException {
EObject resultatObj = getDocRootSrc(inputStream);
validateEObject(EcoreUtil.copy(resultatObj));
return resultatObj;
}
private EObject getDocRootSrc(InputStream stream) throws IOException {
EObject result = null;
Resource rs = null;
|| creation of the resources for the transformation
rs = new XMLResourceFactoryImpl().createResource(null);
((XMLResourceImpl) rs).load(new InputSource(stream), loadOptions);
if (rs.getContents() != null && rs.getContents().size() != 0) {
|| transformation
EObject docRootReturn = rs.getContents().get(0);
|| retrieve the object from the document root if any
result = getEObjectFromDocumentRoot(EcoreUtil.copy(docRootReturn));
}
return result;
}
private EObject getEObjectFromDocumentRoot(EObject docRoot) {
EObject result = docRoot;
|| check if docRoot
EPackage ePackage = (EPackage) docRoot.eClass().eContainer();
EClass eClass = ExtendedMetaData.INSTANCE.getDocumentRoot(ePackage);
if (docRoot.eClass().equals(eClass)) {
List<EStructuralFeature> features = ExtendedMetaData.INSTANCE.getAllElements(ExtendedMetaData.INSTANCE
.getDocumentRoot(ePackage));
for (EStructuralFeature eStructuralFeature : features) {
Object obj = docRoot.eGet(eStructuralFeature);
if (obj instanceof EObject) {
result = (EObject) obj;
docRoot.eUnset(eStructuralFeature);
break;
}
}
}
return result;
}
public void validateEObject(EObject eObject) throws ValidationFailureException {
Diagnostic diagnostic = Diagnostician.INSTANCE.validate(eObject);
if (diagnostic.getSeverity() != Diagnostic.OK) {
throw new exception;
}
}
Where my load options are equal to save options.
And I get the following error when I tried to get back my eObject from XML :
org.springframework.oxm.ValidationFailureException:
The feature 'softConfList' of '....HardConfiguration@19776d7{#|||@hardConfigurationList|@hardwares.0}' contains an unresolved proxy '....SoftConfiguration@656d13{target|configuration.xml#||@systConfigurationElement|@softConfigurationList|@softwares.0}'
The feature 'hardConfList' of '....SoftConfiguration@1ea8d74{#|||@softConfigurationList|@softwares.0}' contains an unresolved proxy '....HardConfiguration@1d0312b{target|configuration.xml#||@systConfigurationElement|@hardConfigurationList|@hardwares.0}'
...
I tried to figure out what is going on by debugging the process... but nothing helpful so far. Any help is greatly welcomed !
Thanks.
|
|
|
Re: Marshalling (XML) / Unresolved Proxy [message #1072265 is a reply to message #1072171] |
Mon, 22 July 2013 08:16   |
Eclipse User |
|
|
|
Pierre,
Comments below.
On 22/07/2013 2:02 PM, Pierre Villard wrote:
> Hi guys,
>
> I'm quite new with EMF and I'm dealing with an issue when
> marshalling|unmarshalling EObject.
>
> NOTE : I replaced all '/' characters by '|' character due to
> limitation I have about posting messages (it's my first post here).
>
> Model
>
> My ecore is generated from a XSD schema (I simplified it) :
>
>
> <complexType name="SystemConfiguration">
> <sequence>
> <element name="hardConfigurationList" minOccurs="1"
> maxOccurs="1" type="systconf:HardConfigList" |>
> <element name="softConfigurationList" minOccurs="1"
> maxOccurs="1" type="systconf:SoftConfigList" |>
> <|sequence>
> <|complexType>
>
> <complexType name="HardConfigList">
> <sequence>
> <element name="hardwares" minOccurs="0" maxOccurs="unbounded"
> type="systconf:HardConfiguration" |>
> <|sequence>
> <|complexType>
>
> <complexType name="SoftConfigList">
> <sequence>
> <element name="softwares" minOccurs="0" maxOccurs="unbounded"
> type="systconf:SoftConfiguration" |>
> <|sequence>
> <|complexType>
>
> <complexType name="HardConfiguration">
> <sequence>
> <element name="softConfList" minOccurs="0"
> maxOccurs="unbounded" type="anyURI"
> ecore:reference="systconf:SoftConfiguration" ecore:containment="false" |>
> <|sequence>
> <attribute name="name" type="string" use="required" |>
> <|complexType>
>
> <complexType name="SoftConfiguration">
> <sequence>
> <element name="hardConfList" minOccurs="0"
> maxOccurs="unbounded" type="anyURI"
> ecore:reference="systconf:HardConfiguration" ecore:containment="false" |>
> <|sequence>
> <attribute name="name" type="string" use="required" |>
> <|complexType>
>
>
>
> Conversion to XML File
>
> To save my EObject as a XML file, I use the following code :
>
>
> public void write(String fileName, EObject eObject) throws IOException {
> || creation of the document root
> EObject obj = getDocumentRoot(eObject);
> || creation of the necessary resources for the transformation
> Resource.Factory factory = new XMLResourceFactoryImpl();
If it's a schema based model, you should be using the generated resource
factory to create the resource (because it configures the necessary
options to serialize according to the originating schema).
> Resource rs = factory.createResource(URI.createFileURI(fileName));
> rs.getContents().add(obj);
>
> || save the resource
> rs.save(saveOptions);
> }
>
> private EObject getDocumentRoot(EObject eObject) {
> EObject obj = eObject;
> || retrieving ePackage of class
> EPackage ePackage = (EPackage) eObject.eClass().eContainer();
There's a getEPackage method.
> || getting eClass of docroot
> EClass docRootClass =
> ExtendedMetaData.INSTANCE.getDocumentRoot(ePackage);
> if (docRootClass != null) {
> || retrieve features of docroot
> List<EStructuralFeature> features =
> ExtendedMetaData.INSTANCE.getAllElements(ExtendedMetaData.INSTANCE
> .getDocumentRoot(ePackage));
>
> || looking for the feature of the element to persist
> EStructuralFeature featureDocRoot =
> getFeatureDocRoot(features, eObject);
>
> || if feature not found, then the element should not be
> persisted...
> if (featureDocRoot == null) {
> || throw exception;
> }
>
> || adding the object to the document root
> EClass eClass =
> ExtendedMetaData.INSTANCE.getDocumentRoot(ePackage);
> obj = EcoreUtil.create(eClass);
> obj.eSet(featureDocRoot, eObject);
> }
>
> return obj;
> }
>
>
> where my save options are :
> OPTION_KEEP_DEFAULT_CONTENT = true
> OPTION_EXTENDED_META_DATA = true
> OPTION_SCHEMA_LOCATION = true
> OPTION_USE_ENCODED_ATTRIBUTE_STYLE = true
> OPTION_USE_LEXICAL_HANDLER = true
> OPTION_DEFER_IDREF_RESOLUTION = true
> OPTION_DEFER_ATTACHMENT = true
>
> Here is the kind of XML file I get :
>
>
> <?xml version="1.0" encoding="ASCII"?>
> <systconf:SystConfigurationElement xmlns:systconf="http:||my|systconf|" >
> <systconf:hardConfigurationList>
> <systconf:hardwares name="name">
> <systconf:softConfList>target|configuration.xml#||@systConfigurationElement|@softConfigurationList|@softwares.0<|systconf:softConfList>
> <|systconf:hardwares>
> <|systconf:hardConfigurationList>
> <systconf:softConfigurationList>
> <systconf:softwares name="name">
> <systconf:hardConfList>target|configuration.xml#||@systConfigurationElement|@hardConfigurationList|@hardwares.0<|systconf:hardConfList>
> <|systconf:softwares>
> <|systconf:softConfigurationList>
> <|systconf:SystConfigurationElement>
>
>
> I perfectly understand there is a need to create such reference in XML
> file to prevent infinite loops.
> But the problem is I can't get back my object from this XML file.
>
>
> Convert XML to EObject
>
> Here is my code to read XML file to convert it in EObject :
>
>
> public EObject read(InputStream inputStream) throws IOException {
> EObject resultatObj = getDocRootSrc(inputStream);
> validateEObject(EcoreUtil.copy(resultatObj));
> return resultatObj;
> }
>
> private EObject getDocRootSrc(InputStream stream) throws IOException {
> EObject result = null;
> Resource rs = null;
>
> || creation of the resources for the transformation
> rs = new XMLResourceFactoryImpl().createResource(null);
> ((XMLResourceImpl) rs).load(new InputSource(stream), loadOptions);
>
> if (rs.getContents() != null && rs.getContents().size() != 0) {
> || transformation
> EObject docRootReturn = rs.getContents().get(0);
> || retrieve the object from the document root if any
> result =
> getEObjectFromDocumentRoot(EcoreUtil.copy(docRootReturn));
> }
>
> return result;
> }
>
> private EObject getEObjectFromDocumentRoot(EObject docRoot) {
> EObject result = docRoot;
> || check if docRoot
> EPackage ePackage = (EPackage) docRoot.eClass().eContainer();
> EClass eClass =
> ExtendedMetaData.INSTANCE.getDocumentRoot(ePackage);
> if (docRoot.eClass().equals(eClass)) {
> List<EStructuralFeature> features =
> ExtendedMetaData.INSTANCE.getAllElements(ExtendedMetaData.INSTANCE
> .getDocumentRoot(ePackage));
> for (EStructuralFeature eStructuralFeature : features) {
> Object obj = docRoot.eGet(eStructuralFeature);
> if (obj instanceof EObject) {
> result = (EObject) obj;
> docRoot.eUnset(eStructuralFeature);
> break;
> }
> }
> }
> return result;
> }
>
> public void validateEObject(EObject eObject) throws
> ValidationFailureException {
> Diagnostic diagnostic = Diagnostician.INSTANCE.validate(eObject);
> if (diagnostic.getSeverity() != Diagnostic.OK) {
> throw new exception;
> }
> }
>
>
> Where my load options are equal to save options.
>
> And I get the following error when I tried to get back my eObject from
> XML :
>
>
> org.springframework.oxm.ValidationFailureException: The feature
> 'softConfList' of
> '....HardConfiguration@19776d7{#|||@hardConfigurationList|@hardwares.0}'
> contains an unresolved proxy
> '....SoftConfiguration@656d13{target|configuration.xml#||@systConfigurationElement|@softConfigurationList|@softwares.0}'
> The feature 'hardConfList' of
> '....SoftConfiguration@1ea8d74{#|||@softConfigurationList|@softwares.0}'
> contains an unresolved proxy
> '....HardConfiguration@1d0312b{target|configuration.xml#||@systConfigurationElement|@hardConfigurationList|@hardwares.0}'
> ..
>
>
>
> I tried to figure out what is going on by debugging the process... but
> nothing helpful so far. Any help is greatly welcomed !
You can't resolve proxies without a resource set. I'd suggesting
invoking Generate Test Code and look at the generated XyzExample.java.
It shows exactly how you should load and save instances of your model.
You might also be interested in
org.eclipse.emf.ecore.xmi.XMLResource.OPTION_SUPPRESS_DOCUMENT_ROOT and
org.eclipse.emf.ecore.xmi.XMLResource.OPTION_ELEMENT_HANDLER for which
you can use org.eclipse.emf.ecore.xmi.impl.ElementHandlerImpl as a
mechanism to avoid needing to create a document root during load and to
avoid needing to create one during saving.
>
> Thanks.
|
|
| |
Re: Marshalling (XML) / Unresolved Proxy [message #1072303 is a reply to message #1072288] |
Mon, 22 July 2013 09:44   |
Eclipse User |
|
|
|
Pierre,
Comments below.
On 22/07/2013 3:11 PM, Pierre Villard wrote:
> Thanks Ed,
>
> I generated the test code, and I try to implement the same in a more
> global way.
>
> The thing is, I need to do something usable with different schema
> based models.
> In other words, when using my methods to marshall/unmarshall, I should
> be able to handle objects from a lot of possible ePackages.
>
> I'd like to do something like this :
>
>
> ResourceSet resourceSet = new ResourceSetImpl();
> resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(Resource.Factory.Registry.DEFAULT_EXTENSION,
> (1));
> EPackage ePackage = eObject.eClass().getEPackage();
> resourceSet.getPackageRegistry().put(ePackage.getNsURI(), (2))
>
> Resource resource =
> resourceSet.createResource(URI.createURI(ePackage.getNsURI()));
Why are you using the nsURI as a resource URI? Better to use something
like "dummy:/dummy.foo" if you don't have a real URI to which you will
save or from which you will load. Be especially careful about using
absolute URIs and look closely at the impact this has on the serialized
XML, in particular at the cross references...
>
>
> where :
> (1) whould be : new XyzResourceFactoryImpl()
If you look in that factory, the only important thing is the options,
and those options are the same for each generated XML Schema-based model.
> (2) would be : XyzPackage.eINSTANCE
In the end you really only need to access XyzPackage.eINSTANCE for each
package you plan to use. That will, as a side effect register it
globally and will make it available in all your resource sets.
>
> Is it possible ? How do I get (1) and (2) ?
It should be yes.
>
> Thanks for your help.
|
|
|
Re: Marshalling (XML) / Unresolved Proxy [message #1072373 is a reply to message #1072303] |
Mon, 22 July 2013 12:17   |
Eclipse User |
|
|
|
OK I think I get your point about resource set and how to use it. But I still get an error, and I think it comes from URI misuse.
Here is the generated test code :
ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
.put(Resource.Factory.Registry.DEFAULT_EXTENSION, new SystconfResourceFactoryImpl());
resourceSet.getPackageRegistry().put(SystconfPackage.eNS_URI, SystconfPackage.eINSTANCE);
if (args.length == 0) {
try {
Resource resource = resourceSet.createResource(URI.createURI("[url]http:///My.systconf[/url]"));
DocumentRoot documentRoot = SystconfFactory.eINSTANCE.createDocumentRoot();
SystemConfiguration root = createSystemConfiguration();
documentRoot.setSystConfigurationElement(root);
resource.getContents().add(documentRoot);
resource.save(System.out, null);
} catch (IOException exception) {
exception.printStackTrace();
}
} else {
for (int i = 0; i < args.length; ++i) {
File file = new File(args[i]);
URI uri = file.isFile() ? URI.createFileURI(file.getAbsolutePath()) : URI.createURI(args[i]);
try {
Resource resource = resourceSet.getResource(uri, true);
System.out.println("Loaded " + uri);
for (EObject eObject : resource.getContents()) {
Diagnostic diagnostic = Diagnostician.INSTANCE.validate(eObject);
if (diagnostic.getSeverity() != Diagnostic.OK) {
printDiagnostic(diagnostic, "");
}
}
} catch (RuntimeException exception) {
System.out.println("Problem loading " + uri);
exception.printStackTrace();
}
}
}
This piece of code works (hopefully) perfectly with my object.
So I'm trying to re-use this code :
public void write(String filePath, EObject eObject) throws IOException {
// validate the xsd
validateEObject(eObject);
ResourceSet resourceSet = new ResourceSetImpl();
ResourceFactoryImpl factoryImpl = new XMLResourceFactoryImpl();
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
.put(Resource.Factory.Registry.DEFAULT_EXTENSION, factoryImpl);
Resource resource = resourceSet.createResource(URI.createURI(filePath));
EObject obj = getDocumentRoot(eObject);
resource.getContents().add(obj);
resource.save(saveOptions);
}
saveOptions contains values as in XyZResourceFactoryImpl.
I assume this code works : it generates a XML file looking like I described it in first post.
Then :
public EObject read(String filePath) throws IOException {
ResourceSet resourceSet = new ResourceSetImpl();
ResourceFactoryImpl factoryImpl = new XMLResourceFactoryImpl();
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
.put(Resource.Factory.Registry.DEFAULT_EXTENSION, factoryImpl);
File file = new File(filePath);
Resource resource = resourceSet.getResource(URI.createFileURI(file.getAbsolutePath()), true);
EObject docRootReturn = resource.getContents().get(0);
EObject resultatObj = getEObjectFromDocumentRoot(EcoreUtil.copy(docRootReturn));
validateEObject(EcoreUtil.copy(resultatObj));
return resultatObj;
}
At the line :
Resource resource = resourceSet.getResource(URI.createFileURI(file.getAbsolutePath()), true);
I get the following exception :
org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1DiagnosticWrappedException: org.eclipse.emf.ecore.xmi.ClassNotFoundException: Class 'SystConfigurationElement' is not found or is abstract. (file:/D:/my/path/to/target/configuration.xml, 2, 226)
But I have defined the element in my XSD :
<element name="SystConfigurationElement" type="systconf:SystemConfiguration" />
I think I misunderstand the URI thing with resource set...
Thanks.
[Updated on: Mon, 22 July 2013 12:17] by Moderator
|
|
|
Re: Marshalling (XML) / Unresolved Proxy [message #1072398 is a reply to message #1072373] |
Mon, 22 July 2013 13:14   |
Eclipse User |
|
|
|
Pierre,
Comments below.
On 22/07/2013 6:17 PM, Pierre Villard wrote:
> OK I think I get your point about resource set and how to use it. But
> I still get an error, and I think it comes from URI misuse.
>
> Here is the generated test code :
>
> [code]
> ResourceSet resourceSet = new ResourceSetImpl();
>
> resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
> .put(Resource.Factory.Registry.DEFAULT_EXTENSION, new
> SystconfResourceFactoryImpl());
>
> resourceSet.getPackageRegistry().put(SystconfPackage.eNS_URI,
> SystconfPackage.eINSTANCE);
>
> if (args.length == 0) {
> try {
> Resource resource =
> resourceSet.createResource(URI.createURI("http:///My.systconf"));
> DocumentRoot documentRoot =
> SystconfFactory.eINSTANCE.createDocumentRoot();
> SystemConfiguration root = createSystemConfiguration();
> documentRoot.setSystConfigurationElement(root);
> resource.getContents().add(documentRoot);
> resource.save(System.out, null);
> } catch (IOException exception) {
> exception.printStackTrace();
> }
> } else {
> for (int i = 0; i < args.length; ++i) {
> File file = new File(args[i]);
> URI uri = file.isFile() ?
> URI.createFileURI(file.getAbsolutePath()) : URI.createURI(args[i]);
>
> try {
> Resource resource = resourceSet.getResource(uri,
> true);
> System.out.println("Loaded " + uri);
>
> for (EObject eObject : resource.getContents()) {
> Diagnostic diagnostic =
> Diagnostician.INSTANCE.validate(eObject);
> if (diagnostic.getSeverity() != Diagnostic.OK) {
> printDiagnostic(diagnostic, "");
> }
> }
> } catch (RuntimeException exception) {
> System.out.println("Problem loading " + uri);
> exception.printStackTrace();
> }
> }
> }
> |/code]
>
> This piece of code works (hopefully) perfectly with my object.
>
> So I'm trying to re-use this code :
>
>
> public void write(String filePath, EObject eObject) throws IOException {
> // validate the xsd
> validateEObject(eObject);
>
> ResourceSet resourceSet = new ResourceSetImpl();
> ResourceFactoryImpl factoryImpl = new XMLResourceFactoryImpl();
>
> resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
> .put(Resource.Factory.Registry.DEFAULT_EXTENSION,
> factoryImpl);
>
> Resource resource =
> resourceSet.createResource(URI.createURI(filePath));
Is the file path absolute? Or is it a file: URI?
>
> EObject obj = getDocumentRoot(eObject);
You didn't try the options for avoiding a document root...
> resource.getContents().add(obj);
>
> resource.save(saveOptions);
> }
>
>
> saveOptions contains values as in XyZResourceFactoryImpl.
> I assume this code works : it generates a XML file looking like I
> described it in first post.
And what do the cross references look like? If you do this properly I
expect nothing to appear before the # in the URI for the cross reference...
>
> Then :
>
>
> public EObject read(String filePath) throws IOException {
>
> ResourceSet resourceSet = new ResourceSetImpl();
> ResourceFactoryImpl factoryImpl = new XMLResourceFactoryImpl();
>
> resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
> .put(Resource.Factory.Registry.DEFAULT_EXTENSION,
> factoryImpl);
>
> File file = new File(filePath);
>
> Resource resource =
> resourceSet.getResource(URI.createFileURI(file.getAbsolutePath()), true);
This code looks like different using a file URI.
>
> EObject docRootReturn = resource.getContents().get(0);
>
> EObject resultatObj =
> getEObjectFromDocumentRoot(EcoreUtil.copy(docRootReturn));
>
> validateEObject(EcoreUtil.copy(resultatObj));
Why all the copying? docRootReturn.eContents().get(0) will return what
you need. I don't see the purpose of copying anything. Let along doing
it twice.
>
> return resultatObj;
> }
>
>
> At the line :
> Resource resource =
> resourceSet.getResource(URI.createFileURI(file.getAbsolutePath()), true);
>
> I get the following exception :
> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1DiagnosticWrappedException:
> org.eclipse.emf.ecore.xmi.ClassNotFoundException: Class
> 'SystConfigurationElement' is not found or is abstract.
> (file:/D:/my/path/to/target/configuration.xml, 2, 226)
Sounds like the right options aren't being use for load. I can only
recommend yet again that you use a factory much like the generated
factory and not just an XMLResourceFactoryImpl.
>
> But I have defined the element in my XSD :
> <element name="SystConfigurationElement"
> type="systconf:SystemConfiguration" />
>
>
> I think I misunderstand the URI thing with resource set...
In one case you create a file URI and in the other you don't. I have no
idea how you ensured the right options are being used for load and
save. It looks like you've done nothing in that regard...
>
> Thanks.
|
|
| |
Re: Marshalling (XML) / Unresolved Proxy [message #1072651 is a reply to message #1072642] |
Tue, 23 July 2013 04:01   |
Eclipse User |
|
|
|
Pierre,
Comments below.
On 23/07/2013 9:50 AM, Pierre Villard wrote:
> Hello,
>
> You were right, I didn't correctly use the options for loading/saving.
> I made a custom resource factory implementation to set options as in
> generated test code.
> Now it works, but, so far, I still use my functions to remove/add
> document root.
>
> So I'm trying what you talked about using
> XMLResource.OPTION_SUPPRESS_DOCUMENT_ROOT.
> It works for writing, but I get a class cast exception for reading.
That option is for reading...
> I guess it is because I'm not using XMLResource.OPTION_ELEMENT_HANDLER ?
Yes, use that option and try the
org.eclipse.emf.ecore.xmi.impl.ElementHandlerImpl.ElementHandlerImpl(boolean,
Collection<? extends EPackage>) constructor to create an instance for
use in that option. Or perhaps start with
org.eclipse.emf.ecore.xmi.impl.ElementHandlerImpl.ElementHandlerImpl(boolean)
passing in false, and see if that simplest approach works for your models.
>
> In your first post, you said :
>
> Quote:
>> org.eclipse.emf.ecore.xmi.XMLResource.OPTION_SUPPRESS_DOCUMENT_ROOT and
>> org.eclipse.emf.ecore.xmi.XMLResource.OPTION_ELEMENT_HANDLER for which
>> you can use org.eclipse.emf.ecore.xmi.impl.ElementHandlerImpl as a
>> mechanism to avoid needing to create a document root during load and to
>> avoid needing to create one during saving.
>
>
> I had a look on this class but I don't understand how it could help.
> Would you have an example ?
Create an instance of it as the value for the OPTION_ELEMENT_HANDLER
save option. It's doing much like the code you had before, i.e.,
determining an appropriate feature of the document root capable of hold
a value of the type of the root object actually in the resource so that
it uses an appropriate element name for the root element in the XML.
>
> Thanks.
|
|
| |
Goto Forum:
Current Time: Thu Jul 10 20:20:44 EDT 2025
Powered by FUDForum. Page generated in 0.05635 seconds
|