KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > java > plugin > registry > xml > BaseHandler


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

19 package org.java.plugin.registry.xml;
20
21 import java.io.IOException JavaDoc;
22 import java.util.LinkedList JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.xml.sax.EntityResolver JavaDoc;
27 import org.xml.sax.InputSource JavaDoc;
28 import org.xml.sax.SAXException JavaDoc;
29 import org.xml.sax.SAXParseException JavaDoc;
30 import org.xml.sax.helpers.DefaultHandler JavaDoc;
31
32 /**
33  *
34  * @version $Id: BaseHandler.java,v 1.2 2007/01/08 09:07:07 ddimon Exp $
35  */

36 abstract class BaseHandler extends DefaultHandler JavaDoc {
37     protected final Log log = LogFactory.getLog(getClass());
38     protected final EntityResolver JavaDoc entityResolver;
39
40     BaseHandler(final EntityResolver JavaDoc anEntityResolver) {
41         entityResolver = anEntityResolver;
42     }
43     
44     /**
45      * @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String,
46      * java.lang.String)
47      */

48     public InputSource JavaDoc resolveEntity(final String JavaDoc publicId,
49             final String JavaDoc systemId) throws SAXException JavaDoc {
50         if (entityResolver != null) {
51             try {
52                 return entityResolver.resolveEntity(publicId, systemId);
53             } catch (SAXException JavaDoc se) {
54                 throw se;
55             } catch (IOException JavaDoc ioe) {
56                 throw new SAXException JavaDoc("I/O error has occurred - " + ioe, ioe); //$NON-NLS-1$
57
}
58         }
59         log.warn("ignoring publicId=" + publicId //$NON-NLS-1$
60
+ " and systemId=" + systemId); //$NON-NLS-1$
61
return null;
62     }
63     
64     /**
65      * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)
66      */

67     public void warning(final SAXParseException JavaDoc e) {
68         log.warn("non-fatal error while parsing XML document", e); //$NON-NLS-1$
69
}
70     
71     /**
72      * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
73      */

74     public void error(final SAXParseException JavaDoc e) throws SAXException JavaDoc {
75         if (entityResolver != null) {
76             // we are in "validating" mode
77
log.error("failed parsing XML resource in validating mode", e); //$NON-NLS-1$
78
throw e;
79         }
80         log.warn("ignoring parse error", e); //$NON-NLS-1$
81
}
82     
83     /**
84      * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)
85      */

86     public void fatalError(final SAXParseException JavaDoc e) throws SAXException JavaDoc {
87         log.fatal("failed parsing XML resource", e); //$NON-NLS-1$
88
throw e;
89     }
90 }
91
92 class SimpleStack {
93     private LinkedList JavaDoc data;
94     
95     SimpleStack() {
96         data = new LinkedList JavaDoc();
97     }
98     
99     Object JavaDoc pop() {
100         return data.isEmpty() ? null : data.removeLast();
101     }
102     
103     void push(final Object JavaDoc obj) {
104         data.addLast(obj);
105     }
106     
107     int size() {
108         return data.size();
109     }
110 }
111
Popular Tags