Author: hardy.ferentschik
Date: 2009-04-02 12:47:24 -0400 (Thu, 02 Apr 2009)
New Revision: 16254
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/ValidatorFactoryImpl.java
validator/trunk/hibernate-validator/src/test/resources/META-INF/validation/user-constraints.xml
Log:
HV-112 Started parsing the mapping files
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-04-02
16:29:06 UTC (rev 16253)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ConfigurationImpl.java 2009-04-02
16:47:24 UTC (rev 16254)
@@ -385,7 +385,7 @@
schema = sf.newSchema( schemaUrl );
}
catch ( SAXException e ) {
- log.warn( "Unable to create schema for {}: {}", VALIDATION_XML_FILE,
e.getMessage() );
+ log.warn( "Unable to create schema for {}: {}",
VALIDATION_CONFIGURATION_XSD, e.getMessage() );
}
return schema;
}
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorFactoryImpl.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorFactoryImpl.java 2009-04-02
16:29:06 UTC (rev 16253)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/ValidatorFactoryImpl.java 2009-04-02
16:47:24 UTC (rev 16254)
@@ -17,6 +17,9 @@
*/
package org.hibernate.validation.engine;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.Set;
import javax.validation.ConstraintValidatorFactory;
import javax.validation.MessageInterpolator;
import javax.validation.TraversableResolver;
@@ -24,13 +27,30 @@
import javax.validation.ValidatorContext;
import javax.validation.ValidatorFactory;
import javax.validation.spi.ConfigurationState;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBElement;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Unmarshaller;
+import javax.xml.transform.stream.StreamSource;
+import javax.xml.validation.Schema;
+import javax.xml.validation.SchemaFactory;
+import org.slf4j.Logger;
+import org.xml.sax.SAXException;
+
+import org.hibernate.validation.util.LoggerFactory;
+import org.hibernate.validation.xml.ConstraintMappingsType;
+
/**
* @author Emmanuel Bernard
* @author Hardy Ferentschik
+ * @todo Is this the right place to parse the mapping files?
*/
public class ValidatorFactoryImpl implements ValidatorFactory {
+ private static final Logger log = LoggerFactory.make();
+ private static final String VALIDATION_MAPPING_XSD =
"META-INF/validation-mapping-1.0.xsd";
+
private final MessageInterpolator messageInterpolator;
private final TraversableResolver traversableResolver;
private final ConstraintValidatorFactory constraintValidatorFactory;
@@ -40,6 +60,9 @@
this.messageInterpolator = configurationState.getMessageInterpolator();
this.constraintValidatorFactory = configurationState.getConstraintValidatorFactory();
this.traversableResolver = configurationState.getTraversableResolver();
+
+ parseMappingFiles( configurationState.getMappingStreams() );
+
}
/**
@@ -60,8 +83,46 @@
* {@inheritDoc}
*/
public ValidatorContext usingContext() {
- return new ValidatorContextImpl( constraintValidatorFactory, messageInterpolator,
traversableResolver,
+ return new ValidatorContextImpl(
+ constraintValidatorFactory, messageInterpolator, traversableResolver,
constraintHelper
);
}
+
+ private void parseMappingFiles(Set<InputStream> mappingStreams) {
+ for ( InputStream in : mappingStreams ) {
+ ConstraintMappingsType mappings = getValidationConfig( in );
+ }
+ }
+
+ private ConstraintMappingsType getValidationConfig(InputStream in) {
+
+ ConstraintMappingsType constraintMappings = null;
+ Schema schema = getMappingSchema();
+ try {
+ JAXBContext jc = JAXBContext.newInstance( ConstraintMappingsType.class );
+ Unmarshaller unmarshaller = jc.createUnmarshaller();
+ unmarshaller.setSchema( schema );
+ StreamSource stream = new StreamSource( in );
+ JAXBElement<ConstraintMappingsType> root = unmarshaller.unmarshal( stream,
ConstraintMappingsType.class );
+ constraintMappings = root.getValue();
+ }
+ catch ( JAXBException e ) {
+ log.error( "Error parsing mapping file: {}", e.getMessage() );
+ }
+ return constraintMappings;
+ }
+
+ public Schema getMappingSchema() {
+ URL schemaUrl = this.getClass().getClassLoader().getResource( VALIDATION_MAPPING_XSD
);
+ SchemaFactory sf = SchemaFactory.newInstance(
javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI );
+ Schema schema = null;
+ try {
+ schema = sf.newSchema( schemaUrl );
+ }
+ catch ( SAXException e ) {
+ log.warn( "Unable to create schema for {}: {}", VALIDATION_MAPPING_XSD,
e.getMessage() );
+ }
+ return schema;
+ }
}
Modified:
validator/trunk/hibernate-validator/src/test/resources/META-INF/validation/user-constraints.xml
===================================================================
---
validator/trunk/hibernate-validator/src/test/resources/META-INF/validation/user-constraints.xml 2009-04-02
16:29:06 UTC (rev 16253)
+++
validator/trunk/hibernate-validator/src/test/resources/META-INF/validation/user-constraints.xml 2009-04-02
16:47:24 UTC (rev 16254)
@@ -0,0 +1,42 @@
+<constraint-mappings
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping
validation-mapping-1.0.xsd"
+
xmlns="http://jboss.org/xml/ns/javax/validation/mapping">
+ <default-package>com.acme.app.domain</default-package>
+ <bean class="Customer" ignore-annotations="false">
+ <field name="firstName">
+ <constraint annotation="com.acme.app.constraint.LooksLike">
+ <element name="patterns">
+ <annotation>
+ <element name="value">myRegExp</element>
+ <element name="flag">
+ <value>INSENSITIVE</value>
+ </element>
+ </annotation>
+ <annotation>
+ <element
name="value">my2ndRegExp</element>
+ </annotation>
+ </element>
+ </constraint>
+ </field>
+ <field name="orders">
+ <valid/>
+ <constraint
annotation="com.acme.app.constraint.DiscreteSize">
+ <element name="value">
+ <value>0</value>
+ <value>20</value>
+ </element>
+ </constraint>
+ </field>
+ <getter name="orders">
+ <valid/>
+ <constraint annotation="javax.validation.constraint.Size">
+ <message>Size is limited</message>
+ <groups>
+ <value>com.acme.app.model.LightValidation</value>
+ <value>javax.persistence.Default</value>
+ </groups>
+ <element name="max">30</element>
+ </constraint>
+ </getter>
+ </bean>
+</constraint-mappings>