Example #1
0
 /** Private constructor */
 private XMLReaders(int validate) {
   SAXParserFactory fac = SAXParserFactory.newInstance();
   boolean val = false;
   // All JDOM parsers are namespace aware.
   fac.setNamespaceAware(true);
   switch (validate) {
     case 0:
       fac.setValidating(false);
       break;
     case 1:
       fac.setValidating(true);
       val = true;
       break;
     case 2:
       fac.setValidating(false);
       try {
         SchemaFactory sfac = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
         Schema schema = sfac.newSchema();
         fac.setSchema(schema);
         val = true;
       } catch (SAXException se) {
         // we could not get a validating system, set the fac to null
         fac = null;
       }
       break;
   }
   jaxpfactory = fac;
   validates = val;
 }
Example #2
0
  protected void validateXmlString(final String xml) throws Exception {
    if (getSchemaFile() == null) {
      LOG.warn("skipping validation, schema file not set");
      return;
    }

    final SchemaFactory schemaFactory =
        SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    final File schemaFile = new File(getSchemaFile());
    LOG.debug("Validating using schema file: {}", schemaFile);
    final Schema schema = schemaFactory.newSchema(schemaFile);

    final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    saxParserFactory.setValidating(true);
    saxParserFactory.setNamespaceAware(true);
    saxParserFactory.setSchema(schema);

    assertTrue("make sure our SAX implementation can validate", saxParserFactory.isValidating());

    final Validator validator = schema.newValidator();
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(xml.getBytes());
    final Source source = new StreamSource(inputStream);

    validator.validate(source);
  }
 /** Private constructor */
 private XMLReaders(int validate) {
   SAXParserFactory fac = SAXParserFactory.newInstance();
   boolean val = false;
   Exception problem = null;
   // All JDOM parsers are namespace aware.
   fac.setNamespaceAware(true);
   switch (validate) {
     case 0:
       fac.setValidating(false);
       break;
     case 1:
       fac.setValidating(true);
       val = true;
       break;
     case 2:
       fac.setValidating(false);
       try {
         SchemaFactory sfac = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
         Schema schema = sfac.newSchema();
         fac.setSchema(schema);
         val = true;
       } catch (SAXException se) {
         // we could not get a validating system, set the fac to null
         fac = null;
         problem = se;
       } catch (IllegalArgumentException iae) {
         // this system does not support XSD Validation.... which is true for android!
         // we could not get a validating system, set the fac to null
         fac = null;
         problem = iae;
       } catch (UnsupportedOperationException uoe) {
         // SAXParserFactory throws this exception when setSchema is called.
         // Therefore every factory throws this exception unless it overrides
         // setSchema. A popular example is Apache Xerces SAXParserFactoryImpl
         // before version 2.7.0.
         fac = null;
         problem = uoe;
       }
       break;
   }
   jaxpfactory = fac;
   validates = val;
   failcause = problem;
 }
  public int Validate(
      File inFile,
      File entitiesResolverConfigFile,
      File schemaFile,
      File schemataResolverConfigFile) {
    try {
      SAXParserFactory parserFactory = SAXParserFactory.newInstance();
      // Disable validating by DTD.
      parserFactory.setValidating(false);
      parserFactory.setNamespaceAware(true);

      SchemaEntityResolverLocal localResolver =
          new SchemaEntityResolverLocal(entitiesResolverConfigFile, schemataResolverConfigFile);

      SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
      schemaFactory.setResourceResolver(localResolver);

      Source schemaSource = new StreamSource(schemaFile);
      parserFactory.setSchema(schemaFactory.newSchema(new Source[] {schemaSource}));

      SAXParser parser = parserFactory.newSAXParser();
      XMLReader reader = parser.getXMLReader();
      reader.setErrorHandler(this);
      reader.setEntityResolver(localResolver);

      // Do XML Schema validation.
      reader.parse(new InputSource(new FileReader(inFile)));
    } catch (SAXException ex) {
      ex.printStackTrace();
      System.exit(-1);
    } catch (ParserConfigurationException ex) {
      ex.printStackTrace();
      System.exit(-1);
    } catch (FileNotFoundException ex) {
      ex.printStackTrace();
      System.exit(-1);
    } catch (IOException ex) {
      ex.printStackTrace();
      System.exit(-1);
    }

    return 0;
  }