[jboss-svn-commits] JBoss Common SVN: r3360 - jbossxb/trunk/src/main/java/org/jboss/xb/util.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu Jul 9 09:16:23 EDT 2009
Author: alesj
Date: 2009-07-09 09:16:22 -0400 (Thu, 09 Jul 2009)
New Revision: 3360
Added:
jbossxb/trunk/src/main/java/org/jboss/xb/util/AbstractSchemaBindingValidator.java
Modified:
jbossxb/trunk/src/main/java/org/jboss/xb/util/DefaultSchemaBindingValidator.java
jbossxb/trunk/src/main/java/org/jboss/xb/util/SchemaBindingValidator.java
Log:
[JBXB-213]; refactor SBV a bit more.
Copied: jbossxb/trunk/src/main/java/org/jboss/xb/util/AbstractSchemaBindingValidator.java (from rev 3358, jbossxb/trunk/src/main/java/org/jboss/xb/util/DefaultSchemaBindingValidator.java)
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/util/AbstractSchemaBindingValidator.java (rev 0)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/util/AbstractSchemaBindingValidator.java 2009-07-09 13:16:22 UTC (rev 3360)
@@ -0,0 +1,208 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.xb.util;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+import javax.xml.namespace.QName;
+
+import org.apache.xerces.xs.XSModel;
+import org.jboss.logging.Logger;
+import org.jboss.xb.binding.Util;
+import org.jboss.xb.binding.resolver.MultiClassSchemaResolver;
+import org.jboss.xb.binding.sunday.unmarshalling.SchemaBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.SchemaBindingResolver;
+import org.xml.sax.InputSource;
+
+/**
+ * Mostly a holder for excluded and validated types, namespaces.
+ *
+ * @author <a href="alex at jboss.com">Alexey Loubyansky</a>
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
+ * @version $Revision: 1.1 $
+ */
+public abstract class AbstractSchemaBindingValidator implements SchemaBindingValidator
+{
+ protected static final Logger log = Logger.getLogger(SchemaBindingValidator.class);
+
+ protected static final QName WILDCARD = new QName("wildcard", "wildcard");
+
+ protected Set<String> excludedNs = new HashSet<String>();
+ protected Set<QName> excludedTypes = new HashSet<QName>();
+
+ protected Set<QName> validatedTypes = new HashSet<QName>();
+ protected Set<QName> validatedElements = new HashSet<QName>();
+
+ private SchemaBindingResolver resolver;
+
+ private boolean loggingEnabled;
+
+ protected AbstractSchemaBindingValidator()
+ {
+ }
+
+ /**
+ * @param resolver default schema resolver
+ */
+ protected AbstractSchemaBindingValidator(SchemaBindingResolver resolver)
+ {
+ this();
+ this.resolver = resolver;
+ }
+
+ /**
+ * Resets instance variables (such as a set of validated types, elements and also loggingEnabled property).
+ * This method is required to invoked before another validation.
+ * It is called internally at the end of validate(XSModel xsSchema, SchemaBinding schemaBinding).
+ * NOTE: this method doesn't clear excluded namespaces and types.
+ */
+ public void reset()
+ {
+ loggingEnabled = log.isTraceEnabled();
+ validatedTypes.clear();
+ validatedElements.clear();
+ }
+
+ public boolean isLoggingEnabled()
+ {
+ return loggingEnabled;
+ }
+
+ public void enableLogging(boolean value)
+ {
+ loggingEnabled = value;
+ }
+
+ public void excludeNs(String ns)
+ {
+ excludedNs.add(ns);
+ }
+
+ public boolean isNsExcluded(String ns)
+ {
+ return excludedNs.contains(ns);
+ }
+
+ public void includeNs(String ns)
+ {
+ excludedNs.remove(ns);
+ }
+
+ public void excludeType(QName qName)
+ {
+ excludedTypes.add(qName);
+ }
+
+ public boolean isTypeExcluded(QName qName)
+ {
+ return excludedTypes.contains(qName);
+ }
+
+ public void includeType(QName qName)
+ {
+ excludedTypes.remove(qName);
+ }
+
+ /**
+ * @return The default resolver used to resolve schemas
+ */
+ public SchemaBindingResolver getSchemaResolver()
+ {
+ return resolver;
+ }
+
+ /**
+ * @param resolver The default resolver used to resolve schemas
+ */
+ public void setSchemaResolver(SchemaBindingResolver resolver)
+ {
+ this.resolver = resolver;
+ }
+
+ public void validate(InputSource is, SchemaBinding binding)
+ {
+ SchemaBindingResolver resolver = binding.getSchemaResolver();
+ if(resolver == null)
+ {
+ resolver = this.resolver;
+ if(resolver == null)
+ log("Schema resolver was not provided");
+ }
+ XSModel xsModel = Util.loadSchema(is, resolver);
+ validate(xsModel, binding);
+ }
+
+ public void validate(String xsdName, Class<?>... cls)
+ {
+ log("validate: " + xsdName + ", " + Arrays.asList(cls));
+
+ URL xsdUrl = Thread.currentThread().getContextClassLoader().getResource("schema/" + xsdName);
+ if(xsdUrl == null)
+ handleError("Failed to load schema from the classpath: schema/" + xsdName);
+
+ MultiClassSchemaResolver multiClassResolver = new MultiClassSchemaResolver();
+ multiClassResolver.mapLocationToClasses(xsdName, cls);
+ SchemaBinding binding = resolver.resolve("", null, xsdName);
+
+ SchemaBindingResolver resolver = this.resolver;
+ if(resolver == null)
+ resolver = multiClassResolver;
+
+ XSModel xsModel;
+ try
+ {
+ xsModel = Util.loadSchema(xsdUrl.openStream(), null, resolver);
+ }
+ catch (IOException e)
+ {
+ throw new IllegalStateException("Failed to read schema " + xsdName, e);
+ }
+
+ validate(xsModel, binding);
+ }
+
+ protected abstract void validate(XSModel xsSchema, SchemaBinding schemaBinding);
+
+ /**
+ * This an error handler method. Default implementation throws IllegalStateException with the message passed in as the argument.
+ *
+ * @param msg the error message
+ */
+ protected void handleError(String msg)
+ {
+ throw new IllegalStateException(msg);
+ }
+
+ /**
+ * This method is supposed to log a message. Default implementation uses trace logging.
+ *
+ * @param msg the message to log.
+ */
+ protected void log(String msg)
+ {
+ if(loggingEnabled)
+ log.trace(msg);
+ }
+}
\ No newline at end of file
Modified: jbossxb/trunk/src/main/java/org/jboss/xb/util/DefaultSchemaBindingValidator.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/util/DefaultSchemaBindingValidator.java 2009-07-09 12:44:41 UTC (rev 3359)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/util/DefaultSchemaBindingValidator.java 2009-07-09 13:16:22 UTC (rev 3360)
@@ -21,18 +21,12 @@
*/
package org.jboss.xb.util;
-import java.io.IOException;
-import java.net.URL;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
-import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
-import java.util.Set;
-
import javax.xml.namespace.QName;
import org.apache.xerces.xs.XSAttributeDeclaration;
@@ -49,10 +43,6 @@
import org.apache.xerces.xs.XSTerm;
import org.apache.xerces.xs.XSTypeDefinition;
import org.apache.xerces.xs.XSWildcard;
-import org.jboss.logging.Logger;
-import org.jboss.xb.binding.Constants;
-import org.jboss.xb.binding.Util;
-import org.jboss.xb.binding.resolver.MultiClassSchemaResolver;
import org.jboss.xb.binding.sunday.unmarshalling.AllBinding;
import org.jboss.xb.binding.sunday.unmarshalling.AttributeBinding;
import org.jboss.xb.binding.sunday.unmarshalling.ChoiceBinding;
@@ -60,13 +50,13 @@
import org.jboss.xb.binding.sunday.unmarshalling.ModelGroupBinding;
import org.jboss.xb.binding.sunday.unmarshalling.ParticleBinding;
import org.jboss.xb.binding.sunday.unmarshalling.SchemaBinding;
-import org.jboss.xb.binding.sunday.unmarshalling.SchemaBindingResolver;
import org.jboss.xb.binding.sunday.unmarshalling.SequenceBinding;
import org.jboss.xb.binding.sunday.unmarshalling.TermBinding;
import org.jboss.xb.binding.sunday.unmarshalling.TypeBinding;
import org.jboss.xb.binding.sunday.unmarshalling.UnorderedSequenceBinding;
import org.jboss.xb.binding.sunday.unmarshalling.WildcardBinding;
-import org.xml.sax.InputSource;
+import org.jboss.xb.binding.sunday.unmarshalling.SchemaBindingResolver;
+import org.jboss.xb.binding.Constants;
/**
* This class is used to check consistency between SchemaBinding instances and their corresponding XSD schemas.
@@ -96,192 +86,24 @@
* @author <a href="ales.justin at jboss.com">Ales Justin</a>
* @version $Revision: 1.1 $
*/
-public class DefaultSchemaBindingValidator implements SchemaBindingValidator
+public class DefaultSchemaBindingValidator extends AbstractSchemaBindingValidator
{
- private static final Logger log = Logger.getLogger(DefaultSchemaBindingValidator.class);
-
- private static final QName WILDCARD = new QName("wildcard", "wildcard");
-
- private Set<String> excludedNs = new HashSet<String>();
- private Set<QName> excludedTypes = new HashSet<QName>();
-
- private Set<QName> validatedTypes = new HashSet<QName>();
- private Set<QName> validatedElements = new HashSet<QName>();
-
- private SchemaBindingResolver resolver;
-
- private boolean loggingEnabled;
-
public DefaultSchemaBindingValidator()
{
- reset();
- excludeNs(Constants.NS_XML_SCHEMA);
+ this(null);
}
- /**
- * @param resolver default schema resolver
- */
public DefaultSchemaBindingValidator(SchemaBindingResolver resolver)
{
- this();
- this.resolver = resolver;
+ super(resolver);
+ reset();
+ excludeNs(Constants.NS_XML_SCHEMA);
}
- /**
- * Resets instance variables (such as a set of validated types, elements and also loggingEnabled property).
- * This method is required to invoked before another validation.
- * It is called internally at the end of validate(XSModel xsSchema, SchemaBinding schemaBinding).
- * NOTE: this method doesn't clear excluded namespaces and types.
- */
- public void reset()
+ protected void validate(XSModel xsSchema, SchemaBinding schemaBinding)
{
- loggingEnabled = log.isTraceEnabled();
- validatedTypes.clear();
- validatedElements.clear();
- }
-
- public boolean isLoggingEnabled()
- {
- return loggingEnabled;
- }
-
- public void enableLogging(boolean value)
- {
- loggingEnabled = value;
- }
-
- /**
- * Types and elements from the namespace passed into this method will be excluded from validation.
- *
- * @param ns namespace to exclude
- */
- public void excludeNs(String ns)
- {
- excludedNs.add(ns);
- }
-
- /**
- * Checks if the specified namespace is excluded from validation.
- *
- * @param ns the namespace to check
- * @return true if the namespace is excluded
- */
- public boolean isNsExcluded(String ns)
- {
- return excludedNs.contains(ns);
- }
-
- /**
- * Removes the namespace from the excluded set. If the namespace has not been excluded, the method does nothing.
- *
- * @param ns the namespace to remove from the excluded set.
- */
- public void includeNs(String ns)
- {
- excludedNs.remove(ns);
- }
-
- /**
- * Excludes the specified type from validation.
- *
- * @param qName the QName of the type to exclude from validation
- */
- public void excludeType(QName qName)
- {
- excludedTypes.add(qName);
- }
-
- /**
- * Checks if the type is excluded from validation.
- *
- * @param qName the QName of the type to check
- * @return true if the type is excluded from validation
- */
- public boolean isTypeExcluded(QName qName)
- {
- return excludedTypes.contains(qName);
- }
-
- /**
- * Removes the specified type from the excluded set. If the type has not been excluded, the method does nothing.
- *
- * @param qName the QName of type to remove from the excluded set.
- */
- public void includeType(QName qName)
- {
- excludedTypes.remove(qName);
- }
-
- /**
- * @return The default resolver used to resolve schemas
- */
- public SchemaBindingResolver getSchemaResolver()
- {
- return resolver;
- }
-
- /**
- * @param resolver The default resolver used to resolve schemas
- */
- public void setSchemaResolver(SchemaBindingResolver resolver)
- {
- this.resolver = resolver;
- }
-
- /**
- * This method will check that the XSD represented with InputSource and SchemaBinding are consistent.
- * The consistency is checked to certain degree and is far from 100%. Currently it checks just for basic things
- * such as the existence of type definitions, attribute and element declarations and element ordering.
- *
- * @param is InputSource of the XSD
- * @param binding SchemaBinding
- */
- public void validate(InputSource is, SchemaBinding binding)
- {
- SchemaBindingResolver resolver = binding.getSchemaResolver();
- if(resolver == null)
- {
- resolver = this.resolver;
- if(resolver == null)
- log("Schema resolver was not provided");
- }
- XSModel xsModel = Util.loadSchema(is, resolver);
- validate(xsModel, binding);
- }
-
- public void validate(String xsdName, Class<?>... cls)
- {
- log("validate: " + xsdName + ", " + Arrays.asList(cls));
-
- URL xsdUrl = Thread.currentThread().getContextClassLoader().getResource("schema/" + xsdName);
- if(xsdUrl == null)
- handleError("Failed to load schema from the classpath: schema/" + xsdName);
-
- MultiClassSchemaResolver multiClassResolver = new MultiClassSchemaResolver();
- multiClassResolver.mapLocationToClasses(xsdName, cls);
- SchemaBinding binding = resolver.resolve("", null, xsdName);
-
- SchemaBindingResolver resolver = this.resolver;
- if(resolver == null)
- resolver = multiClassResolver;
-
- XSModel xsModel;
try
{
- xsModel = Util.loadSchema(xsdUrl.openStream(), null, resolver);
- }
- catch (IOException e)
- {
- throw new IllegalStateException("Failed to read schema " + xsdName, e);
- }
-
- validate(xsModel, binding);
- }
-
- public void validate(XSModel xsSchema, SchemaBinding schemaBinding)
- {
- try
- {
/* TODO groups are not properly bound
XSNamedMap groups = xsSchema.getComponents(XSConstants.MODEL_GROUP_DEFINITION);
for(int i = 0; i < groups.getLength(); ++i)
@@ -799,7 +621,7 @@
*/
protected void log(String msg)
{
- if(loggingEnabled)
+ if(isLoggingEnabled())
log.trace(msg);
}
}
\ No newline at end of file
Modified: jbossxb/trunk/src/main/java/org/jboss/xb/util/SchemaBindingValidator.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/util/SchemaBindingValidator.java 2009-07-09 12:44:41 UTC (rev 3359)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/util/SchemaBindingValidator.java 2009-07-09 13:16:22 UTC (rev 3360)
@@ -35,24 +35,64 @@
public interface SchemaBindingValidator
{
/**
- * Validate schema binding.
+ * Types and elements from the namespace passed into this method will be excluded from validation.
*
- * @param is the input source
- * @param binding the schema binding
+ * @param ns namespace to exclude
*/
- void validate(InputSource is, SchemaBinding binding);
+ void excludeNs(String ns);
/**
- * Exclude QName.
+ * Checks if the specified namespace is excluded from validation.
*
- * @param qName the qname to exclude
+ * @param ns the namespace to check
+ * @return true if the namespace is excluded
*/
+ boolean isNsExcluded(String ns);
+
+ /**
+ * Removes the namespace from the excluded set. If the namespace has not been excluded, the method does nothing.
+ *
+ * @param ns the namespace to remove from the excluded set.
+ */
+ void includeNs(String ns);
+
+ /**
+ * Excludes the specified type from validation.
+ *
+ * @param qName the QName of the type to exclude from validation
+ */
void excludeType(QName qName);
/**
- * Include QName.
+ * Checks if the type is excluded from validation.
*
- * @param qName the qname to include
+ * @param qName the QName of the type to check
+ * @return true if the type is excluded from validation
*/
+ boolean isTypeExcluded(QName qName);
+
+ /**
+ * Removes the specified type from the excluded set. If the type has not been excluded, the method does nothing.
+ *
+ * @param qName the QName of type to remove from the excluded set.
+ */
void includeType(QName qName);
+
+ /**
+ * This method will check that the XSD represented with InputSource and SchemaBinding are consistent.
+ * The consistency is checked to certain degree and is far from 100%. Currently it checks just for basic things
+ * such as the existence of type definitions, attribute and element declarations and element ordering.
+ *
+ * @param is InputSource of the XSD
+ * @param binding SchemaBinding
+ */
+ void validate(InputSource is, SchemaBinding binding);
+
+ /**
+ * Validate xsd schema against classes.
+ *
+ * @param xsdName the schema name
+ * @param cls the classes to check
+ */
+ void validate(String xsdName, Class<?>... cls);
}
More information about the jboss-svn-commits
mailing list