com.crabshue.commons.xml.validation.XmlWellFormednessChecker 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.validation;
import java.io.File;
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;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
/**
* XML well-formedness checker.
*/
@Slf4j
public class XmlWellFormednessChecker {
/**
* 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(@NonNull 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(@NonNull final File xmlFile) {
XmlWellFormednessChecker.checkXmlIsWellFormed(InputSourceBuilder.newInputSource(xmlFile));
}
}