KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > validation > ValidateComponent


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

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 JavaDoc;
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 JavaDoc;
32 import javax.xml.transform.Source JavaDoc;
33 import javax.xml.transform.TransformerException JavaDoc;
34 import javax.xml.transform.dom.DOMResult JavaDoc;
35 import javax.xml.transform.dom.DOMSource JavaDoc;
36 import javax.xml.transform.stream.StreamSource JavaDoc;
37 import javax.xml.validation.Schema JavaDoc;
38 import javax.xml.validation.SchemaFactory JavaDoc;
39 import javax.xml.validation.Validator JavaDoc;
40 import java.io.IOException JavaDoc;
41
42 /**
43  * This component performs a schema validation on the incoming document
44  * and returning a fault if the document does not conform to the schema
45  * otherwise the message is passed on its way.
46  *
47  * @version $Revision: 430194 $
48  */

49 public class ValidateComponent extends TransformComponentSupport {
50     private Schema JavaDoc schema;
51     private String JavaDoc 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 JavaDoc getSchema() {
57         return schema;
58     }
59
60     public void setSchema(Schema JavaDoc schema) {
61         this.schema = schema;
62     }
63
64     public String JavaDoc getSchemaLanguage() {
65         return schemaLanguage;
66     }
67
68     public void setSchemaLanguage(String JavaDoc 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 JavaDoc 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 JavaDoc(schemaResource.getInputStream());
109                     } else {
110                         schemaSource = new StreamSource JavaDoc(schemaResource.getInputStream(), schemaResource.getURL().toExternalForm());
111                     }
112                 }
113                 schema = factory.newSchema(schemaSource);
114             }
115         }
116         catch (IOException JavaDoc e) {
117             throw new JBIException("Failed to load schema: " + e, e);
118         }
119         catch (SAXException JavaDoc 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 JavaDoc validator = schema.newValidator();
126
127         validator.setErrorHandler(errorHandler);
128         DOMResult JavaDoc result = new DOMResult JavaDoc();
129         // Transform first so that the input source will be parsed only once
130
// if it is a StreamSource
131
getMessageTransformer().transform(exchange, in, out);
132         try {
133             SourceTransformer sourceTransformer = new SourceTransformer();
134             // Only DOMSource and SAXSource are allowed for validating
135
// See http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/validation/Validator.html#validate(javax.xml.transform.Source,%20javax.xml.transform.Result)
136
// As we expect a DOMResult as output, we must ensure that the input is a
137
// DOMSource
138
DOMSource JavaDoc src = sourceTransformer.toDOMSource(out.getContent());
139             doValidation(validator,src,result);
140             if (errorHandler.hasErrors()) {
141                 Fault fault = exchange.createFault();
142                 
143                 // set the schema and source document as properties on the fault
144
fault.setProperty("org.apache.servicemix.schema", schema);
145                 fault.setProperty("org.apache.servicemix.xml", src);
146                 
147                 /*
148                  * check if this error handler supports the capturing of
149                  * error messages.
150                  */

151                 if (errorHandler.capturesMessages()) {
152
153                     /*
154                      * In descending order of preference select a format to use. If
155                      * neither DOMSource, StringSource or String are supported throw
156                      * a messaging exception.
157                      */

158                     if (errorHandler.supportsMessageFormat(DOMSource JavaDoc.class)) {
159                         fault.setContent(
160                                 (DOMSource JavaDoc)errorHandler.getMessagesAs(DOMSource JavaDoc.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 JavaDoc.class)) {
165                         fault.setContent(
166                                 sourceTransformer.toDOMSource(
167                                         new StringSource(
168                                                 (String JavaDoc)errorHandler.getMessagesAs(String JavaDoc.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                     /*
176                      * we can't do much here if the ErrorHandler implementation does
177                      * not support capturing messages
178                      */

179                     fault.setContent(new DOMSource JavaDoc(result.getNode(), result.getSystemId()));
180                 }
181                 throw new FaultException("Failed to validate against schema: " + schema, exchange, fault);
182             }
183             else {
184                 // Retrieve the ouput of the validation
185
// as it may have been changed by the validator
186
out.setContent(new DOMSource JavaDoc(result.getNode(), result.getSystemId()));
187                 return true;
188              }
189         }
190         catch (SAXException JavaDoc e) {
191             throw new MessagingException(e);
192         }
193         catch (IOException JavaDoc e) {
194             throw new MessagingException(e);
195         }
196         catch (ParserConfigurationException JavaDoc e) {
197             throw new MessagingException(e);
198         }
199         catch (TransformerException JavaDoc e) {
200             throw new MessagingException(e);
201         }
202     }
203     
204     protected void doValidation(Validator JavaDoc validator, DOMSource JavaDoc src, DOMResult JavaDoc result) throws SAXException JavaDoc, IOException JavaDoc {
205         validator.validate(src,result);
206     }
207 }
208
Popular Tags