Source Code Cross Referenced for DefaultErrorHandler.java in  » XML » xalan » org » apache » xml » utils » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » XML » xalan » org.apache.xml.utils 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 1999-2004 The Apache Software Foundation.
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *     http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:        /*
017:         * $Id: DefaultErrorHandler.java,v 1.16 2005/06/03 15:52:46 mkwan Exp $
018:         */
019:        package org.apache.xml.utils;
020:
021:        import java.io.PrintStream;
022:        import java.io.PrintWriter;
023:
024:        import javax.xml.transform.ErrorListener;
025:        import javax.xml.transform.SourceLocator;
026:        import javax.xml.transform.TransformerException;
027:
028:        import org.apache.xml.res.XMLErrorResources;
029:        import org.apache.xml.res.XMLMessages;
030:
031:        import org.xml.sax.ErrorHandler;
032:        import org.xml.sax.SAXException;
033:        import org.xml.sax.SAXParseException;
034:
035:        /**
036:         * Implement SAX error handler for default reporting.
037:         * @xsl.usage general
038:         */
039:        public class DefaultErrorHandler implements  ErrorHandler, ErrorListener {
040:            PrintWriter m_pw;
041:
042:            /**
043:             * if this flag is set to true, we will rethrow the exception on
044:             * the error() and fatalError() methods. If it is false, the errors 
045:             * are reported to System.err. 
046:             */
047:            boolean m_throwExceptionOnError = true;
048:
049:            /**
050:             * Constructor DefaultErrorHandler
051:             */
052:            public DefaultErrorHandler(PrintWriter pw) {
053:                m_pw = pw;
054:            }
055:
056:            /**
057:             * Constructor DefaultErrorHandler
058:             */
059:            public DefaultErrorHandler(PrintStream pw) {
060:                m_pw = new PrintWriter(pw, true);
061:            }
062:
063:            /**
064:             * Constructor DefaultErrorHandler
065:             */
066:            public DefaultErrorHandler() {
067:                this (true);
068:            }
069:
070:            /**
071:             * Constructor DefaultErrorHandler
072:             */
073:            public DefaultErrorHandler(boolean throwExceptionOnError) {
074:                m_pw = new PrintWriter(System.err, true);
075:                m_throwExceptionOnError = throwExceptionOnError;
076:            }
077:
078:            /**
079:             * Receive notification of a warning.
080:             *
081:             * <p>SAX parsers will use this method to report conditions that
082:             * are not errors or fatal errors as defined by the XML 1.0
083:             * recommendation.  The default behaviour is to take no action.</p>
084:             *
085:             * <p>The SAX parser must continue to provide normal parsing events
086:             * after invoking this method: it should still be possible for the
087:             * application to process the document through to the end.</p>
088:             *
089:             * @param exception The warning information encapsulated in a
090:             *                  SAX parse exception.
091:             * @throws SAXException Any SAX exception, possibly
092:             *            wrapping another exception.
093:             */
094:            public void warning(SAXParseException exception)
095:                    throws SAXException {
096:                printLocation(m_pw, exception);
097:                m_pw.println("Parser warning: " + exception.getMessage());
098:            }
099:
100:            /**
101:             * Receive notification of a recoverable error.
102:             *
103:             * <p>This corresponds to the definition of "error" in section 1.2
104:             * of the W3C XML 1.0 Recommendation.  For example, a validating
105:             * parser would use this callback to report the violation of a
106:             * validity constraint.  The default behaviour is to take no
107:             * action.</p>
108:             *
109:             * <p>The SAX parser must continue to provide normal parsing events
110:             * after invoking this method: it should still be possible for the
111:             * application to process the document through to the end.  If the
112:             * application cannot do so, then the parser should report a fatal
113:             * error even if the XML 1.0 recommendation does not require it to
114:             * do so.</p>
115:             *
116:             * @param exception The error information encapsulated in a
117:             *                  SAX parse exception.
118:             * @throws SAXException Any SAX exception, possibly
119:             *            wrapping another exception.
120:             */
121:            public void error(SAXParseException exception) throws SAXException {
122:                //printLocation(exception);
123:                // m_pw.println(exception.getMessage());
124:
125:                throw exception;
126:            }
127:
128:            /**
129:             * Receive notification of a non-recoverable error.
130:             *
131:             * <p>This corresponds to the definition of "fatal error" in
132:             * section 1.2 of the W3C XML 1.0 Recommendation.  For example, a
133:             * parser would use this callback to report the violation of a
134:             * well-formedness constraint.</p>
135:             *
136:             * <p>The application must assume that the document is unusable
137:             * after the parser has invoked this method, and should continue
138:             * (if at all) only for the sake of collecting addition error
139:             * messages: in fact, SAX parsers are free to stop reporting any
140:             * other events once this method has been invoked.</p>
141:             *
142:             * @param exception The error information encapsulated in a
143:             *                  SAX parse exception.
144:             * @throws SAXException Any SAX exception, possibly
145:             *            wrapping another exception.
146:             */
147:            public void fatalError(SAXParseException exception)
148:                    throws SAXException {
149:                // printLocation(exception);
150:                // m_pw.println(exception.getMessage());
151:
152:                throw exception;
153:            }
154:
155:            /**
156:             * Receive notification of a warning.
157:             *
158:             * <p>SAX parsers will use this method to report conditions that
159:             * are not errors or fatal errors as defined by the XML 1.0
160:             * recommendation.  The default behaviour is to take no action.</p>
161:             *
162:             * <p>The SAX parser must continue to provide normal parsing events
163:             * after invoking this method: it should still be possible for the
164:             * application to process the document through to the end.</p>
165:             *
166:             * @param exception The warning information encapsulated in a
167:             *                  SAX parse exception.
168:             * @throws javax.xml.transform.TransformerException Any SAX exception, possibly
169:             *            wrapping another exception.
170:             * @see javax.xml.transform.TransformerException
171:             */
172:            public void warning(TransformerException exception)
173:                    throws TransformerException {
174:                printLocation(m_pw, exception);
175:
176:                m_pw.println(exception.getMessage());
177:            }
178:
179:            /**
180:             * Receive notification of a recoverable error.
181:             *
182:             * <p>This corresponds to the definition of "error" in section 1.2
183:             * of the W3C XML 1.0 Recommendation.  For example, a validating
184:             * parser would use this callback to report the violation of a
185:             * validity constraint.  The default behaviour is to take no
186:             * action.</p>
187:             *
188:             * <p>The SAX parser must continue to provide normal parsing events
189:             * after invoking this method: it should still be possible for the
190:             * application to process the document through to the end.  If the
191:             * application cannot do so, then the parser should report a fatal
192:             * error even if the XML 1.0 recommendation does not require it to
193:             * do so.</p>
194:             *
195:             * @param exception The error information encapsulated in a
196:             *                  SAX parse exception.
197:             * @throws javax.xml.transform.TransformerException Any SAX exception, possibly
198:             *            wrapping another exception.
199:             * @see javax.xml.transform.TransformerException
200:             */
201:            public void error(TransformerException exception)
202:                    throws TransformerException {
203:                // If the m_throwExceptionOnError flag is true, rethrow the exception.
204:                // Otherwise report the error to System.err.
205:                if (m_throwExceptionOnError)
206:                    throw exception;
207:                else {
208:                    printLocation(m_pw, exception);
209:                    m_pw.println(exception.getMessage());
210:                }
211:            }
212:
213:            /**
214:             * Receive notification of a non-recoverable error.
215:             *
216:             * <p>This corresponds to the definition of "fatal error" in
217:             * section 1.2 of the W3C XML 1.0 Recommendation.  For example, a
218:             * parser would use this callback to report the violation of a
219:             * well-formedness constraint.</p>
220:             *
221:             * <p>The application must assume that the document is unusable
222:             * after the parser has invoked this method, and should continue
223:             * (if at all) only for the sake of collecting addition error
224:             * messages: in fact, SAX parsers are free to stop reporting any
225:             * other events once this method has been invoked.</p>
226:             *
227:             * @param exception The error information encapsulated in a
228:             *                  SAX parse exception.
229:             * @throws javax.xml.transform.TransformerException Any SAX exception, possibly
230:             *            wrapping another exception.
231:             * @see javax.xml.transform.TransformerException
232:             */
233:            public void fatalError(TransformerException exception)
234:                    throws TransformerException {
235:                // If the m_throwExceptionOnError flag is true, rethrow the exception.
236:                // Otherwise report the error to System.err.
237:                if (m_throwExceptionOnError)
238:                    throw exception;
239:                else {
240:                    printLocation(m_pw, exception);
241:                    m_pw.println(exception.getMessage());
242:                }
243:            }
244:
245:            public static void ensureLocationSet(TransformerException exception) {
246:                // SourceLocator locator = exception.getLocator();
247:                SourceLocator locator = null;
248:                Throwable cause = exception;
249:
250:                // Try to find the locator closest to the cause.
251:                do {
252:                    if (cause instanceof  SAXParseException) {
253:                        locator = new SAXSourceLocator(
254:                                (SAXParseException) cause);
255:                    } else if (cause instanceof  TransformerException) {
256:                        SourceLocator causeLocator = ((TransformerException) cause)
257:                                .getLocator();
258:                        if (null != causeLocator)
259:                            locator = causeLocator;
260:                    }
261:
262:                    if (cause instanceof  TransformerException)
263:                        cause = ((TransformerException) cause).getCause();
264:                    else if (cause instanceof  SAXException)
265:                        cause = ((SAXException) cause).getException();
266:                    else
267:                        cause = null;
268:                } while (null != cause);
269:
270:                exception.setLocator(locator);
271:            }
272:
273:            public static void printLocation(PrintStream pw,
274:                    TransformerException exception) {
275:                printLocation(new PrintWriter(pw), exception);
276:            }
277:
278:            public static void printLocation(java.io.PrintStream pw,
279:                    org.xml.sax.SAXParseException exception) {
280:                printLocation(new PrintWriter(pw), exception);
281:            }
282:
283:            public static void printLocation(PrintWriter pw, Throwable exception) {
284:                SourceLocator locator = null;
285:                Throwable cause = exception;
286:
287:                // Try to find the locator closest to the cause.
288:                do {
289:                    if (cause instanceof  SAXParseException) {
290:                        locator = new SAXSourceLocator(
291:                                (SAXParseException) cause);
292:                    } else if (cause instanceof  TransformerException) {
293:                        SourceLocator causeLocator = ((TransformerException) cause)
294:                                .getLocator();
295:                        if (null != causeLocator)
296:                            locator = causeLocator;
297:                    }
298:                    if (cause instanceof  TransformerException)
299:                        cause = ((TransformerException) cause).getCause();
300:                    else if (cause instanceof  WrappedRuntimeException)
301:                        cause = ((WrappedRuntimeException) cause)
302:                                .getException();
303:                    else if (cause instanceof  SAXException)
304:                        cause = ((SAXException) cause).getException();
305:                    else
306:                        cause = null;
307:                } while (null != cause);
308:
309:                if (null != locator) {
310:                    // m_pw.println("Parser fatal error: "+exception.getMessage());
311:                    String id = (null != locator.getPublicId()) ? locator
312:                            .getPublicId()
313:                            : (null != locator.getSystemId()) ? locator
314:                                    .getSystemId()
315:                                    : XMLMessages
316:                                            .createXMLMessage(
317:                                                    XMLErrorResources.ER_SYSTEMID_UNKNOWN,
318:                                                    null); //"SystemId Unknown";
319:
320:                    pw.print(id + "; "
321:                            + XMLMessages.createXMLMessage("line", null)
322:                            + locator.getLineNumber() + "; "
323:                            + XMLMessages.createXMLMessage("column", null)
324:                            + locator.getColumnNumber() + "; ");
325:                } else
326:                    pw
327:                            .print("("
328:                                    + XMLMessages
329:                                            .createXMLMessage(
330:                                                    XMLErrorResources.ER_LOCATION_UNKNOWN,
331:                                                    null) + ")");
332:            }
333:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.