com.crabshue.commons.xml.sax.SaxParsingExecutor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-xml Show documentation
Show all versions of commons-xml Show documentation
Library for XML documents operations.
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);
}
}
}