com.crabshue.commons.xml.validation.XmlWellFormednessChecker Maven / Gradle / Ivy
package com.crabshue.commons.xml.validation;
import java.io.File;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.InputSource;
import com.crabshue.commons.exceptions.ApplicationException;
import com.crabshue.commons.exceptions.ValidationException;
import com.crabshue.commons.xml.XmlDocumentBuilder;
import com.crabshue.commons.xml.exceptions.XmlErrorType;
import com.crabshue.commons.xml.inputsource.InputSourceBuilder;
/**
* XML well-formedness checker.
*
*/
public class XmlWellFormednessChecker {
private static Logger logger = LoggerFactory.getLogger(XmlWellFormednessChecker.class);
/**
* Check that an {@link InputSource XML document} is well-formed.
*
* @param inputSource the input source for the XMl document.
* @throws ValidationException if the XML document is not well-formed.
*/
public static void checkXmlIsWellFormed(final InputSource inputSource) {
try {
XmlDocumentBuilder.of(inputSource)
.withNamespaceAwareness(false)
.withValidationEnabled(false)
.build();
} catch (ApplicationException e) {
throw new ValidationException(XmlErrorType.XML_NOT_WELL_FORMED, "xml is not well-formed", e);
}
}
/**
* Check that an {@link File XML document} is well-formed.
*
* @param xmlFile the XML file
* @see #checkXmlIsWellFormed(InputSource)
*/
public static void checkXmlIsWellFormed(final File xmlFile) {
XmlWellFormednessChecker.checkXmlIsWellFormed(InputSourceBuilder.newInputSource(xmlFile));
}
}