Add escape methods.

This commit is contained in:
Christian P. MOMON 2013-09-09 00:57:35 +02:00
parent 6d7db8b7ef
commit 6ed1e3b20b

View file

@ -16,6 +16,8 @@ import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory; import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator; 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.InputSource;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
@ -28,6 +30,51 @@ import org.xml.sax.SAXException;
*/ */
public class XMLTools 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: case XMLEvent.ATTRIBUTE:
result = "ATTRIBUTE "; result = "ATTRIBUTE ";
break; break;
case XMLEvent.CDATA: case XMLEvent.CDATA:
result = "CDATA"; result = "CDATA";
break; break;
case XMLEvent.CHARACTERS: case XMLEvent.CHARACTERS:
result = "CHARACTERS [" + source.asCharacters().getData() + "]"; result = "CHARACTERS [" + source.asCharacters().getData() + "]";
break; break;
case XMLEvent.COMMENT: case XMLEvent.COMMENT:
result = "COMMENT"; result = "COMMENT";
break; break;
case XMLEvent.DTD: case XMLEvent.DTD:
result = "DTD"; result = "DTD";
break; break;
case XMLEvent.END_DOCUMENT: case XMLEvent.END_DOCUMENT:
result = "END_DOCUMENT"; result = "END_DOCUMENT";
break; break;
case XMLEvent.END_ELEMENT: case XMLEvent.END_ELEMENT:
result = "END_ELEMENT " + source.asEndElement().getName(); result = "END_ELEMENT " + source.asEndElement().getName();
break; break;
case XMLEvent.ENTITY_DECLARATION: case XMLEvent.ENTITY_DECLARATION:
result = "ENTITY_DECLARATION"; result = "ENTITY_DECLARATION";
break; break;
case XMLEvent.ENTITY_REFERENCE: case XMLEvent.ENTITY_REFERENCE:
result = "ENTITY_REFERENCE"; result = "ENTITY_REFERENCE";
break; break;
case XMLEvent.NAMESPACE: case XMLEvent.NAMESPACE:
result = "NAMESPACE"; result = "NAMESPACE";
break; break;
case XMLEvent.NOTATION_DECLARATION: case XMLEvent.NOTATION_DECLARATION:
result = "NOTATION_DECLARATION"; result = "NOTATION_DECLARATION";
break; break;
case XMLEvent.PROCESSING_INSTRUCTION: case XMLEvent.PROCESSING_INSTRUCTION:
result = "PROCESSING_INSTRUCTION"; result = "PROCESSING_INSTRUCTION";
break; break;
case XMLEvent.SPACE: case XMLEvent.SPACE:
result = "SPACE"; result = "SPACE";
break; break;
case XMLEvent.START_DOCUMENT: case XMLEvent.START_DOCUMENT:
result = "START_DOCUMENT"; result = "START_DOCUMENT";
break; break;
case XMLEvent.START_ELEMENT: case XMLEvent.START_ELEMENT:
result = "START_ELEMENT [name=" + source.asStartElement().getName() + "][namespaceURI=" + source.asStartElement().getName().getNamespaceURI() + "][prefix=" result = "START_ELEMENT [name=" + source.asStartElement().getName() + "][namespaceURI=" + source.asStartElement().getName().getNamespaceURI() + "][prefix="
+ source.asStartElement().getName().getPrefix() + "][localPart=" + source.asStartElement().getName().getLocalPart() + "]"; + source.asStartElement().getName().getPrefix() + "][localPart=" + source.asStartElement().getName().getLocalPart() + "]";
break; break;
default: default:
result = null; result = null;
} }