Coverage Report - org.openpermis.policy.io.xmlchecking.W3CXMLSchemaValidator
 
Classes in this File Line Coverage Branch Coverage Complexity
W3CXMLSchemaValidator
46%
20/43
50%
1/2
1.571
 
 1  
 /*
 2  
  * Copyright (c) 2009, Swiss Federal Department of Defence Civil Protection and Sport
 3  
  *                     (http://www.vbs.admin.ch)
 4  
  * Copyright (c) 2009, Ergon Informatik AG (http://www.ergon.ch)
 5  
  * All rights reserved.
 6  
  *
 7  
  * Licensed under the Open Permis License which accompanies this distribution,
 8  
  * and is available at http://www.openpermis.org/BSDlicenceKent.txt
 9  
  */
 10  
 
 11  
 package org.openpermis.policy.io.xmlchecking;
 12  
 
 13  
 import java.io.File;
 14  
 import java.io.FileReader;
 15  
 import java.io.IOException;
 16  
 import java.io.InputStream;
 17  
 import java.io.Reader;
 18  
 import java.io.StringReader;
 19  
 import java.util.ResourceBundle;
 20  
 
 21  
 import javax.xml.XMLConstants;
 22  
 import javax.xml.transform.Source;
 23  
 import javax.xml.transform.dom.DOMSource;
 24  
 import javax.xml.transform.stream.StreamSource;
 25  
 import javax.xml.validation.Schema;
 26  
 import javax.xml.validation.SchemaFactory;
 27  
 
 28  
 import org.slf4j.Logger;
 29  
 import org.slf4j.LoggerFactory;
 30  
 import org.w3c.dom.Node;
 31  
 import org.xml.sax.SAXException;
 32  
 
 33  
 /**
 34  
  * Class that abstracts the W3C XML Schema validation process.
 35  
  * It is an implementation of the SchemaValidatorInterface.
 36  
  *
 37  
  *
 38  
  * @author Dimitry Bragin
 39  
  */
 40  
 public class W3CXMLSchemaValidator  {
 41  
 
 42  
         //---- Static
 43  
 
 44  1
         private static final Logger LOGGER =
 45  
                 LoggerFactory.getLogger(W3CXMLSchemaValidator.class);
 46  
 
 47  
         private static final String SCHEMA_LOCATION =
 48  
                 "org.openpermis.policy.io.xmlchecking/xml_checking";
 49  
 
 50  
         private static final String SCHEMA_LANGUAGE =
 51  
                 XMLConstants.W3C_XML_SCHEMA_NS_URI;
 52  
 
 53  
         /**
 54  
          * Checks the file if it matches the XML scheme.
 55  
          * @param file File to check
 56  
          * @return null if of, errorstring else
 57  
          * @since 0.3.0
 58  
          */
 59  
         public static String validate (File file) {
 60  15
                 String message = null;
 61  
                 try {
 62  15
                         W3CXMLSchemaValidator validator = new W3CXMLSchemaValidator();
 63  15
                         validator.validate(new FileReader(file));
 64  0
                 } catch (org.xml.sax.SAXException ex) {
 65  0
                         message = "XML Schema validation failed: " +  ex.getMessage();
 66  0
                 } catch (java.io.IOException ex) {
 67  0
                         message = "IOException when parsing the policy: " + ex.getMessage();
 68  0
                 } catch (Exception ex) {
 69  0
                         message = "Exception when parsing the policy: " + ex.getMessage();
 70  15
                 }
 71  15
                 return message;
 72  
         }
 73  
 
 74  
 
 75  
         //---- State
 76  
 
 77  
         private Schema schema;
 78  
 
 79  
 
 80  
         //---- Constructors
 81  
 
 82  
         /**
 83  
          * Create a Validator instance with the default Schema as specified in
 84  
          * <tt>schema_checking.properties</tt> file.
 85  
          * @throws org.xml.sax.SAXException Thrown if parsing of the Schema file fails
 86  
          * @throws java.io.IOException Thrown is a file operation fails
 87  
          */
 88  15
         public W3CXMLSchemaValidator () throws SAXException, IOException {
 89  
 
 90  15
                 SchemaFactory schemaFactory = SchemaFactory.newInstance(SCHEMA_LANGUAGE);
 91  
 
 92  15
                 ResourceBundle schemaResourceBundle =
 93  
                         ResourceBundle.getBundle(SCHEMA_LOCATION);
 94  15
                 String schemaLocation = schemaResourceBundle.getString("schemaLocation");
 95  
 
 96  15
                 LOGGER.debug("W3CXMLSchemaValidator: found schema location " + schemaLocation);
 97  
 
 98  15
                 if (new File(schemaLocation).isAbsolute()) {
 99  0
                         File schemaFile = new File(schemaLocation);
 100  0
                         this.schema = schemaFactory.newSchema(new StreamSource(schemaFile));
 101  0
                 } else {
 102  15
                         InputStream schemaInputStream = getClass().getResourceAsStream(schemaLocation);
 103  15
                         this.schema = schemaFactory.newSchema(new StreamSource(schemaInputStream));
 104  
                 }
 105  
 
 106  15
                 LOGGER.debug("W3CXMLSchemaValidator: succesfully constructed using "
 107  
                                         + "schema " + schemaLocation);
 108  15
         }
 109  
 
 110  
         /**
 111  
          * Create a Validator instance with the Schema represented by the given File object.
 112  
          * @param schemaFile File that contains the Schema to be used
 113  
          * @throws org.xml.sax.SAXException Thrown if parsing of the Schema file fails
 114  
          * @throws java.io.IOException Thrown is a file operation fails
 115  
          */
 116  0
         public W3CXMLSchemaValidator (File schemaFile) throws SAXException, IOException {
 117  0
                 SchemaFactory schemaFactory = SchemaFactory.newInstance(SCHEMA_LANGUAGE);
 118  0
                 this.schema = schemaFactory.newSchema(new StreamSource(schemaFile));
 119  0
         }
 120  
 
 121  
 
 122  
         //---- Methods
 123  
 
 124  
         /**
 125  
          * Checks if a given xml string is valid according to the current schema.
 126  
          * @param s String to be validated
 127  
          * @throws org.xml.sax.SAXException If parsing of the schema instance fails
 128  
          * @throws java.io.IOException Thrown on IO error
 129  
          */
 130  
         public void validate (String s) throws SAXException, IOException {
 131  0
                 validate(new StringReader(s));
 132  0
         }
 133  
 
 134  
         /**
 135  
          * Checks if xml content represented by a Reader is valid according to the current schema.
 136  
          * @param r Reader accessing the xml content
 137  
          * @throws org.xml.sax.SAXException If parsing of the schema instance fails
 138  
          * @throws java.io.IOException Thrown on IO error
 139  
          */
 140  
         public void validate (Reader r) throws SAXException, IOException {
 141  15
                 LOGGER.debug("validate with Reader argument called");
 142  15
                 this.schema.newValidator().validate(new StreamSource(r));
 143  15
                 LOGGER.debug("validate ok.");
 144  15
         }
 145  
 
 146  
         /**
 147  
          * Checks if xml represented by a node is valid according to the current schema.
 148  
          * @param r XML Node that is to be validated
 149  
          * @throws org.xml.sax.SAXException If parsing of the schema instance fails
 150  
          * @throws java.io.IOException Thrown on IO error
 151  
          */
 152  
         public void validate (Node r) throws SAXException, IOException {
 153  0
                 LOGGER.debug("validate with Node argument called");
 154  0
                 this.schema.newValidator().validate(new DOMSource(r));
 155  0
                 LOGGER.debug("validate ok.");
 156  0
         }
 157  
 
 158  
         /**
 159  
          * Check if xml represented by a Source is valid according to the current schema.
 160  
          * @param source Source to be validated
 161  
          * @throws org.xml.sax.SAXException If parsing of the schema instance fails
 162  
          * @throws java.io.IOException Thrown on IO error
 163  
          */
 164  
         public void validate (Source source) throws SAXException, IOException {
 165  0
                 LOGGER.debug("validate with Source argument called");
 166  0
                 this.schema.newValidator().validate(source);
 167  0
                 LOGGER.debug("validate ok.");
 168  0
         }
 169  
 
 170  
 }