Coverage Report - net.mtu.eggplant.app.SchemaValidator
 
Classes in this File Line Coverage Branch Coverage Complexity
SchemaValidator
0%
0/56
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 org.w3c.dom.bootstrap.DOMImplementationRegistry;
 47  
 import org.w3c.dom.ls.DOMImplementationLS;
 48  
 import org.w3c.dom.ls.LSInput;
 49  
 import org.w3c.dom.ls.LSResourceResolver;
 50  
 import org.xml.sax.SAXException;
 51  
 import org.xml.sax.SAXParseException;
 52  
 
 53  
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 54  
 import net.mtu.eggplant.util.BasicFileFilter;
 55  
 import net.mtu.eggplant.util.gui.GraphicsUtils;
 56  
 
 57  
 /**
 58  
  * Validate a schema.
 59  
  */
 60  0
 public class SchemaValidator {
 61  
 
 62  
   /**
 63  
    * @param args
 64  
    */
 65  
   public static void main(final String[] args) {
 66  0
     final File initialDirectory = getInitialDirectory();
 67  0
     final JFileChooser chooser = new JFileChooser(initialDirectory);
 68  0
     chooser.setDialogTitle("Choose an XML schema to parse");
 69  0
     chooser.setFileFilter(new BasicFileFilter("Schema Files", "xsd"));
 70  
     while (true) {
 71  0
       final int state = chooser.showOpenDialog(null);
 72  0
       if (JFileChooser.APPROVE_OPTION == state) {
 73  0
         final File file = chooser.getSelectedFile();
 74  0
         setInitialDirectory(file);
 75  
         try {
 76  0
           parseSchema(file);
 77  0
           JOptionPane.showMessageDialog(null, "Parse Successful", "Error", JOptionPane.INFORMATION_MESSAGE);
 78  0
         } catch (final IOException e) {
 79  0
           GraphicsUtils.error(e.getMessage());
 80  0
         } catch (final SAXParseException spe) {
 81  0
           GraphicsUtils.error("Error parsing schema "
 82  0
               + " line: " + spe.getLineNumber() + " column: " + spe.getColumnNumber() + " " + spe.getMessage());
 83  0
         } catch (final SAXException e) {
 84  0
           GraphicsUtils.error(e.getMessage());
 85  0
         } catch (final ClassCastException e) {
 86  0
           GraphicsUtils.error(e.getMessage());
 87  0
         } catch (final ClassNotFoundException e) {
 88  0
           GraphicsUtils.error(e.getMessage());
 89  0
         } catch (final InstantiationException e) {
 90  0
           GraphicsUtils.error(e.getMessage());
 91  0
         } catch (final IllegalAccessException e) {
 92  0
           GraphicsUtils.error(e.getMessage());
 93  0
         }
 94  0
       } else {
 95  0
         return;
 96  
       }
 97  0
     }
 98  
   }
 99  
 
 100  
   public static Schema parseSchema(final File xsdFile) throws SAXException, ClassCastException, ClassNotFoundException,
 101  
       InstantiationException, IllegalAccessException, IOException {
 102  0
     FileInputStream stream = null;
 103  
     try {
 104  0
       stream = new FileInputStream(xsdFile);
 105  
 
 106  
       // get an instance of the DOMImplementation registry
 107  0
       final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
 108  0
       final DOMImplementationLS domImplementationLS = (DOMImplementationLS) registry.getDOMImplementation("LS");
 109  
 
 110  0
       final SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
 111  
 
 112  0
       factory.setResourceResolver(new LSResourceResolver() {
 113  
         @SuppressFBWarnings(value = { "OBL_UNSATISFIED_OBLIGATION" }, justification = "Input must be cleaned up by caller")
 114  
         public LSInput resolveResource(final String type,
 115  
                                        final String namespaceURI,
 116  
                                        final String publicId,
 117  
                                        final String systemId,
 118  
                                        final String baseURI) {
 119  
 
 120  0
           if (null == systemId) {
 121  0
             return null;
 122  
           }
 123  0
           final LSInput input = domImplementationLS.createLSInput();
 124  0
           input.setBaseURI(baseURI);
 125  0
           input.setPublicId(publicId);
 126  0
           input.setSystemId(systemId);
 127  
 
 128  
           try {
 129  
             final Reader inputStream;
 130  0
             if (systemId.startsWith("/")) {
 131  0
               inputStream = new FileReader(systemId);
 132  
             } else {
 133  0
               final File resource = new File(xsdFile.getParent(), systemId);
 134  0
               inputStream = new FileReader(resource);
 135  
             }
 136  0
             input.setCharacterStream(inputStream);
 137  
 
 138  0
             return input;
 139  0
           } catch (final FileNotFoundException e) {
 140  0
             throw new RuntimeException(e);
 141  
           }
 142  
         }
 143  
       });
 144  
 
 145  0
       final Source schemaFile = new StreamSource(xsdFile);
 146  0
       final Schema schema = factory.newSchema(schemaFile);
 147  0
       return schema;
 148  
     } finally {
 149  0
       if (null != stream) {
 150  0
         stream.close();
 151  
       }
 152  0
     }
 153  
   }
 154  
 
 155  
   /**
 156  
    * Set the initial directory preference. This supports opening new file
 157  
    * dialogs to a (hopefully) better default in the user's next session.
 158  
    * 
 159  
    * @param dir the File for the directory in which file dialogs should open
 160  
    */
 161  
   private static void setInitialDirectory(final File dir) {
 162  
     // Store only directories
 163  
     final File directory;
 164  0
     if (dir.isDirectory()) {
 165  0
       directory = dir;
 166  
     } else {
 167  0
       directory = dir.getParentFile();
 168  
     }
 169  
 
 170  0
     final Preferences preferences = Preferences.userNodeForPackage(SchemaValidator.class);
 171  0
     final String previousPath = preferences.get(INITIAL_DIRECTORY_PREFERENCE_KEY, null);
 172  
 
 173  0
     if (!directory.toString().equals(previousPath)) {
 174  0
       preferences.put(INITIAL_DIRECTORY_PREFERENCE_KEY, directory.toString());
 175  
     }
 176  0
   }
 177  
 
 178  
   /**
 179  
    * Get the initial directory to which file dialogs should open. This supports
 180  
    * opening to a better directory across sessions.
 181  
    * 
 182  
    * @return the File for the initial directory
 183  
    */
 184  
   private static File getInitialDirectory() {
 185  0
     final Preferences preferences = Preferences.userNodeForPackage(SchemaValidator.class);
 186  0
     final String path = preferences.get(INITIAL_DIRECTORY_PREFERENCE_KEY, null);
 187  
 
 188  0
     File dir = null;
 189  0
     if (null != path) {
 190  0
       dir = new File(path);
 191  
     }
 192  0
     return dir;
 193  
   }
 194  
 
 195  
   /**
 196  
    * Preferences key for file dialog initial directory
 197  
    */
 198  
   private static final String INITIAL_DIRECTORY_PREFERENCE_KEY = "InitialDirectory";
 199  
 
 200  
 }