com.crabshue.commons.xml.schema.XmlSchemaUtils 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.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;
}
}