001: /**
002: * Copyright (C) 2006, 2007 David Bulmore, Software Sensation Inc.
003: * All Rights Reserved.
004: *
005: * This file is part of jCommonTk.
006: *
007: * jCommonTk is free software; you can redistribute it and/or modify it under
008: * the terms of the GNU General Public License (Version 2) as published by
009: * the Free Software Foundation.
010: *
011: * jCommonTk is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with jCommonTk; if not, write to the Free Software Foundation,
018: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
019: */package jcommontk.utils;
020:
021: import java.io.InputStream;
022: import java.net.URL;
023: import java.util.logging.Level;
024: import java.util.logging.Logger;
025: import javax.xml.parsers.DocumentBuilder;
026: import javax.xml.parsers.DocumentBuilderFactory;
027: import org.w3c.dom.Document;
028: import org.w3c.dom.Element;
029: import org.xml.sax.ErrorHandler;
030: import org.xml.sax.SAXParseException;
031:
032: public abstract class XMLParser {
033: private static Logger logger = Logger.getLogger(XMLParser.class
034: .getName());
035:
036: protected XMLParser() {
037: }
038:
039: protected XMLParser(String filename, boolean validate)
040: throws XMLParserException {
041: loadData(ClassLoader.getSystemResourceAsStream(filename),
042: validate);
043: }
044:
045: protected XMLParser(URL fileUrl, boolean validate)
046: throws XMLParserException {
047: loadData(fileUrl, validate);
048: }
049:
050: protected void loadData(URL fileUrl, boolean validate)
051: throws XMLParserException {
052: try {
053: loadData(fileUrl.openStream(), validate);
054: } catch (Exception e) {
055: throw new XMLParserException(e);
056: }
057: }
058:
059: protected void loadData(InputStream in, boolean validate)
060: throws XMLParserException {
061: try {
062: DocumentBuilderFactory f = DocumentBuilderFactory
063: .newInstance();
064:
065: f.setValidating(validate);
066:
067: DocumentBuilder db = f.newDocumentBuilder();
068:
069: db.setErrorHandler(new XMLErrorHandler());
070:
071: Document d = db.parse(in);
072:
073: in.close();
074:
075: Element e = d.getDocumentElement();
076:
077: processXML(e);
078: } catch (Exception ex) {
079: throw new XMLParserException(ex);
080: }
081: }
082:
083: static class XMLErrorHandler implements ErrorHandler {
084: public void error(SAXParseException exception) {
085: logger.log(Level.SEVERE, "Error: at line "
086: + exception.getLineNumber() + ", column "
087: + exception.getColumnNumber(), exception);
088: exception.printStackTrace(System.err);
089: }
090:
091: public void fatalError(SAXParseException exception) {
092: logger.log(Level.SEVERE, "Error: at line "
093: + exception.getLineNumber() + ", column "
094: + exception.getColumnNumber(), exception);
095: exception.printStackTrace(System.err);
096: }
097:
098: public void warning(SAXParseException exception) {
099: logger.log(Level.SEVERE, "Error: at line "
100: + exception.getLineNumber() + ", column "
101: + exception.getColumnNumber(), exception);
102: exception.printStackTrace(System.err);
103: }
104: }
105:
106: protected abstract void processXML(Element e) throws Exception;
107: }
|