Author: epbernard
Date: 2009-01-31 01:36:02 -0500 (Sat, 31 Jan 2009)
New Revision: 15842
Added:
validator/trunk/validation-api/src/main/resources/validation-configuration-1.0.xsd
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConfigurationImpl.java
validator/trunk/validation-api/src/main/java/javax/validation/Configuration.java
validator/trunk/validation-api/src/main/java/javax/validation/spi/ConfigurationState.java
Log:
BVAL-102 add XML configuration support
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConfigurationImpl.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConfigurationImpl.java 2009-01-30
19:37:29 UTC (rev 15841)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConfigurationImpl.java 2009-01-31
06:36:02 UTC (rev 15842)
@@ -19,6 +19,8 @@
import java.io.InputStream;
import java.util.List;
+import java.util.Set;
+import java.util.Map;
import javax.validation.Configuration;
import javax.validation.ConstraintValidatorFactory;
import javax.validation.MessageInterpolator;
@@ -52,6 +54,7 @@
private final ValidationProvider provider;
private final ValidationProviderResolver providerResolver;
private TraversableResolver traversableResolver;
+ private boolean ignoreXmlConfiguration;
public ConfigurationImpl(BootstrapState state) {
if ( state.getValidationProviderResolver() == null ) {
@@ -75,6 +78,10 @@
this.traversableResolver = defaultTraversableResolver;
}
+ public HibernateValidatorConfiguration ignoreXmlConfiguration() {
+ return null; //To change body of implemented methods use File | Settings | File
Templates.
+ }
+
public ConfigurationImpl messageInterpolator(MessageInterpolator interpolator) {
this.messageInterpolator = interpolator;
return this;
@@ -90,6 +97,14 @@
return this;
}
+ public HibernateValidatorConfiguration addMapping(InputStream stream) {
+ return null; //To change body of implemented methods use File | Settings | File
Templates.
+ }
+
+ public HibernateValidatorConfiguration addProperty(String name, String value) {
+ return null; //To change body of implemented methods use File | Settings | File
Templates.
+ }
+
public ValidatorFactory buildValidatorFactory() {
if ( isSpecificProvider() ) {
return provider.buildValidatorFactory( this );
@@ -118,10 +133,18 @@
return provider != null;
}
+ public boolean isIgnoreXmlConfiguration() {
+ return ignoreXmlConfiguration;
+ }
+
public MessageInterpolator getMessageInterpolator() {
return messageInterpolator;
}
+ public Set<InputStream> getMappingStreams() {
+ throw new UnsupportedOperationException( "TODO" );
+ }
+
public ConstraintValidatorFactory getConstraintValidatorFactory() {
return constraintValidatorFactory;
}
@@ -130,15 +153,12 @@
return traversableResolver;
}
- public ConfigurationImpl customConfiguration(InputStream stream) {
- return null; //To change body of implemented methods use File | Settings | File
Templates.
+ public Map<String, String> getProperties() {
+ throw new UnsupportedOperationException( "TODO" );
}
public MessageInterpolator getDefaultMessageInterpolator() {
return defaultMessageInterpolator;
}
- public InputStream getConfigurationStream() {
- return null; //To change body of implemented methods use File | Settings | File
Templates.
- }
}
Modified:
validator/trunk/validation-api/src/main/java/javax/validation/Configuration.java
===================================================================
---
validator/trunk/validation-api/src/main/java/javax/validation/Configuration.java 2009-01-30
19:37:29 UTC (rev 15841)
+++
validator/trunk/validation-api/src/main/java/javax/validation/Configuration.java 2009-01-31
06:36:02 UTC (rev 15842)
@@ -32,6 +32,12 @@
* .buildValidatorFactory();
* </pre>
* <p/>
+ *
+ * By default, the configuration information is retrieved from
+ * META-INF/validation.xml
+ * It is possible to override the configuration retrieved from the XML file
+ * by using one or more of the Configuration methods.
+ *
* The ValidationProviderResolver is specified at Configuration time
* (see {@link javax.validation.spi.ValidationProvider}).
* If none is explicitely requested, the default ValidationProviderResolver is used.
@@ -48,7 +54,16 @@
* @author Emmanuel Bernard
*/
public interface Configuration<T extends Configuration<T>> {
+
/**
+ * Ignore data from the META-INF/validation.xml file if this
+ * method is called.
+ * This method is typically useful for containers that parse
+ * META-INF/validation.xml themselves and pass the information
+ * via the Configuration methods.
+ */
+ T ignoreXmlConfiguration();
+ /**
* Defines the message interpolator used. Has priority over the configuration
* based message interpolator.
*
@@ -79,19 +94,43 @@
T constraintValidatorFactory(ConstraintValidatorFactory constraintValidatorFactory);
/**
- * Configure the ValidatorFactory based on <code>stream</code>
- * If not specified, META-INF/validation.xml is used
+ * Add a stream describing constaint mapping in the Bean Validation
+ * XML format.
* <p/>
* The stream should be closed by the client API after the
- * ValidatorFactory has been returned
+ * ValidatorFactory has been built. The Bean Validation provider
+ * must not close the stream.
*
- * @param stream configuration stream.
+ * @param stream XML mapping stream.
*
* @return <code>this</code> following the chaining method pattern.
*/
- T customConfiguration(InputStream stream);
+ T addMapping(InputStream stream);
/**
+ * Add a provider specific property. This property is equivalent to
+ * XML Configuration properties.
+ * If the underlying provider does not know how to handle the property,
+ * it must silently ignore it.
+ *
+ * Note: Using this non type-safe method is generally not recommended.
+ *
+ * It is more appropriate to use, if available, the type-safe equivalent provided
+ * by a specific provider in its Configuration subclass.
+ * <code>ValidatorFactory factory =
Validation.byProvider(ACMEConfiguration.class)
+ * .configure()
+ * .providerSpecificProperty(ACMEState.FAST)
+ * .buildValidatorFactory();
+ * </code>
+ * This method is typically used by containers parsing META-INF/validation.xml
+ * themselves and injecting the state to the Configuration object.
+ *
+ * If a property with a given name is defined both via this method and in the
+ * XML configuration, the value set programmatically has priority.
+ */
+ T addProperty(String name, String value);
+
+ /**
* Return an implementation of the MessageInterpolator interface following the
* default MessageInterpolator defined in the specification:
* - use the ValidationMessages resource bundle to load keys
Modified:
validator/trunk/validation-api/src/main/java/javax/validation/spi/ConfigurationState.java
===================================================================
---
validator/trunk/validation-api/src/main/java/javax/validation/spi/ConfigurationState.java 2009-01-30
19:37:29 UTC (rev 15841)
+++
validator/trunk/validation-api/src/main/java/javax/validation/spi/ConfigurationState.java 2009-01-31
06:36:02 UTC (rev 15842)
@@ -18,6 +18,8 @@
package javax.validation.spi;
import java.io.InputStream;
+import java.util.Set;
+import java.util.Map;
import javax.validation.ConstraintValidatorFactory;
import javax.validation.MessageInterpolator;
import javax.validation.TraversableResolver;
@@ -25,42 +27,80 @@
/**
* Contract between a <code>Configuration</code> and a
* </code>ValidatorProvider</code> to create a
<code>ValidatorFactory</code>.
- * The configuration artifacts provided to the
- * <code>Configuration</code> are passed along.
+ * The configuration artifacts defined in the XML configuration and provided to the
+ * <code>Configuration</code> are merged and passed along via
ConfigurationState.
*
* @author Emmanuel Bernard
* @author Hardy Ferentschik
*/
public interface ConfigurationState {
+
/**
- * Message interpolator as defined by the client programmatically
- * or null if undefined.
+ * returns true if Configuration.ignoreXMLConfiguration() has been called
+ * In this case, the ValiatorFactory must ignore META-INF/validation.xml
+ * @return true if META-INF/validation.xml should be ignored
+ */
+ boolean isIgnoreXmlConfiguration();
+
+ /**
+ * Message interpolator as defined in the following decresing priority:
+ * - set via the Configuration programmatic API
+ * - defined in META-INF/validation.xml provided that ignoredXmlConfiguration
+ * is false. In this case the instance is created via its no-arg constructor.
+ * - null if undefined.
*
* @return message provider instance or null if not defined
*/
MessageInterpolator getMessageInterpolator();
/**
- * Returns the configuration stream defined by the client programmatically
- * or null if undefined.
+ * Returns a set of stream corresponding to:
+ * - mapping XML streams passed programmatically in Configuration
+ * - mapping XML stream located in the resources defined in
+ * META-INF/validation.xml (constraint-mapping element)
*
- * @return the configuration input stream or null
+ * Streams represented in the XML configuration and opened by the
+ * configuration implementation must be closed by the configuration
+ * implementation after the ValidatorFactory creation (or if an exception
+ * occurs).
+ *
+ * @return set of input stream
*/
- InputStream getConfigurationStream();
+ Set<InputStream> getMappingStreams();
/**
- * Defines the constraint validator implementation factory as defined by
- * the client programmatically or null if undefined
+ * ConstraintValidatorFactory implementation as defined in the following
+ * decreasing priority:
+ * - set via the Configuration programmatic API
+ * - defined in META-INF/validation.xml provided that ignoredXmlConfiguration
+ * is false. In this case the instance is created via its no-arg constructor.
+ * - null if undefined.
*
* @return factory instance or null if not defined
*/
ConstraintValidatorFactory getConstraintValidatorFactory();
/**
- * Traversable resolver as defined by the client programmatically
- * or null if undefined.
+ * TraversableResolver as defined in the following decresing priority:
+ * - set via the Configuration programmatic API
+ * - defined in META-INF/validation.xml provided that ignoredXmlConfiguration
+ * is false. In this case the instance is created via its no-arg constructor.
+ * - null if undefined.
*
* @return traversable provider instance or null if not defined
*/
TraversableResolver getTraversableResolver();
+
+ /**
+ * return non type-safe properties defined via:
+ * - Configuration.addProperty(String, String)
+ * - META-INF/validation.xml provided that ignoredXmlConfiguration
+ * is false.
+ *
+ * If a property is defined both programmatically and in XML,
+ * the value defined programmatically has priority
+ *
+ * @return Map whose key is the property key and the value the property value
+ */
+ Map<String, String> getProperties();
}
Added: validator/trunk/validation-api/src/main/resources/validation-configuration-1.0.xsd
===================================================================
--- validator/trunk/validation-api/src/main/resources/validation-configuration-1.0.xsd
(rev 0)
+++
validator/trunk/validation-api/src/main/resources/validation-configuration-1.0.xsd 2009-01-31
06:36:02 UTC (rev 15842)
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xs:schema attributeFormDefault="unqualified"
+ elementFormDefault="qualified"
+
targetNamespace="http://jboss.org/xml/ns/javax/validation/configurat...
+
xmlns:xs="http://www.w3.org/2001/XMLSchema"
+ version="1.0">
+ <xs:element name="validation-config"
type="validation-configType"/>
+ <xs:complexType name="validation-configType">
+ <xs:sequence>
+ <xs:element type="xs:string" name="default-provider"
minOccurs="0"/>
+ <xs:element type="xs:string"
name="message-interpolator" minOccurs="0"/>
+ <xs:element type="xs:string"
name="traversable-resolver" minOccurs="0"/>
+ <xs:element type="xs:string"
name="constraint-validator-factory" minOccurs="0"/>
+ <xs:element type="xs:string" name="constraint-mapping"
maxOccurs="unbounded" minOccurs="0"/>
+ <xs:element type="propertyType" name="property"
maxOccurs="unbounded" minOccurs="0"/>
+ </xs:sequence>
+ </xs:complexType>
+ <xs:complexType name="propertyType">
+ <xs:attribute name="name" use="required"
type="xs:string"/>
+ </xs:complexType>
+</xs:schema>
\ No newline at end of file