Debian Codegraph @

net/sf/saxon/serialize/TEXTEmitter.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
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 * This class generates TEXT output
25 *
26 * @author Michael H. Kay
27 */
28
29 public class TEXTEmitter extends XMLEmitter {
30
31 private Pattern newlineMatcher = null;
32 private String newlineRepresentation = null;
33
34 /**
35 * Start of the document.
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 // Write a BOM if requested
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 // Might be an encoding exception; just ignore it
69 }
70 }
71 started = true;
72 }
73
74 /**
75 * Output the XML declaration. This implementation does nothing.
76 */
77
78 public void writeDeclaration() throws XPathException {
79 }
80
81 /**
82 * Produce output using the current Writer. <BR>
83 * Special characters are not escaped.
84 *
85 * @param chars Character sequence to be output
86 * @param locationId the location of the node in the source, or of the instruction that created it
87 *@param properties bit fields holding special properties of the characters @throws XPathException for any failure
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 * Output an element start tag. <br>
113 * Does nothing with this output method.
114 * @param elemName The element name (tag)
115 * @param typeCode The type annotation
116 * @param location
117 * @param properties Bit fields holding any special properties of the element
118 */
119
120 public void startElement(NodeName elemName, SchemaType typeCode, Location location, int properties) {
121 // no-op
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 * Output an element end tag. <br>
134 * Does nothing with this output method.
135 */
136
137 public void endElement() {
138 // no-op
139 }
140
141 /**
142 * Output a processing instruction. <br>
143 * Does nothing with this output method.
144 */
145
146 public void processingInstruction(String name, /*@NotNull*/ CharSequence value, Location locationId, int properties)
147 throws XPathException {
148 }
149
150 /**
151 * Output a comment. <br>
152 * Does nothing with this output method.
153 */
154
155 public void comment(CharSequence chars, Location locationId, int properties) throws XPathException {
156 }
157
158 }
159