Debian Codegraph @

net/sf/saxon/serialize/MessageWarner.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.om.StructuredQName;
13 import net.sf.saxon.trans.XPathException;
14
15 import javax.xml.transform.ErrorListener;
16 import javax.xml.transform.TransformerException;
17 import java.io.StringWriter;
18
19
20 /**
21 * MessageWarner is a user-selectable receiver for XSLT xsl:message output. It causes xsl:message output
22 * to be notified to the warning() method of the JAXP ErrorListener, or to the error() method if
23 * terminate="yes" is specified. This behaviour is specified in recent versions of the JAXP interface
24 * specifications, but it is not the default behaviour, for backwards compatibility reasons.
25 * <p>The text of the message that is sent to the ErrorListener is an XML serialization of the actual
26 * message content.</p>
27 */
28
29 public class MessageWarner extends XMLEmitter {
30
31 boolean abort = false;
32 String errorCode = null;
33
34 public void startDocument(int properties) throws XPathException {
35 setWriter(new StringWriter());
36 abort = (properties & ReceiverOptions.TERMINATE) != 0;
37 super.startDocument(properties);
38 }
39
40 @Override
41 public void processingInstruction(String target, CharSequence data, Location locationId, int properties) throws XPathException {
42 if (target.equals("error-code")) {
43 errorCode = data.toString();
44 } else {
45 super.processingInstruction(target, data, locationId, properties);
46 }
47 }
48
49 public void endDocument() throws XPathException {
50 ErrorListener listener = getPipelineConfiguration().getErrorListener();
51 XPathException de = new XPathException(getWriter().toString());
52 if (errorCode != null) {
53 de.setErrorCodeQName(StructuredQName.fromEQName(errorCode));
54 } else {
55 de.setErrorCode("XTMM9000");
56 }
57 try {
58 if (abort) {
59 listener.error(de);
60 } else {
61 listener.warning(de);
62 }
63 } catch (TransformerException te) {
64 throw XPathException.makeXPathException(te);
65 }
66 }
67
68 public void close() {
69 // do nothing
70 }
71
72 }
73