Coverage Report - org.crosswire.common.xml.XMLProcess
 
Classes in this File Line Coverage Branch Coverage Complexity
XMLProcess
0%
0/111
0%
0/26
2.769
 
 1  
 /**
 2  
  * Distribution License:
 3  
  * JSword is free software; you can redistribute it and/or modify it under
 4  
  * the terms of the GNU Lesser General Public License, version 2.1 or later
 5  
  * as published by the Free Software Foundation. This program is distributed
 6  
  * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 7  
  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 8  
  * See the GNU Lesser General Public License for more details.
 9  
  *
 10  
  * The License is available on the internet at:
 11  
  *      http://www.gnu.org/copyleft/lgpl.html
 12  
  * or by writing to:
 13  
  *      Free Software Foundation, Inc.
 14  
  *      59 Temple Place - Suite 330
 15  
  *      Boston, MA 02111-1307, USA
 16  
  *
 17  
  * © CrossWire Bible Society, 2005 - 2016
 18  
  *
 19  
  */
 20  
 package org.crosswire.common.xml;
 21  
 
 22  
 import java.io.IOException;
 23  
 
 24  
 import org.crosswire.common.util.ClassUtil;
 25  
 import org.xml.sax.SAXException;
 26  
 import org.xml.sax.XMLReader;
 27  
 import org.xml.sax.helpers.XMLReaderFactory;
 28  
 
 29  
 /**
 30  
  * Runs an xml parser on an xml file using an xml handler. The default behavior
 31  
  * is to check that the xml file is well-formed.
 32  
  * 
 33  
  * @see gnu.lgpl.License The GNU Lesser General Public License for details.
 34  
  * @author DM Smith
 35  
  */
 36  
 public class XMLProcess {
 37  
 
 38  0
     public XMLProcess() {
 39  0
         features = new XMLFeatureSet();
 40  0
     }
 41  
 
 42  
     /**
 43  
      * @return Returns the features.
 44  
      */
 45  
     public XMLFeatureSet getFeatures() {
 46  0
         return features;
 47  
     }
 48  
 
 49  
     /**
 50  
      * Process an xml file according to the arguments.
 51  
      * 
 52  
      * @param argv the command-line arguments
 53  
      */
 54  
     public static void main(String[] argv) {
 55  0
         XMLProcess checker = new XMLProcess();
 56  
 
 57  
         // is there anything to do?
 58  0
         if (argv.length == 0) {
 59  0
             checker.usage();
 60  0
             System.exit(1);
 61  
         }
 62  
 
 63  
         // variables
 64  0
         String arg = null;
 65  
 
 66  
         // process arguments
 67  0
         for (int i = 0; i < argv.length; i++) {
 68  0
             arg = argv[i];
 69  0
             if (arg.charAt(0) == '-') {
 70  0
                 String option = arg.substring(1);
 71  0
                 if ("h".equals(option)) {
 72  0
                     checker.usage();
 73  0
                     System.exit(0);
 74  
                 }
 75  
             }
 76  
         }
 77  
 
 78  0
         checker.initialize(argv);
 79  0
         checker.parse(arg);
 80  
 
 81  0
     }
 82  
 
 83  
     private void initialize(String[] argv) {
 84  
         // process arguments
 85  0
         int i = 0;
 86  0
         for (i = 0; i < argv.length; i++) {
 87  0
             String arg = argv[i];
 88  0
             if (arg.charAt(0) == '-') {
 89  0
                 String option = arg.substring(1);
 90  0
                 if ("p".equals(option)) {
 91  
                     // get parser name
 92  0
                     if (++i == argv.length) {
 93  0
                         System.err.println("error: Missing argument to -p option.");
 94  
                     }
 95  0
                     parserName = argv[i];
 96  
 
 97  0
                     createParser();
 98  0
                     continue;
 99  
                 }
 100  0
                 if ("a".equals(option)) {
 101  
                     // get parser name
 102  0
                     if (++i == argv.length) {
 103  0
                         System.err.println("error: Missing argument to -a option.");
 104  
                     }
 105  0
                     adapterName = argv[i];
 106  
 
 107  0
                     createAdapter();
 108  0
                     continue;
 109  
                 }
 110  
             }
 111  
         }
 112  
 
 113  0
         features.setFeatureStates(argv);
 114  0
     }
 115  
 
 116  
     private void bind() {
 117  0
         createParser();
 118  0
         createAdapter();
 119  
 
 120  
         // Now that we have a parser and a handler
 121  
         // make the parser use them.
 122  0
         setHandlers();
 123  0
         features.setFeatures(parser);
 124  
 
 125  0
     }
 126  
 
 127  
     private void createParser() {
 128  0
         if (parser != null) {
 129  0
             return;
 130  
         }
 131  
 
 132  
         try {
 133  0
             parser = XMLReaderFactory.createXMLReader(parserName);
 134  0
         } catch (SAXException e) {
 135  0
             System.err.println("error: Unable to instantiate parser (" + parserName + ")");
 136  0
         }
 137  
 
 138  0
     }
 139  
 
 140  
     private void createAdapter() {
 141  0
         if (adapter != null) {
 142  0
             return;
 143  
         }
 144  
 
 145  
         try {
 146  0
             adapter = (XMLHandlerAdapter) ClassUtil.forName(adapterName).newInstance();
 147  0
         } catch (ClassNotFoundException e) {
 148  0
             System.err.println("error: Unable to instantiate XMLHandlerAdpater (" + adapterName + ")");
 149  0
         } catch (InstantiationException e) {
 150  0
             System.err.println("error: Unable to instantiate XMLHandlerAdpater (" + adapterName + ")");
 151  0
         } catch (IllegalAccessException e) {
 152  0
             System.err.println("error: Unable to instantiate XMLHandlerAdpater (" + adapterName + ")");
 153  0
         }
 154  
 
 155  0
     }
 156  
 
 157  
     private void setHandlers() {
 158  0
         parser.setDTDHandler(adapter);
 159  0
         parser.setErrorHandler(adapter);
 160  0
         parser.setContentHandler(adapter);
 161  
 
 162  
         try {
 163  0
             parser.setProperty(DECLARATION_HANDLER_PROPERTY_ID, adapter);
 164  0
         } catch (SAXException e) {
 165  0
             e.printStackTrace(System.err);
 166  0
         }
 167  
 
 168  
         try {
 169  0
             parser.setProperty(LEXICAL_HANDLER_PROPERTY_ID, adapter);
 170  0
         } catch (SAXException e) {
 171  0
             e.printStackTrace(System.err);
 172  0
         }
 173  0
     }
 174  
 
 175  
     public void parse(String xmlFile) {
 176  0
         bind();
 177  
         // parse file
 178  
         try {
 179  0
             System.out.println("Parsing with the following:");
 180  0
             printActual();
 181  0
             parser.parse(xmlFile);
 182  0
             System.out.println("Done: no problems found.");
 183  0
         } catch (SAXException e) {
 184  0
             System.err.println("error: Parse error occurred - " + e.getMessage());
 185  0
             Exception nested = e.getException();
 186  0
             if (nested != null) {
 187  0
                 nested.printStackTrace(System.err);
 188  
             } else {
 189  0
                 e.printStackTrace(System.err);
 190  
             }
 191  0
         } catch (IOException e) {
 192  0
             e.printStackTrace(System.err);
 193  0
         }
 194  0
     }
 195  
 
 196  
     /** Prints the usage. */
 197  
     private void usage() {
 198  0
         System.err.println("usage: java org.crosswire.common.xml.XMLProcess (options) uri");
 199  0
         System.err.println();
 200  
 
 201  0
         System.err.println("options:");
 202  0
         printUsage();
 203  0
         System.err.println("  -h          This help screen.");
 204  0
         System.err.println();
 205  
 
 206  0
         System.err.println("defaults:");
 207  0
         printDefaults();
 208  0
     }
 209  
 
 210  
     public void printUsage() {
 211  0
         System.err.println("  -p name     Select parser by name.");
 212  0
         System.err.println("  -a name     Select XMLHandlerAdapter by name.");
 213  0
         features.printUsage();
 214  0
     }
 215  
 
 216  
     public void printDefaults() {
 217  0
         System.err.println("Parser:     " + DEFAULT_PARSER_NAME);
 218  0
         System.err.println("Handler:    " + DEFAULT_HANDLER_NAME);
 219  0
         System.err.println(new XMLFeatureSet().toString());
 220  0
     }
 221  
 
 222  
     public void printActual() {
 223  0
         System.err.println("Parser:     " + parserName);
 224  0
         System.err.println("Handler:    " + adapterName);
 225  0
         System.err.println(new XMLFeatureSet().toString());
 226  0
     }
 227  
 
 228  
     // property ids
 229  
 
 230  
     /**
 231  
      * Lexical handler property id
 232  
      */
 233  
     private static final String LEXICAL_HANDLER_PROPERTY_ID = "http://xml.org/sax/properties/lexical-handler";
 234  
 
 235  
     /**
 236  
      * Declaration handler property id
 237  
      */
 238  
     private static final String DECLARATION_HANDLER_PROPERTY_ID = "http://xml.org/sax/properties/declaration-handler";
 239  
 
 240  
     // default settings
 241  
 
 242  
     /** Default parser name. */
 243  
     private static final String DEFAULT_PARSER_NAME = "org.apache.xerces.parsers.SAXParser";
 244  
     private static final String DEFAULT_HANDLER_NAME = "org.crosswire.common.xml.XMLHandlerAdapter";
 245  
 
 246  0
     private String parserName = DEFAULT_PARSER_NAME;
 247  
     private XMLReader parser;
 248  0
     private String adapterName = DEFAULT_HANDLER_NAME;
 249  
     private XMLHandlerAdapter adapter;
 250  
     private XMLFeatureSet features;
 251  
 }