Package javax.xml.validation

Examples of javax.xml.validation.Validator


    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    docBuilderFactory.setNamespaceAware( true );
    final Schema v2Schema = SchemaFactory.newInstance( javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI )
        .newSchema( new StreamSource( getStreamFromClasspath( "persistence_2_0.xsd" ) ) );
    final Validator v2Validator = v2Schema.newValidator();
    final Schema v1Schema = SchemaFactory.newInstance( javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI )
        .newSchema( new StreamSource( getStreamFromClasspath( "persistence_1_0.xsd" ) ) );
    final Validator v1Validator = v1Schema.newValidator();

    InputSource source = new InputSource( is );
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    docBuilder.setEntityResolver( resolver );
    List<SAXParseException> errors = new ArrayList<SAXParseException>();
    Document doc = null;

    //first sparse document and collect syntaxic errors
    try {
      doc = docBuilder.parse( source );
    }
    catch ( SAXParseException e ) {
      errors.add( e );
    }

    if (errors.size() == 0) {
      v2Validator.setErrorHandler( new ErrorLogger( errors ) );
            LOG.trace("Validate with persistence_2_0.xsd schema on file " + configURL);
      v2Validator.validate( new DOMSource( doc ) );
      boolean isV1Schema = false;
      if ( errors.size() != 0 ) {
        //v2 fails, it could be because the file is v1.
                LOG.trace("Found error with persistence_2_0.xsd schema on file " + configURL);
        SAXParseException exception = errors.get( 0 );
        final String errorMessage = exception.getMessage();
        //is it a validation error due to a v1 schema validated by a v2
        isV1Schema = errorMessage.contains("1.0")
            && errorMessage.contains("2.0")
            && errorMessage.contains("version");

      }
      if (isV1Schema) {
                LOG.trace("Validate with persistence_1_0.xsd schema on file " + configURL);
        errors.clear();
        v1Validator.setErrorHandler( new ErrorLogger( errors ) );
        v1Validator.validate( new DOMSource( doc ) );
      }
    }
    if ( errors.size() != 0 ) {
      //report all errors in the exception
      StringBuilder errorMessage = new StringBuilder( );
View Full Code Here


      throw new InvalidMappingException( "Unable to read StAX source as dom4j Document for processing", origin, e );
    }
  }

  private void validateMapping(SupportedOrmXsdVersion xsdVersion, XMLEventReader staxEventReader, Origin origin) {
    final Validator validator = xsdVersion.getSchema().newValidator();
    final StAXSource staxSource;
    try {
      staxSource = new StAXSource( staxEventReader );
    }
    catch (XMLStreamException e) {
      throw new InvalidMappingException( "Unable to generate StAXSource from mapping", origin, e );
    }

    try {
      validator.validate( staxSource );
    }
    catch (SAXException e) {
      throw new InvalidMappingException( "SAXException performing validation", origin, e );
    }
    catch (IOException e) {
View Full Code Here

    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    docBuilderFactory.setNamespaceAware( true );
    final Schema v2Schema = SchemaFactory.newInstance( javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI )
        .newSchema( new StreamSource( getStreamFromClasspath( "persistence_2_0.xsd" ) ) );
    final Validator v2Validator = v2Schema.newValidator();
    final Schema v1Schema = SchemaFactory.newInstance( javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI )
        .newSchema( new StreamSource( getStreamFromClasspath( "persistence_1_0.xsd" ) ) );
    final Validator v1Validator = v1Schema.newValidator();

    InputSource source = new InputSource( is );
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    docBuilder.setEntityResolver( resolver );
    List<SAXParseException> errors = new ArrayList<SAXParseException>();
    Document doc = null;

    //first sparse document and collect syntaxic errors
    try {
      doc = docBuilder.parse( source );
    }
    catch ( SAXParseException e ) {
      errors.add( e );
    }

    if (errors.size() == 0) {
      v2Validator.setErrorHandler( new ErrorLogger( errors ) );
            LOG.trace("Validate with persistence_2_0.xsd schema on file " + configURL);
      v2Validator.validate( new DOMSource( doc ) );
      boolean isV1Schema = false;
      if ( errors.size() != 0 ) {
        //v2 fails, it could be because the file is v1.
                LOG.trace("Found error with persistence_2_0.xsd schema on file " + configURL);
        SAXParseException exception = errors.get( 0 );
        final String errorMessage = exception.getMessage();
        //is it a validation error due to a v1 schema validated by a v2
        isV1Schema = errorMessage.contains("1.0")
            && errorMessage.contains("2.0")
            && errorMessage.contains("version");

      }
      if (isV1Schema) {
                LOG.trace("Validate with persistence_1_0.xsd schema on file " + configURL);
        errors.clear();
        v1Validator.setErrorHandler( new ErrorLogger( errors ) );
        v1Validator.validate( new DOMSource( doc ) );
      }
    }
    if ( errors.size() != 0 ) {
      //report all errors in the exception
      StringBuilder errorMessage = new StringBuilder( );
View Full Code Here

    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    docBuilderFactory.setNamespaceAware( true );
    final Schema v2Schema = SchemaFactory.newInstance( javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI )
        .newSchema( new StreamSource( getStreamFromClasspath( "persistence_2_0.xsd" ) ) );
    final Validator v2Validator = v2Schema.newValidator();
    final Schema v1Schema = SchemaFactory.newInstance( javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI )
        .newSchema( new StreamSource( getStreamFromClasspath( "persistence_1_0.xsd" ) ) );
    final Validator v1Validator = v1Schema.newValidator();

    InputSource source = new InputSource( is );
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    docBuilder.setEntityResolver( resolver );
    List<SAXParseException> errors = new ArrayList<SAXParseException>();
    Document doc = null;

    //first sparse document and collect syntaxic errors
    try {
      doc = docBuilder.parse( source );
    }
    catch ( SAXParseException e ) {
      errors.add( e );
    }

    if (errors.size() == 0) {
      v2Validator.setErrorHandler( new ErrorLogger( errors ) );
            LOG.trace("Validate with persistence_2_0.xsd schema on file " + configURL);
      v2Validator.validate( new DOMSource( doc ) );
      boolean isV1Schema = false;
      if ( errors.size() != 0 ) {
        //v2 fails, it could be because the file is v1.
                LOG.trace("Found error with persistence_2_0.xsd schema on file " + configURL);
        SAXParseException exception = errors.get( 0 );
        final String errorMessage = exception.getMessage();
        //is it a validation error due to a v1 schema validated by a v2
        isV1Schema = errorMessage.contains("1.0")
            && errorMessage.contains("2.0")
            && errorMessage.contains("version");

      }
      if (isV1Schema) {
                LOG.trace("Validate with persistence_1_0.xsd schema on file " + configURL);
        errors.clear();
        v1Validator.setErrorHandler( new ErrorLogger( errors ) );
        v1Validator.validate( new DOMSource( doc ) );
      }
    }
    if ( errors.size() != 0 ) {
      //report all errors in the exception
      StringBuilder errorMessage = new StringBuilder( );
View Full Code Here

    private void parseXml(String xmlName) throws ParserConfigurationException, SAXException, IOException {
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        schemaFactory.setErrorHandler(new ErrorHandlerImpl());
        schemaFactory.setResourceResolver(DEFAULT_RESOURCE_RESOLVER);
        Schema schema = schemaFactory.newSchema(SCHEMAS);
        Validator validator = schema.newValidator();
        validator.setFeature("http://apache.org/xml/features/validation/schema", true);
        validator.setResourceResolver(DEFAULT_RESOURCE_RESOLVER);
        validator.validate(new StreamSource(getXmlFile(xmlName)));
    }
View Full Code Here

   public static void validate(final Node node, final String schemaFile) throws Exception
   {
      SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

      Schema schema = factory.newSchema(findResource(schemaFile));
      Validator validator = schema.newValidator();

      // validate the DOM tree
      try
      {
         validator.validate(new DOMSource(node));
      }
      catch (SAXException e)
      {
         XMLUtil.log.error("Invalid configuration", e);
View Full Code Here

    final ByteArrayOutputStream bos = new ByteArrayOutputStream(512);
    generateSchema(bos);
    try {
      final SpringApplicationService springApplicationService = (SpringApplicationService) u.unmarshal(xml);
      final Schema schema = schemaFactory.newSchema(new StreamSource(new ByteArrayInputStream(bos.toByteArray())));
      final Validator validator = schema.newValidator();
      validator.validate(new JAXBSource(jc, springApplicationService));
      return springApplicationService;
    } catch (SAXException | IOException e) {
      throw new IllegalArgumentException("Failed to parse XML. The XML must conform to the following schema:\n" + bos, e);
    }
  }
View Full Code Here

   public static void validate(final Node node, final String schemaFile) throws Exception
   {
      SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

      Schema schema = factory.newSchema(findResource(schemaFile));
      Validator validator = schema.newValidator();

      // validate the DOM tree
      try
      {
         validator.validate(new DOMSource(node));
      }
      catch (SAXException e)
      {
         XMLUtil.log.error("Invalid configuration", e);
View Full Code Here

                        "sqoop-action-0.2.xsd")));
                sources.add(new StreamSource(Thread.currentThread().getContextClassLoader().getResourceAsStream(
                        "ssh-action-0.1.xsd")));
                SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
                Schema schema = factory.newSchema(sources.toArray(new StreamSource[sources.size()]));
                Validator validator = schema.newValidator();
                validator.validate(new StreamSource(new FileReader(file)));
                System.out.println("Valid worflow-app");
            }
            catch (Exception ex) {
                throw new OozieCLIException("Invalid workflow-app, " + ex.toString(), ex);
            }
View Full Code Here

        assertNotNull(Services.get().get(SchemaService.class));
    }

    public void testWfSchema() throws Exception {
        SchemaService wss = Services.get().get(SchemaService.class);
        Validator validator = wss.getSchema(SchemaName.WORKFLOW).newValidator();
        validator.validate(new StreamSource(new StringReader(APP1)));
    }
View Full Code Here

TOP

Related Classes of javax.xml.validation.Validator

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.