package gnu.xml.util;
import java.io.BufferedWriter;
import java.io.CharConversionException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Stack;
import org.xml.sax.*;
import org.xml.sax.ext.*;
import org.xml.sax.helpers.*;
public class XMLWriter
implements ContentHandler, LexicalHandler, DTDHandler, DeclHandler
{
private static final int CTX_ENTITY = 1;
private static final int CTX_ATTRIBUTE = 2;
private static final int CTX_CONTENT = 3;
private static final int CTX_UNPARSED = 4;
private static final int CTX_NAME = 5;
private static String sysEOL;
static {
try {
sysEOL = System.getProperty ("line.separator", "\n");
if (!isLineEnd (sysEOL))
sysEOL = "\n";
} catch (SecurityException e) {
sysEOL = "\n";
}
}
private static boolean isLineEnd (String eol)
{
return "\n".equals (eol)
|| "\r".equals (eol)
|| "\r\n".equals (eol);
}
private Writer out;
private boolean inCDATA;
private int elementNestLevel;
private String eol = sysEOL;
private short dangerMask;
private StringBuffer stringBuf;
private Locator locator;
private ErrorHandler errHandler;
private boolean expandingEntities = false;
private int entityNestLevel;
private boolean xhtml;
private boolean startedDoctype;
private String encoding;
private boolean canonical;
private boolean inDoctype;
private boolean inEpilogue;
private boolean prettyPrinting;
private int column;
private boolean noWrap;
private Stack space = new Stack ();
private static final int lineLength = 75;
public XMLWriter () throws IOException
{ this (System.out); }
public XMLWriter (OutputStream out) throws IOException
{
this (new OutputStreamWriter (out, "UTF8"));
}
public XMLWriter (Writer writer)
{
this (writer, null);
}
public XMLWriter (Writer writer, String encoding)
{
setWriter (writer, encoding);
}
private void setEncoding (String encoding)
{
if (encoding == null && out instanceof OutputStreamWriter)
encoding = ((OutputStreamWriter)out).getEncoding ();
if (encoding != null) {
encoding = encoding.toUpperCase ();
if ("UTF8".equals (encoding)) {
encoding = "UTF-8";
} else if ("US-ASCII".equals (encoding)
|| "ASCII".equals (encoding)) {
dangerMask = (short) 0xff80;
encoding = "US-ASCII";
} else if ("ISO-8859-1".equals (encoding)
|| "8859_1".equals (encoding)
|| "ISO8859_1".equals (encoding)) {
dangerMask = (short) 0xff00;
encoding = "ISO-8859-1";
} else if ("UNICODE".equals (encoding)
|| "UNICODE-BIG".equals (encoding)
|| "UNICODE-LITTLE".equals (encoding)) {
encoding = "UTF-16";
}
if (dangerMask != 0)
stringBuf = new StringBuffer ();
}
this.encoding = encoding;
}
final public void setWriter (Writer writer, String encoding)
{
if (out != null)
throw new IllegalStateException (
"can't change stream in mid course");
out = writer;
if (out != null)
setEncoding (encoding);
if (!(out instanceof BufferedWriter))
out = new BufferedWriter (out);
space.push ("default");
}
final public void setEOL (String eolString)
{
if (eolString == null)
eol = sysEOL;
else if (!isLineEnd (eolString))
eol = eolString;
else
throw new IllegalArgumentException (eolString);
}
public void setErrorHandler (ErrorHandler handler)
{
errHandler = handler;
}
protected void fatal (String message, Exception e)
throws SAXException
{
SAXParseException x;
if (locator == null)
x = new SAXParseException (message, null, null, -1, -1, e);
else
x = new SAXParseException (message, locator, e);
if (errHandler != null)
errHandler.fatalError (x);
throw x;
}
final public void setXhtml (boolean value)
{
if (locator != null)
throw new IllegalStateException ("started parsing");
xhtml = value;
if (xhtml)
canonical = false;
}
final public boolean isXhtml ()
{
return xhtml;
}
final public void setExpandingEntities (boolean value)
{
if (locator != null)
throw new IllegalStateException ("started parsing");
expandingEntities = value;
if (!expandingEntities)
canonical = false;
}
final public boolean isExpandingEntities ()
{
return expandingEntities;
}
final public void setPrettyPrinting (boolean value)
{
if (locator != null)
throw new IllegalStateException ("started parsing");
prettyPrinting = value;
if (prettyPrinting)
canonical = false;
}
final public boolean isPrettyPrinting ()
{
return prettyPrinting;
}
final public void setCanonical (boolean value)
{
if (value && !"UTF-8".equals (encoding))
throw new IllegalArgumentException ("encoding != UTF-8");
canonical = value;
if (canonical) {
prettyPrinting = xhtml = false;
expandingEntities = true;
eol = "\n";
}
}
final public boolean isCanonical ()
{
return canonical;
}
final public void flush ()
throws IOException
{
if (out != null)
out.flush ();
}
final public void write (String data)
throws SAXException
{
char buf [] = data.toCharArray ();
characters (buf, 0, buf.length);
}
public void writeElement (
String uri,
String localName,
String qName,
Attributes atts,
String content
) throws SAXException
{
if (content == null || content.length () == 0) {
writeEmptyElement (uri, localName, qName, atts);
return;
}
startElement (uri, localName, qName, atts);
char chars [] = content.toCharArray ();
characters (chars, 0, chars.length);
endElement (uri, localName, qName);
}
public void writeElement (
String uri,
String localName,
String qName,
Attributes atts,
int content
) throws SAXException
{
writeElement (uri, localName, qName, atts, Integer.toString (content));
}
final public void setDocumentLocator (Locator l)
{
locator = l;
}
private static final String xhtmlFullDTD =
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";
public void startDocument ()
throws SAXException
{
try {
if (out == null)
throw new IllegalStateException (
"null Writer given to XMLWriter");
if (locator == null)
locator = new LocatorImpl ();
if (!canonical
&& dangerMask != (short) 0xff80
&& encoding != null) {
rawWrite ("<?xml version='1.0'");
rawWrite (" encoding='" + encoding + "'");
rawWrite ("?>");
newline ();
}
if (xhtml) {
rawWrite ("<!DOCTYPE html PUBLIC");
newline ();
rawWrite (" '-//W3C//DTD XHTML 1.0 Transitional//EN'");
newline ();
rawWrite (" '");
rawWrite (xhtmlFullDTD);
rawWrite ("'>");
newline ();
newline ();
startedDoctype = true;
}
entityNestLevel = 0;
} catch (IOException e) {
fatal ("can't write", e);
}
}
public void endDocument ()
throws SAXException
{
try {
if (!canonical) {
newline ();
newline ();
}
out.close ();
out = null;
locator = null;
} catch (IOException e) {
fatal ("can't write", e);
}
}
final private static boolean isEmptyElementTag (String tag)
{
switch (tag.charAt (0)) {
case 'a': return "area".equals (tag);
case 'b': return "base".equals (tag)
|| "basefont".equals (tag)
|| "br".equals (tag);
case 'c': return "col".equals (tag);
case 'f': return "frame".equals (tag);
case 'h': return "hr".equals (tag);
case 'i': return "img".equals (tag)
|| "input".equals (tag)
|| "isindex".equals (tag);
case 'l': return "link".equals (tag);
case 'm': return "meta".equals (tag);
case 'p': return "param".equals (tag);
}
return false;
}
private static boolean indentBefore (String tag)
{
switch (tag.charAt (0)) {
case 'a': return "applet".equals (tag);
case 'b': return "body".equals (tag)
|| "blockquote".equals (tag);
case 'c': return "center".equals (tag);
case 'f': return "frame".equals (tag)
|| "frameset".equals (tag);
case 'h': return "head".equals (tag);
case 'm': return "meta".equals (tag);
case 'o': return "object".equals (tag);
case 'p': return "param".equals (tag)
|| "pre".equals (tag);
case 's': return "style".equals (tag);
case 't': return "title".equals (tag)
|| "td".equals (tag)
|| "th".equals (tag);
}
return false;
}
private static boolean spaceBefore (String tag)
{
switch (tag.charAt (0)) {
case 'h': return "h1".equals (tag)
|| "h2".equals (tag)
|| "h3".equals (tag)
|| "h4".equals (tag)
|| "h5".equals (tag)
|| "h6".equals (tag)
|| "hr".equals (tag);
case 'l': return "li".equals (tag);
case 'o': return "ol".equals (tag);
case 'p': return "p".equals (tag);
case 't': return "table".equals (tag)
|| "tr".equals (tag);
case 'u': return "ul".equals (tag);
}
return false;
}
private static boolean spacePreserve (String tag)
{
return "pre".equals (tag)
|| "style".equals (tag)
|| "script".equals (tag);
}
final public void startPrefixMapping (String prefix, String uri)
{}
final public void endPrefixMapping (String prefix)
{}
private void writeStartTag (
String name,
Attributes atts,
boolean isEmpty
) throws SAXException, IOException
{
rawWrite ('<');
rawWrite (name);
if (atts != null && atts.getLength () != 0) {
int indices [] = new int [atts.getLength ()];
for (int i= 0; i < indices.length; i++)
indices [i] = i;
if (canonical || prettyPrinting) {
for (int i = 1; i < indices.length; i++) {
int n = indices [i], j;
String s = atts.getQName (n);
for (j = i - 1; j >= 0; j--) {
if (s.compareTo (atts.getQName (indices [j]))
>= 0)
break;
indices [j + 1] = indices [j];
}
indices [j + 1] = n;
}
}
for (int i= 0; i < indices.length; i++) {
String s = atts.getQName (indices [i]);
if (s == null || "".equals (s))
throw new IllegalArgumentException ("no XML name");
rawWrite (" ");
rawWrite (s);
rawWrite ("=");
writeQuotedValue (atts.getValue (indices [i]),
CTX_ATTRIBUTE);
}
}
if (isEmpty)
rawWrite (" /");
rawWrite ('>');
}
final public void startElement (
String uri,
String localName,
String qName,
Attributes atts
) throws SAXException
{
startedDoctype = false;
if (locator == null)
locator = new LocatorImpl ();
if (qName == null || "".equals (qName))
throw new IllegalArgumentException ("no XML name");
try {
if (entityNestLevel != 0)
return;
if (prettyPrinting) {
String whitespace = null;
if (xhtml && spacePreserve (qName))
whitespace = "preserve";
else if (atts != null)
whitespace = atts.getValue ("xml:space");
if (whitespace == null)
whitespace = (String) space.peek ();
space.push (whitespace);
if ("default".equals (whitespace)) {
if (xhtml) {
if (spaceBefore (qName)) {
newline ();
doIndent ();
} else if (indentBefore (qName))
doIndent ();
} else
doIndent ();
}
}
elementNestLevel++;
writeStartTag (qName, atts, xhtml && isEmptyElementTag (qName));
if (xhtml) {
}
} catch (IOException e) {
fatal ("can't write", e);
}
}
public void writeEmptyElement (
String uri,
String localName,
String qName,
Attributes atts
) throws SAXException
{
if (canonical) {
startElement (uri, localName, qName, atts);
endElement (uri, localName, qName);
} else {
try {
writeStartTag (qName, atts, true);
} catch (IOException e) {
fatal ("can't write", e);
}
}
}
final public void endElement (String uri, String localName, String qName)
throws SAXException
{
if (qName == null || "".equals (qName))
throw new IllegalArgumentException ("no XML name");
try {
elementNestLevel--;
if (entityNestLevel != 0)
return;
if (xhtml && isEmptyElementTag (qName))
return;
rawWrite ("</");
rawWrite (qName);
rawWrite ('>');
if (prettyPrinting) {
if (!space.empty ())
space.pop ();
else
fatal ("stack discipline", null);
}
if (elementNestLevel == 0)
inEpilogue = true;
} catch (IOException e) {
fatal ("can't write", e);
}
}
final public void characters (char ch [], int start, int length)
throws SAXException
{
if (locator == null)
locator = new LocatorImpl ();
try {
if (entityNestLevel != 0)
return;
if (inCDATA) {
escapeChars (ch, start, length, CTX_UNPARSED);
} else {
escapeChars (ch, start, length, CTX_CONTENT);
}
} catch (IOException e) {
fatal ("can't write", e);
}
}
final public void ignorableWhitespace (char ch [], int start, int length)
throws SAXException
{
if (locator == null)
locator = new LocatorImpl ();
try {
if (entityNestLevel != 0)
return;
escapeChars (ch, start, length, CTX_CONTENT);
} catch (IOException e) {
fatal ("can't write", e);
}
}
final public void processingInstruction (String target, String data)
throws SAXException
{
if (locator == null)
locator = new LocatorImpl ();
if (xhtml && startedDoctype)
return;
try {
if (entityNestLevel != 0)
return;
if (canonical && inEpilogue)
newline ();
rawWrite ("<?");
rawWrite (target);
rawWrite (' ');
escapeChars (data.toCharArray (), -1, -1, CTX_UNPARSED);
rawWrite ("?>");
if (elementNestLevel == 0 && !(canonical && inEpilogue))
newline ();
} catch (IOException e) {
fatal ("can't write", e);
}
}
public void skippedEntity (String name)
throws SAXException
{
try {
rawWrite ("&");
rawWrite (name);
rawWrite (";");
} catch (IOException e) {
fatal ("can't write", e);
}
}
final public void startCDATA ()
throws SAXException
{
if (locator == null)
locator = new LocatorImpl ();
if (canonical)
return;
try {
inCDATA = true;
if (entityNestLevel == 0)
rawWrite ("<![CDATA[");
} catch (IOException e) {
fatal ("can't write", e);
}
}
final public void endCDATA ()
throws SAXException
{
if (canonical)
return;
try {
inCDATA = false;
if (entityNestLevel == 0)
rawWrite ("]]>");
} catch (IOException e) {
fatal ("can't write", e);
}
}
final public void startDTD (String name, String publicId, String systemId)
throws SAXException
{
if (locator == null)
locator = new LocatorImpl ();
if (xhtml)
return;
try {
inDoctype = startedDoctype = true;
if (canonical)
return;
rawWrite ("<!DOCTYPE ");
rawWrite (name);
rawWrite (' ');
if (!expandingEntities) {
if (publicId != null)
rawWrite ("PUBLIC '" + publicId + "' '" + systemId + "' ");
else if (systemId != null)
rawWrite ("SYSTEM '" + systemId + "' ");
}
rawWrite ('[');
newline ();
} catch (IOException e) {
fatal ("can't write", e);
}
}
final public void endDTD ()
throws SAXException
{
inDoctype = false;
if (canonical || xhtml)
return;
try {
rawWrite ("]>");
newline ();
} catch (IOException e) {
fatal ("can't write", e);
}
}
final public void startEntity (String name)
throws SAXException
{
try {
boolean writeEOL = true;
if (xhtml || expandingEntities)
return;
entityNestLevel++;
if (name.equals ("[dtd]"))
return;
if (entityNestLevel != 1)
return;
if (!name.startsWith ("%")) {
writeEOL = false;
rawWrite ('&');
}
rawWrite (name);
rawWrite (';');
if (writeEOL)
newline ();
} catch (IOException e) {
fatal ("can't write", e);
}
}
final public void endEntity (String name)
throws SAXException
{
if (xhtml || expandingEntities)
return;
entityNestLevel--;
}
final public void comment (char ch [], int start, int length)
throws SAXException
{
if (locator == null)
locator = new LocatorImpl ();
if (xhtml && startedDoctype)
return;
if (canonical && inDoctype)
return;
try {
boolean indent;
if (prettyPrinting && space.empty ())
fatal ("stack discipline", null);
indent = prettyPrinting && "default".equals (space.peek ());
if (entityNestLevel != 0)
return;
if (indent)
doIndent ();
if (canonical && inEpilogue)
newline ();
rawWrite ("<!--");
escapeChars (ch, start, length, CTX_UNPARSED);
rawWrite ("-->");
if (indent)
doIndent ();
if (elementNestLevel == 0 && !(canonical && inEpilogue))
newline ();
} catch (IOException e) {
fatal ("can't write", e);
}
}
final public void notationDecl (String name,
String publicId, String systemId)
throws SAXException
{
if (xhtml)
return;
try {
if (!startedDoctype)
return;
if (entityNestLevel != 0)
return;
rawWrite ("<!NOTATION " + name + " ");
if (publicId != null)
rawWrite ("PUBLIC \"" + publicId + '"');
else
rawWrite ("SYSTEM ");
if (systemId != null)
rawWrite ('"' + systemId + '"');
rawWrite (">");
newline ();
} catch (IOException e) {
fatal ("can't write", e);
}
}
final public void unparsedEntityDecl (String name,
String publicId, String systemId,
String notationName)
throws SAXException
{
if (xhtml)
return;
try {
if (!startedDoctype) {
return;
}
if (entityNestLevel != 0)
return;
rawWrite ("<!ENTITY " + name + " ");
if (publicId != null)
rawWrite ("PUBLIC \"" + publicId + '"');
else
rawWrite ("SYSTEM ");
rawWrite ('"' + systemId + '"');
rawWrite (" NDATA " + notationName + ">");
newline ();
} catch (IOException e) {
fatal ("can't write", e);
}
}
final public void attributeDecl (String eName, String aName,
String type, String mode, String value)
throws SAXException
{
if (xhtml)
return;
try {
if (!startedDoctype)
return;
if (entityNestLevel != 0)
return;
rawWrite ("<!ATTLIST " + eName + ' ' + aName + ' ');
rawWrite (type);
rawWrite (' ');
if (mode != null)
rawWrite (mode + ' ');
if (value != null)
writeQuotedValue (value, CTX_ATTRIBUTE);
rawWrite ('>');
newline ();
} catch (IOException e) {
fatal ("can't write", e);
}
}
final public void elementDecl (String name, String model)
throws SAXException
{
if (xhtml)
return;
try {
if (!startedDoctype)
return;
if (entityNestLevel != 0)
return;
rawWrite ("<!ELEMENT " + name + ' ' + model + '>');
newline ();
} catch (IOException e) {
fatal ("can't write", e);
}
}
final public void externalEntityDecl (
String name,
String publicId,
String systemId)
throws SAXException
{
if (xhtml)
return;
try {
if (!startedDoctype)
return;
if (entityNestLevel != 0)
return;
rawWrite ("<!ENTITY ");
if (name.startsWith ("%")) {
rawWrite ("% ");
rawWrite (name.substring (1));
} else
rawWrite (name);
if (publicId != null)
rawWrite (" PUBLIC \"" + publicId + '"');
else
rawWrite (" SYSTEM ");
rawWrite ('"' + systemId + "\">");
newline ();
} catch (IOException e) {
fatal ("can't write", e);
}
}
final public void internalEntityDecl (String name, String value)
throws SAXException
{
if (xhtml)
return;
try {
if (!startedDoctype)
return;
if (entityNestLevel != 0)
return;
rawWrite ("<!ENTITY ");
if (name.startsWith ("%")) {
rawWrite ("% ");
rawWrite (name.substring (1));
} else
rawWrite (name);
rawWrite (' ');
writeQuotedValue (value, CTX_ENTITY);
rawWrite ('>');
newline ();
} catch (IOException e) {
fatal ("can't write", e);
}
}
private void writeQuotedValue (String value, int code)
throws SAXException, IOException
{
char buf [] = value.toCharArray ();
int off = 0, len = buf.length;
noWrap = true;
rawWrite ('"');
escapeChars (buf, off, len, code);
rawWrite ('"');
noWrap = false;
}
private static final String HTMLlat1x [] = {
"nbsp", "iexcl", "cent", "pound", "curren",
"yen", "brvbar", "sect", "uml", "copy",
"ordf", "laquo", "not", "shy", "reg",
"macr", "deg", "plusmn", "sup2", "sup3",
"acute", "micro", "para", "middot", "cedil",
"sup1", "ordm", "raquo", "frac14", "frac12",
"frac34", "iquest", "Agrave", "Aacute", "Acirc",
"Atilde", "Auml", "Aring", "AElig", "Ccedil",
"Egrave", "Eacute", "Ecirc", "Euml", "Igrave",
"Iacute", "Icirc", "Iuml", "ETH", "Ntilde",
"Ograve", "Oacute", "Ocirc", "Otilde", "Ouml",
"times", "Oslash", "Ugrave", "Uacute", "Ucirc",
"Uuml", "Yacute", "THORN", "szlig", "agrave",
"aacute", "acirc", "atilde", "auml", "aring",
"aelig", "ccedil", "egrave", "eacute", "ecirc",
"euml", "igrave", "iacute", "icirc", "iuml",
"eth", "ntilde", "ograve", "oacute", "ocirc",
"otilde", "ouml", "divide", "oslash", "ugrave",
"uacute", "ucirc", "uuml", "yacute", "thorn",
"yuml"
};
private static final String HTMLsymbolx_GR [] = {
"Alpha", "Beta", "Gamma", "Delta", "Epsilon",
"Zeta", "Eta", "Theta", "Iota", "Kappa",
"Lambda", "Mu", "Nu", "Xi", "Omicron",
"Pi", "Rho", null, "Sigma", "Tau",
"Upsilon", "Phi", "Chi", "Psi", "Omega"
};
private static final String HTMLsymbolx_gr [] = {
"alpha", "beta", "gamma", "delta", "epsilon",
"zeta", "eta", "theta", "iota", "kappa",
"lambda", "mu", "nu", "xi", "omicron",
"pi", "rho", "sigmaf", "sigma", "tau",
"upsilon", "phi", "chi", "psi", "omega"
};
private void escapeChars (char buf [], int off, int len, int code)
throws SAXException, IOException
{
int first = 0;
if (off < 0) {
off = 0;
len = buf.length;
}
for (int i = 0; i < len; i++) {
String esc;
char c = buf [off + i];
switch (c) {
case '&':
if (code == CTX_ENTITY || code == CTX_UNPARSED)
continue;
esc = "amp";
break;
case '<':
if (code == CTX_ENTITY || code == CTX_UNPARSED)
continue;
esc = "lt";
break;
case '>':
if (code == CTX_ENTITY || code == CTX_UNPARSED)
continue;
esc = "gt";
break;
case '\'':
if (code == CTX_CONTENT || code == CTX_UNPARSED)
continue;
if (canonical)
continue;
esc = "apos";
break;
case '"':
if (code == CTX_CONTENT || code == CTX_UNPARSED)
continue;
esc = "quot";
break;
case '\n':
esc = eol;
break;
default:
if ((c > 0xfffd)
|| ((c < 0x0020) && !((c == 0x0009)
|| (c == 0x000A) || (c == 0x000D)))
|| (((c & dangerMask) != 0)
&& (code == CTX_UNPARSED))) {
throw new CharConversionException (
"Illegal or non-writable character: U+"
+ Integer.toHexString (c));
}
if ((c & dangerMask) == 0)
continue;
esc = null;
if (xhtml) {
if (c >= 160 && c <= 255)
esc = HTMLlat1x [c - 160];
else if (c >= 913 && c <= 937)
esc = HTMLsymbolx_GR [c - 913];
else if (c >= 945 && c <= 969)
esc = HTMLsymbolx_gr [c - 945];
else switch (c) {
case 338: esc = "OElig"; break;
case 339: esc = "oelig"; break;
case 352: esc = "Scaron"; break;
case 353: esc = "scaron"; break;
case 376: esc = "Yuml"; break;
case 710: esc = "circ"; break;
case 732: esc = "tilde"; break;
case 8194: esc = "ensp"; break;
case 8195: esc = "emsp"; break;
case 8201: esc = "thinsp"; break;
case 8204: esc = "zwnj"; break;
case 8205: esc = "zwj"; break;
case 8206: esc = "lrm"; break;
case 8207: esc = "rlm"; break;
case 8211: esc = "ndash"; break;
case 8212: esc = "mdash"; break;
case 8216: esc = "lsquo"; break;
case 8217: esc = "rsquo"; break;
case 8218: esc = "sbquo"; break;
case 8220: esc = "ldquo"; break;
case 8221: esc = "rdquo"; break;
case 8222: esc = "bdquo"; break;
case 8224: esc = "dagger"; break;
case 8225: esc = "Dagger"; break;
case 8240: esc = "permil"; break;
case 8249: esc = "lsaquo"; break;
case 8250: esc = "rsaquo"; break;
case 8364: esc = "euro"; break;
case 402: esc = "fnof"; break;
case 977: esc = "thetasym"; break;
case 978: esc = "upsih"; break;
case 982: esc = "piv"; break;
case 8226: esc = "bull"; break;
case 8230: esc = "hellip"; break;
case 8242: esc = "prime"; break;
case 8243: esc = "Prime"; break;
case 8254: esc = "oline"; break;
case 8260: esc = "frasl"; break;
case 8472: esc = "weierp"; break;
case 8465: esc = "image"; break;
case 8476: esc = "real"; break;
case 8482: esc = "trade"; break;
case 8501: esc = "alefsym"; break;
case 8592: esc = "larr"; break;
case 8593: esc = "uarr"; break;
case 8594: esc = "rarr"; break;
case 8595: esc = "darr"; break;
case 8596: esc = "harr"; break;
case 8629: esc = "crarr"; break;
case 8656: esc = "lArr"; break;
case 8657: esc = "uArr"; break;
case 8658: esc = "rArr"; break;
case 8659: esc = "dArr"; break;
case 8660: esc = "hArr"; break;
case 8704: esc = "forall"; break;
case 8706: esc = "part"; break;
case 8707: esc = "exist"; break;
case 8709: esc = "empty"; break;
case 8711: esc = "nabla"; break;
case 8712: esc = "isin"; break;
case 8713: esc = "notin"; break;
case 8715: esc = "ni"; break;
case 8719: esc = "prod"; break;
case 8721: esc = "sum"; break;
case 8722: esc = "minus"; break;
case 8727: esc = "lowast"; break;
case 8730: esc = "radic"; break;
case 8733: esc = "prop"; break;
case 8734: esc = "infin"; break;
case 8736: esc = "ang"; break;
case 8743: esc = "and"; break;
case 8744: esc = "or"; break;
case 8745: esc = "cap"; break;
case 8746: esc = "cup"; break;
case 8747: esc = "int"; break;
case 8756: esc = "there4"; break;
case 8764: esc = "sim"; break;
case 8773: esc = "cong"; break;
case 8776: esc = "asymp"; break;
case 8800: esc = "ne"; break;
case 8801: esc = "equiv"; break;
case 8804: esc = "le"; break;
case 8805: esc = "ge"; break;
case 8834: esc = "sub"; break;
case 8835: esc = "sup"; break;
case 8836: esc = "nsub"; break;
case 8838: esc = "sube"; break;
case 8839: esc = "supe"; break;
case 8853: esc = "oplus"; break;
case 8855: esc = "otimes"; break;
case 8869: esc = "perp"; break;
case 8901: esc = "sdot"; break;
case 8968: esc = "lceil"; break;
case 8969: esc = "rceil"; break;
case 8970: esc = "lfloor"; break;
case 8971: esc = "rfloor"; break;
case 9001: esc = "lang"; break;
case 9002: esc = "rang"; break;
case 9674: esc = "loz"; break;
case 9824: esc = "spades"; break;
case 9827: esc = "clubs"; break;
case 9829: esc = "hearts"; break;
case 9830: esc = "diams"; break;
}
}
if (esc == null) {
stringBuf.setLength (0);
stringBuf.append ("#x");
stringBuf.append (Integer.toHexString (c).toUpperCase ());
esc = stringBuf.toString ();
}
break;
}
if (i != first)
rawWrite (buf, off + first, i - first);
first = i + 1;
if (esc == eol)
newline ();
else {
rawWrite ('&');
rawWrite (esc);
rawWrite (';');
}
}
if (first < len)
rawWrite (buf, off + first, len - first);
}
private void newline ()
throws SAXException, IOException
{
out.write (eol);
column = 0;
}
private void doIndent ()
throws SAXException, IOException
{
int space = elementNestLevel * 2;
newline ();
column = space;
while (space > 8) {
out.write ("\t");
space -= 8;
}
while (space > 0) {
out.write (" ");
space -= 2;
}
}
private void rawWrite (char c)
throws IOException
{
out.write (c);
column++;
}
private void rawWrite (String s)
throws SAXException, IOException
{
if (prettyPrinting && "default".equals (space.peek ())) {
char data [] = s.toCharArray ();
rawWrite (data, 0, data.length);
} else {
out.write (s);
column += s.length ();
}
}
private void rawWrite (char buf [], int offset, int length)
throws SAXException, IOException
{
boolean wrap;
if (prettyPrinting && space.empty ())
fatal ("stack discipline", null);
wrap = prettyPrinting && "default".equals (space.peek ());
if (!wrap) {
out.write (buf, offset, length);
column += length;
return;
}
while (length > 0) {
int target = lineLength - column;
boolean wrote = false;
if (target > length || noWrap) {
out.write (buf, offset, length);
column += length;
return;
}
char c;
for (int i = target - 1; i >= 0; i--) {
if ((c = buf [offset + i]) == ' ' || c == '\t') {
i++;
out.write (buf, offset, i);
doIndent ();
offset += i;
length -= i;
wrote = true;
break;
}
}
if (wrote)
continue;
if (target < 0)
target = 0;
for (int i = target; i < length; i++)
if ((c = buf [offset + i]) == ' ' || c == '\t') {
i++;
out.write (buf, offset, i);
doIndent ();
offset += i;
length -= i;
wrote = true;
break;
}
if (wrote)
continue;
out.write (buf, offset, length);
column += length;
break;
}
}
}