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

com.crabshue.commons.xml.schema.XmlSchemaUtils Maven / Gradle / Ivy

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

import java.io.File;
import java.net.URL;

import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.crabshue.commons.xml.sax.SaxParsingExecutor;

/**
 * Utility class for XML schema operations.
 */
public class XmlSchemaUtils {

    private static Logger logger = LoggerFactory.getLogger(XmlSchemaUtils.class);

    protected XmlSchemaUtils() {

    }

    /**
     * Parse an {@link File XML file} and extract its schema location ({@code xsi:schemaLocation} or {@code xsi:noNamespaceSchemaLocation}).
     *
     * @param xmlFile the XML file
     * @return the URL of the schema location
     */
    public static URL extractSchemaUrl(final File xmlFile) {
        Validate.notNull(xmlFile);

        final File parentFolder = xmlFile.getParentFile();
        return extractSchemaUrl(xmlFile, parentFolder);
    }

    /**
     * Parse an {@link File XML file} and extract its schema location ({@code xsi:schemaLocation} or {@code xsi:noNamespaceSchemaLocation}).
     *
     * @param xmlFile      the XML file
     * @param schemaFolder the base folder containing the schemas (in case schema is located on file system)
     * @return the URL of the schema location
     */
    public static URL extractSchemaUrl(final File xmlFile, final File schemaFolder) {
        Validate.notNull(xmlFile);
        Validate.notNull(schemaFolder);

        final SchemaLocationSaxHandler schemaLocationHandler = new SchemaLocationSaxHandler(schemaFolder);
        SaxParsingExecutor.parseXmlFile(xmlFile, schemaLocationHandler);
        final URL ret = schemaLocationHandler.getSchemaUrl();
        logger.info("Schema of XML file [{}] is located at [{}]", xmlFile, ret);
        return ret;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy