001    /*
002     * Copyright (c) 2005 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 fi.iki.hsivonen.xml;
024    
025    import java.io.IOException;
026    import java.io.OutputStreamWriter;
027    import java.io.UnsupportedEncodingException;
028    import java.io.Writer;
029    
030    import org.xml.sax.ErrorHandler;
031    import org.xml.sax.SAXException;
032    import org.xml.sax.SAXParseException;
033    
034    /**
035     * @version $Id: SystemErrErrorHandler.java,v 1.3 2006/03/13 11:01:09 hsivonen Exp $
036     * @author hsivonen
037     */
038    public class SystemErrErrorHandler implements ErrorHandler {
039    
040        private Writer out;
041        
042        private boolean inError = false;
043        
044        public SystemErrErrorHandler() {
045            try {
046                out = new OutputStreamWriter(System.err, "UTF-8");
047            } catch (UnsupportedEncodingException e) {
048                throw new RuntimeException(e);
049            }
050        }
051        
052        /**
053         * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)
054         */
055        public void warning(SAXParseException e) throws SAXException {
056            try {
057                out.write("Warning:\n");
058                out.write(e.getMessage());
059                out.write("\nFile: ");
060                String systemId = e.getSystemId();
061                out.write((systemId == null) ? "Unknown" : systemId);
062                out.write("\nLine: ");
063                out.write(Integer.toString(e.getLineNumber()));
064                out.write(" Col: ");
065                out.write(Integer.toString(e.getColumnNumber()));
066                out.write("\n\n");
067                out.flush();
068            } catch (IOException e1) {
069                throw new SAXException(e1);
070            }
071        }
072    
073        /**
074         * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
075         */
076        public void error(SAXParseException e) throws SAXException {
077            inError = true;
078            try {
079                out.write("Error:\n");
080                out.write(e.getMessage());
081                out.write("\nFile: ");
082                String systemId = e.getSystemId();
083                out.write((systemId == null) ? "Unknown" : systemId);
084                out.write("\nLine: ");
085                out.write(Integer.toString(e.getLineNumber()));
086                out.write(" Col: ");
087                out.write(Integer.toString(e.getColumnNumber()));
088                out.write("\n\n");
089                out.flush();
090            } catch (IOException e1) {
091                throw new SAXException(e1);
092            }
093        }
094    
095        /**
096         * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)
097         */
098        public void fatalError(SAXParseException e) throws SAXException {
099            inError = true;
100            try {
101                out.write("Fatal Error:\n");
102                out.write(e.getMessage());
103                out.write("\nFile: ");
104                String systemId = e.getSystemId();
105                out.write((systemId == null) ? "Unknown" : systemId);
106                out.write("\nLine: ");
107                out.write(Integer.toString(e.getLineNumber()));
108                out.write(" Col: ");
109                out.write(Integer.toString(e.getColumnNumber()));
110                out.write("\n\n");
111                out.flush();
112            } catch (IOException e1) {
113                throw new SAXException(e1);
114            }
115        }
116    
117        /**
118         * Returns the inError.
119         * 
120         * @return the inError
121         */
122        public boolean isInError() {
123            return inError;
124        }
125    
126        public void reset() {
127            inError = false;
128        }
129        
130        
131    }