1 /*
   2  * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package validation;
  25 
  26 import java.io.IOException;
  27 import java.io.InputStreamReader;
  28 import java.io.Reader;
  29 import java.io.UnsupportedEncodingException;
  30 import java.net.MalformedURLException;
  31 import java.net.URL;
  32 
  33 import javax.xml.parsers.DocumentBuilder;
  34 import javax.xml.parsers.DocumentBuilderFactory;
  35 import javax.xml.parsers.ParserConfigurationException;
  36 
  37 import org.w3c.dom.Document;
  38 import org.xml.sax.EntityResolver;
  39 import org.xml.sax.ErrorHandler;
  40 import org.xml.sax.InputSource;
  41 import org.xml.sax.SAXException;
  42 import org.xml.sax.SAXParseException;
  43 
  44 public class XMLDocBuilder {
  45 
  46     private DocumentBuilderFactory factory = null;
  47     private DocumentBuilder builder = null;
  48     private Document doc = null;
  49     private Reader reader = null;
  50     private Reader schema = null;
  51     private String encoding = null;
  52     private String entityPath = null;
  53 
  54     public XMLDocBuilder(String file, String encoding, String schema) {
  55         this.encoding = encoding;
  56         reader = getReaderFromSystemResource(file, encoding);
  57         this.schema = getReaderFromSystemResource(schema, encoding);
  58     }
  59 
  60     public Document getDocument() {
  61         if (reader == null)
  62             return null;
  63 
  64         try {
  65             factory = DocumentBuilderFactory.newInstance();
  66 
  67             builder = factory.newDocumentBuilder();
  68             builder.setErrorHandler(new myErrorHandler());
  69             builder.setEntityResolver(new myEntityResolver());
  70 
  71             InputSource source = new InputSource(reader);
  72             source.setEncoding(encoding);
  73 
  74             try {
  75                 doc = builder.parse(source);
  76                 new XMLSchemaValidator(doc, schema).validate();
  77 
  78             } catch (SAXException e) {
  79                 System.err.println(getClass() + " SAXException: " + e.getMessage());
  80                 return null;
  81             } catch (IOException e) {
  82                 System.err.println(getClass() + " IOException: " + e.getMessage());
  83                 return null;
  84             } catch (OutOfMemoryError e) {
  85                 e.printStackTrace();
  86                 System.err.println(e.getCause().getLocalizedMessage());
  87                 return null;
  88             }
  89 
  90         } catch (ParserConfigurationException e) {
  91             System.err.println(getClass() + " ParserConfigurationException: " + e.getMessage());
  92             return null;
  93         }
  94         return doc;
  95     }
  96 
  97     public Reader getReaderFromSystemResource(String file, String encoding) {
  98 
  99         try {
 100             return new InputStreamReader(getClass().getResourceAsStream(file), encoding);
 101         } catch (UnsupportedEncodingException e) {
 102             System.err.println(getClass() + " UnsupportedEncodingException: " + e.getMessage());
 103         } catch (IOException e) {
 104             System.err.println(getClass() + " IOException: " + e.getMessage());
 105         }
 106         return null;
 107     }
 108 
 109     public void setEntityPath(String entityPath) {
 110         this.entityPath = entityPath;
 111     }
 112 
 113     private class myErrorHandler implements ErrorHandler {
 114 
 115         public void warning(SAXParseException e) {
 116             showErrorMessage(e);
 117         }
 118 
 119         public void error(SAXParseException e) {
 120             showErrorMessage(e);
 121         }
 122 
 123         public void fatalError(SAXParseException e) {
 124             showErrorMessage(e);
 125         }
 126 
 127         private void showErrorMessage(SAXParseException e) {
 128             System.err.println(getClass() + " SAXParseException" + e.getMessage());
 129             System.err.println("Line: " + e.getLineNumber() + " Column: " + e.getColumnNumber());
 130         }
 131     }
 132 
 133     private class myEntityResolver implements EntityResolver {
 134         public InputSource resolveEntity(String publicId, String systemId) {
 135             if (entityPath == null)
 136                 return null;
 137 
 138             systemId = entityPath + systemId.subSequence(systemId.lastIndexOf("/"), systemId.length());
 139 
 140             return new InputSource(systemId);
 141         }
 142     }
 143 }