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

com.crabshue.commons.xml.validation.XmlWellFormednessChecker Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
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));
    }


}






© 2015 - 2024 Weber Informatics LLC | Privacy Policy