001 /*
002 * Copyright (c) 2005, 2007 Henri Sivonen
003 *
004 * Permission is hereby granted, free of charge, to any person obtaining a
005 * copy of this software and associated documentation files (the "Software"),
006 * to deal in the Software without restriction, including without limitation
007 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
008 * and/or sell copies of the Software, and to permit persons to whom the
009 * Software is furnished to do so, subject to the following conditions:
010 *
011 * The above copyright notice and this permission notice shall be included in
012 * all copies or substantial portions of the Software.
013 *
014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
017 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
019 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
020 * DEALINGS IN THE SOFTWARE.
021 */
022
023 package nu.validator.htmlparser.test;
024
025 import java.io.IOException;
026 import java.io.OutputStreamWriter;
027 import java.io.UnsupportedEncodingException;
028 import java.io.Writer;
029
030 import javax.xml.transform.ErrorListener;
031 import javax.xml.transform.SourceLocator;
032 import javax.xml.transform.TransformerException;
033
034 import org.xml.sax.ErrorHandler;
035 import org.xml.sax.SAXException;
036 import org.xml.sax.SAXParseException;
037
038 /**
039 * @version $Id: SystemErrErrorHandler.java 150 2007-08-16 19:21:25Z hsivonen $
040 * @author hsivonen
041 */
042 public class SystemErrErrorHandler implements ErrorHandler, ErrorListener {
043
044 private Writer out;
045
046 private boolean inError = false;
047
048 public SystemErrErrorHandler() {
049 try {
050 out = new OutputStreamWriter(System.err, "UTF-8");
051 } catch (UnsupportedEncodingException e) {
052 throw new RuntimeException(e);
053 }
054 }
055
056 /**
057 * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)
058 */
059 public void warning(SAXParseException e) throws SAXException {
060 try {
061 out.write("Warning:\n");
062 out.write(e.getMessage());
063 out.write("\nFile: ");
064 String systemId = e.getSystemId();
065 out.write((systemId == null) ? "Unknown" : systemId);
066 out.write("\nLine: ");
067 out.write(Integer.toString(e.getLineNumber()));
068 out.write(" Col: ");
069 out.write(Integer.toString(e.getColumnNumber()));
070 out.write("\n\n");
071 out.flush();
072 } catch (IOException e1) {
073 throw new SAXException(e1);
074 }
075 }
076
077 /**
078 * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
079 */
080 public void error(SAXParseException e) throws SAXException {
081 inError = true;
082 try {
083 out.write("Error:\n");
084 out.write(e.getMessage());
085 out.write("\nFile: ");
086 String systemId = e.getSystemId();
087 out.write((systemId == null) ? "Unknown" : systemId);
088 out.write("\nLine: ");
089 out.write(Integer.toString(e.getLineNumber()));
090 out.write(" Col: ");
091 out.write(Integer.toString(e.getColumnNumber()));
092 out.write("\n\n");
093 out.flush();
094 } catch (IOException e1) {
095 throw new SAXException(e1);
096 }
097 }
098
099 /**
100 * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)
101 */
102 public void fatalError(SAXParseException e) throws SAXException {
103 inError = true;
104 try {
105 out.write("Fatal Error:\n");
106 out.write(e.getMessage());
107 out.write("\nFile: ");
108 String systemId = e.getSystemId();
109 out.write((systemId == null) ? "Unknown" : systemId);
110 out.write("\nLine: ");
111 out.write(Integer.toString(e.getLineNumber()));
112 out.write(" Col: ");
113 out.write(Integer.toString(e.getColumnNumber()));
114 out.write("\n\n");
115 out.flush();
116 } catch (IOException e1) {
117 throw new SAXException(e1);
118 }
119 }
120
121 /**
122 * Returns the inError.
123 *
124 * @return the inError
125 */
126 public boolean isInError() {
127 return inError;
128 }
129
130 public void reset() {
131 inError = false;
132 }
133
134 public void error(TransformerException e) throws TransformerException {
135 inError = true;
136 try {
137 out.write("Error:\n");
138 out.write(e.getMessage());
139 SourceLocator sourceLocator = e.getLocator();
140 if (sourceLocator != null) {
141 out.write("\nFile: ");
142 String systemId = sourceLocator.getSystemId();
143 out.write((systemId == null) ? "Unknown" : systemId);
144 out.write("\nLine: ");
145 out.write(Integer.toString(sourceLocator.getLineNumber()));
146 out.write(" Col: ");
147 out.write(Integer.toString(sourceLocator.getColumnNumber()));
148 }
149 out.write("\n\n");
150 out.flush();
151 } catch (IOException e1) {
152 throw new TransformerException(e1);
153 }
154 }
155
156 public void fatalError(TransformerException e)
157 throws TransformerException {
158 inError = true;
159 try {
160 out.write("Fatal Error:\n");
161 out.write(e.getMessage());
162 SourceLocator sourceLocator = e.getLocator();
163 if (sourceLocator != null) {
164 out.write("\nFile: ");
165 String systemId = sourceLocator.getSystemId();
166 out.write((systemId == null) ? "Unknown" : systemId);
167 out.write("\nLine: ");
168 out.write(Integer.toString(sourceLocator.getLineNumber()));
169 out.write(" Col: ");
170 out.write(Integer.toString(sourceLocator.getColumnNumber()));
171 }
172 out.write("\n\n");
173 out.flush();
174 } catch (IOException e1) {
175 throw new TransformerException(e1);
176 }
177 }
178
179 public void warning(TransformerException e)
180 throws TransformerException {
181 try {
182 out.write("Warning:\n");
183 out.write(e.getMessage());
184 SourceLocator sourceLocator = e.getLocator();
185 if (sourceLocator != null) {
186 out.write("\nFile: ");
187 String systemId = sourceLocator.getSystemId();
188 out.write((systemId == null) ? "Unknown" : systemId);
189 out.write("\nLine: ");
190 out.write(Integer.toString(sourceLocator.getLineNumber()));
191 out.write(" Col: ");
192 out.write(Integer.toString(sourceLocator.getColumnNumber()));
193 }
194 out.write("\n\n");
195 out.flush();
196 } catch (IOException e1) {
197 throw new TransformerException(e1);
198 }
199 }
200
201 }