Package javax.xml.validation

Examples of javax.xml.validation.Validator


   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)
      {
         HornetQClientLogger.LOGGER.errorOnXMLTransformInvalidConf(e);
View Full Code Here


            child.setTextContent(a.getName());
        }
    }

    void validate(Document doc) throws Exception {
        Validator validator = _xsd.newValidator();
        validator.validate(new DOMSource(doc));
    }
View Full Code Here

    private URL schemaUrl;
    private File schemaFile;

    public void process(Exchange exchange) throws Exception {
        Schema schema = getSchema();
        Validator validator = schema.newValidator();

        Source source = exchange.getIn().getBody(DOMSource.class);
        if (source == null) {
            throw new NoXmlBodyValidationException(exchange);
        }

        // create a new errorHandler and set it on the validator
        errorHandler.reset();
        validator.setErrorHandler(errorHandler);

        DOMResult result = new DOMResult();
        validator.validate(source, result);

        errorHandler.handleErrors(exchange, schema, result);
        /*
         * Fault fault = exchange.createFault(); if (errorHandler.hasErrors()) { //
         * set the schema and source document as properties on the fault
View Full Code Here

            schema = getSchema();
        } else {
            schema = createSchema();
        }

        Validator validator = schema.newValidator();

        // the underlying input stream, which we need to close to avoid locking files or other resources
        Source source = null;
        InputStream is = null;
        try {
            Result result = null;
            // only convert to input stream if really needed
            if (isInputStreamNeeded(exchange)) {
                is = getContentToValidate(exchange, InputStream.class);
                if (is != null) {
                    source = getSource(exchange, is);
                }
            } else {
                Object content = getContentToValidate(exchange);
                if (content != null) {
                    source = getSource(exchange, content);
                }
            }

            if (shouldUseHeader()) {
                if (source == null && isFailOnNullHeader()) {
                    throw new NoXmlHeaderValidationException(exchange, headerName);
                }
            } else {
                if (source == null && isFailOnNullBody()) {
                    throw new NoXmlBodyValidationException(exchange);
                }
            }

            //CAMEL-7036 We don't need to set the result if the source is an instance of StreamSource
            if (source instanceof DOMSource) {
                result = new DOMResult();
            } else if (source instanceof SAXSource) {
                result = new SAXResult();
            } else if (source instanceof StAXSource || source instanceof StreamSource) {
                result = null;
            }

            if (source != null) {
                // create a new errorHandler and set it on the validator
                // must be a local instance to avoid problems with concurrency (to be
                // thread safe)
                ValidatorErrorHandler handler = errorHandler.getClass().newInstance();
                validator.setErrorHandler(handler);

                try {
                    LOG.trace("Validating {}", source);
                    validator.validate(source, result);
                    handler.handleErrors(exchange, schema, result);
                } catch (SAXParseException e) {
                    // can be thrown for non well formed XML
                    throw new SchemaValidationException(exchange, schema, Collections.singletonList(e),
                            Collections.<SAXParseException>emptyList(),
View Full Code Here

                    schema = factory.newSchema(src);
                } catch (final SAXException ex) {
                    LOGGER.error("Error parsing Log4j schema", ex);
                }
                if (schema != null) {
                    final Validator validator = schema.newValidator();
                    try {
                        validator.validate(new StreamSource(new ByteArrayInputStream(buffer)));
                    } catch (final IOException ioe) {
                        LOGGER.error("Error reading configuration for validation", ioe);
                    } catch (final SAXException ex) {
                        LOGGER.error("Error validating configuration", ex);
                    }
View Full Code Here

                .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Source schemaSource = new StreamSource(filexsd);

        Schema schema = factory.newSchema(schemaSource);
        // validate source xml to xsd
        Validator validator = schema.newValidator();
        validator.validate(new StreamSource(filexml));

        // parse xml to dom
        DocumentBuilder parser = DocumentBuilderFactory.newInstance()
                .newDocumentBuilder();
        Document document = parser.parse(filexml);
View Full Code Here

                .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Source schemaSource = new StreamSource(filexsd);

        Schema schema = factory.newSchema(schemaSource);
        // validate source xml to xsd
        Validator validator = schema.newValidator();
        validator.validate(new StreamSource(filexml));

        // parse xml to dom
        DocumentBuilder parser = DocumentBuilderFactory.newInstance()
                .newDocumentBuilder();
        Document document = parser.parse(filexml);
View Full Code Here

      if (schemaURL != null)
      {
         try
         {
            Schema schema = factory.newSchema(schemaURL);
            Validator validator = schema.newValidator();
            Reporter reporter = new Reporter(url);
            validator.setErrorHandler(reporter);

            // Validate the document
            validator.validate(new StreamSource(url.openStream()));
            return reporter.valid;
         }
         catch (SAXException e)
         {
            log.error("Got a sax exception when doing XSD validation");
View Full Code Here

  }

  private void validate(Document document) {
    // todo : add ability to disable validation...

    final Validator validator;
    final String version = document.getDocumentElement().getAttribute( "version" );
    if ( "2.1".equals( version ) ) {
      validator = v21Schema().newValidator();
    }
    else if ( "2.0".equals( version ) ) {
      validator = v2Schema().newValidator();
    }
    else if ( "1.0".equalsversion ) ) {
      validator = v1Schema().newValidator();
    }
    else {
      throw new PersistenceException( "Unrecognized persistence.xml version [" + version + "]" );
    }

    List<SAXException> errors = new ArrayList<SAXException>();
    validator.setErrorHandler( new ErrorHandlerImpl( errors ) );
    try {
      validator.validate( new DOMSource( document ) );
    }
    catch (SAXException e) {
      errors.add( 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

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.