Home » Modeling » EMF » OPTION_ESCAPE_USING_CDATA
OPTION_ESCAPE_USING_CDATA [message #687381] |
Sat, 04 June 2011 03:44 |
John T.E. Timm 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 #688488 is a reply to message #688438] |
Fri, 24 June 2011 19:27 |
John T.E. Timm 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 |
Ed Merks Messages: 33140 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/
|
|
|
Goto Forum:
Current Time: Tue Apr 23 16:54:51 GMT 2024
Powered by FUDForum. Page generated in 0.03554 seconds
|