org.opensaml.xml.schema.SchemaBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xmltooling Show documentation
Show all versions of xmltooling Show documentation
XMLTooling-J is a low-level library that may be used to construct libraries that allow developers to work with
XML in a Java beans manner.
The newest version!
/*
* Licensed to the University Corporation for Advanced Internet Development,
* Inc. (UCAID) under one or more contributor license agreements. See the
* NOTICE file distributed with this work for additional information regarding
* copyright ownership. The UCAID licenses this file to You under the Apache
* License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.opensaml.xml.schema;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.opensaml.xml.parse.LoggingErrorHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.SAXException;
/** A helper class for building {@link Schema} from a set of input. */
public final class SchemaBuilder {
/** Language of the schema files. */
public static enum SchemaLanguage {
/** W3 XML Schema. */
XML("xsd"),
/** OASIS RELAX NG Schema. */
RELAX("rng");
/** File extension used for the schema files. */
private String schemaFileExtension;
/**
* Constructor.
*
* @param extension file extension used for the schema files
*/
private SchemaLanguage(String extension) {
schemaFileExtension = extension;
}
/**
* Gets the file extension used for the schema files.
*
* @return file extension used for the schema files
*/
public String getSchemaFileExtension() {
return schemaFileExtension;
}
};
/** Constructor. */
private SchemaBuilder() {}
/**
* Builds a schema from the given schema source.
*
* @param lang schema language, must not be null
* @param schemaFileOrDirectory file or directory which contains schema sources
*
* @return the constructed schema
*
* @throws SAXException thrown if there is a problem converting the schema sources in to a schema
*/
public static Schema buildSchema(SchemaLanguage lang, String schemaFileOrDirectory) throws SAXException {
if(schemaFileOrDirectory == null){
return null;
}
return buildSchema(lang, new File(schemaFileOrDirectory));
}
/**
* Builds a schema from the given schema sources.
*
* @param lang schema language, must not be null
* @param schemaFilesOrDirectories files or directories which contains schema sources
*
* @return the constructed schema
*
* @throws SAXException thrown if there is a problem converting the schema sources in to a schema
*/
public static Schema buildSchema(SchemaLanguage lang, String[] schemaFilesOrDirectories) throws SAXException {
if(schemaFilesOrDirectories == null || schemaFilesOrDirectories.length == 0){
return null;
}
return buildSchema(lang, schemaFilesOrDirectories);
}
/**
* Builds a schema from the given schema source.
*
* @param lang schema language, must not be null
* @param schemaFileOrDirectory file or directory which contains schema sources
*
* @return the constructed schema
*
* @throws SAXException thrown if there is a problem converting the schema sources in to a schema
*/
public static Schema buildSchema(SchemaLanguage lang, File schemaFileOrDirectory) throws SAXException {
if(schemaFileOrDirectory == null){
return null;
}
return buildSchema(lang, new File[]{schemaFileOrDirectory});
}
/**
* Builds a schema from the given schema sources.
*
* @param lang schema language, must not be null
* @param schemaFilesOrDirectories files or directories which contains schema sources
*
* @return the constructed schema
*
* @throws SAXException thrown if there is a problem converting the schema sources in to a schema
*/
public static Schema buildSchema(SchemaLanguage lang, File[] schemaFilesOrDirectories) throws SAXException {
if(schemaFilesOrDirectories == null || schemaFilesOrDirectories.length == 0){
return null;
}
ArrayList schemaFiles = new ArrayList();
getSchemaFiles(lang, schemaFilesOrDirectories, schemaFiles);
if(schemaFiles.isEmpty()){
return null;
}
ArrayList