List of usage examples for javax.xml.validation Schema newValidator
public abstract Validator newValidator();
From source file:com.maxl.java.aips2sqlite.Aips2Sqlite.java
static List<MedicalInformations.MedicalInformation> readAipsFile() { List<MedicalInformations.MedicalInformation> med_list = null; try {//from ww w .jav a2 s .co m JAXBContext context = JAXBContext.newInstance(MedicalInformations.class); // Validation SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = sf.newSchema(new File(Constants.FILE_MEDICAL_INFOS_XSD)); Validator validator = schema.newValidator(); validator.setErrorHandler(new MyErrorHandler()); // Marshaller /* * Marshaller ma = context.createMarshaller(); * ma.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); * MedicalInformations medi_infos = new MedicalInformations(); * ma.marshal(medi_infos, System.out); */ // Unmarshaller long startTime = System.currentTimeMillis(); if (CmlOptions.SHOW_LOGS) System.out.print("- Unmarshalling Swissmedic xml... "); FileInputStream fis = new FileInputStream(new File(Constants.FILE_MEDICAL_INFOS_XML)); Unmarshaller um = context.createUnmarshaller(); MedicalInformations med_infos = (MedicalInformations) um.unmarshal(fis); med_list = med_infos.getMedicalInformation(); long stopTime = System.currentTimeMillis(); if (CmlOptions.SHOW_LOGS) System.out.println(med_list.size() + " medis in " + (stopTime - startTime) / 1000.0f + " sec"); } catch (IOException e) { e.printStackTrace(); } catch (JAXBException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } return med_list; }
From source file:fr.cls.atoll.motu.library.misc.xml.XMLUtils.java
/** * Validate xml./*from ww w. ja v a 2 s . co m*/ * * @param inSchemas the in schemas * @param inXml the in xml * @param schemaLanguage the schema language * * @return the xML error handler * * @throws MotuException the motu exception */ public static XMLErrorHandler validateXML(InputStream[] inSchemas, InputStream inXml, String schemaLanguage) throws MotuException { // parse an XML document into a DOM tree Document document; // create a Validator instance, which can be used to validate an instance document Validator validator; XMLErrorHandler errorHandler = new XMLErrorHandler(); try { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); // Must enable namespace processing!!!!! try { documentBuilderFactory.setXIncludeAware(true); } catch (Exception e) { // Do Nothing } // documentBuilderFactory.setExpandEntityReferences(true); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); // document = documentBuilder.parse(new File(xmlUrl.toURI())); documentBuilder.setErrorHandler(errorHandler); document = documentBuilder.parse(inXml); // create a SchemaFactory capable of understanding WXS schemas SchemaFactory schemaFactory = SchemaFactory.newInstance(schemaLanguage); schemaFactory.setErrorHandler(errorHandler); // load a WXS schema, represented by a Schema instance Source[] schemaFiles = new Source[inSchemas.length]; // InputStream inShema = null; int i = 0; for (InputStream inSchema : inSchemas) { schemaFiles[i] = new StreamSource(inSchema); i++; } Schema schema = schemaFactory.newSchema(schemaFiles); validator = schema.newValidator(); validator.setErrorHandler(errorHandler); validator.validate(new DOMSource(document)); } catch (Exception e) { throw new MotuException(e); // instance document is invalid! } return errorHandler; }
From source file:de.fhg.iais.model.aip.util.XmlUtilsTest.java
private static Document parseAndValidate(String xml, URL xsd) { final SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema; try {/*ww w . j a v a2 s. c om*/ schema = factory.newSchema(xsd); } catch (final SAXException e) { throw new DbcException(e); } // final Document xmlDocument = XmlUtils.parse(xml); final Document xmlDocument = XmlProcessor.buildDocumentFrom(xml); final Validator validator = schema.newValidator(); try { validator.validate(new JDOMSource(xmlDocument)); return xmlDocument; } catch (final SAXException e) { throw new DbcException(e); } catch (final IOException e) { throw new DbcException(e); } }
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 va2s. c o 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.// w w w. j av a2 s . co m * * @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:edu.stanford.epad.common.util.EPADFileUtils.java
public static boolean isValidXmlUsingClassPathSchema(File f, String xsdSchema) { try {//from w w w. j a v a2 s .c om 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: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); }
From source file:net.geoprism.gis.sld.SLDValidator.java
public void validate(String sld) { try {//from w ww . jav a 2 s. c o m SchemaFactory f = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Source xsdS = new StreamSource(xsd); Schema schema = f.newSchema(xsdS); Validator v = schema.newValidator(); InputStream stream; stream = new ByteArrayInputStream(sld.getBytes("UTF-8")); Source s = new StreamSource(stream); v.validate(s); } catch (Throwable e) { log.error(sld, e); throw new ProgrammingErrorException("Invalid SLD", e); } }
From source file:gov.nih.nci.ncicb.cadsr.bulkloader.schema.validator.SchemaValidatorImpl.java
/** * // w w w .ja v a 2 s .c o m * @return <code>javax.xml.validation.Validator</code> * @throws SchemaValidationException */ private Validator getValidator() throws SAXException { Schema schema = getSchema(); Validator validator = schema.newValidator(); return validator; }