Debian Codegraph @

net/sf/saxon/serialize/MessageEmitter.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.PipelineConfiguration;
11 import net.sf.saxon.expr.parser.Location;
12 import net.sf.saxon.trans.XPathException;
13
14 import javax.xml.transform.OutputKeys;
15 import java.util.Properties;
16
17
18 /**
19 * MessageEmitter is the default Receiver for xsl:message output.
20 * It is the same as XMLEmitter except that (a) it adds an extra newline at the end of the message, and
21 * (b) it recognizes a processing-instruction with name "error-code" specially; this is assumed to contain
22 * the error-code requested on the {@code xsl:message} instruction. These changes can be overridden
23 * in a user-supplied subclass.
24 */
25
26 public class MessageEmitter extends XMLEmitter {
27
28 public MessageEmitter() {
29
30 }
31
32 @Override
33 public void setPipelineConfiguration(PipelineConfiguration pipelineConfiguration) {
34 super.setPipelineConfiguration(pipelineConfiguration);
35 if (writer == null && outputStream == null) {
36 try {
37 setOutputStream(getConfiguration().getStandardErrorOutput());
38 } catch (XPathException e) {
39 throw new AssertionError(e);
40 }
41 }
42 try {
43 Properties props = new Properties();
44 props.setProperty(OutputKeys.METHOD, "xml");
45 props.setProperty(OutputKeys.INDENT, "yes");
46 props.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
47 setOutputProperties(props);
48 } catch (XPathException e) {
49 throw new IllegalStateException(e);
50 }
51 }
52
53 @Override
54 public void processingInstruction(String target, CharSequence data, Location locationId, int properties) throws XPathException {
55 if (!suppressProcessingInstruction(target, data, locationId, properties)) {
56 super.processingInstruction(target, data, locationId, properties);
57 }
58 }
59
60 /**
61 * Method to decide whether a processing instruction in the message should be suppressed. The default
62 * implementation suppresses a processing instruction whose target is "error-code"; this processing
63 * instruction is inserted into the message; its data part is an EQName containing the error code.
64 * @param target the processing instruction target (that is, name)
65 * @param data the data part of the processing instruction
66 * @param locationId the location, which in the case of the error-code processing instruction, holds
67 * the location of the originating xsl:message instruction
68 * @param properties currently 0.
69 * @return true if the processing instruction is to be suppressed from the displayed message.
70 */
71
72 protected boolean suppressProcessingInstruction(String target, CharSequence data, Location locationId, int properties) {
73 return target.equals("error-code");
74 }
75
76 @Override
77 public void endDocument() throws XPathException {
78 try {
79 if (writer != null) {
80 writer.write('\n');
81 writer.flush();
82 }
83 } catch (java.io.IOException err) {
84 throw new XPathException(err);
85 }
86 super.endDocument();
87 }
88
89 @Override
90 public void close() throws XPathException {
91 try {
92 if (writer != null) {
93 writer.flush();
94 }
95 } catch (java.io.IOException err) {
96 throw new XPathException(err);
97 }
98 super.close();
99 }
100
101 }
102