public static void main(String args[]) {
    if (args == null || args.length == 0) {
      System.out.println(
          "Usage: java sereneSamples.MissingDatatypeLibraryValidation schema-file xml-file ... ");
      return;
    }
    if (!args[0].endsWith(".rng")) {
      System.out.println(
          "Usage: java sereneSamples.MissingDatatypeLibraryValidation schema-file xml-file ... ");
      return;
    }
    for (int i = 1; i < args.length; i++) {
      if (!args[i].endsWith(".xml")) {
        System.out.println(
            "Usage: java sereneSamples.MissingDatatypeLibraryValidation schema-file xml-file ... ");
        return;
      }
    }

    SchemaFactory schemaFactory = null;
    Schema schema;

    MessageWriter debugWriter;
    WriteErrorHandler debugErrorHandler;

    debugWriter = new MessageWriter();
    debugWriter.setWriteHandler(new ConsoleHandler());

    debugErrorHandler = new WriteErrorHandler();
    debugErrorHandler.setWriteHandler(new ConsoleHandler());

    debugErrorHandler.print("SCHEMA " + args[0]);

    schemaFactory = SchemaFactory.newInstance(XMLConstants.RELAXNG_NS_URI);
    schemaFactory.setErrorHandler(debugErrorHandler);

    debugErrorHandler.print(
        "FEATURE http://serenerng.com/features/schemaFactory/replaceMissingDatatypeLibrary  true");
    try {
      schemaFactory.setFeature(
          "http://serenerng.com/features/schemaFactory/replaceMissingDatatypeLibrary", true);
      debugErrorHandler.setFeature("http://example.com/countMissingLibraryExceptions", false);
    } catch (SAXNotRecognizedException e) {
      e.printStackTrace();
    } catch (SAXNotSupportedException e) {
      e.printStackTrace();
    }
    try {
      schema = schemaFactory.newSchema(new File(args[0]));
    } catch (Exception e) {
      e.printStackTrace();
      return;
    }
    if (!debugErrorHandler.hasError()) {
      Validator v = schema.newValidator();
      v.setErrorHandler(debugErrorHandler);

      for (int i = 1; i < args.length; i++) {
        String name = args[i].substring(args[i].lastIndexOf(File.separator) + 1);
        debugErrorHandler.print("FILE " + name);
        try {
          Source source = new SAXSource(new InputSource(args[i]));
          v.validate(source);
        } catch (IOException e) {
          e.printStackTrace();
        } catch (SAXParseException e) {
          e.printStackTrace();
        } catch (SAXException e) {
          e.printStackTrace();
        }
      }
    } else {
      debugErrorHandler.print("No document validation, schema contains unrecoverable errors.");
    }

    debugErrorHandler.print(
        "FEATURE http://serenerng.com/features/schemaFactory/replaceMissingDatatypeLibrary  false");
    try {
      schemaFactory.setFeature(
          "http://serenerng.com/features/schemaFactory/replaceMissingDatatypeLibrary", false);
      debugErrorHandler.setFeature("http://example.com/countMissingLibraryExceptions", true);
    } catch (SAXNotRecognizedException e) {
      e.printStackTrace();
    } catch (SAXNotSupportedException e) {
      e.printStackTrace();
    }
    try {
      schema = schemaFactory.newSchema(new File(args[0]));
    } catch (Exception e) {
      e.printStackTrace();
      return;
    }
    if (!debugErrorHandler.hasError()) {
      Validator v = schema.newValidator();
      v.setErrorHandler(debugErrorHandler);

      for (int i = 1; i < args.length; i++) {
        String name = args[i].substring(args[i].lastIndexOf(File.separator) + 1);
        debugErrorHandler.print("FILE " + name);
        try {
          Source source = new SAXSource(new InputSource(args[i]));
          v.validate(source);
        } catch (IOException e) {
          e.printStackTrace();
        } catch (SAXParseException e) {
          e.printStackTrace();
        } catch (SAXException e) {
          e.printStackTrace();
        }
      }
    } else {
      debugErrorHandler.print("No document validation, schema contains unrecoverable errors.");
    }

    debugErrorHandler.close();
  }
