1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.camel.component.validator.jing; |
18 |
|
|
19 |
|
import java.io.IOException; |
20 |
|
import java.io.InputStream; |
21 |
|
|
22 |
|
import javax.xml.XMLConstants; |
23 |
|
import javax.xml.transform.Source; |
24 |
|
import javax.xml.transform.sax.SAXSource; |
25 |
|
|
26 |
|
import org.xml.sax.InputSource; |
27 |
|
import org.xml.sax.SAXException; |
28 |
|
import org.xml.sax.XMLReader; |
29 |
|
|
30 |
|
import com.thaiopensource.relaxng.SchemaFactory; |
31 |
|
import com.thaiopensource.util.PropertyMap; |
32 |
|
import com.thaiopensource.util.PropertyMapBuilder; |
33 |
|
import com.thaiopensource.validate.IncorrectSchemaException; |
34 |
|
import com.thaiopensource.validate.Schema; |
35 |
|
import com.thaiopensource.validate.ValidateProperty; |
36 |
|
import com.thaiopensource.validate.Validator; |
37 |
|
import com.thaiopensource.xml.sax.Jaxp11XMLReaderCreator; |
38 |
|
|
39 |
|
import org.apache.camel.Exchange; |
40 |
|
import org.apache.camel.Message; |
41 |
|
import org.apache.camel.Processor; |
42 |
|
import org.apache.camel.processor.validation.DefaultValidationErrorHandler; |
43 |
|
import org.apache.camel.util.ExchangeHelper; |
44 |
|
import org.apache.commons.logging.Log; |
45 |
|
import org.apache.commons.logging.LogFactory; |
46 |
|
|
47 |
|
import org.springframework.core.io.Resource; |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
4 |
public class JingValidator implements Processor { |
57 |
1 |
private static final transient Log LOG = LogFactory.getLog(JingValidator.class); |
58 |
|
private Schema schema; |
59 |
|
private SchemaFactory schemaFactory; |
60 |
4 |
private String schemaNamespace = XMLConstants.RELAXNG_NS_URI; |
61 |
|
private Resource schemaResource; |
62 |
|
private InputSource inputSource; |
63 |
|
private boolean compactSyntax; |
64 |
|
|
65 |
|
public void process(Exchange exchange) throws Exception { |
66 |
4 |
Jaxp11XMLReaderCreator xmlCreator = new Jaxp11XMLReaderCreator(); |
67 |
4 |
DefaultValidationErrorHandler errorHandler = new DefaultValidationErrorHandler(); |
68 |
|
|
69 |
4 |
PropertyMapBuilder mapBuilder = new PropertyMapBuilder(); |
70 |
4 |
mapBuilder.put(ValidateProperty.XML_READER_CREATOR, xmlCreator); |
71 |
4 |
mapBuilder.put(ValidateProperty.ERROR_HANDLER, errorHandler); |
72 |
4 |
PropertyMap propertyMap = mapBuilder.toPropertyMap(); |
73 |
|
|
74 |
4 |
Validator validator = getSchema().createValidator(propertyMap); |
75 |
|
|
76 |
4 |
Message in = exchange.getIn(); |
77 |
4 |
SAXSource saxSource = in.getBody(SAXSource.class); |
78 |
4 |
if (saxSource == null) { |
79 |
0 |
Source source = ExchangeHelper.getMandatoryInBody(exchange, Source.class); |
80 |
0 |
saxSource = ExchangeHelper.convertToMandatoryType(exchange, SAXSource.class, source); |
81 |
|
} |
82 |
4 |
InputSource bodyInput = saxSource.getInputSource(); |
83 |
|
|
84 |
|
|
85 |
4 |
XMLReader reader = xmlCreator.createXMLReader(); |
86 |
4 |
reader.setContentHandler(validator.getContentHandler()); |
87 |
4 |
reader.setDTDHandler(validator.getDTDHandler()); |
88 |
4 |
reader.setErrorHandler(errorHandler); |
89 |
4 |
reader.parse(bodyInput); |
90 |
|
|
91 |
4 |
errorHandler.handleErrors(exchange, schema); |
92 |
2 |
} |
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
public Schema getSchema() throws IOException, IncorrectSchemaException, SAXException { |
98 |
4 |
if (schema == null) { |
99 |
4 |
SchemaFactory factory = getSchemaFactory(); |
100 |
4 |
schema = factory.createSchema(getInputSource()); |
101 |
|
} |
102 |
4 |
return schema; |
103 |
|
} |
104 |
|
|
105 |
|
public void setSchema(Schema schema) { |
106 |
0 |
this.schema = schema; |
107 |
0 |
} |
108 |
|
|
109 |
|
public InputSource getInputSource() throws IOException { |
110 |
4 |
if (inputSource == null) { |
111 |
4 |
Resource resource = getSchemaResource(); |
112 |
4 |
if (resource == null) { |
113 |
0 |
throw new IllegalArgumentException("No schemaResource or inputSource specified"); |
114 |
|
} else { |
115 |
4 |
InputStream inputStream = resource.getInputStream(); |
116 |
4 |
if (inputStream == null) { |
117 |
0 |
throw new IllegalArgumentException("No inputStream available for: " + resource); |
118 |
|
} |
119 |
4 |
inputSource = new InputSource(inputStream); |
120 |
|
} |
121 |
|
} |
122 |
4 |
return inputSource; |
123 |
|
} |
124 |
|
|
125 |
|
public void setInputSource(InputSource inputSource) { |
126 |
0 |
this.inputSource = inputSource; |
127 |
0 |
} |
128 |
|
|
129 |
|
public SchemaFactory getSchemaFactory() { |
130 |
4 |
if (schemaFactory == null) { |
131 |
4 |
schemaFactory = new SchemaFactory(); |
132 |
4 |
schemaFactory.setCompactSyntax(compactSyntax); |
133 |
4 |
schemaFactory.setXMLReaderCreator(new Jaxp11XMLReaderCreator()); |
134 |
|
} |
135 |
4 |
return schemaFactory; |
136 |
|
} |
137 |
|
|
138 |
|
public void setSchemaFactory(SchemaFactory schemaFactory) { |
139 |
0 |
this.schemaFactory = schemaFactory; |
140 |
0 |
} |
141 |
|
|
142 |
|
public Resource getSchemaResource() { |
143 |
4 |
return schemaResource; |
144 |
|
} |
145 |
|
|
146 |
|
public void setSchemaResource(Resource schemaResource) { |
147 |
4 |
this.schemaResource = schemaResource; |
148 |
4 |
} |
149 |
|
|
150 |
|
public String getSchemaNamespace() { |
151 |
0 |
return schemaNamespace; |
152 |
|
} |
153 |
|
|
154 |
|
public void setSchemaNamespace(String schemaNamespace) { |
155 |
0 |
this.schemaNamespace = schemaNamespace; |
156 |
0 |
} |
157 |
|
|
158 |
|
public boolean isCompactSyntax() { |
159 |
0 |
return compactSyntax; |
160 |
|
} |
161 |
|
|
162 |
|
public void setCompactSyntax(boolean compactSyntax) { |
163 |
2 |
this.compactSyntax = compactSyntax; |
164 |
2 |
} |
165 |
|
} |