View Javadoc

1   /*
2    * Copyright 2010-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.xml.handler;
17  
18  import java.io.PrintStream;
19  import java.io.PrintWriter;
20  
21  import javax.xml.transform.ErrorListener;
22  import javax.xml.transform.SourceLocator;
23  import javax.xml.transform.TransformerException;
24  
25  import org.xml.sax.ErrorHandler;
26  import org.xml.sax.SAXException;
27  import org.xml.sax.SAXParseException;
28  
29  /**
30   * Implements SAX error handler for default reporting.
31   */
32  public class DefaultErrorHandler implements ErrorHandler, ErrorListener
33  { // NOPMD
34    // ********************************* Fields *********************************
35  
36    // --- constants ------------------------------------------------------------
37  
38    // --- members --------------------------------------------------------------
39  
40    /**
41     * The writer to write fatals, errors and warnings to.
42     */
43    private final PrintWriter messageWriter;
44  
45    // ****************************** Initializer *******************************
46  
47    // ****************************** Constructors ******************************
48  
49    /**
50     * Convenience constructor passing in a stream, not rethrowing exceptions.
51     *
52     * @param messageStream the stream to write messages to.
53     */
54    public DefaultErrorHandler(final PrintStream messageStream)
55    {
56      this(new PrintWriter(messageStream, true));
57    }
58  
59    /**
60     * Convenience constructor for writing to <code>System.err</code>.
61     */
62    public DefaultErrorHandler()
63    {
64      this(new PrintWriter(System.err, true));
65    }
66  
67    /**
68     * Constructor constructor.
69     *
70     * @param messageWriter the writer to write fatals, errors and warnings to.
71     */
72    public DefaultErrorHandler(final PrintWriter messageWriter)
73    {
74      this.messageWriter = messageWriter;
75    }
76  
77    // ****************************** Inner Classes *****************************
78  
79    // ********************************* Methods ********************************
80  
81    // --- init -----------------------------------------------------------------
82  
83    // --- get&set --------------------------------------------------------------
84  
85    // --- business -------------------------------------------------------------
86  
87    @Override
88    public void warning(final TransformerException exception)
89      throws TransformerException
90    {
91      print(exception, "Transformer Warning");
92    }
93  
94    @Override
95    public void error(final TransformerException exception)
96      throws TransformerException
97    {
98      print(exception, "Transformer Error");
99    }
100 
101   @Override
102   public void fatalError(final TransformerException exception)
103     throws TransformerException
104   {
105     print(exception, "Fatal Transformer Error");
106   }
107 
108   @Override
109   public void warning(final SAXParseException exception) throws SAXException
110   {
111     print(exception, "Parse Warning");
112   }
113 
114   @Override
115   public void error(final SAXParseException exception) throws SAXException
116   {
117     print(exception, "Parse Error");
118   }
119 
120   @Override
121   public void fatalError(final SAXParseException exception) throws SAXException
122   {
123     print(exception, "Fatal Parse Error");
124   }
125 
126   // ... helper
127 
128   private void print(final Throwable exception, final String type)
129   {
130     printLocation(exception);
131     messageWriter.println(type + ": " + exception.getMessage());
132   }
133 
134   private void printLocation(final Throwable exception)
135   {
136     final SourceLocator locator = calcLocator(exception);
137     printMessage(locator);
138   }
139 
140   private void printMessage(final SourceLocator locator)
141   {
142     if (null != locator)
143     {
144       final String publicId = locator.getPublicId();
145       final String systemId = locator.getSystemId();
146 
147       final String id =
148           (null != publicId) ? publicId : (null != systemId) ? systemId
149               : "unknown XML ID";
150 
151       messageWriter.print(id + ": line " + locator.getLineNumber()
152                           + " / column " + locator.getColumnNumber());
153     }
154     else
155     {
156       messageWriter.print("(unknown location)");
157     }
158   }
159 
160   @SuppressWarnings("restriction")
161   private SourceLocator calcLocator(final Throwable exception)
162   {
163     SourceLocator locator = null;
164     Throwable cause = exception;
165 
166     do
167     {
168       if (cause instanceof SAXParseException)
169       {
170         locator =
171             new com.sun.org.apache.xml.internal.utils.SAXSourceLocator(
172                 (SAXParseException) cause);
173       }
174       else if (cause instanceof TransformerException)
175       {
176         locator = handleTransformerException(locator, cause);
177       }
178 
179       if (cause instanceof TransformerException)
180       {
181         cause = ((TransformerException) cause).getCause();
182       }
183       else if (cause instanceof com.sun.org.apache.xml.internal.utils.WrappedRuntimeException)
184       {
185         cause =
186             ((com.sun.org.apache.xml.internal.utils.WrappedRuntimeException) cause)
187                 .getException();
188       }
189       else if (cause instanceof SAXException)
190       {
191         cause = ((SAXException) cause).getException();
192       }
193       else
194       {
195         cause = null;
196       }
197     }
198     while (null != cause);
199 
200     return locator;
201   }
202 
203   private SourceLocator handleTransformerException(final SourceLocator locator,
204       final Throwable cause)
205   {
206     final SourceLocator causeLocator =
207         ((TransformerException) cause).getLocator();
208     if (null != causeLocator)
209     {
210       return causeLocator;
211     }
212     return locator;
213   }
214 
215   // --- object basics --------------------------------------------------------
216 
217 }