private Validator newValidator(Schema schema) { Validator validator = schema.newValidator(); validator.setResourceResolver(resourceResolver); return validator; }
public static List validate( Source xml, URL schemaURL, boolean skipTargetNamespaceException, EntityResolver entityResolver) { try { Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) .newSchema(schemaURL); Validator v = schema.newValidator(); if (entityResolver != null) { v.setResourceResolver( new EntityResolverToLSResourceResolver( v.getResourceResolver(), entityResolver)); } Handler handler = new Handler(skipTargetNamespaceException, entityResolver); v.setErrorHandler(handler); v.validate(xml); return handler.errors; } catch (SAXException e) { return exception(e); } catch (IOException e) { return exception(e); } }
private Validator newValidator(Schema schema) { Validator validator = schema.newValidator(); validator.setResourceResolver(resourceResolver); return validator; }
private Validator getValidator(Schema schema) { if (schema != null) { Validator validator = schema.newValidator(); validator.setResourceResolver(new DescriptorLSResourceResolver(this)); return validator; } return null; }
/** * Creates a new validator. * @param resolver The resolver for resources * @return A new validator */ private static Validator newValidator(final LSResourceResolver resolver) { try { final Validator validator = SchemaFactory .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) .newSchema() .newValidator(); validator.setResourceResolver(resolver); return validator; } catch (final SAXException ex) { throw new IllegalStateException(ex); } } }
/** * Creates a new validator. * @param resolver The resolver for resources * @return A new validator */ private static Validator newValidator(final LSResourceResolver resolver) { try { final Validator validator = SchemaFactory .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) .newSchema() .newValidator(); validator.setResourceResolver(resolver); return validator; } catch (final SAXException ex) { throw new IllegalStateException(ex); } } }
private Validator getValidator(Schema schema) { if (schema != null) { Validator validator = schema.newValidator(); validator.setResourceResolver(new DescriptorLSResourceResolver(this)); return validator; } return null; }
public void parseAndValidate(File xmlFile, ContentHandler handler) { SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); schemaFactory.setResourceResolver(new MySchemaResolver()); Schema schema = schemaFactory.newSchema(); Validator v = schema.newValidator(); v.setResourceResolver(schemaFactory.getResourceResolver()); InputSource is = new InputSource(new FileInputStream(xmlFile)); v.validate(new SAXSource(is), new SAXResult(handler)); }
protected void validateSchema(final ValidationMethod method, final Resolver resolver, final byte[] schemaData, byte[] xmlData) throws SAXException, IOException { final SchemaFactory schemaFactory = SchemaFactory.newInstance(method.schemaLanguage); schemaFactory.setResourceResolver(resolver); final Schema schema = schemaFactory.newSchema(new StreamSource(new ByteArrayInputStream(schemaData))); final Validator validator = schema.newValidator(); validator.setErrorHandler(new ErrorHandler()); validator.setResourceResolver(resolver); validator.validate(new StreamSource(new ByteArrayInputStream(xmlData))); }
private ResultadoValidacao valide(TipoSchema tipoSchema, Source source) throws IOException { ResultadoValidacao res = new ResultadoValidacao(); Schema schema = schemas.get(tipoSchema); Validator validator = schema.newValidator(); validator.setErrorHandler(res); validator.setResourceResolver(resourceResolver); try { validator.validate(source); } catch (SAXException e) { throw new IllegalStateException("Unexpected exception", e); } return res; }
/** * Validate the supplied source against the namespaces referenced in it. * @throws org.xml.sax.SAXException Validation error. * @throws java.io.IOException Error reading the XSD Sources. */ public void validate(Source source) throws SAXException, IOException { AssertArgument.isNotNull(source, "source"); if (schema == null) { throw new IllegalStateException("Invalid call to validate. XSD sources not set."); } // Create the merged Schema instance and from that, create the Validator instance... Validator validator = schema.newValidator(); if(schemaSourceResolver != null) { validator.setResourceResolver(schemaSourceResolver); } if(errorHandler != null) { validator.setErrorHandler(errorHandler); } // Validate the source... validator.validate(source); }
private void initialize() { if (prismContext == null) { throw new IllegalStateException("No prism context set during validator initialization"); } SchemaRegistry schemaRegistry = prismContext.getSchemaRegistry(); midPointJavaxSchema = schemaRegistry.getJavaxSchema(); xsdValidator = midPointJavaxSchema.newValidator(); xsdValidator.setResourceResolver(prismContext.getEntityResolver()); }
/** * Validate the supplied source against the namespaces referenced in it. * @throws org.xml.sax.SAXException Validation error. * @throws java.io.IOException Error reading the XSD Sources. */ public void validate(Source source) throws SAXException, IOException { AssertArgument.isNotNull(source, "source"); if (schema == null) { throw new IllegalStateException("Invalid call to validate. XSD sources not set."); } // Create the merged Schema instance and from that, create the Validator instance... Validator validator = schema.newValidator(); if(schemaSourceResolver != null) { validator.setResourceResolver(schemaSourceResolver); } if(errorHandler != null) { validator.setErrorHandler(errorHandler); } // Validate the source... validator.validate(source); }
/** * Validate the supplied source against the namespaces referenced in it. * @throws org.xml.sax.SAXException Validation error. * @throws java.io.IOException Error reading the XSD Sources. */ public void validate(Source source) throws SAXException, IOException { AssertArgument.isNotNull(source, "source"); if (schema == null) { throw new IllegalStateException("Invalid call to validate. XSD sources not set."); } // Create the merged Schema instance and from that, create the Validator instance... Validator validator = schema.newValidator(); if(schemaSourceResolver != null) { validator.setResourceResolver(schemaSourceResolver); } if(errorHandler != null) { validator.setErrorHandler(errorHandler); } // Validate the source... validator.validate(source); }
public void validate( List<XmlSchemaIndex> schemaIndexes, IType type, Source source, String description ) { try { Schema schema = XmlSchemaIndex.parseSchemas( schemaIndexes ); Validator validator = schema.newValidator(); validator.setResourceResolver( new XmlSchemaLocalResourceResolver() ); validator.validate( source ); } catch ( SAXParseException ex ) { throw new XmlException( "Failed to validate XML from " + description + " using schema root " + type.getName() + " at line " + ex.getLineNumber() + " column " + ex.getColumnNumber(), ex ); } catch ( Exception ex ) { throw new XmlException( "Failed to validate XML from " + description + " using schema root " + type.getName(), ex ); } }
public List<ValidationError> processValidation(Source[] xsdSources, File inputXML) throws ValidationException { ValidationHandler handler = new ValidationHandler(); try { SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); schemaFactory.setErrorHandler(handler); Schema schemaGrammar = schemaFactory.newSchema(xsdSources); Resolver resolver = new Resolver(); Validator schemaValidator = schemaGrammar.newValidator(); schemaValidator.setErrorHandler(handler); schemaValidator.setResourceResolver(resolver); schemaValidator.validate(new StreamSource(inputXML)); for (int i = 0; i < xsdSources.length; i++) { xsdSources[i] = null; } return handler.getErrors(); } catch (SAXException sae) { return handler.getErrors(); } catch (IOException ioe) { throw new ValidationException("Validation error", ioe); } }
public List<ValidationError> processValidation(Source[] xsdSources, File inputXML) throws ValidationException { ValidationHandler handler = new ValidationHandler(); try { SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); schemaFactory.setErrorHandler(handler); Schema schemaGrammar = schemaFactory.newSchema(xsdSources); Resolver resolver = new Resolver(); Validator schemaValidator = schemaGrammar.newValidator(); schemaValidator.setErrorHandler(handler); schemaValidator.setResourceResolver(resolver); schemaValidator.validate(new StreamSource(inputXML)); for (int i = 0; i < xsdSources.length; i++) { xsdSources[i] = null; } return handler.getErrors(); } catch (SAXException sae) { return handler.getErrors(); } catch (IOException ioe) { throw new ValidationException("Validation error", ioe); } }
private void validate(String[] schemaList) { // Create a new validator Validator validator = createSchema(schemaList).newValidator(); setFeature(validator, Constants.XERCES_FEATURE_PREFIX + "continue-after-fatal-error", true); validator.setErrorHandler(new MessageHandler()); validator.setResourceResolver(new SchemaResolver()); // Validate and catch the exceptions. MessageHandler will receive the errors and warnings. try { LOG.info("Validate " + getWebSourceCode() + " with schema " + StringUtils.join(schemaList, ",")); validator.validate(new StreamSource(getWebSourceCode().createInputStream())); } catch (SAXException e) { if (!containsMessage(e)) { createViolation(0, e.getMessage()); } } catch (IOException e) { throw new SonarException(e); } catch (UnrecoverableParseError e) { // ignore, message already reported. } }
private void serializeAndValidate(PrismObject<UserType> user, PrismContext prismContext) throws SchemaException, SAXException, IOException { String xmlString = prismContext.serializeObjectToString(user, PrismContext.LANG_XML); System.out.println("Serialized XML"); System.out.println(xmlString); Document xmlDocument = DOMUtil.parseDocument(xmlString); Schema javaxSchema = prismContext.getSchemaRegistry().getJavaxSchema(); Validator validator = javaxSchema.newValidator(); validator.setResourceResolver(prismContext.getEntityResolver()); validator.validate(new DOMSource(xmlDocument)); }
@Test public void test200ExportUsers() throws Exception { final String TEST_NAME = "test200ExportUsers"; displayTestTitle(TEST_NAME); // GIVEN Task task = createTask(TEST_NAME); OperationResult result = task.getResult(); // WHEN displayWhen(TEST_NAME); List<PrismObject<UserType>> users = modelService.searchObjects(UserType.class, null, SelectorOptions.createCollection(GetOperationOptions.createRaw()), task, result); // THEN displayThen(TEST_NAME); assertSuccess(result); assertEquals("Unexpected number of users", 5, users.size()); for (PrismObject<UserType> user: users) { display("Exporting user", user); assertNotNull("Null definition in "+user, user.getDefinition()); display("Definition", user.getDefinition()); String xmlString = prismContext.serializerFor(PrismContext.LANG_XML).serialize(user); display("Exported user", xmlString); Document xmlDocument = DOMUtil.parseDocument(xmlString); Schema javaxSchema = prismContext.getSchemaRegistry().getJavaxSchema(); Validator validator = javaxSchema.newValidator(); validator.setResourceResolver(prismContext.getEntityResolver()); validator.validate(new DOMSource(xmlDocument)); PrismObject<Objectable> parsedUser = prismContext.parseObject(xmlString); assertTrue("Re-parsed user is not equal to original: "+user, user.equals(parsedUser)); } }