Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
ValidatingProcessor |
|
| 1.5555555555555556;1.556 |
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.camel.processor.validation; |
|
18 | ||
19 | import java.io.File; |
|
20 | import java.io.IOException; |
|
21 | import java.net.URL; |
|
22 | ||
23 | import javax.xml.XMLConstants; |
|
24 | import javax.xml.transform.Source; |
|
25 | import javax.xml.transform.dom.DOMResult; |
|
26 | import javax.xml.transform.dom.DOMSource; |
|
27 | import javax.xml.validation.Schema; |
|
28 | import javax.xml.validation.SchemaFactory; |
|
29 | import javax.xml.validation.Validator; |
|
30 | ||
31 | import org.xml.sax.SAXException; |
|
32 | ||
33 | import org.apache.camel.Exchange; |
|
34 | import org.apache.camel.Processor; |
|
35 | ||
36 | /** |
|
37 | * A processor which validates the XML version of the inbound message body |
|
38 | * against some schema either in XSD or RelaxNG |
|
39 | * |
|
40 | * @version $Revision: 453155 $ |
|
41 | */ |
|
42 | 0 | public class ValidatingProcessor implements Processor { |
43 | private Schema schema; |
|
44 | 0 | private ValidatorErrorHandler errorHandler = new DefaultValidationErrorHandler(); |
45 | ||
46 | // for lazy creation of the Schema |
|
47 | 0 | private String schemaLanguage = XMLConstants.W3C_XML_SCHEMA_NS_URI; |
48 | private Source schemaSource; |
|
49 | private SchemaFactory schemaFactory; |
|
50 | private URL schemaUrl; |
|
51 | private File schemaFile; |
|
52 | ||
53 | public void process(Exchange exchange) throws Exception { |
|
54 | 0 | Schema schema = getSchema(); |
55 | 0 | Validator validator = schema.newValidator(); |
56 | ||
57 | 0 | Source source = exchange.getIn().getBody(DOMSource.class); |
58 | 0 | if (source == null) { |
59 | 0 | throw new NoXmlBodyValidationException(exchange); |
60 | } |
|
61 | ||
62 | // create a new errorHandler and set it on the validator |
|
63 | 0 | errorHandler.reset(); |
64 | 0 | validator.setErrorHandler(errorHandler); |
65 | ||
66 | 0 | DOMResult result = new DOMResult(); |
67 | 0 | validator.validate(source, result); |
68 | ||
69 | 0 | errorHandler.handleErrors(exchange, schema, result); |
70 | /* |
|
71 | * Fault fault = exchange.createFault(); if (errorHandler.hasErrors()) { // |
|
72 | * set the schema and source document as properties on the fault |
|
73 | * fault.setProperty("org.apache.servicemix.schema", schema); |
|
74 | * fault.setProperty("org.apache.servicemix.xml", source); |
|
75 | * |
|
76 | *//* |
|
77 | * check if this error handler supports the capturing of error |
|
78 | * messages. |
|
79 | *//* |
|
80 | * if (errorHandler.capturesMessages()) { |
|
81 | * |
|
82 | *//* |
|
83 | * In descending order of preference select a format to use. If |
|
84 | * neither DOMSource, StringSource or String are supported throw a |
|
85 | * messaging exception. |
|
86 | *//* |
|
87 | * if (errorHandler.supportsMessageFormat(DOMSource.class)) { |
|
88 | * fault.setContent( (DOMSource) |
|
89 | * errorHandler.getMessagesAs(DOMSource.class)); } else if |
|
90 | * (errorHandler.supportsMessageFormat(StringSource.class)) { |
|
91 | * fault.setContent(sourceTransformer.toDOMSource( (StringSource) |
|
92 | * errorHandler.getMessagesAs(StringSource.class))); } else if |
|
93 | * (errorHandler.supportsMessageFormat(String.class)) { |
|
94 | * fault.setContent( sourceTransformer.toDOMSource( new |
|
95 | * StringSource( (String) |
|
96 | * errorHandler.getMessagesAs(String.class)))); } else { throw new |
|
97 | * MessagingException("MessageAwareErrorHandler implementation " + |
|
98 | * errorHandler.getClass().getName() + " does not support a |
|
99 | * compatible error message format."); } } else { |
|
100 | *//* |
|
101 | * we can't do much here if the ErrorHandler implementation does not |
|
102 | * support capturing messages |
|
103 | *//* |
|
104 | * fault.setContent(new DOMSource(result.getNode(), |
|
105 | * result.getSystemId())); } throw new FaultException("Failed to |
|
106 | * validate against schema: " + schema, exchange, fault); } else { // |
|
107 | * Retrieve the ouput of the validation // as it may have been |
|
108 | * changed by the validator out.setContent(new |
|
109 | * DOMSource(result.getNode(), result.getSystemId())); } } |
|
110 | */ |
|
111 | 0 | } |
112 | ||
113 | // Properties |
|
114 | // ----------------------------------------------------------------------- |
|
115 | ||
116 | public Schema getSchema() throws IOException, SAXException { |
|
117 | 0 | if (schema == null) { |
118 | 0 | schema = createSchema(); |
119 | } |
|
120 | 0 | return schema; |
121 | } |
|
122 | ||
123 | public void setSchema(Schema schema) { |
|
124 | 0 | this.schema = schema; |
125 | 0 | } |
126 | ||
127 | public String getSchemaLanguage() { |
|
128 | 0 | return schemaLanguage; |
129 | } |
|
130 | ||
131 | public void setSchemaLanguage(String schemaLanguage) { |
|
132 | 0 | this.schemaLanguage = schemaLanguage; |
133 | 0 | } |
134 | ||
135 | public Source getSchemaSource() throws IOException { |
|
136 | 0 | if (schemaSource == null) { |
137 | 0 | schemaSource = createSchemaSource(); |
138 | } |
|
139 | 0 | return schemaSource; |
140 | } |
|
141 | ||
142 | public void setSchemaSource(Source schemaSource) { |
|
143 | 0 | this.schemaSource = schemaSource; |
144 | 0 | } |
145 | ||
146 | public URL getSchemaUrl() { |
|
147 | 0 | return schemaUrl; |
148 | } |
|
149 | ||
150 | public void setSchemaUrl(URL schemaUrl) { |
|
151 | 0 | this.schemaUrl = schemaUrl; |
152 | 0 | } |
153 | ||
154 | public File getSchemaFile() { |
|
155 | 0 | return schemaFile; |
156 | } |
|
157 | ||
158 | public void setSchemaFile(File schemaFile) { |
|
159 | 0 | this.schemaFile = schemaFile; |
160 | 0 | } |
161 | ||
162 | public SchemaFactory getSchemaFactory() { |
|
163 | 0 | if (schemaFactory == null) { |
164 | 0 | schemaFactory = createSchemaFactory(); |
165 | } |
|
166 | 0 | return schemaFactory; |
167 | } |
|
168 | ||
169 | public void setSchemaFactory(SchemaFactory schemaFactory) { |
|
170 | 0 | this.schemaFactory = schemaFactory; |
171 | 0 | } |
172 | ||
173 | public ValidatorErrorHandler getErrorHandler() { |
|
174 | 0 | return errorHandler; |
175 | } |
|
176 | ||
177 | public void setErrorHandler(ValidatorErrorHandler errorHandler) { |
|
178 | 0 | this.errorHandler = errorHandler; |
179 | 0 | } |
180 | ||
181 | // Implementation methods |
|
182 | // ----------------------------------------------------------------------- |
|
183 | ||
184 | protected SchemaFactory createSchemaFactory() { |
|
185 | 0 | return SchemaFactory.newInstance(schemaLanguage); |
186 | } |
|
187 | ||
188 | protected Source createSchemaSource() throws IOException { |
|
189 | 0 | throw new IllegalArgumentException("You must specify a schema, " |
190 | + "schemaFile, schemaSource or schemaUrl property"); |
|
191 | } |
|
192 | ||
193 | protected Schema createSchema() throws SAXException, IOException { |
|
194 | 0 | SchemaFactory factory = getSchemaFactory(); |
195 | ||
196 | 0 | URL url = getSchemaUrl(); |
197 | 0 | if (url != null) { |
198 | 0 | return factory.newSchema(url); |
199 | } |
|
200 | 0 | File file = getSchemaFile(); |
201 | 0 | if (file != null) { |
202 | 0 | return factory.newSchema(file); |
203 | } |
|
204 | 0 | return factory.newSchema(getSchemaSource()); |
205 | } |
|
206 | ||
207 | } |