| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.serialize; |
| 9 |
|
| 10 |
import net.sf.saxon.event.ReceiverOptions; |
| 11 |
import net.sf.saxon.expr.parser.Location; |
| 12 |
import net.sf.saxon.lib.SaxonOutputKeys; |
| 13 |
import net.sf.saxon.om.NamespaceBindingSet; |
| 14 |
import net.sf.saxon.om.NodeName; |
| 15 |
import net.sf.saxon.serialize.charcode.UTF8CharacterSet; |
| 16 |
import net.sf.saxon.trans.XPathException; |
| 17 |
import net.sf.saxon.type.SchemaType; |
| 18 |
import net.sf.saxon.type.SimpleType; |
| 19 |
|
| 20 |
import javax.xml.transform.OutputKeys; |
| 21 |
import java.util.regex.Pattern; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public class TEXTEmitter extends XMLEmitter { |
| 30 |
|
| 31 |
private Pattern newlineMatcher = null; |
| 32 |
private String newlineRepresentation = null; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public void open() throws XPathException { |
| 39 |
} |
| 40 |
|
| 41 |
protected void openDocument() throws XPathException { |
| 42 |
|
| 43 |
if (writer == null) { |
| 44 |
makeWriter(); |
| 45 |
} |
| 46 |
if (characterSet == null) { |
| 47 |
characterSet = UTF8CharacterSet.getInstance(); |
| 48 |
} |
| 49 |
|
| 50 |
String encoding = outputProperties.getProperty(OutputKeys.ENCODING); |
| 51 |
if (encoding == null || encoding.equalsIgnoreCase("utf8")) { |
| 52 |
encoding = "UTF-8"; |
| 53 |
} |
| 54 |
String byteOrderMark = outputProperties.getProperty(SaxonOutputKeys.BYTE_ORDER_MARK); |
| 55 |
String nl = outputProperties.getProperty(SaxonOutputKeys.NEWLINE); |
| 56 |
if (nl != null && !nl.equals("\n")) { |
| 57 |
newlineRepresentation = nl; |
| 58 |
newlineMatcher = Pattern.compile("\\n"); |
| 59 |
} |
| 60 |
|
| 61 |
if ("yes".equals(byteOrderMark) && ( |
| 62 |
"UTF-8".equalsIgnoreCase(encoding) || |
| 63 |
"UTF-16LE".equalsIgnoreCase(encoding) || |
| 64 |
"UTF-16BE".equalsIgnoreCase(encoding))) { |
| 65 |
try { |
| 66 |
writer.write('\uFEFF'); |
| 67 |
} catch (java.io.IOException err) { |
| 68 |
|
| 69 |
} |
| 70 |
} |
| 71 |
started = true; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public void writeDeclaration() throws XPathException { |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public void characters(CharSequence chars, Location locationId, int properties) throws XPathException { |
| 91 |
if (!started) { |
| 92 |
openDocument(); |
| 93 |
} |
| 94 |
if ((properties & ReceiverOptions.NO_SPECIAL_CHARS) == 0) { |
| 95 |
int badchar = testCharacters(chars); |
| 96 |
if (badchar != 0) { |
| 97 |
throw new XPathException( |
| 98 |
"Output character not available in this encoding (x" + Integer.toString(badchar, 16) + ")", "SERE0008"); |
| 99 |
} |
| 100 |
} |
| 101 |
if (newlineMatcher != null) { |
| 102 |
chars = newlineMatcher.matcher(chars).replaceAll(newlineRepresentation); |
| 103 |
} |
| 104 |
try { |
| 105 |
writer.write(chars.toString()); |
| 106 |
} catch (java.io.IOException err) { |
| 107 |
throw new XPathException(err); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public void startElement(NodeName elemName, SchemaType typeCode, Location location, int properties) { |
| 121 |
|
| 122 |
previousAtomic = false; |
| 123 |
} |
| 124 |
|
| 125 |
public void namespace(NamespaceBindingSet namespaceBindings, int properties) { |
| 126 |
} |
| 127 |
|
| 128 |
public void attribute(NodeName nameCode, SimpleType typeCode, CharSequence value, Location locationId, int properties) { |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public void endElement() { |
| 138 |
|
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public void processingInstruction(String name, CharSequence value, Location locationId, int properties) |
| 147 |
throws XPathException { |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public void comment(CharSequence chars, Location locationId, int properties) throws XPathException { |
| 156 |
} |
| 157 |
|
| 158 |
} |
| 159 |
|