Skip to main content



      Home
Home » Modeling » EMF » Resource.save(); omits attributes with default values
Resource.save(); omits attributes with default values [message #392890] Mon, 16 May 2005 15:00 Go to next message
Eclipse UserFriend
I'm using Resource.save() to produce an XML representation of my object.

The trouble I have is that when I specify a Default Value Literal for an
attribute of my class in ecore, the value isn't coming out in my XML. I
can interrogate the instance before I .save() it, and tell that the value
is there.

If I change the value of the attribute on my object to a non-default
value, and save, then I see the attribute, with its non-default value in
the XML. If I change it back to the default value, I don't. (The attribute
does not appear at all.)

What's the explanation for this behavior? Is there any way to get the
attribute in every case, even if it's not the default?
Re: Resource.save(); omits attributes with default values [message #392892 is a reply to message #392890] Mon, 16 May 2005 15:23 Go to previous messageGo to next message
Eclipse UserFriend
Alan,

If a feature returns false for eIsSet, then the value won't be saved.
For "normal" features, this means that it won't serialize default
values. If you want it to serialize whenever it's set, even if it's set
to the default value, you can make the feature unsettable. This takes
more space (to record the extra state) and is not a great idea if you
can avoid it. Saving defaults is of little value...


Alan Canon wrote:

> I'm using Resource.save() to produce an XML representation of my object.
>
> The trouble I have is that when I specify a Default Value Literal for
> an attribute of my class in ecore, the value isn't coming out in my
> XML. I can interrogate the instance before I .save() it, and tell that
> the value is there.
>
> If I change the value of the attribute on my object to a non-default
> value, and save, then I see the attribute, with its non-default value
> in the XML. If I change it back to the default value, I don't. (The
> attribute does not appear at all.)
>
> What's the explanation for this behavior? Is there any way to get the
> attribute in every case, even if it's not the default?
>
>
Re: Resource.save(); omits attributes with default values [message #392899 is a reply to message #392892] Mon, 16 May 2005 16:31 Go to previous messageGo to next message
Eclipse UserFriend
Ed Merks wrote:

> can avoid it. Saving defaults is of little value...

Thanks, Ed. I was using "save" to XML-ify my object so that I could
transform it via XSLT into an HTML form to display for the user, like this:

public String getXml() {
DataGraph dataGraph = SDOFactory.eINSTANCE.createEDataGraph();
((EDataGraph) dataGraph).setERootObject(this);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try
{
((EDataGraph) dataGraph).getDataGraphResource().save(baos,
null);
}
catch(IOException e)
{
System.err.println("Could not get XML from " +
this.toString());
return null;
}
return baos.toString();
}

So I should probably choose another strategy.

-- Alan
Re: Resource.save(); omits attributes with default values [message #392901 is a reply to message #392899] Mon, 16 May 2005 16:50 Go to previous messageGo to next message
Eclipse UserFriend
Alan,

It just occurred to me that there's a save option that might do the
trick: XMLResource.OPTION_KEEP_DEFAULT_CONTENT.


Alan Canon wrote:

> Ed Merks wrote:
>
>> can avoid it. Saving defaults is of little value...
>
>
> Thanks, Ed. I was using "save" to XML-ify my object so that I could
> transform it via XSLT into an HTML form to display for the user, like
> this:
>
> public String getXml() {
> DataGraph dataGraph = SDOFactory.eINSTANCE.createEDataGraph();
> ((EDataGraph) dataGraph).setERootObject(this);
> ByteArrayOutputStream baos = new ByteArrayOutputStream();
> try
> {
> ((EDataGraph) dataGraph).getDataGraphResource().save(baos,
> null);
> }
> catch(IOException e)
> {
> System.err.println("Could not get XML from " +
> this.toString());
> return null;
> }
> return baos.toString();
> }
>
> So I should probably choose another strategy.
>
> -- Alan
>
Re: Resource.save(); omits attributes with default values [message #392921 is a reply to message #392901] Tue, 17 May 2005 13:40 Go to previous messageGo to next message
Eclipse UserFriend
Unfortunately, the XMLResource.OPTION_KEEP_DEFAULT_CONTENT option didn't
result in attributes with default values being saved, at least using the
following code:

public String getXml() {
DataGraph dataGraph = SDOFactory.eINSTANCE.createEDataGraph();
((EDataGraph) dataGraph).setERootObject(this);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Map options = new HashMap();
options.put(XMLResource.OPTION_KEEP_DEFAULT_CONTENT, Boolean.TRUE);
try
{
((EDataGraph) dataGraph).getDataGraphResource().save(baos,
options);
}
catch(IOException e)
{
System.err.println("Could not get XML from " +
this.toString());
return null;
}
return baos.toString();
}
Re: Resource.save(); omits attributes with default values [message #392922 is a reply to message #392921] Tue, 17 May 2005 13:49 Go to previous messageGo to next message
Eclipse UserFriend
Alan,

The only place we test eIsSet in XMLSaveImpl.saveFeatures:

if (kind != TRANSIENT && (o.eIsSet(f) || keepDefaults &&
f.getDefaultValueLiteral() != null))
{

Maybe the stuff you are saving is part of a feature map (sequence) and
in that case only explicit entries in the feature map will be saved. If
you send a sample I can run, I will look more closely...


Alan Canon wrote:

> Unfortunately, the XMLResource.OPTION_KEEP_DEFAULT_CONTENT option
> didn't result in attributes with default values being saved, at least
> using the following code:
>
> public String getXml() {
> DataGraph dataGraph = SDOFactory.eINSTANCE.createEDataGraph();
> ((EDataGraph) dataGraph).setERootObject(this);
> ByteArrayOutputStream baos = new ByteArrayOutputStream();
> Map options = new HashMap();
> options.put(XMLResource.OPTION_KEEP_DEFAULT_CONTENT,
> Boolean.TRUE);
> try
> {
> ((EDataGraph) dataGraph).getDataGraphResource().save(baos,
> options);
> }
> catch(IOException e)
> {
> System.err.println("Could not get XML from " +
> this.toString());
> return null;
> }
> return baos.toString();
> }
>
Re: Resource.save(); omits attributes with default values [message #392925 is a reply to message #392921] Tue, 17 May 2005 14:40 Go to previous message
Eclipse UserFriend
I spoke too soon. I was compiling against a newer version of EMF/SDO, but
deploying with an older version. I replaced the jars in my web application
with the May 5, 2005 build, and all is well. Thanks, Ed!
Previous Topic:How to save two related model elements into two resource files?
Next Topic:ItemPropertyDescriptor ID
Goto Forum:
  


Current Time: Fri Jul 04 16:33:10 EDT 2025

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

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

Back to the top