KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > xml > JBossErrorHandler


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util.xml;
23
24 import javax.xml.transform.ErrorListener JavaDoc;
25 import javax.xml.transform.TransformerException JavaDoc;
26
27 import org.jboss.logging.Logger;
28 import org.jboss.util.JBossStringBuilder;
29 import org.xml.sax.ErrorHandler JavaDoc;
30 import org.xml.sax.SAXParseException JavaDoc;
31
32 public class JBossErrorHandler implements ErrorHandler JavaDoc, ErrorListener JavaDoc
33 {
34    private static final Logger log =Logger.getLogger(JBossErrorHandler.class);
35    
36    // The xml file being parsed
37
private String JavaDoc fileName;
38    private JBossEntityResolver resolver;
39    private boolean error;
40    
41    public JBossErrorHandler(String JavaDoc fileName, JBossEntityResolver resolver)
42    {
43       this.fileName = fileName;
44       this.resolver = resolver;
45       this.error = false;
46    }
47    
48    public void error(SAXParseException JavaDoc e)
49    {
50       if (resolver == null || resolver.isEntityResolved())
51       {
52          error = true;
53          log.error(formatError("error", e));
54       }
55    }
56    
57    public void fatalError(SAXParseException JavaDoc e)
58    {
59       if (resolver == null || resolver.isEntityResolved())
60       {
61          error = true;
62          log.error(formatError("fatal", e));
63       }
64    }
65    
66    public void warning(SAXParseException JavaDoc e)
67    {
68       if (resolver == null || resolver.isEntityResolved())
69       {
70          error = true;
71          log.error(formatError("warning", e));
72       }
73    }
74    
75    public void error(TransformerException JavaDoc e)
76    {
77       if (resolver == null || resolver.isEntityResolved())
78       {
79          error = true;
80          log.error(formatError("error", e));
81       }
82    }
83    
84    public void fatalError(TransformerException JavaDoc e)
85    {
86       if (resolver == null || resolver.isEntityResolved())
87       {
88          error = true;
89          log.error(formatError("fatal", e));
90       }
91    }
92    
93    public void warning(TransformerException JavaDoc e)
94    {
95       if (resolver == null || resolver.isEntityResolved())
96       {
97          error = true;
98          log.error(formatError("warning", e));
99       }
100    }
101
102    protected String JavaDoc formatError(String JavaDoc context, SAXParseException JavaDoc e)
103    {
104       JBossStringBuilder buffer = new JBossStringBuilder();
105       buffer.append("File ").append(fileName);
106       buffer.append(" process ").append(context);
107       buffer.append(". Line: ").append(e.getLineNumber());
108       buffer.append(". Error message: ").append(e.getMessage());
109       return buffer.toString();
110    }
111
112    protected String JavaDoc formatError(String JavaDoc context, TransformerException JavaDoc e)
113    {
114       JBossStringBuilder buffer = new JBossStringBuilder();
115       buffer.append("File ").append(fileName);
116       buffer.append(" process ").append(context);
117       buffer.append(". Location: ").append(e.getLocationAsString());
118       buffer.append(". Error message: ").append(e.getMessage());
119       return buffer.toString();
120    }
121    
122    public boolean hadError()
123    {
124       return error;
125    }
126 }
127
Popular Tags