SchemaFactory schemaFactory = SchemaFactory .newInstance(XMLConstants.XML_DTD_NS_URI); Schema schema = schemaFactory.newSchema(new File( "xmlValidate.dtd")); Validator validator = schema.newValidator(); validator.validate(new StreamSource("xmlValidate.xml"));
/** * 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())); }
builderFactory.setNamespaceAware(true); DocumentBuilder parser = builderFactory.newDocumentBuilder(); Document document = parser.parse(xmlFile); SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Source schemaFile = new StreamSource(xsdFile); Schema schema = factory.newSchema(schemaFile); Validator validator = schema.newValidator(); validator.validate(new DOMSource(document)); throw new OpenClinicaSystemException(e.getMessage(), e.getCause()); } catch (ParserConfigurationException pce) { throw new OpenClinicaSystemException(pce.getMessage(), pce.getCause());
@Override public String isValid(String xml) { String errorMessage = ""; try { try { schema.newValidator().validate(new StreamSource(new ByteArrayInputStream(xml.getBytes(UTF_8)))); } catch (SAXException e) { errorMessage = e.getMessage(); } } catch (Exception e) { mockServerLogger.error("Exception validating JSON", e); return e.getClass().getSimpleName() + " - " + e.getMessage(); } return errorMessage; } }
Validator v = schema.newValidator(); final SAXException[] error = {null}; v.setErrorHandler(new ErrorHandler() { public @Override void warning(SAXParseException x) throws SAXException {} public @Override void error(SAXParseException x) throws SAXException { v.validate(new DOMSource(fixupAttrs(data))); } catch (IOException x) { assert false : x;
/** * Validates a Role DOM against the XMLSchema. The schema is determined by the version of the * Role DOM * * @param doc * @throws IOException */ public void validateRoleRegistry(Document doc) throws IOException { if (versionMapRR == null) initializeSchemataRR(); XPathExpression expr = XMLXpathFactory.Singleton.getVersionExpressionRR(); String versionString; try { versionString = expr.evaluate(doc); } catch (XPathExpressionException e) { throw new IOException(e); } Schema schema = versionMapRR.get(versionString); Validator val = schema.newValidator(); try { val.validate(new DOMSource(doc)); } catch (SAXException e) { throw new IOException(e); } }
public void validateModel(InputStreamProvider inputStreamProvider) throws Exception { Schema schema = createSchema(); Validator validator = schema.newValidator(); validator.validate(new StreamSource(inputStreamProvider.getInputStream())); }
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); } }
Validator validator = schema.newValidator(); MyErrorHandler handler = new MyErrorHandler(theContext); validator.setErrorHandler(handler); String encodedResource; if (theContext.getResourceAsStringEncoding() == EncodingEnum.XML) { validator.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, ""); validator.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, ""); }catch (SAXNotRecognizedException ex){ ourLog.warn("Jaxp 1.5 Support not found.",ex); validator.validate(new StreamSource(new StringReader(encodedResource))); } catch (SAXParseException e) { SingleValidationMessage message = new SingleValidationMessage();
public SAXParseException[] validate(Source source) throws IOException { javax.xml.validation.Validator validator = schema.newValidator(); ValidationErrorHandler errorHandler = new ValidationErrorHandler(); validator.setErrorHandler(errorHandler); try { validator.validate(source); return errorHandler.getErrors(); } catch (SAXException ex) { throw new XmlValidationException("Could not validate source: " + ex.getMessage(), ex); } } }
private Validator getValidator(URL xmlSchema) throws SAXException { SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = sf.newSchema(xmlSchema); Validator validator = schema.newValidator(); validator.setErrorHandler(new ErrorHandler()); return validator; }
public ValidatorHandler newValidator() { synchronized(this) { if(schema==null) { try { schema = SchemaFactory.newInstance(WellKnownNamespace.XML_SCHEMA).newSchema(source); } catch (SAXException e) { // we make sure that the schema is correct before we ship. throw new AssertionError(e); } } } ValidatorHandler handler = schema.newValidatorHandler(); fixValidatorBug6246922(handler); return handler; }
public void validateModel(XMLStreamReader xmlStreamReader) throws Exception { Schema schema = createSchema(); Validator validator = schema.newValidator(); validator.validate(new StAXSource(xmlStreamReader)); }
@Test public void validatorProperties() throws Exception { SetPropertyAnswer setPropertyAnswer = new SetPropertyAnswer(schemaFactory.newSchema().newValidator()); doAnswer(setPropertyAnswer).when(validatorWrapper).setProperty(anyString(), anyObject()); defaultXMLSecureFactories.configureValidator(validatorWrapper); assertThat(setPropertyAnswer.exception, is(nullValue())); for (String property : VALIDATOR_PROPERTIES) { verify(validatorWrapper).setProperty(property, ""); } }
private Validator newValidator(Schema schema) { Validator validator = schema.newValidator(); validator.setResourceResolver(resourceResolver); return validator; }
DSL.using(c) .fetch(sql) .map(r -> new Schema( r.getValue("SCHEMA_NAME", String.class), r.getValue("IS_DEFAULT", boolean.class) ))
@Override public String isValid(String xml) { String errorMessage = ""; try { try { schema.newValidator().validate(new StreamSource(new ByteArrayInputStream(xml.getBytes(UTF_8)))); } catch (SAXException e) { errorMessage = e.getMessage(); } } catch (Exception e) { mockServerLogger.error("Exception validating JSON", e); return e.getClass().getSimpleName() + " - " + e.getMessage(); } return errorMessage; } }
private void validate(Document document) { // todo : add ability to disable validation... final String version = document.getDocumentElement().getAttribute( "version" ); final Validator validator = new ConfigXsdSupport().jpaXsd( version ).getSchema().newValidator(); List<SAXException> errors = new ArrayList<>(); validator.setErrorHandler( new ErrorHandlerImpl( errors ) ); try { validator.validate( new DOMSource( document ) ); } catch (SAXException e) { errors.add( e ); } catch (IOException e) { throw new PersistenceException( "Unable to validate persistence.xml", e ); } if ( errors.size() != 0 ) { //report all errors in the exception StringBuilder errorMessage = new StringBuilder( ); for ( SAXException error : errors ) { errorMessage.append( extractInfo( error ) ).append( '\n' ); } throw new PersistenceException( "Invalid persistence.xml.\n" + errorMessage.toString() ); } }
/** * Validates a User/Group DOM against the XMLSchema. The schema is determined by the version of * the User/Group DOM * * @param doc * @throws IOException */ public void validateUserGroupRegistry(Document doc) throws IOException { if (versionMapUR == null) initializeSchemataUR(); XPathExpression expr = XMLXpathFactory.Singleton.getVersionExpressionUR(); String versionString = null; try { versionString = expr.evaluate(doc); } catch (XPathExpressionException e) { throw new IOException(e); // this should not happen } Schema schema = versionMapUR.get(versionString); Validator val = schema.newValidator(); try { val.validate(new DOMSource(doc)); } catch (SAXException e) { throw new IOException(e); // this should not happen } }
@Override public SAXParseException[] validate(Source source, ValidationErrorHandler errorHandler) throws IOException { if (errorHandler == null) { errorHandler = new DefaultValidationErrorHandler(); } Validator validator = schema.newValidator(); validator.setErrorHandler(errorHandler); try { validator.validate(source); return errorHandler.getErrors(); } catch (SAXException ex) { throw new XmlValidationException("Could not validate source: " + ex.getMessage(), ex); } } }