|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| ConfigValidatorFactory.java | 100% | 94.1% | 100% | 96.6% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* Copyright (C) The Spice Group. All rights reserved.
|
|
| 3 |
*
|
|
| 4 |
* This software is published under the terms of the Spice
|
|
| 5 |
* Software License version 1.1, a copy of which has been included
|
|
| 6 |
* with this distribution in the LICENSE.txt file.
|
|
| 7 |
*/
|
|
| 8 |
package org.codehaus.spice.configkit;
|
|
| 9 |
|
|
| 10 |
import java.io.InputStream;
|
|
| 11 |
import org.iso_relax.verifier.Schema;
|
|
| 12 |
import org.iso_relax.verifier.VerifierConfigurationException;
|
|
| 13 |
import org.iso_relax.verifier.VerifierFactory;
|
|
| 14 |
import org.xml.sax.EntityResolver;
|
|
| 15 |
import org.xml.sax.InputSource;
|
|
| 16 |
|
|
| 17 |
/**
|
|
| 18 |
* The ConfigValidatorFactory is responsible for creating ConfigValidator
|
|
| 19 |
* objects to validate configuration according to specified schemas.
|
|
| 20 |
*
|
|
| 21 |
* @author Peter Donald
|
|
| 22 |
* @version $Revision: 1.1 $ $Date: 2003/12/03 03:19:28 $
|
|
| 23 |
*/
|
|
| 24 |
public final class ConfigValidatorFactory |
|
| 25 |
{
|
|
| 26 |
/** A constant defining namespace of RELAX_NG schema language. */
|
|
| 27 |
public static final String RELAX_NG = "http://relaxng.org/ns/structure/1.0"; |
|
| 28 |
|
|
| 29 |
/** A constant defining namespace of W3C XMLSchema language. */
|
|
| 30 |
public static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema"; |
|
| 31 |
|
|
| 32 |
/** The Class object that points at MSV class. */
|
|
| 33 |
private static Class c_msvClass; |
|
| 34 |
|
|
| 35 |
/**
|
|
| 36 |
* Create a ConfigValidator and attempt to guess Schema Type. The config
|
|
| 37 |
* validator loads the schema from specified publicID, systemID, classloader
|
|
| 38 |
* combination using the {@link ResolverFactory}.
|
|
| 39 |
*
|
|
| 40 |
* @param publicID the publicID of schema (may be null)
|
|
| 41 |
* @param systemID the systemID of schema (may be null)
|
|
| 42 |
* @param classLoader the classloader from which to load schema
|
|
| 43 |
* @return the ConfigValidatorthat conforms to input
|
|
| 44 |
* @throws Exception if unable to create validator
|
|
| 45 |
*/
|
|
| 46 | 3 |
public static ConfigValidator create( final String publicID, |
| 47 |
final String systemID, |
|
| 48 |
final ClassLoader classLoader ) |
|
| 49 |
throws Exception
|
|
| 50 |
{
|
|
| 51 | 3 |
return create( null, publicID, systemID, classLoader ); |
| 52 |
} |
|
| 53 |
|
|
| 54 |
/**
|
|
| 55 |
* Create a ConfigValidator with specified type. The config validator loads
|
|
| 56 |
* the schema from specified publicID, systemID, classloader combination
|
|
| 57 |
* using the {@link ResolverFactory}.
|
|
| 58 |
*
|
|
| 59 |
* @param schemaType the type of the schema. (Usually a URL such as
|
|
| 60 |
* "http://relaxng.org/ns/structure/1.0")
|
|
| 61 |
* @param publicID the publicID of schema (may be null)
|
|
| 62 |
* @param systemID the systemID of schema (may be null)
|
|
| 63 |
* @param classLoader the classloader from which to load schema
|
|
| 64 |
* @return the ConfigValidatorthat conforms to input
|
|
| 65 |
* @throws Exception if unable to create validator
|
|
| 66 |
*/
|
|
| 67 | 8 |
public static ConfigValidator create( final String schemaType, |
| 68 |
final String publicID, |
|
| 69 |
final String systemID, |
|
| 70 |
final ClassLoader classLoader ) |
|
| 71 |
throws Exception
|
|
| 72 |
{
|
|
| 73 | 8 |
if( null == publicID && null == systemID ) |
| 74 |
{
|
|
| 75 | 1 |
throw new NullPointerException( "publicID" ); |
| 76 |
} |
|
| 77 |
|
|
| 78 | 7 |
final EntityResolver resolver = ResolverFactory.createResolver( |
| 79 |
classLoader ); |
|
| 80 | 6 |
final InputSource inputSource = resolver.resolveEntity( publicID, |
| 81 |
systemID ); |
|
| 82 | 6 |
if( null == inputSource ) |
| 83 |
{
|
|
| 84 | 1 |
final String message = |
| 85 |
"Unable to locate schema with id " + publicID + "/" + systemID; |
|
| 86 | 1 |
throw new Exception( message ); |
| 87 |
} |
|
| 88 |
|
|
| 89 | 5 |
return create( schemaType, inputSource, resolver );
|
| 90 |
} |
|
| 91 |
|
|
| 92 |
/**
|
|
| 93 |
* Create a ConfigValidator and guess Schema type. The schema is loaded from
|
|
| 94 |
* specified stream.
|
|
| 95 |
*
|
|
| 96 |
* @param inputStream the stream to load schema from
|
|
| 97 |
* @return the ConfigValidatorthat conforms to input
|
|
| 98 |
* @throws Exception if unable to create validator
|
|
| 99 |
*/
|
|
| 100 | 1 |
public static ConfigValidator create( final InputStream inputStream ) |
| 101 |
throws Exception
|
|
| 102 |
{
|
|
| 103 | 1 |
return create( null, inputStream ); |
| 104 |
} |
|
| 105 |
|
|
| 106 |
/**
|
|
| 107 |
* Create a ConfigValidator with specified type. The schema is loaded from
|
|
| 108 |
* specified stream.
|
|
| 109 |
*
|
|
| 110 |
* @param schemaType the type of the schema. (Usually a URL such as
|
|
| 111 |
* "http://relaxng.org/ns/structure/1.0")
|
|
| 112 |
* @param inputStream the stream to load schema from
|
|
| 113 |
* @return the ConfigValidatorthat conforms to input
|
|
| 114 |
* @throws Exception if unable to create validator
|
|
| 115 |
*/
|
|
| 116 | 3 |
public static ConfigValidator create( final String schemaType, |
| 117 |
final InputStream inputStream ) |
|
| 118 |
throws Exception
|
|
| 119 |
{
|
|
| 120 | 3 |
if( null == inputStream ) |
| 121 |
{
|
|
| 122 | 1 |
throw new NullPointerException( "inputStream" ); |
| 123 |
} |
|
| 124 |
|
|
| 125 | 2 |
final InputSource inputSource = new InputSource( inputStream );
|
| 126 | 2 |
return create( schemaType, inputSource );
|
| 127 |
} |
|
| 128 |
|
|
| 129 |
/**
|
|
| 130 |
* Create a ConfigValidator with specified type. The schema is loaded from
|
|
| 131 |
* specified stream.
|
|
| 132 |
*
|
|
| 133 |
* @param schemaType the type of the schema. (Usually a URL such as
|
|
| 134 |
* "http://relaxng.org/ns/structure/1.0")
|
|
| 135 |
* @param inputStream the stream to load schema from
|
|
| 136 |
* @param resolver a resolver used to resolve entitys for input data
|
|
| 137 |
* @return the ConfigValidatorthat conforms to input
|
|
| 138 |
* @throws Exception if unable to create validator
|
|
| 139 |
*/
|
|
| 140 | 2 |
public static ConfigValidator create( final String schemaType, |
| 141 |
final InputStream inputStream, |
|
| 142 |
final EntityResolver resolver ) |
|
| 143 |
throws Exception
|
|
| 144 |
{
|
|
| 145 | 2 |
if( null == inputStream ) |
| 146 |
{
|
|
| 147 | 1 |
throw new NullPointerException( "inputStream" ); |
| 148 |
} |
|
| 149 |
|
|
| 150 | 1 |
final InputSource inputSource = new InputSource( inputStream );
|
| 151 | 1 |
return create( schemaType, inputSource, resolver );
|
| 152 |
} |
|
| 153 |
|
|
| 154 |
/**
|
|
| 155 |
* Create a ConfigValidator and guess Schema type. The schema is loaded from
|
|
| 156 |
* specified source.
|
|
| 157 |
*
|
|
| 158 |
* @param inputSource the source to load schema from
|
|
| 159 |
* @return the ConfigValidatorthat conforms to input
|
|
| 160 |
* @throws Exception if unable to create validator
|
|
| 161 |
*/
|
|
| 162 | 1 |
public static ConfigValidator create( final InputSource inputSource ) |
| 163 |
throws Exception
|
|
| 164 |
{
|
|
| 165 | 1 |
return create( null, inputSource, null ); |
| 166 |
} |
|
| 167 |
|
|
| 168 |
/**
|
|
| 169 |
* Create a ConfigValidator and guess Schema type. The schema is loaded from
|
|
| 170 |
* specified source.
|
|
| 171 |
*
|
|
| 172 |
* @param inputSource the source to load schema from
|
|
| 173 |
* @param resolver a resolver used to resolve entitys for input data
|
|
| 174 |
* @return the ConfigValidatorthat conforms to input
|
|
| 175 |
* @throws Exception if unable to create validator
|
|
| 176 |
*/
|
|
| 177 | 1 |
public static ConfigValidator create( final InputSource inputSource, |
| 178 |
final EntityResolver resolver ) |
|
| 179 |
throws Exception
|
|
| 180 |
{
|
|
| 181 | 1 |
return create( null, inputSource, resolver ); |
| 182 |
} |
|
| 183 |
|
|
| 184 |
/**
|
|
| 185 |
* Create a ConfigValidator with specified type. The schema is loaded from
|
|
| 186 |
* specified source.
|
|
| 187 |
*
|
|
| 188 |
* @param schemaType the type of the schema. (Usually a URL such as
|
|
| 189 |
* "http://relaxng.org/ns/structure/1.0")
|
|
| 190 |
* @param inputSource the source to load schema from
|
|
| 191 |
* @return the ConfigValidatorthat conforms to input
|
|
| 192 |
* @throws Exception if unable to create validator
|
|
| 193 |
*/
|
|
| 194 | 5 |
public static ConfigValidator create( final String schemaType, |
| 195 |
final InputSource inputSource ) |
|
| 196 |
throws Exception
|
|
| 197 |
{
|
|
| 198 | 5 |
return create( schemaType, inputSource, null ); |
| 199 |
} |
|
| 200 |
|
|
| 201 |
/**
|
|
| 202 |
* Create a ConfigValidator with specified type. The schema is loaded from
|
|
| 203 |
* specified source. Also specified is entity resolver used when loading
|
|
| 204 |
* files to validate.
|
|
| 205 |
*
|
|
| 206 |
* @param schemaType the type of the schema. (Usually a URL such as
|
|
| 207 |
* "http://relaxng.org/ns/structure/1.0")
|
|
| 208 |
* @param inputSource the source to load schema from
|
|
| 209 |
* @param entityResolver a resolver used to resolve entitys for input data
|
|
| 210 |
* @return the ConfigValidatorthat conforms to input
|
|
| 211 |
* @throws Exception if unable to create validator
|
|
| 212 |
*/
|
|
| 213 | 13 |
public static ConfigValidator create( final String schemaType, |
| 214 |
final InputSource inputSource, |
|
| 215 |
final EntityResolver entityResolver ) |
|
| 216 |
throws Exception
|
|
| 217 |
{
|
|
| 218 | 13 |
if( null == inputSource ) |
| 219 |
{
|
|
| 220 | 1 |
throw new NullPointerException( "inputSource" ); |
| 221 |
} |
|
| 222 | 12 |
final VerifierFactory factory = createFactory( schemaType ); |
| 223 | 12 |
final Schema schema = factory.compileSchema( inputSource ); |
| 224 | 12 |
return new ConfigValidator( schema, entityResolver ); |
| 225 |
} |
|
| 226 |
|
|
| 227 |
/**
|
|
| 228 |
* Create a VerifierFactory according to specified schemaType. If the
|
|
| 229 |
* schemaType is not specified then
|
|
| 230 |
*
|
|
| 231 |
* against DTDs, XML Schema, RelaxNG, Relax or TREX.
|
|
| 232 |
*
|
|
| 233 |
* @param schemaType the type of schema (may be null)
|
|
| 234 |
* @return the VerifierFactory created
|
|
| 235 |
* @throws VerifierConfigurationException if unable to get factory
|
|
| 236 |
*/
|
|
| 237 | 12 |
private static VerifierFactory createFactory( final String schemaType ) |
| 238 |
throws VerifierConfigurationException
|
|
| 239 |
{
|
|
| 240 | 12 |
if( null == schemaType ) |
| 241 |
{
|
|
| 242 | 7 |
try
|
| 243 |
{
|
|
| 244 | 7 |
if( null == c_msvClass ) |
| 245 |
{
|
|
| 246 | 2 |
c_msvClass = |
| 247 |
Class.forName( "com.sun.msv.verifier.jarv.TheFactoryImpl" );
|
|
| 248 |
} |
|
| 249 | 7 |
return (VerifierFactory)c_msvClass.newInstance();
|
| 250 |
} |
|
| 251 |
catch( final Exception e )
|
|
| 252 |
{
|
|
| 253 | 0 |
final String message = |
| 254 |
"Unable to load MSV factory and thus can " +
|
|
| 255 |
"not auto-discover schema type.";
|
|
| 256 | 0 |
throw new VerifierConfigurationException( message, e ); |
| 257 |
} |
|
| 258 |
} |
|
| 259 |
else
|
|
| 260 |
{
|
|
| 261 | 5 |
return VerifierFactory.newInstance( schemaType );
|
| 262 |
} |
|
| 263 |
} |
|
| 264 |
} |
|
| 265 |
|
|
||||||||||