Exemplo n.º 2
0
 public Phylogeny[] parse() throws IOException, PhylogenyParserException {
   reset();
   final TolXmlHandler handler = new TolXmlHandler();
   final SAXParserFactory factory = SAXParserFactory.newInstance();
   factory.setNamespaceAware(true);
   try {
     if (!ForesterUtil.isEmpty(getSchemaLocation())) {
       factory.setFeature(SAX_FEATURES_VALIDATION, true);
       factory.setFeature(APACHE_FEATURES_VALIDATION_SCHEMA, true);
       factory.setFeature(APACHE_FEATURES_VALIDATION_SCHEMA_FULL, true);
     }
   } catch (final SAXNotRecognizedException e) {
     e.printStackTrace();
     throw new PhylogenyParserException("sax not recognized exception: " + e.getMessage());
   } catch (final SAXNotSupportedException e) {
     e.printStackTrace();
     throw new PhylogenyParserException("sax not supported exception: " + e.getMessage());
   } catch (final ParserConfigurationException e) {
     e.printStackTrace();
     throw new PhylogenyParserException("parser _configuration exception: " + e.getMessage());
   } catch (final Exception e) {
     e.printStackTrace();
     throw new PhylogenyParserException("error while configuring sax parser: " + e.getMessage());
   }
   try {
     final SAXParser parser = factory.newSAXParser();
     if (!ForesterUtil.isEmpty(getSchemaLocation())) {
       parser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
       parser.setProperty(JAXP_SCHEMA_SOURCE, getSchemaLocation());
       parser.setProperty(APACHE_PROPERTIES_SCHEMA_EXTERNAL_LOCATION, getSchemaLocation());
     }
     final XMLReader xml_reader = parser.getXMLReader();
     xml_reader.setContentHandler(handler);
     xml_reader.setErrorHandler(new TolParserErrorHandler());
     if (getSource() instanceof File) {
       if (!getSource().toString().toLowerCase().endsWith(".zip")) {
         xml_reader.parse(new InputSource(new FileReader((File) getSource())));
       } else {
         final Reader reader = getReaderFromZipFile();
         if (reader == null) {
           throw new PhylogenyParserException(
               "Zip file \"" + getSource() + "\" appears not to contain any entries");
         }
         xml_reader.parse(new InputSource(reader));
       }
     } else if (getSource() instanceof InputSource) {
       xml_reader.parse((InputSource) getSource());
     } else if (getSource() instanceof InputStream) {
       if (!isZippedInputstream()) {
         final InputStream is = (InputStream) getSource();
         final Reader reader = new InputStreamReader(is);
         xml_reader.parse(new InputSource(reader));
       } else {
         final ZipInputStream zip_is = new ZipInputStream((InputStream) getSource());
         zip_is.getNextEntry();
         final Reader reader = new InputStreamReader(zip_is);
         if (reader == null) {
           throw new PhylogenyParserException(
               "Zip input stream \"" + getSource() + "\" appears not to contain any data");
         }
         xml_reader.parse(new InputSource(reader));
       }
     } else if (getSource() instanceof String) {
       final File file = new File(getSource().toString());
       final Reader reader = new FileReader(file);
       xml_reader.parse(new InputSource(reader));
     } else if (getSource() instanceof StringBuffer) {
       final StringReader string_reader = new StringReader(getSource().toString());
       xml_reader.parse(new InputSource(string_reader));
     } else {
       throw new PhylogenyParserException(
           "attempt to parse object of unsupported type: \"" + getSource().getClass() + "\"");
     }
   } catch (final SAXException sax_exception) {
     throw new PhylogenyParserException(
         "Failed to parse [" + getSource() + "]: " + sax_exception.getMessage());
   } catch (final ParserConfigurationException parser_config_exception) {
     throw new PhylogenyParserException(
         "Failed to parse ["
             + getSource()
             + "] Problem with xml parser _configuration: "
             + parser_config_exception.getMessage());
   } catch (final IOException e) {
     throw new PhylogenyParserException(
         "Problem with input source [" + getSource() + "]: \n" + e.getMessage());
   } catch (final Exception e) {
     e.printStackTrace();
     throw new PhylogenyParserException(
         "Failed to parse [" + getSource() + "]: " + e.getMessage());
   } catch (final Error err) {
     err.printStackTrace();
     throw new PhylogenyParserException("Severe error: " + err.getMessage());
   }
   final Phylogeny[] ps = new Phylogeny[handler.getPhylogenies().size()];
   int i = 0;
   for (final Phylogeny phylogeny : handler.getPhylogenies()) {
     ps[i++] = phylogeny;
   }
   return ps;
 }