Description
Write the given string to the given writer, using either writeCharacters or, if there are more than a few less than signs in the string (e.g.
License
Open Source License
Parameter
Parameter | Description |
---|
xsw | the writer to write to |
string | the string to write |
Exception
Parameter | Description |
---|
XMLStreamException | an exception |
Declaration
static void writeCharactersOrCDATA(XMLStreamWriter xsw, String string) throws XMLStreamException
Method Source Code
//package com.java2s;
/*/*from w w w . j a va2s .c o m*/
* DocumentStaxUtils.java
*
* Copyright (c) 1995-2012, The University of Sheffield. See the file
* COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
*
* This file is part of GATE (see http://gate.ac.uk/), and is free
* software, licenced under the GNU Library General Public License,
* Version 2, June 1991 (in the distribution as file licence.html,
* and also available at http://gate.ac.uk/gate/licence.html).
*
* Ian Roberts, 20/Jul/2006
*
* $Id$
*/
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
public class Main {
/**
* The number of < signs after which we encode a string using CDATA
* rather than writeCharacters.
*/
public static final int LT_THRESHOLD = 5;
/**
* The regular expression pattern that will match the end of a CDATA
* section.
*/
private static Pattern CDATA_END_PATTERN = Pattern.compile("\\]\\]>");
/**
* Write the given string to the given writer, using either
* writeCharacters or, if there are more than a few less than signs in
* the string (e.g. if it is an XML fragment itself), write it with
* writeCData. This method properly handles the case where the string
* contains other CDATA sections - as a CDATA section cannot contain
* the CDATA end marker <code>]]></code>, we split the output CDATA
* at any occurrences of this marker and write the marker using a
* normal writeCharacters call in between.
*
* @param xsw the writer to write to
* @param string the string to write
* @throws XMLStreamException
*/
static void writeCharactersOrCDATA(XMLStreamWriter xsw, String string) throws XMLStreamException {
if (containsEnoughLTs(string)) {
Matcher m = CDATA_END_PATTERN.matcher(string);
int startFrom = 0;
while (m.find()) {
// we found a CDATA end marker, so write everything up to the
// marker as CDATA...
xsw.writeCData(string.substring(startFrom, m.start()));
// then write the marker as characters
xsw.writeCharacters("]]>");
startFrom = m.end();
}
if (startFrom == 0) {
// no "]]>" in the string, the normal case
xsw.writeCData(string);
} else if (startFrom < string.length()) {
// there is some trailing text after the last ]]>
xsw.writeCData(string.substring(startFrom));
}
// else the last ]]> was the end of the string, so nothing more to
// do.
} else {
// if fewer '<' characters, just writeCharacters as normal
xsw.writeCharacters(string);
}
}
/**
* Checks whether the given string contains at least
* <code>LT_THRESHOLD</code> < characters.
*/
private static boolean containsEnoughLTs(String string) {
int numLTs = 0;
int index = -1;
while ((index = string.indexOf('<', index + 1)) >= 0) {
numLTs++;
if (numLTs >= LT_THRESHOLD) {
return true;
}
}
return false;
}
}
Related
- isPrefixDeclared(XMLStreamWriter writer, String nsUri, String prefix)
- renderPath(XMLStreamWriter writer, String pathData, String id, String style)
- serializeEndpart(XMLStreamWriter writer)
- setValueType(XMLStreamWriter xml, String dataType)
- writeAttribute(XMLStreamWriter out, QName name, String value)
- writeColorConfig(XMLStreamWriter writer, Boolean enabled, String error, String warn, String success, String required, String batch)
- writeComment(String comment, XMLStreamWriter xmlsw)
- writeEndElement(XMLStreamWriter out, QName name)
- writeFloat(String name, float value, XMLStreamWriter writer)