Author: hoang_to
Date: 2010-11-03 06:48:58 -0400 (Wed, 03 Nov 2010)
New Revision: 4916
Modified:
portal/branches/branch-GTNPORTAL-1592/component/common/src/main/java/org/exoplatform/commons/xml/DOMSerializer.java
portal/branches/branch-GTNPORTAL-1592/component/common/src/test/java/org/exoplatform/commons/xml/TestDOMSerializer.java
Log:
GTNPORTAL-1626: Text content of XML Element, serialized by DOMSerializer, is always
wrapped in a CDATA section
Modified:
portal/branches/branch-GTNPORTAL-1592/component/common/src/main/java/org/exoplatform/commons/xml/DOMSerializer.java
===================================================================
---
portal/branches/branch-GTNPORTAL-1592/component/common/src/main/java/org/exoplatform/commons/xml/DOMSerializer.java 2010-11-03
09:06:49 UTC (rev 4915)
+++
portal/branches/branch-GTNPORTAL-1592/component/common/src/main/java/org/exoplatform/commons/xml/DOMSerializer.java 2010-11-03
10:48:58 UTC (rev 4916)
@@ -21,7 +21,9 @@
import org.gatein.common.logging.Logger;
import org.gatein.common.logging.LoggerFactory;
+import org.gatein.common.text.EntityEncoder;
import org.w3c.dom.Attr;
+import org.w3c.dom.CDATASection;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
@@ -145,10 +147,14 @@
for (int i = 0;i < length;i++)
{
Node child = children.item(i);
- if (child instanceof CharacterData)
+ if(child instanceof CDATASection)
{
- writer.writeCData(((CharacterData)child).getData());
+ writer.writeCData(((CDATASection)child).getData());
}
+ else if (child instanceof CharacterData)
+ {
+ writeTextData(writer, ((CharacterData)child).getData());
+ }
else if (child instanceof Element)
{
serialize((Element)child, writer);
@@ -159,4 +165,22 @@
writer.writeEndElement();
}
}
+
+ private static void writeTextData(XMLStreamWriter writer, String data) throws
XMLStreamException
+ {
+ for(int i = 0; i < data.length(); i++)
+ {
+ char c = data.charAt(i);
+ String encodedValue = EntityEncoder.FULL.lookup(c);
+
+ if(encodedValue == null)
+ {
+ writer.writeCharacters("" + c);
+ }
+ else
+ {
+ writer.writeCharacters(encodedValue);
+ }
+ }
+ }
}
Modified:
portal/branches/branch-GTNPORTAL-1592/component/common/src/test/java/org/exoplatform/commons/xml/TestDOMSerializer.java
===================================================================
---
portal/branches/branch-GTNPORTAL-1592/component/common/src/test/java/org/exoplatform/commons/xml/TestDOMSerializer.java 2010-11-03
09:06:49 UTC (rev 4915)
+++
portal/branches/branch-GTNPORTAL-1592/component/common/src/test/java/org/exoplatform/commons/xml/TestDOMSerializer.java 2010-11-03
10:48:58 UTC (rev 4916)
@@ -58,6 +58,16 @@
{
assertSerialization("<meta
http-equiv=\"Content-Type\"/>", "<meta
http-equiv='Content-Type'></meta>");
}
+
+ public void testOrdinaryTextElement() throws Exception
+ {
+ assertSerialization("<div>Blah Blah</div>",
"<div>Blah Blah</div>");
+ }
+
+ public void testCDATaElement() throws Exception
+ {
+ assertSerialization("<div><![CDATA[Test
Content]]></div>", "<div><![CDATA[Test
Content]]></div>");
+ }
private void assertSerialization(String expectedMarkup, String markup) throws
Exception
{
Show replies by date