From 6ed1e3b20baee634ef1a2bee17fdd0d897b891be Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Mon, 9 Sep 2013 00:57:35 +0200 Subject: [PATCH] Add escape methods. --- src/fr/devinsy/util/xml/XMLTools.java | 77 +++++++++++++++++++++------ 1 file changed, 62 insertions(+), 15 deletions(-) diff --git a/src/fr/devinsy/util/xml/XMLTools.java b/src/fr/devinsy/util/xml/XMLTools.java index fffe435..5b791dc 100644 --- a/src/fr/devinsy/util/xml/XMLTools.java +++ b/src/fr/devinsy/util/xml/XMLTools.java @@ -16,6 +16,8 @@ import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import javax.xml.validation.Validator; +import org.apache.commons.lang3.StringEscapeUtils; +import org.apache.commons.lang3.StringUtils; import org.xml.sax.InputSource; import org.xml.sax.SAXException; @@ -28,6 +30,51 @@ import org.xml.sax.SAXException; */ public class XMLTools { + + /** + * + * @param source + * @return + */ + public String escapeXMLNull(final String source) + { + String result; + + if (StringUtils.isBlank(source)) + { + result = ""; + } + else + { + result = StringEscapeUtils.escapeXml(source); + } + + // + return result; + } + + /** + * + * @param source + * @return + */ + public String unescapeXMLNull(final String source) + { + String result; + + if (StringUtils.isBlank(source)) + { + result = null; + } + else + { + result = StringEscapeUtils.unescapeXml(source); + } + + // + return result; + } + /** * * @@ -230,50 +277,50 @@ public class XMLTools { case XMLEvent.ATTRIBUTE: result = "ATTRIBUTE "; - break; + break; case XMLEvent.CDATA: result = "CDATA"; - break; + break; case XMLEvent.CHARACTERS: result = "CHARACTERS [" + source.asCharacters().getData() + "]"; - break; + break; case XMLEvent.COMMENT: result = "COMMENT"; - break; + break; case XMLEvent.DTD: result = "DTD"; - break; + break; case XMLEvent.END_DOCUMENT: result = "END_DOCUMENT"; - break; + break; case XMLEvent.END_ELEMENT: result = "END_ELEMENT " + source.asEndElement().getName(); - break; + break; case XMLEvent.ENTITY_DECLARATION: result = "ENTITY_DECLARATION"; - break; + break; case XMLEvent.ENTITY_REFERENCE: result = "ENTITY_REFERENCE"; - break; + break; case XMLEvent.NAMESPACE: result = "NAMESPACE"; - break; + break; case XMLEvent.NOTATION_DECLARATION: result = "NOTATION_DECLARATION"; - break; + break; case XMLEvent.PROCESSING_INSTRUCTION: result = "PROCESSING_INSTRUCTION"; - break; + break; case XMLEvent.SPACE: result = "SPACE"; - break; + break; case XMLEvent.START_DOCUMENT: result = "START_DOCUMENT"; - break; + break; case XMLEvent.START_ELEMENT: result = "START_ELEMENT [name=" + source.asStartElement().getName() + "][namespaceURI=" + source.asStartElement().getName().getNamespaceURI() + "][prefix=" + source.asStartElement().getName().getPrefix() + "][localPart=" + source.asStartElement().getName().getLocalPart() + "]"; - break; + break; default: result = null; }