KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > StandardErrorListener


1 package com.icl.saxon;
2
3 import org.xml.sax.SAXException JavaDoc;
4 import javax.xml.transform.*;
5 import javax.xml.transform.dom.DOMLocator JavaDoc;
6 import java.io.PrintStream JavaDoc;
7
8 /**
9   * <B>StandardErrorListener</B> is the standard error handler for XSLT processing
10   * errors, used if no other ErrorListener is nominated.
11   * @author Michael H. Kay (mhkay@iclway.co.uk)
12   */

13   
14 public class StandardErrorListener implements ErrorListener {
15
16     int recoveryPolicy = Controller.RECOVER_WITH_WARNINGS;
17     int warningCount = 0;
18     PrintStream JavaDoc errorOutput = System.err;
19
20     /**
21     * Set output destination for error messages (default is System.err)
22     * @param writer The PrintStream to use for error messages
23     */

24
25     public void setErrorOutput(PrintStream JavaDoc writer) {
26         errorOutput = writer;
27     }
28
29     /**
30     * Set the recovery policy
31     */

32
33     public void setRecoveryPolicy(int policy) {
34         recoveryPolicy = policy;
35     }
36
37     /**
38      * Receive notification of a warning.
39      *
40      * <p>Transformers can use this method to report conditions that
41      * are not errors or fatal errors. The default behaviour is to
42      * take no action.</p>
43      *
44      * <p>After invoking this method, the Transformer must continue with
45      * the transformation. It should still be possible for the
46      * application to process the document through to the end.</p>
47      *
48      * @param exception The warning information encapsulated in a
49      * transformer exception.
50      *
51      * @throws javax.xml.transform.TransformerException if the application
52      * chooses to discontinue the transformation.
53      *
54      * @see javax.xml.transform.TransformerException
55      */

56      
57     public void warning(TransformerException exception)
58         throws TransformerException {
59
60         if (recoveryPolicy==Controller.RECOVER_SILENTLY) {
61             // do nothing
62
return;
63         }
64
65         String JavaDoc message = "";
66         if (exception.getLocator()!=null) {
67             message = getLocationMessage(exception) + "\n ";
68         }
69         message += getExpandedMessage(exception);
70         
71         if (recoveryPolicy==Controller.RECOVER_WITH_WARNINGS) {
72             errorOutput.println("Recoverable error");
73             errorOutput.println(message);
74             warningCount++;
75             if (warningCount > 25) {
76                 System.err.println("No more warnings will be displayed");
77                 recoveryPolicy = Controller.RECOVER_SILENTLY;
78                 warningCount = 0;
79             }
80         } else {
81             errorOutput.println("Recoverable error");
82             errorOutput.println(message);
83             errorOutput.println("Processing terminated because error recovery is disabled");
84             throw new TransformerException(exception);
85         }
86     }
87
88     /**
89      * Receive notification of a recoverable error.
90      *
91      * <p>The transformer must continue to provide normal parsing events
92      * after invoking this method. It should still be possible for the
93      * application to process the document through to the end.</p>
94      *
95      * <p>The action of the standard error listener depends on the
96      * recovery policy that has been set, which may be one of RECOVER_SILENTLY,
97      * RECOVER_WITH_WARNING, or DO_NOT_RECOVER
98      *
99      * @param exception The error information encapsulated in a
100      * transformer exception.
101      *
102      * @throws javax.xml.transform.TransformerException if the application
103      * chooses to discontinue the transformation.
104      *
105      * @see javax.xml.transform.TransformerException
106      */

107      
108     public void error(TransformerException exception) throws TransformerException {
109         //new NullPointerException("dd").printStackTrace();
110
String JavaDoc message = "Error " +
111                          getLocationMessage(exception) +
112                          "\n " +
113                          getExpandedMessage(exception);
114         errorOutput.println(message);
115     }
116
117     /**
118      * Receive notification of a non-recoverable error.
119      *
120      * <p>The application must assume that the transformation cannot
121      * continue after the Transformer has invoked this method,
122      * and should continue (if at all) only to collect
123      * addition error messages. In fact, Transformers are free
124      * to stop reporting events once this method has been invoked.</p>
125      *
126      * @param exception The error information encapsulated in a
127      * transformer exception.
128      *
129      * @throws javax.xml.transform.TransformerException if the application
130      * chooses to discontinue the transformation.
131      *
132      * @see javax.xml.transform.TransformerException
133      */

134      
135     public void fatalError(TransformerException exception) throws TransformerException {
136         error(exception);
137         throw exception;
138     }
139
140     /**
141     * Get a string identifying the location of an error.
142     */

143     
144     public static String JavaDoc getLocationMessage(TransformerException err) {
145         SourceLocator loc = err.getLocator();
146         if (loc==null) {
147             return "";
148         } else {
149             String JavaDoc locmessage = "";
150             if (loc instanceof DOMLocator JavaDoc) {
151                 locmessage += "at " + ((DOMLocator JavaDoc)loc).getOriginatingNode().getNodeName() + " ";
152             }
153             int line = loc.getLineNumber();
154             int column = loc.getColumnNumber();
155             if (line<0 && column>0) {
156                 locmessage += "at byte " + column + " ";
157             } else {
158                 locmessage += "on line " + line + " ";
159                 if (loc.getColumnNumber() != -1) {
160                     locmessage += "column " + column + " ";
161                 }
162             }
163             locmessage += "of " + loc.getSystemId() + ":";
164             return locmessage;
165         }
166     }
167     
168     /**
169     * Get a string containing the message for this exception and all contained exceptions
170     */

171     
172     public static String JavaDoc getExpandedMessage(TransformerException err) {
173         String JavaDoc message = "";
174         Throwable JavaDoc e = err;
175         while (true) {
176             if (e == null) {
177                 break;
178             }
179             String JavaDoc next = e.getMessage();
180             if (!next.equals("TRaX Transform Exception") && !message.endsWith(next)) {
181                 if (!message.equals("")) {
182                     message += ": ";
183                 }
184                 message += e.getMessage();
185             }
186             if (e instanceof TransformerException) {
187                 e = ((TransformerException)e).getException();
188             } else if (e instanceof SAXException JavaDoc) {
189                 e = ((SAXException JavaDoc)e).getException();
190             } else {
191                 break;
192             }
193         }
194         
195         return message;
196     }
197 }
198
199 // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
200
// you may not use this file except in compliance with the License. You may obtain a copy of the
201
// License at http://www.mozilla.org/MPL/
202
//
203
// Software distributed under the License is distributed on an "AS IS" basis,
204
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
205
// See the License for the specific language governing rights and limitations under the License.
206
//
207
// The Original Code is: all this file.
208
//
209
// The Initial Developer of the Original Code is
210
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
211
//
212
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
213
//
214
// Contributor(s):
215
// Portions marked "e.g." are from Edwin Glaser (edwin@pannenleiter.de)
216
//
217
Popular Tags