javax.xml.validation.ValidatorHandler Java Examples

The following examples show how to use javax.xml.validation.ValidatorHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SCDBasedBindingSet.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Applies the additional binding customizations.
 */
public void apply(XSSchemaSet schema, ErrorReceiver errorReceiver) {
    if(topLevel!=null) {
        this.errorReceiver = errorReceiver;
        Unmarshaller u =  BindInfo.getCustomizationUnmarshaller();
        this.unmarshaller = u.getUnmarshallerHandler();
        ValidatorHandler v = BindInfo.bindingFileSchema.newValidator();
        v.setErrorHandler(errorReceiver);
        loader = new ForkContentHandler(v,unmarshaller);

        topLevel.applyAll(schema.getSchemas());

        this.loader = null;
        this.unmarshaller = null;
        this.errorReceiver = null;
    }
}
 
Example #2
Source File: SCDBasedBindingSet.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Applies the additional binding customizations.
 */
public void apply(XSSchemaSet schema, ErrorReceiver errorReceiver) {
    if(topLevel!=null) {
        this.errorReceiver = errorReceiver;
        Unmarshaller u =  BindInfo.getCustomizationUnmarshaller();
        this.unmarshaller = u.getUnmarshallerHandler();
        ValidatorHandler v = BindInfo.bindingFileSchema.newValidator();
        v.setErrorHandler(errorReceiver);
        loader = new ForkContentHandler(v,unmarshaller);

        topLevel.applyAll(schema.getSchemas());

        this.loader = null;
        this.unmarshaller = null;
        this.errorReceiver = null;
    }
}
 
Example #3
Source File: ValidatorHelper.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
protected static ValidatorHandler createValidatorForSchemaFiles(String... schemaFiles) throws TechnicalConnectorException {
   SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

   try {
      Source[] sources = new Source[0];

      for(int i = 0; i < schemaFiles.length; ++i) {
         String schemaFile = schemaFiles[i];
         InputStream in = SchemaValidatorHandler.class.getResourceAsStream(schemaFile);
         if (in == null) {
            throw new TechnicalConnectorException(TechnicalConnectorExceptionValues.ERROR_XML_INVALID, new Object[]{"Unable to find schemaFile " + schemaFile});
         }

         if (schemaFiles.length == 1) {
            sources = (Source[])((Source[])ArrayUtils.add(sources, new StreamSource(SchemaValidatorHandler.class.getResource(schemaFile).toExternalForm())));
         } else {
            Source source = new StreamSource(in);
            sources = (Source[])((Source[])ArrayUtils.add(sources, source));
         }
      }

      return schemaFactory.newSchema(sources).newValidatorHandler();
   } catch (Exception var7) {
      throw handleException(var7);
   }
}
 
Example #4
Source File: SchemaCache.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public ValidatorHandler newValidator() {
    synchronized(this) {
        if(schema==null) {
            try {
                // do not disable secure processing - these are well-known schemas
                SchemaFactory sf = XmlFactory.createSchemaFactory(XMLConstants.W3C_XML_SCHEMA_NS_URI, false);
                schema = allowExternalAccess(sf, "file", false).newSchema(source);
            } catch (SAXException e) {
                // we make sure that the schema is correct before we ship.
                throw new AssertionError(e);
            }
        }
    }

    ValidatorHandler handler = schema.newValidatorHandler();
    return handler;
}
 
Example #5
Source File: SchemaCache.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public ValidatorHandler newValidator() {
    synchronized(this) {
        if(schema==null) {
            try {
                // do not disable secure processing - these are well-known schemas
                SchemaFactory sf = XmlFactory.createSchemaFactory(XMLConstants.W3C_XML_SCHEMA_NS_URI, false);
                schema = allowExternalAccess(sf, "file", false).newSchema(source);
            } catch (SAXException e) {
                // we make sure that the schema is correct before we ship.
                throw new AssertionError(e);
            }
        }
    }

    ValidatorHandler handler = schema.newValidatorHandler();
    return handler;
}
 
Example #6
Source File: ValidatorHelper.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
protected static ValidatorHandler createValidatorForSchemaFiles(String... schemaFiles) throws TechnicalConnectorException {
   SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

   try {
      Source[] sources = new Source[0];

      for(int i = 0; i < schemaFiles.length; ++i) {
         String schemaFile = schemaFiles[i];
         InputStream in = SchemaValidatorHandler.class.getResourceAsStream(schemaFile);
         if (in == null) {
            throw new TechnicalConnectorException(TechnicalConnectorExceptionValues.ERROR_XML_INVALID, new Object[]{"Unable to find schemaFile " + schemaFile});
         }

         if (schemaFiles.length == 1) {
            sources = (Source[])((Source[])ArrayUtils.add(sources, new StreamSource(SchemaValidatorHandler.class.getResource(schemaFile).toExternalForm())));
         } else {
            Source source = new StreamSource(in);
            sources = (Source[])((Source[])ArrayUtils.add(sources, source));
         }
      }

      return schemaFactory.newSchema(sources).newValidatorHandler();
   } catch (Exception var7) {
      throw handleException(var7);
   }
}
 
Example #7
Source File: Json2Xml.java    From iaf with Apache License 2.0 6 votes vote down vote up
public static String translate(JsonStructure json, URL schemaURL, boolean compactJsonArrays, String rootElement, boolean strictSyntax, boolean deepSearch, String targetNamespace, Map<String,Object> overrideValues) throws SAXException, IOException {
	ValidatorHandler validatorHandler = getValidatorHandler(schemaURL);
	List<XSModel> schemaInformation = getSchemaInformation(schemaURL);

	// create the validator, setup the chain
	Json2Xml j2x = new Json2Xml(validatorHandler,schemaInformation,compactJsonArrays,rootElement,strictSyntax);
	if (overrideValues!=null) {
		j2x.setOverrideValues(overrideValues);
	}
	if (targetNamespace!=null) {
		//if (DEBUG) System.out.println("setting targetNamespace ["+targetNamespace+"]");
		j2x.setTargetNamespace(targetNamespace);
	}
	j2x.setDeepSearch(deepSearch);

	return j2x.translate(json);
}
 
Example #8
Source File: ValidatorHelper.java    From freehealth-connector with GNU Affero General Public License v3.0 6 votes vote down vote up
protected static ValidatorHandler createValidatorForSchemaFiles(String... schemaFiles) throws TechnicalConnectorException {
   SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

   try {
      Source[] sources = new Source[0];

      for(int i = 0; i < schemaFiles.length; ++i) {
         String schemaFile = schemaFiles[i];
         InputStream in = SchemaValidatorHandler.class.getResourceAsStream(schemaFile);
         if (in == null) {
            throw new TechnicalConnectorException(TechnicalConnectorExceptionValues.ERROR_XML_INVALID, new Object[]{"Unable to find schemaFile " + schemaFile});
         }

         if (schemaFiles.length == 1) {
            sources = (Source[])((Source[])ArrayUtils.add(sources, new StreamSource(SchemaValidatorHandler.class.getResource(schemaFile).toExternalForm())));
         } else {
            Source source = new StreamSource(in);
            sources = (Source[])((Source[])ArrayUtils.add(sources, source));
         }
      }

      return schemaFactory.newSchema(sources).newValidatorHandler();
   } catch (Exception var7) {
      throw handleException(var7);
   }
}
 
Example #9
Source File: SchemaCache.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public ValidatorHandler newValidator() {
    synchronized(this) {
        if(schema==null) {
            try {
                // do not disable secure processing - these are well-known schemas
                SchemaFactory sf = XmlFactory.createSchemaFactory(XMLConstants.W3C_XML_SCHEMA_NS_URI, false);
                schema = allowExternalAccess(sf, "file", false).newSchema(source);
            } catch (SAXException e) {
                // we make sure that the schema is correct before we ship.
                throw new AssertionError(e);
            }
        }
    }

    ValidatorHandler handler = schema.newValidatorHandler();
    return handler;
}
 
Example #10
Source File: SCDBasedBindingSet.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Applies the additional binding customizations.
 */
public void apply(XSSchemaSet schema, ErrorReceiver errorReceiver) {
    if(topLevel!=null) {
        this.errorReceiver = errorReceiver;
        Unmarshaller u =  BindInfo.getCustomizationUnmarshaller();
        this.unmarshaller = u.getUnmarshallerHandler();
        ValidatorHandler v = BindInfo.bindingFileSchema.newValidator();
        v.setErrorHandler(errorReceiver);
        loader = new ForkContentHandler(v,unmarshaller);

        topLevel.applyAll(schema.getSchemas());

        this.loader = null;
        this.unmarshaller = null;
        this.errorReceiver = null;
    }
}
 
Example #11
Source File: AbstractXmlValidator.java    From iaf with Apache License 2.0 6 votes vote down vote up
public String validate(Object input, IPipeLineSession session, String logPrefix, ValidatorHandler validatorHandler, XMLFilterImpl filter, ValidationContext context) throws XmlValidatorException, PipeRunException, ConfigurationException {

		if (filter != null) {
			// If a filter is present, connect its output to the context.contentHandler.
			// It is assumed that the filter input is already properly connected.
			filter.setContentHandler(context.getContentHandler());
			filter.setErrorHandler(context.getErrorHandler());
		} else {
			validatorHandler.setContentHandler(context.getContentHandler());
		}
		validatorHandler.setErrorHandler(context.getErrorHandler());

		InputSource is = getInputSource(Message.asMessage(input));

		return validate(is, validatorHandler, session, context);
	}
 
Example #12
Source File: EdfiRecordParserImpl2.java    From secure-data-service with Apache License 2.0 6 votes vote down vote up
private void parseAndValidate(InputStream input, Schema schema) throws XmlParseException, IOException {
    ValidatorHandler vHandler = schema.newValidatorHandler();
    vHandler.setContentHandler(this);
    vHandler.setErrorHandler(this);

    InputSource is = new InputSource(new InputStreamReader(input, "UTF-8"));
    is.setEncoding("UTF-8");

    try {
        XMLReader parser = XMLReaderFactory.createXMLReader();
        parser.setContentHandler(vHandler);
        parser.setErrorHandler(this);

        vHandler.setFeature("http://apache.org/xml/features/continue-after-fatal-error", false);

        parser.setFeature("http://apache.org/xml/features/validation/id-idref-checking", false);
        parser.setFeature("http://apache.org/xml/features/continue-after-fatal-error", false);
        parser.setFeature("http://xml.org/sax/features/external-general-entities", false);
        parser.setFeature("http://xml.org/sax/features/external-parameter-entities", false);

        parser.parse(is);
    } catch (SAXException e) {
        throw new XmlParseException(e.getMessage(), e);
    }
}
 
Example #13
Source File: SchemaCache.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public ValidatorHandler newValidator() {
    synchronized(this) {
        if(schema==null) {
            try {
                // do not disable secure processing - these are well-known schemas
                SchemaFactory sf = XmlFactory.createSchemaFactory(XMLConstants.W3C_XML_SCHEMA_NS_URI, false);
                schema = allowExternalAccess(sf, "file", false).newSchema(source);
            } catch (SAXException e) {
                // we make sure that the schema is correct before we ship.
                throw new AssertionError(e);
            }
        }
    }

    ValidatorHandler handler = schema.newValidatorHandler();
    return handler;
}
 
Example #14
Source File: SCDBasedBindingSet.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Applies the additional binding customizations.
 */
public void apply(XSSchemaSet schema, ErrorReceiver errorReceiver) {
    if(topLevel!=null) {
        this.errorReceiver = errorReceiver;
        Unmarshaller u =  BindInfo.getCustomizationUnmarshaller();
        this.unmarshaller = u.getUnmarshallerHandler();
        ValidatorHandler v = BindInfo.bindingFileSchema.newValidator();
        v.setErrorHandler(errorReceiver);
        loader = new ForkContentHandler(v,unmarshaller);

        topLevel.applyAll(schema.getSchemas());

        this.loader = null;
        this.unmarshaller = null;
        this.errorReceiver = null;
    }
}
 
Example #15
Source File: SCDBasedBindingSet.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Applies the additional binding customizations.
 */
public void apply(XSSchemaSet schema, ErrorReceiver errorReceiver) {
    if(topLevel!=null) {
        this.errorReceiver = errorReceiver;
        Unmarshaller u =  BindInfo.getCustomizationUnmarshaller();
        this.unmarshaller = u.getUnmarshallerHandler();
        ValidatorHandler v = BindInfo.bindingFileSchema.newValidator();
        v.setErrorHandler(errorReceiver);
        loader = new ForkContentHandler(v,unmarshaller);

        topLevel.applyAll(schema.getSchemas());

        this.loader = null;
        this.unmarshaller = null;
        this.errorReceiver = null;
    }
}
 
Example #16
Source File: SchemaCache.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public ValidatorHandler newValidator() {
    synchronized(this) {
        if(schema==null) {
            try {
                // do not disable secure processing - these are well-known schemas
                SchemaFactory sf = XmlFactory.createSchemaFactory(XMLConstants.W3C_XML_SCHEMA_NS_URI, false);
                schema = allowExternalAccess(sf, "file", false).newSchema(source);
            } catch (SAXException e) {
                // we make sure that the schema is correct before we ship.
                throw new AssertionError(e);
            }
        }
    }

    ValidatorHandler handler = schema.newValidatorHandler();
    return handler;
}
 
Example #17
Source File: SCDBasedBindingSet.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Applies the additional binding customizations.
 */
public void apply(XSSchemaSet schema, ErrorReceiver errorReceiver) {
    if(topLevel!=null) {
        this.errorReceiver = errorReceiver;
        Unmarshaller u =  BindInfo.getCustomizationUnmarshaller();
        this.unmarshaller = u.getUnmarshallerHandler();
        ValidatorHandler v = BindInfo.bindingFileSchema.newValidator();
        v.setErrorHandler(errorReceiver);
        loader = new ForkContentHandler(v,unmarshaller);

        topLevel.applyAll(schema.getSchemas());

        this.loader = null;
        this.unmarshaller = null;
        this.errorReceiver = null;
    }
}
 
Example #18
Source File: SchemaCache.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public ValidatorHandler newValidator() {
    synchronized(this) {
        if(schema==null) {
            try {
                // do not disable secure processing - these are well-known schemas
                SchemaFactory sf = XmlFactory.createSchemaFactory(XMLConstants.W3C_XML_SCHEMA_NS_URI, false);
                schema = allowExternalAccess(sf, "file", false).newSchema(source);
            } catch (SAXException e) {
                // we make sure that the schema is correct before we ship.
                throw new AssertionError(e);
            }
        }
    }

    ValidatorHandler handler = schema.newValidatorHandler();
    return handler;
}
 
Example #19
Source File: SCDBasedBindingSet.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Applies the additional binding customizations.
 */
public void apply(XSSchemaSet schema, ErrorReceiver errorReceiver) {
    if(topLevel!=null) {
        this.errorReceiver = errorReceiver;
        Unmarshaller u =  BindInfo.getCustomizationUnmarshaller();
        this.unmarshaller = u.getUnmarshallerHandler();
        ValidatorHandler v = BindInfo.bindingFileSchema.newValidator();
        v.setErrorHandler(errorReceiver);
        loader = new ForkContentHandler(v,unmarshaller);

        topLevel.applyAll(schema.getSchemas());

        this.loader = null;
        this.unmarshaller = null;
        this.errorReceiver = null;
    }
}
 
Example #20
Source File: AbstractXmlValidator.java    From iaf with Apache License 2.0 5 votes vote down vote up
public String validate(InputSource inputSource, ValidatorHandler validatorHandler, IPipeLineSession session, ValidationContext context) throws XmlValidatorException {
	try {
		XmlUtils.parseXml(inputSource, validatorHandler, context.getErrorHandler());
	} catch (IOException | SAXException e) {
		return finalizeValidation(context, session, e);
	}
	return finalizeValidation(context, session, null);
}
 
Example #21
Source File: XmlTo.java    From iaf with Apache License 2.0 5 votes vote down vote up
public static void translate(String xml, URL schemaURL, DocumentContainer documentContainer) throws SAXException, IOException {

		ValidatorHandler validatorHandler = XmlAligner.getValidatorHandler(schemaURL);

		// create the parser, setup the chain
		XmlAligner aligner = new XmlAligner(validatorHandler);
		XmlTo<DocumentContainer> xml2object = new XmlTo<DocumentContainer>(aligner, documentContainer);
		aligner.setContentHandler(xml2object);

		XmlUtils.parseXml(xml, validatorHandler);
	}
 
Example #22
Source File: ValidatorHandlerTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void testFeature() throws SAXNotRecognizedException, SAXNotSupportedException {
    ValidatorHandler validatorHandler = getValidatorHandler();
    assertFalse(validatorHandler.getFeature(FEATURE_NAME), "The feature should be false by default.");

    validatorHandler.setFeature(FEATURE_NAME, true);
    assertTrue(validatorHandler.getFeature(FEATURE_NAME), "The feature should be false by default.");

}
 
Example #23
Source File: Bug4969042.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private ValidatorHandler createValidatorHandler(String xsd) throws SAXException {
    SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

    StringReader reader = new StringReader(xsd);
    StreamSource xsdSource = new StreamSource(reader);

    Schema schema = schemaFactory.newSchema(xsdSource);
    return schema.newValidatorHandler();
}
 
Example #24
Source File: Bug4970400.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test1() throws Exception {
    SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    ValidatorHandler validatorHandler = schemaFactory.newSchema().newValidatorHandler();
    validatorHandler.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
    validatorHandler.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
}
 
Example #25
Source File: Bug4970383.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test() throws Exception {
    SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    ValidatorHandler validatorHandler = schemaFactory.newSchema().newValidatorHandler();
    try {
        validatorHandler.setFeature(null, false);
        Assert.fail("should report an error");
    } catch (NullPointerException e) {
        ; // expected
    }
}
 
Example #26
Source File: ValidatorHandlerTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test(expectedExceptions = NullPointerException.class)
public void testGetNullProperty() throws SAXNotRecognizedException, SAXNotSupportedException {
    ValidatorHandler validatorHandler = getValidatorHandler();
    assertNotNull(validatorHandler);
    validatorHandler.getProperty(null);

}
 
Example #27
Source File: ValidatorHandlerTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testContentHandler() {
    ValidatorHandler validatorHandler = getValidatorHandler();
    assertNull(validatorHandler.getContentHandler(), "When ValidatorHandler is created, initially ContentHandler should not be set.");

    ContentHandler handler = new DefaultHandler();
    validatorHandler.setContentHandler(handler);
    assertSame(validatorHandler.getContentHandler(), handler);

    validatorHandler.setContentHandler(null);
    assertNull(validatorHandler.getContentHandler());

}
 
Example #28
Source File: Bug6509668.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private ValidatorHandler createValidatorHandler(String xsd) throws SAXException {
    SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

    InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(xsd.getBytes()));
    StreamSource xsdSource = new StreamSource(reader);

    Schema schema = schemaFactory.newSchema(xsdSource);
    return schema.newValidatorHandler();
}
 
Example #29
Source File: Bug4970951.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private ValidatorHandler createValidatorHandler(String xsd) throws SAXException {
    SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

    StringReader reader = new StringReader(xsd);
    StreamSource xsdSource = new StreamSource(reader);

    Schema schema = schemaFactory.newSchema(xsdSource);
    return schema.newValidatorHandler();
}
 
Example #30
Source File: ConfigReader.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Parses an xml config file and returns a Config object.
 *
 * @param xmlFile
 *        The xml config file which is passed by the user to annotation processing
 * @return
 *        A non null Config object
 */
private Config parseAndGetConfig (File xmlFile, ErrorHandler errorHandler, boolean disableSecureProcessing) throws SAXException, IOException {
    XMLReader reader;
    try {
        SAXParserFactory factory = XmlFactory.createParserFactory(disableSecureProcessing);
        reader = factory.newSAXParser().getXMLReader();
    } catch (ParserConfigurationException e) {
        // in practice this will never happen
        throw new Error(e);
    }
    NGCCRuntimeEx runtime = new NGCCRuntimeEx(errorHandler);

    // set up validator
    ValidatorHandler validator = configSchema.newValidator();
    validator.setErrorHandler(errorHandler);

    // the validator will receive events first, then the parser.
    reader.setContentHandler(new ForkContentHandler(validator,runtime));

    reader.setErrorHandler(errorHandler);
    Config config = new Config(runtime);
    runtime.setRootHandler(config);
    reader.parse(new InputSource(xmlFile.toURL().toExternalForm()));
    runtime.reset();

    return config;
}