Skip to content
Permalink
Browse files
refactor XML schema version handling to require non-blank version/ns,…
… map empty version to lowest occurring key; exception on unrecognized version/ns combination
  • Loading branch information
mbenson committed Oct 16, 2018
1 parent 431617b commit f5124e2674172dda60638a2d39812872dac84801
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
@@ -29,6 +29,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.validation.ValidationException;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
@@ -38,7 +39,10 @@
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.ValidatorHandler;

import org.apache.bval.util.Exceptions;
import org.apache.bval.util.Lazy;
import org.apache.bval.util.StringUtils;
import org.apache.bval.util.Validate;
import org.apache.bval.util.reflection.Reflection;
import org.w3c.dom.Document;
import org.xml.sax.Attributes;
@@ -75,8 +79,10 @@ private static class Key implements Comparable<Key> {

Key(String version, String ns) {
super();
this.version = Objects.toString(version, "");
this.ns = Objects.toString(ns, "");
Validate.isTrue(StringUtils.isNotBlank(version), "version cannot be null/empty/blank");
this.version = version;
Validate.isTrue(StringUtils.isNotBlank(ns), "ns cannot be null/empty/blank");
this.ns = ns;
}

public String getVersion() {
@@ -126,14 +132,16 @@ public void setContentHandler(ContentHandler handler) {
@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
if (getContentHandler() == ch) {
final Key schemaKey = new Key(Objects.toString(atts.getValue("version"), ""), uri);
if (data.containsKey(schemaKey)) {
final Schema schema = data.get(schemaKey).get();
final ValidatorHandler vh = schema.newValidatorHandler();
vh.startDocument();
vh.setContentHandler(ch);
super.setContentHandler(vh);
}
final String version = Objects.toString(atts.getValue("version"), data.firstKey().getVersion());
final Key schemaKey = new Key(version, uri);
Exceptions.raiseUnless(data.containsKey(schemaKey), ValidationException::new,
"Unknown validation schema %s", schemaKey);

final Schema schema = data.get(schemaKey).get();
final ValidatorHandler vh = schema.newValidatorHandler();
vh.startDocument();
vh.setContentHandler(ch);
super.setContentHandler(vh);
}
try {
super.startElement(uri, localName, qName, atts);
@@ -172,7 +180,8 @@ private class SchemaRewriter extends XMLFilterImpl {

@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
final Key schemaKey = new Key(Objects.toString(atts.getValue("version"), ""), uri);
final Key schemaKey =
new Key(Objects.toString(atts.getValue("version"), data.firstKey().getVersion()), uri);

if (!target.equals(schemaKey) && data.containsKey(schemaKey)) {
uri = target.ns;
@@ -44,7 +44,8 @@
@Privilizing(@CallTo(Reflection.class))
public class ValidationMappingParser implements MetadataSource {
private static final SchemaManager SCHEMA_MANAGER = new SchemaManager.Builder()
.add(null, "http://jboss.org/xml/ns/javax/validation/mapping", "META-INF/validation-mapping-1.0.xsd")
.add(XmlBuilder.Version.v10.getId(), "http://jboss.org/xml/ns/javax/validation/mapping",
"META-INF/validation-mapping-1.0.xsd")
.add(XmlBuilder.Version.v11.getId(), "http://jboss.org/xml/ns/javax/validation/mapping",
"META-INF/validation-mapping-1.1.xsd")
.add(XmlBuilder.Version.v20.getId(), "http://xmlns.jcp.org/xml/ns/validation/mapping",
@@ -57,7 +57,7 @@ public class ValidationParser {
private static final Logger log = Logger.getLogger(ValidationParser.class.getName());

private static final SchemaManager SCHEMA_MANAGER = new SchemaManager.Builder()
.add(null, "http://jboss.org/xml/ns/javax/validation/configuration",
.add(XmlBuilder.Version.v10.getId(), "http://jboss.org/xml/ns/javax/validation/configuration",
"META-INF/validation-configuration-1.0.xsd")
.add(XmlBuilder.Version.v11.getId(), "http://jboss.org/xml/ns/javax/validation/configuration",
"META-INF/validation-configuration-1.1.xsd")

0 comments on commit f5124e2

Please sign in to comment.