001: /*****************************************************************************
002: * Java Plug-in Framework (JPF)
003: * Copyright (C) 2004-2007 Dmitry Olshansky
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *****************************************************************************/package org.java.plugin.registry.xml;
019:
020: import java.io.IOException;
021: import java.util.LinkedList;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.xml.sax.EntityResolver;
026: import org.xml.sax.InputSource;
027: import org.xml.sax.SAXException;
028: import org.xml.sax.SAXParseException;
029: import org.xml.sax.helpers.DefaultHandler;
030:
031: /**
032: *
033: * @version $Id$
034: */
035: abstract class BaseHandler extends DefaultHandler {
036: protected final Log log = LogFactory.getLog(getClass());
037: protected final EntityResolver entityResolver;
038:
039: BaseHandler(final EntityResolver anEntityResolver) {
040: entityResolver = anEntityResolver;
041: }
042:
043: /**
044: * @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String,
045: * java.lang.String)
046: */
047: @Override
048: public InputSource resolveEntity(final String publicId,
049: final String systemId) throws SAXException {
050: if (entityResolver != null) {
051: try {
052: return entityResolver.resolveEntity(publicId, systemId);
053: } catch (SAXException se) {
054: throw se;
055: } catch (IOException ioe) {
056: throw new SAXException(
057: "I/O error has occurred - " + ioe, ioe); //$NON-NLS-1$
058: }
059: }
060: log.warn("ignoring publicId=" + publicId //$NON-NLS-1$
061: + " and systemId=" + systemId); //$NON-NLS-1$
062: return null;
063: }
064:
065: /**
066: * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)
067: */
068: @Override
069: public void warning(final SAXParseException e) {
070: log.warn("non-fatal error while parsing XML document", e); //$NON-NLS-1$
071: }
072:
073: /**
074: * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
075: */
076: @Override
077: public void error(final SAXParseException e) throws SAXException {
078: if (entityResolver != null) {
079: // we are in "validating" mode
080: log
081: .error(
082: "failed parsing XML resource in validating mode", e); //$NON-NLS-1$
083: throw e;
084: }
085: log.warn("ignoring parse error", e); //$NON-NLS-1$
086: }
087:
088: /**
089: * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)
090: */
091: @Override
092: public void fatalError(final SAXParseException e)
093: throws SAXException {
094: log.fatal("failed parsing XML resource", e); //$NON-NLS-1$
095: throw e;
096: }
097: }
098:
099: class SimpleStack<T> {
100: private LinkedList<T> data;
101:
102: SimpleStack() {
103: data = new LinkedList<T>();
104: }
105:
106: T pop() {
107: return data.isEmpty() ? null : data.removeLast();
108: }
109:
110: void push(final T obj) {
111: data.addLast(obj);
112: }
113:
114: int size() {
115: return data.size();
116: }
117: }
|