1 17 package org.apache.servicemix.components.validation; 18 19 import org.apache.servicemix.components.util.TransformComponentSupport; 20 import org.apache.servicemix.jbi.FaultException; 21 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 22 import org.apache.servicemix.jbi.jaxp.StringSource; 23 import org.springframework.core.io.Resource; 24 import org.xml.sax.SAXException ; 25 26 import javax.jbi.JBIException; 27 import javax.jbi.messaging.Fault; 28 import javax.jbi.messaging.MessageExchange; 29 import javax.jbi.messaging.MessagingException; 30 import javax.jbi.messaging.NormalizedMessage; 31 import javax.xml.parsers.ParserConfigurationException ; 32 import javax.xml.transform.Source ; 33 import javax.xml.transform.TransformerException ; 34 import javax.xml.transform.dom.DOMResult ; 35 import javax.xml.transform.dom.DOMSource ; 36 import javax.xml.transform.stream.StreamSource ; 37 import javax.xml.validation.Schema ; 38 import javax.xml.validation.SchemaFactory ; 39 import javax.xml.validation.Validator ; 40 import java.io.IOException ; 41 42 49 public class ValidateComponent extends TransformComponentSupport { 50 private Schema schema; 51 private String schemaLanguage = "http://www.w3.org/2001/XMLSchema"; 52 private Source schemaSource; 53 private Resource schemaResource; 54 private MessageAwareErrorHandler errorHandler = new CountingErrorHandler(); 55 56 public Schema getSchema() { 57 return schema; 58 } 59 60 public void setSchema(Schema schema) { 61 this.schema = schema; 62 } 63 64 public String getSchemaLanguage() { 65 return schemaLanguage; 66 } 67 68 public void setSchemaLanguage(String schemaLanguage) { 69 this.schemaLanguage = schemaLanguage; 70 } 71 72 public Source getSchemaSource() { 73 return schemaSource; 74 } 75 76 public void setSchemaSource(Source schemaSource) { 77 this.schemaSource = schemaSource; 78 } 79 80 public Resource getSchemaResource() { 81 return schemaResource; 82 } 83 84 public void setSchemaResource(Resource schemaResource) { 85 this.schemaResource = schemaResource; 86 } 87 88 public MessageAwareErrorHandler getErrorHandler() { 89 return errorHandler; 90 } 91 92 public void setErrorHandler(MessageAwareErrorHandler errorHandler) { 93 this.errorHandler = errorHandler; 94 } 95 96 protected void init() throws JBIException { 97 super.init(); 98 99 try { 100 if (schema == null) { 101 SchemaFactory factory = SchemaFactory.newInstance(schemaLanguage); 102 103 if (schemaSource == null) { 104 if (schemaResource == null) { 105 throw new JBIException("You must specify a schema, schemaSource or schemaResource property"); 106 } 107 if (schemaResource.getURL() == null) { 108 schemaSource = new StreamSource (schemaResource.getInputStream()); 109 } else { 110 schemaSource = new StreamSource (schemaResource.getInputStream(), schemaResource.getURL().toExternalForm()); 111 } 112 } 113 schema = factory.newSchema(schemaSource); 114 } 115 } 116 catch (IOException e) { 117 throw new JBIException("Failed to load schema: " + e, e); 118 } 119 catch (SAXException e) { 120 throw new JBIException("Failed to load schema: " + e, e); 121 } 122 } 123 124 protected boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws MessagingException { 125 Validator validator = schema.newValidator(); 126 127 validator.setErrorHandler(errorHandler); 128 DOMResult result = new DOMResult (); 129 getMessageTransformer().transform(exchange, in, out); 132 try { 133 SourceTransformer sourceTransformer = new SourceTransformer(); 134 DOMSource src = sourceTransformer.toDOMSource(out.getContent()); 139 doValidation(validator,src,result); 140 if (errorHandler.hasErrors()) { 141 Fault fault = exchange.createFault(); 142 143 fault.setProperty("org.apache.servicemix.schema", schema); 145 fault.setProperty("org.apache.servicemix.xml", src); 146 147 151 if (errorHandler.capturesMessages()) { 152 153 158 if (errorHandler.supportsMessageFormat(DOMSource .class)) { 159 fault.setContent( 160 (DOMSource )errorHandler.getMessagesAs(DOMSource .class)); 161 } else if (errorHandler.supportsMessageFormat(StringSource.class)) { 162 fault.setContent(sourceTransformer.toDOMSource( 163 (StringSource)errorHandler.getMessagesAs(StringSource.class))); 164 } else if (errorHandler.supportsMessageFormat(String .class)) { 165 fault.setContent( 166 sourceTransformer.toDOMSource( 167 new StringSource( 168 (String )errorHandler.getMessagesAs(String .class)))); 169 } else { 170 throw new MessagingException("MessageAwareErrorHandler implementation " + 171 errorHandler.getClass().getName() + 172 " does not support a compatible error message format."); 173 } 174 } else { 175 179 fault.setContent(new DOMSource (result.getNode(), result.getSystemId())); 180 } 181 throw new FaultException("Failed to validate against schema: " + schema, exchange, fault); 182 } 183 else { 184 out.setContent(new DOMSource (result.getNode(), result.getSystemId())); 187 return true; 188 } 189 } 190 catch (SAXException e) { 191 throw new MessagingException(e); 192 } 193 catch (IOException e) { 194 throw new MessagingException(e); 195 } 196 catch (ParserConfigurationException e) { 197 throw new MessagingException(e); 198 } 199 catch (TransformerException e) { 200 throw new MessagingException(e); 201 } 202 } 203 204 protected void doValidation(Validator validator, DOMSource src, DOMResult result) throws SAXException , IOException { 205 validator.validate(src,result); 206 } 207 } 208 | Popular Tags |