List of usage examples for javax.xml.validation Validator validate
public void validate(Source source) throws SAXException, IOException
From source file:org.psikeds.knowledgebase.xml.impl.XSDValidator.java
/** * Validate XML against specified XSD schmema.<br> * <b>Note:</b> The XML source/stream will not be closed. This must be * invoked by the caller afterwards!<br> * // w w w . ja v a2s . co m * @param xsd * Source for XSD-schema that will be used to validate the XML * @param xml * Source for XML * @throws SAXException * if XML is not valid against XSD * @throws IOException */ public static void validate(final Source xsd, final Source xml) throws SAXException, IOException { final SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); final Schema schema = factory.newSchema(xsd); final Validator validator = schema.newValidator(); validator.validate(xml); }
From source file:info.novatec.ita.check.config.StereotypeCheckReader.java
/** * Read and validate the given file to a configuration for stereotype check. * /* w w w. j a v a2s . co m*/ * @param file * The file to read. * @param additionalCheckCfg * a previously read configuration which may override parts of * the configuration read by the file. * @param readingAdditionalCfg * Are we reading the additionalCfg. * @return the configuration. * @throws XMLStreamException * @throws IllegalArgumentException * @throws SAXException * @throws IOException */ private static StereotypeCheckConfiguration read(File file, String checkstyleStereotypeXsd, StereotypeCheckConfiguration additionalCheckCfg, boolean readingAdditionalCfg) throws XMLStreamException, IllegalArgumentException, SAXException, IOException { // Validate with StreamSource because Stax Validation is not provided // with every implementation of JAXP SchemaFactory schemafactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = schemafactory .newSchema(StereotypeCheckReader.class.getClassLoader().getResource(checkstyleStereotypeXsd)); Validator validator = schema.newValidator(); validator.validate(new StreamSource(file)); // Parse with Stax XMLStreamReader reader = XMLInputFactory.newInstance() .createXMLStreamReader(new BufferedInputStream(new FileInputStream(file))); StereotypeCheckConfigurationReader delegate = new StereotypeCheckConfigurationReader(reader, additionalCheckCfg, readingAdditionalCfg); while (delegate.hasNext()) { delegate.next(); } return delegate.getConfig(); }
From source file:com.impetus.kundera.loader.PersistenceXMLLoader.java
/** * Validates an xml object graph against its schema. Therefore it reads the * version from the root tag and tries to load the related xsd file from the * classpath.//from w ww . j a v a 2 s.com * * @param xmlRootNode * root xml node of the document to validate * @throws InvalidConfigurationException * if the validation could not be performed or the xml graph is * invalid against the schema */ private static void validateDocumentAgainstSchema(final Document xmlRootNode) throws InvalidConfigurationException { final Element rootElement = xmlRootNode.getDocumentElement(); final String version = rootElement.getAttribute("version"); String schemaFileName = "persistence_" + version.replace(".", "_") + ".xsd"; try { final List validationErrors = new ArrayList(); final String schemaLanguage = XMLConstants.W3C_XML_SCHEMA_NS_URI; final StreamSource streamSource = new StreamSource(getStreamFromClasspath(schemaFileName)); final Schema schemaDefinition = SchemaFactory.newInstance(schemaLanguage).newSchema(streamSource); final Validator schemaValidator = schemaDefinition.newValidator(); schemaValidator.setErrorHandler(new ErrorLogger("XML InputStream", validationErrors)); schemaValidator.validate(new DOMSource(xmlRootNode)); if (!validationErrors.isEmpty()) { final String exceptionText = "persistence.xml is not conform against the supported schema definitions."; throw new InvalidConfigurationException(exceptionText); } } catch (SAXException e) { final String exceptionText = "Error validating persistence.xml against schema defintion, caused by: "; throw new InvalidConfigurationException(exceptionText, e); } catch (IOException e) { final String exceptionText = "Error opening xsd schema file. The given persistence.xml descriptor version " + version + " might not be supported yet."; throw new InvalidConfigurationException(exceptionText, e); } }
From source file:Main.java
/** * Check whether a DOM tree is valid according to a schema. Example of * usage:/*from www.j a va 2 s . c o m*/ * <pre> * Element fragment = ...; * SchemaFactory f = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); * Schema s = f.newSchema(This.class.getResource("something.xsd")); * try { * XMLUtil.validate(fragment, s); * // valid * } catch (SAXException x) { * // invalid * } * </pre> * * @param data a DOM tree * @param schema a parsed schema * @throws SAXException if validation failed * @since org.openide.util 7.17 */ public static void validate(Element data, Schema schema) throws SAXException { Validator v = schema.newValidator(); final SAXException[] error = { null }; v.setErrorHandler(new ErrorHandler() { @Override public void warning(SAXParseException x) throws SAXException { } @Override public void error(SAXParseException x) throws SAXException { // Just rethrowing it is bad because it will also print it to stderr. error[0] = x; } @Override public void fatalError(SAXParseException x) throws SAXException { error[0] = x; } }); try { v.validate(new DOMSource(fixupAttrs(data))); } catch (IOException x) { assert false : x; } if (error[0] != null) { throw error[0]; } }
From source file:edu.stanford.epad.common.util.EPADFileUtils.java
public static boolean isValidXmlUsingClassPathSchema(File f, String xsdSchema) { try {//w ww. j a v a2 s . c o m log.debug("xml:" + f.getName() + " xsd:" + xsdSchema); FileInputStream xml = new FileInputStream(f); InputStream xsd = null; try { xsd = EPADFileUtils.class.getClassLoader().getResourceAsStream(xsdSchema); SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = factory.newSchema(new StreamSource(xsd)); Validator validator = schema.newValidator(); validator.validate(new StreamSource(xml)); return true; } catch (SAXException ex) { log.info("Error: validating template/annotation " + ex.getMessage()); } catch (IOException e) { log.info("Error: validating template/annotation " + e.getMessage()); } } catch (IOException e) { log.info("Exception validating a file: " + f.getName()); } return false; }
From source file:edu.stanford.epad.common.util.EPADFileUtils.java
public static String validateXml(File f, String xsdSchema) throws Exception { try {/*from www .j av a 2 s .c o m*/ FileInputStream xml = new FileInputStream(f); InputStream xsd = null; try { xsd = new FileInputStream(xsdSchema); SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = factory.newSchema(new StreamSource(xsd)); Validator validator = schema.newValidator(); validator.validate(new StreamSource(xml)); return ""; } catch (SAXException ex) { log.info("Error: validating template/annotation " + ex.getMessage()); return ex.getMessage(); } catch (IOException e) { log.info("Error: validating template/annotation " + e.getMessage()); throw e; } } catch (IOException e) { log.info("Exception validating a file: " + f.getName()); throw e; } }
From source file:eu.europa.esig.dss.DSSXMLUtils.java
/** * This method allows to validate an XML against the XAdES XSD schema. * * @param streamSource {@code InputStream} XML to validate * @return null if the XSD validates the XML, error message otherwise */// www . j a va 2s . c o m public static String validateAgainstXSD(final StreamSource streamSource) { try { if (schema == null) { schema = getSchema(); } final Validator validator = schema.newValidator(); validator.validate(streamSource); return StringUtils.EMPTY; } catch (Exception e) { LOG.warn("Error during the XML schema validation!", e); return e.getMessage(); } }
From source file:eu.delving.x3ml.X3MLEngine.java
public static List<String> validateStream(InputStream inputStream) throws SAXException, IOException { Schema schema = schemaFactory().newSchema(new StreamSource(inputStream("x3ml_v1.0.xsd"))); Validator validator = schema.newValidator(); final List<String> errors = new ArrayList<String>(); validator.setErrorHandler(new ErrorHandler() { @Override//from w w w . j av a2 s.com public void warning(SAXParseException exception) throws SAXException { errors.add(errorMessage(exception)); } @Override public void error(SAXParseException exception) throws SAXException { errors.add(errorMessage(exception)); } @Override public void fatalError(SAXParseException exception) throws SAXException { errors.add(errorMessage(exception)); } }); StreamSource source = new StreamSource(inputStream); validator.validate(source); return errors; }
From source file:org.geowebcache.config.XMLConfiguration.java
static void validate(Node rootNode) throws SAXException, IOException { // Perform validation // TODO dont know why this one suddenly failed to look up, revert to // XMLConstants.W3C_XML_SCHEMA_NS_URI SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); InputStream is = XMLConfiguration.class.getResourceAsStream("geowebcache.xsd"); Schema schema = factory.newSchema(new StreamSource(is)); Validator validator = schema.newValidator(); // debugPrint(rootNode); DOMSource domSrc = new DOMSource(rootNode); validator.validate(domSrc);//w w w . j av a 2s .c om }
From source file:com.predic8.membrane.core.interceptor.schemavalidation.SOAPMessageValidatorInterceptor.java
private Outcome validateMessage(Exchange exc, Message msg) throws Exception { List<Exception> exceptions = new ArrayList<Exception>(); for (Validator validator : validators) { validator.validate(getSOAPBody(msg.getBodyAsStream())); SchemaValidatorErrorHandler handler = (SchemaValidatorErrorHandler) validator.getErrorHandler(); // the message must be valid for one schema embedded into WSDL if (handler.noErrors()) { return Outcome.CONTINUE; }/*w w w . j a va 2 s .co m*/ exceptions.add(handler.getException()); handler.reset(); } exc.setResponse(HttpUtil.createSOAPFaultResponse(getErrorMsg(exceptions))); return Outcome.ABORT; }