KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > kilim > repository > BasicParserErrorHandler


1 /**
2  * Copyright (C) 2002 Kelua SA
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.kilim.repository;
19
20 import java.io.IOException JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.io.PrintStream JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24 import java.io.StringWriter JavaDoc;
25
26 import javax.xml.parsers.ParserConfigurationException JavaDoc;
27 import org.objectweb.kilim.KilimException;
28 import org.xml.sax.SAXException JavaDoc;
29 import org.xml.sax.SAXParseException JavaDoc;
30
31 /**
32  * @author dutoo, horn
33  * Implementation of {@link org.objectweb.kilim.repository.ParserErrorHandler}
34  * logging exceptions in an output print stream.
35  */

36 public class BasicParserErrorHandler implements ParserErrorHandler {
37     private PrintStream JavaDoc out;
38     private static final String JavaDoc BASE_HEADER = "Kilim Parser v1.0 ";
39     private String JavaDoc header = BASE_HEADER;
40     private String JavaDoc templateName;
41     
42     /**
43      * Creates a new BasicParserErrorHandler logging errors to System.err.
44      */

45     public BasicParserErrorHandler() {
46         out = System.err;
47     }
48     
49     /**
50      * Creates a new BasicParserErrorHandler logging errors to the given stream.
51      * @param os an output stream
52      */

53     public BasicParserErrorHandler(OutputStream JavaDoc os) {
54         out = new PrintStream JavaDoc(os);
55     }
56     
57     /**
58      * Method setTemplateName.
59      * @param aName : the name of the template being parsed.
60      */

61     public void setTemplateName(String JavaDoc aName) {
62         templateName = aName;
63         header = BASE_HEADER + " [ template : " + aName + " ] ";
64     }
65     
66     /**
67      * @see org.objectweb.kilim.repository.ParserErrorHandler#handleIOException(IOException)
68      */

69     public void handleIOException(IOException JavaDoc ioex) {
70         out.println(header + ioex.getLocalizedMessage());
71     }
72     
73     /**
74      * @see org.objectweb.kilim.repository.ParserErrorHandler#handleSAXException(SAXException)
75      */

76     public void handleSAXException(SAXException JavaDoc saxex) {
77         if (saxex instanceof SAXParseException JavaDoc) {
78             handleFatalSAXParseException((SAXParseException JavaDoc) saxex);
79         } else {
80             out.println(header + saxex.getLocalizedMessage());
81         }
82     }
83     
84     /**
85      * @see org.objectweb.kilim.repository.ParserErrorHandler#handleParserConfigurationException(ParserConfigurationException)
86      */

87     public void handleParserConfigurationException(ParserConfigurationException JavaDoc pcex) {
88         out.println(header + pcex.getLocalizedMessage());
89     }
90     
91     /**
92      * @see org.objectweb.kilim.repository.ParserErrorHandler#handleWarningSAXParseException(SAXParseException)
93      */

94     public void handleWarningSAXParseException(SAXParseException JavaDoc saxpex) {
95         out.println(getParseErrorMessage(saxpex, "WARNING"));
96     }
97     
98     /**
99      * @see org.objectweb.kilim.repository.ParserErrorHandler#handleErrorSAXParseException(SAXParseException)
100      */

101     public void handleErrorSAXParseException(SAXParseException JavaDoc saxpex) {
102         out.println(getParseErrorMessage(saxpex, "ERROR"));
103     }
104     
105     /**
106      * @see org.objectweb.kilim.repository.ParserErrorHandler#handleFatalSAXParseException(SAXParseException)
107      */

108     public void handleFatalSAXParseException(SAXParseException JavaDoc saxpex) {
109         out.println(getParseErrorMessage(saxpex, "FATAL"));
110     }
111     
112     /**
113     * @see org.objectweb.kilim.repository.ParserErrorHandler#handleKilimException(KilimException)
114     */

115     public void handleKilimException(KilimException ex) {
116         //out.println(header + "Bad property typing of value " + ptex.getValue() + " in template " + ptex.getContext());
117
out.println("Kilim Exception " + ex);
118     }
119     
120     private String JavaDoc getParseErrorMessage(SAXParseException JavaDoc saxex, String JavaDoc level) {
121         StringBuffer JavaDoc msg = new StringBuffer JavaDoc(header + level);
122         int line = saxex.getLineNumber();
123         int col = saxex.getColumnNumber();
124         String JavaDoc publicId = saxex.getPublicId();
125         String JavaDoc systemId = saxex.getSystemId();
126         Exception JavaDoc embedded = saxex.getException();
127         if (line > -1) {
128             msg.append(" at line " + line);
129         }
130         if (col > -1) {
131             msg.append(" at column " + col);
132         }
133         if (publicId != null) {
134             msg.append(" at public id " + publicId);
135         }
136         if (systemId != null) {
137             msg.append(" at system id " + systemId);
138         }
139         if (embedded != null) {
140             msg.append(" with embedded exception " + embedded);
141         }
142         msg.append(" : " + saxex.getLocalizedMessage());
143         
144         StringWriter JavaDoc strng_wrtr = new StringWriter JavaDoc();
145         PrintWriter JavaDoc prt_wrtr = new PrintWriter JavaDoc(strng_wrtr);
146         return msg.toString();
147     }
148 }
149
Popular Tags