Coverage Report - net.mtu.eggplant.app.SchemaValidator
 
Classes in this File Line Coverage Branch Coverage Complexity
SchemaValidator
0%
0/54
0%
0/10
5
SchemaValidator$1
0%
0/15
0%
0/4
5
 
 1  
 /*
 2  
  * Copyright (c) 2008
 3  
  *      Jon Schewe.  All rights reserved
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met:
 8  
  * 1. Redistributions of source code must retain the above copyright
 9  
  *    notice, this list of conditions and the following disclaimer.
 10  
  * 2. Redistributions in binary form must reproduce the above copyright
 11  
  *    notice, this list of conditions and the following disclaimer in the
 12  
  *    documentation and/or other materials provided with the distribution.
 13  
  *
 14  
  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 15  
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 16  
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 17  
  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 18  
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 19  
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 20  
  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 21  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 22  
  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 23  
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 24  
  * SUCH DAMAGE.
 25  
  *
 26  
  * I'd appreciate comments/suggestions on the code jpschewe@mtu.net
 27  
  */
 28  
 package net.mtu.eggplant.app;
 29  
 
 30  
 import java.io.File;
 31  
 import java.io.FileInputStream;
 32  
 import java.io.FileNotFoundException;
 33  
 import java.io.FileReader;
 34  
 import java.io.IOException;
 35  
 import java.io.Reader;
 36  
 import java.util.prefs.Preferences;
 37  
 
 38  
 import javax.swing.JFileChooser;
 39  
 import javax.swing.JOptionPane;
 40  
 import javax.xml.XMLConstants;
 41  
 import javax.xml.transform.Source;
 42  
 import javax.xml.transform.stream.StreamSource;
 43  
 import javax.xml.validation.Schema;
 44  
 import javax.xml.validation.SchemaFactory;
 45  
 
 46  
 import net.mtu.eggplant.util.BasicFileFilter;
 47  
 import net.mtu.eggplant.util.gui.GraphicsUtils;
 48  
 
 49  
 import org.w3c.dom.bootstrap.DOMImplementationRegistry;
 50  
 import org.w3c.dom.ls.DOMImplementationLS;
 51  
 import org.w3c.dom.ls.LSInput;
 52  
 import org.w3c.dom.ls.LSResourceResolver;
 53  
 import org.xml.sax.SAXException;
 54  
 import org.xml.sax.SAXParseException;
 55  
 
 56  
 /**
 57  
  * Validate a schema.
 58  
  */
 59  0
 public class SchemaValidator {
 60  
 
 61  
   /**
 62  
    * @param args
 63  
    */
 64  
   public static void main(final String[] args) {
 65  0
     final File initialDirectory = getInitialDirectory();
 66  0
     final JFileChooser chooser = new JFileChooser(initialDirectory);
 67  0
     chooser.setDialogTitle("Choose an XML schema to parse");
 68  0
     chooser.setFileFilter(new BasicFileFilter("Schema Files", "xsd"));
 69  
     while (true) {
 70  0
       final int state = chooser.showOpenDialog(null);
 71  0
       if (JFileChooser.APPROVE_OPTION == state) {
 72  0
         final File file = chooser.getSelectedFile();
 73  0
         setInitialDirectory(file);
 74  
         try {
 75  0
           parseSchema(file);
 76  0
           JOptionPane.showMessageDialog(null, "Parse Successful", "Error", JOptionPane.INFORMATION_MESSAGE);
 77  0
         } catch (final IOException e) {
 78  0
           GraphicsUtils.error(e.getMessage());
 79  0
         } catch (final SAXParseException spe) {
 80  0
           GraphicsUtils.error("Error parsing schema "
 81  
               + " line: " + spe.getLineNumber() + " column: " + spe.getColumnNumber() + " " + spe.getMessage());
 82  0
         } catch (final SAXException e) {
 83  0
           GraphicsUtils.error(e.getMessage());
 84  0
         } catch (final ClassCastException e) {
 85  0
           GraphicsUtils.error(e.getMessage());
 86  0
         } catch (final ClassNotFoundException e) {
 87  0
           GraphicsUtils.error(e.getMessage());
 88  0
         } catch (final InstantiationException e) {
 89  0
           GraphicsUtils.error(e.getMessage());
 90  0
         } catch (final IllegalAccessException e) {
 91  0
           GraphicsUtils.error(e.getMessage());
 92  0
         }
 93  0
       } else {
 94  0
         return;
 95  
       }
 96  0
     }
 97  
   }
 98  
 
 99  
   public static Schema parseSchema(final File xsdFile) throws SAXException, ClassCastException, ClassNotFoundException,
 100  
       InstantiationException, IllegalAccessException, IOException {
 101  0
     FileInputStream stream = null;
 102  
     try {
 103  0
       stream = new FileInputStream(xsdFile);
 104  
 
 105  
       // get an instance of the DOMImplementation registry
 106  0
       final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
 107  0
       final DOMImplementationLS domImplementationLS = (DOMImplementationLS) registry.getDOMImplementation("LS");
 108  
 
 109  0
       final SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
 110  
 
 111  0
       factory.setResourceResolver(new LSResourceResolver() {
 112  
         @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = { "OBL_UNSATISFIED_OBLIGATION" }, justification = "Input must be cleaned up by caller")
 113  
         public LSInput resolveResource(final String type,
 114  
                                        final String namespaceURI,
 115  
                                        final String publicId,
 116  
                                        final String systemId,
 117  
                                        final String baseURI) {
 118  
 
 119  0
           if (null == systemId) {
 120  0
             return null;
 121  
           }
 122  0
           final LSInput input = domImplementationLS.createLSInput();
 123  0
           input.setBaseURI(baseURI);
 124  0
           input.setPublicId(publicId);
 125  0
           input.setSystemId(systemId);
 126  
 
 127  
           try {
 128  
             final Reader inputStream;
 129  0
             if (systemId.startsWith("/")) {
 130  0
               inputStream = new FileReader(systemId);
 131  
             } else {
 132  0
               final File resource = new File(xsdFile.getParent(), systemId);
 133  0
               inputStream = new FileReader(resource);
 134  
             }
 135  0
             input.setCharacterStream(inputStream);
 136  
 
 137  0
             return input;
 138  0
           } catch (final FileNotFoundException e) {
 139  0
             throw new RuntimeException(e);
 140  
           }
 141  
         }
 142  
       });
 143  
 
 144  0
       final Source schemaFile = new StreamSource(xsdFile);
 145  0
       final Schema schema = factory.newSchema(schemaFile);
 146  0
       return schema;
 147  
     } finally {
 148  0
       if (null != stream) {
 149  0
         stream.close();
 150  
       }
 151  
     }
 152  
   }
 153  
 
 154  
   /**
 155  
    * Set the initial directory preference. This supports opening new file
 156  
    * dialogs to a (hopefully) better default in the user's next session.
 157  
    * 
 158  
    * @param dir the File for the directory in which file dialogs should open
 159  
    */
 160  
   private static void setInitialDirectory(final File dir) {
 161  
     // Store only directories
 162  
     final File directory;
 163  0
     if (dir.isDirectory()) {
 164  0
       directory = dir;
 165  
     } else {
 166  0
       directory = dir.getParentFile();
 167  
     }
 168  
 
 169  0
     final Preferences preferences = Preferences.userNodeForPackage(SchemaValidator.class);
 170  0
     final String previousPath = preferences.get(INITIAL_DIRECTORY_PREFERENCE_KEY, null);
 171  
 
 172  0
     if (!directory.toString().equals(previousPath)) {
 173  0
       preferences.put(INITIAL_DIRECTORY_PREFERENCE_KEY, directory.toString());
 174  
     }
 175  0
   }
 176  
 
 177  
   /**
 178  
    * Get the initial directory to which file dialogs should open. This supports
 179  
    * opening to a better directory across sessions.
 180  
    * 
 181  
    * @return the File for the initial directory
 182  
    */
 183  
   private static File getInitialDirectory() {
 184  0
     final Preferences preferences = Preferences.userNodeForPackage(SchemaValidator.class);
 185  0
     final String path = preferences.get(INITIAL_DIRECTORY_PREFERENCE_KEY, null);
 186  
 
 187  0
     File dir = null;
 188  0
     if (null != path) {
 189  0
       dir = new File(path);
 190  
     }
 191  0
     return dir;
 192  
   }
 193  
 
 194  
   /**
 195  
    * Preferences key for file dialog initial directory
 196  
    */
 197  
   private static final String INITIAL_DIRECTORY_PREFERENCE_KEY = "InitialDirectory";
 198  
 
 199  
 }