View Javadoc

1   /*
2    * $Id: DefaultErrorHandler.java,v 1.11 2007/07/04 19:42:48 edankert Exp $
3    *
4    * The contents of this file are subject to the Mozilla Public License 
5    * Version 1.1 (the "License"); you may not use this file except in 
6    * compliance with the License. You may obtain a copy of the License at 
7    * http://www.mozilla.org/MPL/ 
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis, 
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
11   * for the specific language governing rights and limitations under the License.
12   *
13   * The Original Code is XML Hammer code. (org.xmlhammer.*)
14   *
15   * The Initial Developer of the Original Code is Edwin Dankert. Portions created 
16   * by the Initial Developer are Copyright (C) 2005 - 2006 the Initial Developer. 
17   * All Rights Reserved.
18   *
19   * Contributor(s): Edwin Dankert <edankert@gmail.com>
20   */
21  package org.xmlhammer;
22  
23  import java.io.IOException;
24  import java.net.URI;
25  import java.util.ArrayList;
26  
27  import javax.xml.transform.ErrorListener;
28  import javax.xml.transform.SourceLocator;
29  import javax.xml.transform.TransformerException;
30  
31  import org.bounce.util.URIUtils;
32  import org.xml.sax.ErrorHandler;
33  import org.xml.sax.SAXParseException;
34  
35  public class DefaultErrorHandler implements ErrorHandler, ErrorListener {
36      private ArrayList<Problem> problems = null;
37      private ArrayList<Exception> exceptions = null;
38      private URI systemId = null;
39      
40      public DefaultErrorHandler(URI systemId) {
41          problems = new ArrayList<Problem>();
42          exceptions = new ArrayList<Exception>();
43          
44          this.systemId = systemId;
45      }
46  
47      public void message(String type, String message) {
48          problems.add(new Message(type, message));
49      }
50  
51      public void warning(SAXParseException e) {
52          if (!exceptions.contains(e) && !exceptions.contains(e.getException())) {
53              problems.add(new Warning(e));
54              exceptions.add(e);
55          }
56      }
57  
58      public void warning(TransformerException e) {
59          if (!exceptions.contains(e) && !exceptions.contains(e.getException())) {
60              addLocator(e);
61              
62              problems.add(new Warning(e));
63              exceptions.add(e);
64          }
65      }
66  
67      public void error(SAXParseException e) {
68          if (!exceptions.contains(e) && !exceptions.contains(e.getException())) {
69              problems.add(new Error(e));
70              exceptions.add(e);
71          }
72      }
73  
74      public void error(TransformerException e) {
75          if (!exceptions.contains(e) && !exceptions.contains(e.getException())) {
76              addLocator(e);
77  
78              problems.add(new Error(e));
79              exceptions.add(e);
80          }
81      }
82  
83      public void fatalError(SAXParseException e) {
84          if (!exceptions.contains(e) && !exceptions.contains(e.getException())) {
85              problems.add(new Fatal(e));
86              exceptions.add(e);
87          }
88      }
89      
90      public void fatalError(TransformerException e) {
91          if (!exceptions.contains(e) && !exceptions.contains(e.getException())) {
92              addLocator(e);
93  
94              problems.add(new Fatal(e));
95              exceptions.add(e);
96          }
97      }
98  
99      public void fatalError(IOException e) {
100         if (!exceptions.contains(e)) {
101             problems.add(new Fatal(e));
102             exceptions.add(e);
103         }
104     }
105 
106     public ArrayList<Problem> getProblems() {
107         return problems;
108     }
109     
110     public Exception getFatal() {
111         for (Problem problem : problems) {
112             if (problem instanceof Fatal) {
113                 return problem.getException();
114             }
115         }
116         
117         return null;
118     }
119     
120     private void addLocator(TransformerException e){
121         if (e.getLocator() == null && systemId != null) {
122             e.setLocator(new DummySourceLocator(systemId));
123         }
124     }
125 
126 
127     public class Warning extends Problem {
128         public Warning(SAXParseException exception) {
129             super( exception);
130         }
131 
132         public Warning(TransformerException exception) {
133             super( exception);
134         }
135     }
136 
137     public class Error extends Problem {
138         public Error(SAXParseException exception) {
139             super( exception);
140         }
141 
142         public Error(TransformerException exception) {
143             super( exception);
144         }
145     }
146 
147     public class Fatal extends Problem {
148         public Fatal(SAXParseException exception) {
149             super( exception);
150         }
151 
152         public Fatal(TransformerException exception) {
153             super( exception);
154         }
155 
156         public Fatal(IOException exception) {
157             super( exception);
158         }
159     }
160 
161     public class Problem {
162         Exception exception = null;
163         
164         public Problem(Exception exception) {
165             this.exception = exception;
166         }
167         
168         public Exception getException() {
169             return exception;
170         }
171     }
172 
173     public class Message extends Problem {
174         String message = null;
175         String type = null;
176         
177         public Message(String type, String message) {
178             super(null);
179             this.type = type;
180             this.message = message;
181         }
182         
183         public String getType() {
184             return type;
185         }
186 
187         public String getMessage() {
188             return message;
189         }
190     }
191     
192     private class DummySourceLocator implements SourceLocator {
193         private URI uri = null;
194         
195         public DummySourceLocator(URI uri) {
196             this.uri = uri;
197         }
198 
199         public String getPublicId() {
200             return null;
201         }
202 
203         public String getSystemId() {
204             return URIUtils.toString(uri);
205         }
206 
207         public int getLineNumber() {
208             return -1;
209         }
210 
211         public int getColumnNumber() {
212             return -1;
213         }
214         
215     }
216 }