All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.crabshue.commons.xml.sax.SaxParsingExecutor Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
package com.crabshue.commons.xml.sax;

import java.io.File;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.helpers.DefaultHandler;

import com.crabshue.commons.exceptions.ApplicationException;
import com.crabshue.commons.exceptions.SystemException;
import com.crabshue.commons.xml.exceptions.XmlErrorContext;
import com.crabshue.commons.xml.exceptions.XmlErrorType;

/**
 * Utility class for executing XML SAX parsing.
 *
 */
public class SaxParsingExecutor {

    /**
     * Parse an XML file using a SAX parser, with a given SAX handler.
     *
     * @param xmlFile    the XML file.
     * @param saxHandler the SAX handler.
     */
    public static void parseXmlFile(final File xmlFile, final DefaultHandler saxHandler) {
        try {
            SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
            saxParser.parse(xmlFile, saxHandler);
        } catch (ApplicationException e) {
            throw e;
        } catch (Exception e) {
            throw new SystemException(XmlErrorType.ERROR_PARSING_XML, "Cannot parse xml file with sax parser", e)
                .addContextValue(XmlErrorContext.XML_FILE, xmlFile)
                .addContextValue("saxHandler", saxHandler);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy