Java Code Examples for javax.xml.validation.Schema#newValidatorHandler()

The following examples show how to use javax.xml.validation.Schema#newValidatorHandler() . 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: 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 2
Source File: TypeInfoProviderTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test() throws SAXException, ParserConfigurationException, IOException {

    SchemaFactory sf = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(new File(XML_DIR + "shiporder11.xsd"));
    validatorHandler = schema.newValidatorHandler();
    MyDefaultHandler myDefaultHandler = new MyDefaultHandler();
    validatorHandler.setContentHandler(myDefaultHandler);

    InputSource is = new InputSource(filenameToURL(XML_DIR + "shiporder11.xml"));

    SAXParserFactory parserFactory = SAXParserFactory.newInstance();
    parserFactory.setNamespaceAware(true);
    XMLReader xmlReader = parserFactory.newSAXParser().getXMLReader();
    xmlReader.setContentHandler(validatorHandler);
    xmlReader.parse(is);

}
 
Example 3
Source File: Bug4969732.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 4
Source File: ValidatingUnmarshaller.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new instance of ValidatingUnmarshaller.
 */
public ValidatingUnmarshaller( Schema schema, XmlVisitor next ) {
    this.validator = schema.newValidatorHandler();
    this.next = next;
    this.predictor = next.getPredictor();
    // if the user bothers to use a validator, make validation errors fatal
    // so that it will abort unmarshalling.
    validator.setErrorHandler(new FatalAdapter(getContext()));
}
 
Example 5
Source File: ValidatingUnmarshaller.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new instance of ValidatingUnmarshaller.
 */
public ValidatingUnmarshaller( Schema schema, XmlVisitor next ) {
    this.validator = schema.newValidatorHandler();
    this.next = next;
    this.predictor = next.getPredictor();
    // if the user bothers to use a validator, make validation errors fatal
    // so that it will abort unmarshalling.
    validator.setErrorHandler(new FatalAdapter(getContext()));
}
 
Example 6
Source File: ValidatingUnmarshaller.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new instance of ValidatingUnmarshaller.
 */
public ValidatingUnmarshaller( Schema schema, XmlVisitor next ) {
    this.validator = schema.newValidatorHandler();
    this.next = next;
    this.predictor = next.getPredictor();
    // if the user bothers to use a validator, make validation errors fatal
    // so that it will abort unmarshalling.
    validator.setErrorHandler(new FatalAdapter(getContext()));
}
 
Example 7
Source File: Bug5011500.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@BeforeMethod
public void setUp() throws Exception {
    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);

    this.validatorHandler = schema.newValidatorHandler();
    this.validator = schema.newValidator();
}
 
Example 8
Source File: ValidatingUnmarshaller.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new instance of ValidatingUnmarshaller.
 */
public ValidatingUnmarshaller( Schema schema, XmlVisitor next ) {
    this.validator = schema.newValidatorHandler();
    this.next = next;
    this.predictor = next.getPredictor();
    // if the user bothers to use a validator, make validation errors fatal
    // so that it will abort unmarshalling.
    validator.setErrorHandler(new FatalAdapter(getContext()));
}
 
Example 9
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 10
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 11
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 12
Source File: Bug4970402.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 13
Source File: ValidatingUnmarshaller.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new instance of ValidatingUnmarshaller.
 */
public ValidatingUnmarshaller( Schema schema, XmlVisitor next ) {
    this.validator = schema.newValidatorHandler();
    this.next = next;
    this.predictor = next.getPredictor();
    // if the user bothers to use a validator, make validation errors fatal
    // so that it will abort unmarshalling.
    validator.setErrorHandler(new FatalAdapter(getContext()));
}
 
Example 14
Source File: ValidatingUnmarshaller.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new instance of ValidatingUnmarshaller.
 */
public ValidatingUnmarshaller( Schema schema, XmlVisitor next ) {
    this.validator = schema.newValidatorHandler();
    this.next = next;
    this.predictor = next.getPredictor();
    // if the user bothers to use a validator, make validation errors fatal
    // so that it will abort unmarshalling.
    validator.setErrorHandler(new FatalAdapter(getContext()));
}
 
Example 15
Source File: ValidatingUnmarshaller.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new instance of ValidatingUnmarshaller.
 */
public ValidatingUnmarshaller( Schema schema, XmlVisitor next ) {
    this.validator = schema.newValidatorHandler();
    this.next = next;
    this.predictor = next.getPredictor();
    // if the user bothers to use a validator, make validation errors fatal
    // so that it will abort unmarshalling.
    validator.setErrorHandler(new FatalAdapter(getContext()));
}
 
Example 16
Source File: ValidatingUnmarshaller.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new instance of ValidatingUnmarshaller.
 */
public ValidatingUnmarshaller( Schema schema, XmlVisitor next ) {
    this.validator = schema.newValidatorHandler();
    this.next = next;
    this.predictor = next.getPredictor();
    // if the user bothers to use a validator, make validation errors fatal
    // so that it will abort unmarshalling.
    validator.setErrorHandler(new FatalAdapter(getContext()));
}
 
Example 17
Source File: JavaxXmlValidator.java    From iaf with Apache License 2.0 4 votes vote down vote up
@Override
public ValidatorHandler getValidatorHandler(IPipeLineSession session, ValidationContext context) throws ConfigurationException, PipeRunException {
	Schema schema=getSchemaObject(context.getSchemasId(), schemasProvider.getSchemas(session));
	return schema.newValidatorHandler();
}
 
Example 18
Source File: XmlAligner.java    From iaf with Apache License 2.0 4 votes vote down vote up
protected static ValidatorHandler getValidatorHandler(URL schemaURL) throws SAXException {
	SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
	Schema schema = sf.newSchema(schemaURL); 
	return schema.newValidatorHandler();
}
 
Example 19
Source File: TestDomTreeAligner.java    From iaf with Apache License 2.0 4 votes vote down vote up
@Override
	public void testFiles(String schemaFile, String namespace, String rootElement, String inputFile, boolean potentialCompactionProblems, String expectedFailureReason) throws Exception {
		URL schemaUrl=getSchemaURL(schemaFile);
    	String xsdUri=schemaUrl.toExternalForm();
    	Schema schema = Utils.getSchemaFromResource(schemaUrl);

    	XMLSchemaLoader xsLoader = new XMLSchemaLoader();
		XSModel xsModel = xsLoader.loadURI(xsdUri);
		List<XSModel> schemaInformation= new LinkedList<XSModel>();
		schemaInformation.add(xsModel);
   	
    	String xmlString=getTestFile(inputFile+".xml");
    	
    	boolean expectValid=expectedFailureReason==null;
    	
    	assertEquals("valid XML", expectValid, Utils.validate(schemaUrl, xmlString));
 	

    	ValidatorHandler validator = schema.newValidatorHandler();
    	DomTreeAligner aligner = new DomTreeAligner(validator, schemaInformation);
 //   	Xml2Json xml2json = new Xml2Json(aligner, false);
    	
    	validator.setContentHandler(aligner);
//    	aligner.setContentHandler(xml2json);
 
    	Document domIn = Utils.string2Dom(xmlString);
    	System.out.println("input DOM ["+Utils.dom2String1(domIn)+"]");
//    	System.out.println("xmlString "+xmlString);
//    	System.out.println("dom in "+ToStringBuilder.reflectionToString(domIn.getDocumentElement()));
    	Source source=aligner.asSource(domIn);
    	System.out.println();
    	System.out.println("start aligning "+inputFile);
   	
		
    	String xmlAct = Utils.source2String(source);
    	System.out.println("xml aligned via source="+xmlAct);
    	assertNotNull("xmlAct is null",xmlAct);
    	assertTrue("XML is not aligned",  Utils.validate(schemaUrl, xmlAct));

//    	InputSource is = new InputSource(new StringReader(xmlString));
//    	try {
// //       	String jsonOut=xml2json.toString();
//        	System.out.println("jsonOut="+jsonOut);
//        	if (!expectValid) {
//    			fail("expected to fail");
//    		}
//    	} catch (Exception e) {
//    		if (expectValid) {
//    			e.printStackTrace();
//    			fail(e.getMessage());
//    		}
//    	}
//    	String xmlString=getTestXml(xml);
//    	String xsdUri=Utils.class.getResource(xsd).toExternalForm();
//    	
//    	assertEquals("valid XML", expectValid, Utils.validate(namespace, xsdUri, xmlString));
//    	
//    	Document dom = Utils.string2Dom(xmlString);
//    	Utils.clean(dom);
//    	
//    	XMLReader parser = new SAXParser();
//    	Schema schema=Utils.getSchemaFromResource(namespace, xsdUri);
//    	ValidatorHandler validator = schema.newValidatorHandler();
//    	XmlAligner instance = implementation.newInstance();
//    	instance.setPsviProvider((PSVIProvider)validator);
//    	
//    	parser.setContentHandler(validator);
//    	validator.setContentHandler(instance);
//    	
//    	System.out.println();
//    	System.out.println("start aligning "+xml);

//    	instance.parse(dom.getDocumentElement());
//    	System.out.println("alignedXml="+Utils.dom2String1(dom));
//    	assertTrue("valid aligned XML", Utils.validate(namespace, xsdUri, dom)); // only if dom  itself is modified...

//    	testXmlAligner(instance, dom.getDocumentElement(), namespace, xsdUri);

//    	JSONObject json=Utils.xml2Json(xmlString);
//    	System.out.println("JSON:"+json);
//    	String jsonString = json.toString();
//    	
//    	jsonString=jsonString.replaceAll(",\"xmlns\":\"[^\"]*\"", "");
//    	System.out.println("JSON with fixed xmlns:"+jsonString);
//    	
//    	String xmlFromJson = Utils.json2Xml(Utils.string2Json(jsonString));
//    	if (StringUtils.isNotEmpty(namespace)) {
//    		xmlFromJson=xmlFromJson.replaceFirst(">", " xmlns=\""+namespace+"\">");
//    	}
//    	System.out.println("start aligning xml from json "+xmlFromJson);
//    	Document domFromJson = Utils.string2Dom(xmlFromJson);
//    	instance.startParse(domFromJson.getDocumentElement());
    	
    }