View Javadoc
1   package org.kuali.ole.ingest;
2   
3   import org.apache.commons.io.IOUtils;
4   import org.kuali.ole.exception.ParseException;
5   import org.kuali.ole.exception.XmlErrorHandler;
6   import org.xml.sax.SAXException;
7   
8   import javax.xml.XMLConstants;
9   import javax.xml.transform.Source;
10  import javax.xml.transform.stream.StreamSource;
11  import javax.xml.validation.Schema;
12  import javax.xml.validation.SchemaFactory;
13  import javax.xml.validation.Validator;
14  import java.io.ByteArrayInputStream;
15  import java.io.IOException;
16  import java.io.InputStream;
17  
18  /**
19   *  KrmsXMLSchemaValidator is used to validate the xsd schema for KRMS builder.
20   */
21  public class KrmsXMLSchemaValidator {
22  
23      private static final String KRMS_SCHEMA_FILE = "license.xsd";
24  
25      /**
26       *  This method return True/False.
27       *  This method checks fileContent schema with W3 Xml schema standards,If it matches it return True else return False.
28       * @param inputStream
29       * @return  boolean
30       * @throws org.kuali.ole.exception.ParseException
31       * @throws java.io.IOException
32       * @throws org.xml.sax.SAXException
33       */
34      public boolean validateContentsAgainstSchema(InputStream inputStream)
35              throws ParseException, IOException, SAXException {
36          try {
37                  SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
38                  Source schemaSource = null;
39                  schemaSource = new StreamSource(getFileContents());
40                  Schema schema = null;
41                  schema = factory.newSchema(schemaSource);
42                  Validator validator = schema.newValidator();
43                  validator.setErrorHandler(new XmlErrorHandler());
44                  validator.validate(new StreamSource(inputStream));
45                  return true;
46          }
47          catch(Exception ex){
48             //TODO: logging required
49          }
50          return false;
51      }
52  
53      /**
54       *  This method will return fileContents based on KRMS Schema File
55       * @return  profileXmlFile.
56       */
57      private InputStream getFileContents(){
58          byte[] profileByteArray;
59          ByteArrayInputStream profileXmlFile=null;
60          try{
61                  profileByteArray = IOUtils.toByteArray(getClass().getResourceAsStream(KRMS_SCHEMA_FILE));
62                  profileXmlFile = new ByteArrayInputStream(profileByteArray);
63          }
64          catch(Exception e){
65            //TODO: logging required
66          }
67          return profileXmlFile;
68      }
69  }