Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » OPTION_ESCAPE_USING_CDATA
OPTION_ESCAPE_USING_CDATA [message #687381] Sat, 04 June 2011 03:44 Go to next message
John T.E. Timm is currently offline John T.E. TimmFriend
Messages: 161
Registered: July 2009
Senior Member
I have an Ecore model with an EAttribute named "rule" that is typed to EString. I am using the default XMI serialization which makes it an XML attribute. I want to use this feature to store XQuery which may contain XML content and special characters.

I added an EAnnotation with source set to ExtendedMetaData and kind => 'element'. Then I set default load/save options to OPTION_USE_EXTENDED_META_DATA to true. Additionally, I set save options for OPTION_ESCAPE_USING_CDATA and OPTION_SKIP_ESCAPING to true. So everything appears to be set up correctly. My expectation is that the XQuery will remain "as-is" containing unescapted '<' and '>' characters, etc. However, I am still seeing these types of special characters getting escaped.

What criteria does the deserializer use to insert <![CDATA[ ]]>?
Is there anything that I can do to ensure that what gets serialized remains "as-is"?

Thanks,

JT

[Updated on: Fri, 24 June 2011 17:01]

Report message to a moderator

Re: OPTION_ESCAPE_USING_CDATA [message #687382 is a reply to message #687381] Sat, 04 June 2011 04:10 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33136
Registered: July 2009
Senior Member
John,

Is it coming out as an element? You can see the option being used in
convertText; it's called by getDatatypeValue for element content...


John T.E. Timm wrote:
> I have an Ecore model with an EAttribute named "rule" that is typed to
> EString. I am using the default XMI serialization which makes it an
> XML attribute. I want to use this feature to store XQuery which may
> contain XML content and special characters.
>
> I added an EAnnotation with source set to ExtendedMetaData and kind =>
> 'element'. Then I set default load/save options to
> OPTION_USE_EXTENDED_META_DATA to true. Additionally, I set save
> options for OPTION_ESCAPE_USING_CDATA and OPTION_SKIP_ESCAPING to
> true. So everything appears to be set up correctly. My expectation is
> that the XQuery will remain "as-is" containing unescapted '<' and '>'
> characters, etc. However, I am still seeing these types of special
> characters getting escaped.
>
> What criteria does the deserializer use to insert <![CDATA[ ]]>?
> Is there anything that I can do to ensure that what gets serialized
> remains "as-is"?
>
> Thanks,
>
> JT


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: OPTION_ESCAPE_USING_CDATA [message #688422 is a reply to message #687382] Fri, 24 June 2011 16:46 Go to previous messageGo to next message
John T.E. Timm is currently offline John T.E. TimmFriend
Messages: 161
Registered: July 2009
Senior Member
Ed,

It does appear to be coming out correctly (if deserliaze and invoke the getRule() method). The serialized version of the document, however, does not use CDATA, instead it excapes all the XML "special" characters (even iwth the options set as mentioned above). Any ideas?

Thanks,

JT
Re: OPTION_ESCAPE_USING_CDATA [message #688438 is a reply to message #688422] Fri, 24 June 2011 17:25 Go to previous messageGo to next message
John T.E. Timm is currently offline John T.E. TimmFriend
Messages: 161
Registered: July 2009
Senior Member
Ed,

I looked at XMLSaveImpl and did some more experiments. What I found was that there is a conflict between OPTION_ESCAPE_USING_CDATA and OPTION_IGNORE_ESCAPE. So I got rid of the second option and the XMLSaveImpl.Escape class does what I would expect and puts things in CDATA sections when they contain special chararcters. However, to complicate things, I found that this does not work when I serialize to DOM. I'm still investigating why there is no createCDATASection in there when toDOM is true.

Thanks,

JT
Re: OPTION_ESCAPE_USING_CDATA [message #688488 is a reply to message #688438] Fri, 24 June 2011 19:27 Go to previous messageGo to next message
John T.E. Timm is currently offline John T.E. TimmFriend
Messages: 161
Registered: July 2009
Senior Member
To get it to work when serializing to DOM, I actually had to override the default XMISaveImpl implementation as follows:

public class MyCustomXMISaveImpl extends XMISaveImpl {
	private Escape domEscape = null;
	
	public MyCustomXMISaveImpl(XMLHelper helper) {
		super(helper);
	}

	@Override
	protected void init(XMLResource resource, Map<?, ?> options) {
	    super.init(resource, options);
	    if (toDOM) {
	    	domEscape = new Escape();
	    	domEscape.setUseCDATA(true);
	    }
	}

	@Override
	protected void saveElement(EObject o, Object value, EStructuralFeature f) {
		if (value == null) {
			saveNil(o, f);
		} else {
			String svalue = getDatatypeValue(value, f, false);
			if (!toDOM) {
				doc.saveDataValueElement(helper.getQName(f), svalue);
			} else {
				helper.populateNameInfo(nameInfo, f);
				Element elem = document.createElementNS(nameInfo.getNamespaceURI(), nameInfo.getQualifiedName());
				// BEGIN: CDATA handling code
				Node text = null;
				String converted = domEscape.convertText(svalue);
				if (converted != null && converted.startsWith("<![CDATA[")) {
					text = document.createCDATASection(svalue);
				} else {
					text = document.createTextNode(svalue);
				}
				// END: CDATA handling code
//				Node text = document.createTextNode(svalue);
				elem.appendChild(text);
				currentNode.appendChild(elem);
				handler.recordValues(elem, o, f, value);
				handler.recordValues(text, o, f, value);
			}
		}
	}
}


Kind of a hack, but it works. Though I'm not sure that it works for *all* cases.

Thanks,

JT

[Updated on: Fri, 24 June 2011 19:57]

Report message to a moderator

Re: OPTION_ESCAPE_USING_CDATA [message #688513 is a reply to message #688488] Fri, 24 June 2011 20:38 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33136
Registered: July 2009
Senior Member
John,

I see. It would seem good for it to be supported for the DOM case too...


John T.E. Timm wrote:
> To get it to work when serializing to DOM, I actually had to override
> the default XMISaveImpl implementation as follows:
>
>
> public class MyCustomXMISaveImpl extends XMISaveImpl {
> private Escape domEscape = null;
>
> public MDMISaveImpl(XMLHelper helper) {
> super(helper);
> }
>
> @Override
> protected void init(XMLResource resource, Map<?, ?> options) {
> super.init(resource, options);
> if (toDOM) {
> domEscape = new Escape();
> domEscape.setUseCDATA(true);
> }
> }
>
> @Override
> protected void saveElement(EObject o, Object value,
> EStructuralFeature f) {
> if (value == null) {
> saveNil(o, f);
> } else {
> String svalue = getDatatypeValue(value, f, false);
> if (!toDOM) {
> doc.saveDataValueElement(helper.getQName(f), svalue);
> } else {
> helper.populateNameInfo(nameInfo, f);
> Element elem =
> document.createElementNS(nameInfo.getNamespaceURI(),
> nameInfo.getQualifiedName());
> // BEGIN: CDATA handling code
> Node text = null;
> String converted = domEscape.convertText(svalue);
> if (converted != null &&
> converted.startsWith("<![CDATA[")) {
> text = document.createCDATASection(svalue);
> } else {
> text = document.createTextNode(svalue);
> }
> // END: CDATA handling code
> // Node text = document.createTextNode(svalue);
> elem.appendChild(text);
> currentNode.appendChild(elem);
> handler.recordValues(elem, o, f, value);
> handler.recordValues(text, o, f, value);
> }
> }
> }
> }
>
>
> Kind of a hack, but it works. I'm also not sure if it works for *all*
> cases.
>
> Thanks,
>
> JT


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:[2.3M5] Problems validating Ecore models
Next Topic:(no subject)
Goto Forum:
  


Current Time: Fri Apr 19 01:34:06 GMT 2024

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

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

Back to the top