Coverage Report - org.apache.camel.component.validator.jing.JingValidator
 
Classes in this File Line Coverage Branch Coverage Complexity
JingValidator
75% 
100% 
1.615
 
 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.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  
  * A validator which uses the <a
 51  
  * href="http://www.thaiopensource.com/relaxng/jing.html">Jing</a> library to
 52  
  * validate XML against RelaxNG
 53  
  * 
 54  
  * @version $Revision: 1.1 $
 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  
         // now lets parse the body using the validator
 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  
     // Properties
 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  
 }