List of usage examples for javax.xml.validation SchemaFactory newInstance
public static SchemaFactory newInstance(String schemaLanguage)
From source file:org.kuali.kfs.module.tem.batch.PerDiemXmlInputFileType.java
/** * @see org.kuali.kfs.sys.batch.XmlBatchInputFileTypeBase#validateContentsAgainstSchema(java.lang.String, java.io.InputStream) */// www . j a va2s . c o m @Override protected void validateContentsAgainstSchema(String schemaLocation, InputStream fileContents) throws ParseException { try { // get schemaFile UrlResource schemaResource = new UrlResource(schemaLocation); // load a WXS schema, represented by a Schema instance Source schemaSource = new StreamSource(schemaResource.getInputStream()); // create a SchemaFactory capable of understanding WXS schemas SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = factory.newSchema(schemaSource); // create a validator instance, which can be used to validate an instance document Validator validator = schema.newValidator(); validator.setErrorHandler(new XmlErrorHandler()); Source source = this.transform(fileContents); validator.validate(source); } catch (MalformedURLException e2) { LOG.error("error getting schema url: " + e2.getMessage()); throw new RuntimeException("error getting schema url: " + e2.getMessage(), e2); } catch (SAXException e) { LOG.error("error encountered while parsing xml " + e.getMessage()); throw new ParseException("Schema validation error occured while processing file: " + e.getMessage(), e); } catch (IOException e1) { LOG.error("error occured while validating file contents: " + e1.getMessage()); throw new RuntimeException("error occurred while validating file contents: " + e1.getMessage(), e1); } }
From source file:org.kuali.kfs.sys.batch.XmlBatchInputFileTypeBase.java
/** * Validates the xml contents against the batch input type schema using the java 1.5 validation package. * /*from w w w . j a v a 2 s. co m*/ * @param schemaLocation - location of the schema file * @param fileContents - xml contents to validate against the schema */ protected void validateContentsAgainstSchema(String schemaLocation, InputStream fileContents) throws ParseException { // create a SchemaFactory capable of understanding WXS schemas SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); // get schemaFile Resource schemaResource = SpringContext.getResource(schemaLocation); // load a WXS schema, represented by a Schema instance Source schemaSource = null; try { schemaSource = new StreamSource(schemaResource.getInputStream()); } catch (IOException e2) { LOG.error("error getting schema stream from url: " + e2.getMessage()); throw new RuntimeException("error getting schema stream from url: " + e2.getMessage(), e2); } Schema schema = null; try { schema = factory.newSchema(schemaSource); } catch (SAXException e) { LOG.error("error occured while setting schema file: " + e.getMessage()); throw new RuntimeException("error occured while setting schema file: " + e.getMessage(), e); } // create a Validator instance, which can be used to validate an instance document Validator validator = schema.newValidator(); validator.setErrorHandler(new XmlErrorHandler()); // validate try { validator.validate(new StreamSource(fileContents)); } catch (SAXException e) { LOG.error("error encountered while parsing xml " + e.getMessage()); throw new ParseException("Schema validation error occured while processing file: " + e.getMessage(), e); } catch (IOException e1) { LOG.error("error occured while validating file contents: " + e1.getMessage()); throw new RuntimeException("error occured while validating file contents: " + e1.getMessage(), e1); } }
From source file:org.kuali.ole.ingest.KrmsXMLSchemaValidator.java
/** * This method return True/False.//from w ww . j a v a 2 s . c om * This method checks fileContent schema with W3 Xml schema standards,If it matches it return True else return False. * @param inputStream * @return boolean * @throws org.kuali.ole.exception.ParseException * @throws java.io.IOException * @throws org.xml.sax.SAXException */ public boolean validateContentsAgainstSchema(InputStream inputStream) throws ParseException, IOException, SAXException { try { SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Source schemaSource = null; schemaSource = new StreamSource(getFileContents()); Schema schema = null; schema = factory.newSchema(schemaSource); Validator validator = schema.newValidator(); validator.setErrorHandler(new XmlErrorHandler()); validator.validate(new StreamSource(inputStream)); return true; } catch (Exception ex) { //TODO: logging required } return false; }
From source file:org.kuali.ole.sys.batch.XmlBatchInputFileTypeBase.java
/** * Validates the xml contents against the batch input type schema using the java 1.5 validation package. * //from w ww. j a v a 2s .co m * @param schemaLocation - location of the schema file * @param fileContents - xml contents to validate against the schema */ protected void validateContentsAgainstSchema(String schemaLocation, InputStream fileContents) throws ParseException { // create a SchemaFactory capable of understanding WXS schemas SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); // get schemaFile UrlResource schemaResource = null; try { schemaResource = new UrlResource(schemaLocation); } catch (MalformedURLException e2) { LOG.error("error getting schema url: " + e2.getMessage()); throw new RuntimeException("error getting schema url: " + e2.getMessage(), e2); } // load a WXS schema, represented by a Schema instance Source schemaSource = null; try { schemaSource = new StreamSource(schemaResource.getInputStream()); } catch (IOException e2) { LOG.error("error getting schema stream from url: " + e2.getMessage()); throw new RuntimeException("error getting schema stream from url: " + e2.getMessage(), e2); } Schema schema = null; try { schema = factory.newSchema(schemaSource); } catch (SAXException e) { LOG.error("error occured while setting schema file: " + e.getMessage()); throw new RuntimeException("error occured while setting schema file: " + e.getMessage(), e); } // create a Validator instance, which can be used to validate an instance document Validator validator = schema.newValidator(); validator.setErrorHandler(new XmlErrorHandler()); // validate try { validator.validate(new StreamSource(fileContents)); } catch (SAXException e) { LOG.error("error encountered while parsing xml " + e.getMessage()); throw new ParseException("Schema validation error occured while processing file: " + e.getMessage(), e); } catch (IOException e1) { LOG.error("error occured while validating file contents: " + e1.getMessage()); throw new RuntimeException("error occured while validating file contents: " + e1.getMessage(), e1); } }
From source file:org.lib4j.cli.Options.java
public static Options parse(final URL cliURL, final Class<?> mainClass, final String[] args) { try {/*from w w w. j a v a2 s . c om*/ final Unmarshaller unmarshaller = JAXBContext.newInstance(Cli.class).createUnmarshaller(); unmarshaller .setSchema( Options.schema == null ? Options.schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) .newSchema(Thread.currentThread().getContextClassLoader() .getResource("cli.xsd")) : Options.schema); try (final InputStream in = cliURL.openStream()) { final JAXBElement<Cli> element = unmarshaller .unmarshal(XMLInputFactory.newInstance().createXMLStreamReader(in), Cli.class); return parse(element.getValue(), mainClass, args); } } catch (final FactoryConfigurationError e) { throw new UnsupportedOperationException(e); } catch (final IOException e) { throw new RuntimeException(e); } catch (final JAXBException | SAXException | XMLStreamException e) { throw new IllegalArgumentException(e); } }
From source file:org.lib4j.dbcp.DataSources.java
/** * Create a <code>BasicDataSource</code> given a dbcp JAXB binding. * * @param dbcpXml URL of dbcp xml resource. * @param driverClassLoader Class loader to be used to load the JDBC driver. * @return the <code>BasicDataSource</code> instance. * @throws SAXException If a XML validation error occurs. * @throws SQLException If a database access error occurs. * @throws IOException If an IO exception occurs. *///from www . j a v a 2 s .c o m public static BasicDataSource createDataSource(final URL dbcpXml, final ClassLoader driverClassLoader) throws IOException, SAXException, SQLException { try { final Unmarshaller unmarshaller = JAXBContext.newInstance(Dbcp.class).createUnmarshaller(); unmarshaller .setSchema( DataSources.schema == null ? DataSources.schema = SchemaFactory .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) .newSchema(Thread.currentThread().getContextClassLoader() .getResource("dbcp.xsd")) : DataSources.schema); try (final InputStream in = dbcpXml.openStream()) { final JAXBElement<Dbcp> element = unmarshaller .unmarshal(XMLInputFactory.newInstance().createXMLStreamReader(in), Dbcp.class); return createDataSource(element.getValue(), driverClassLoader); } } catch (final FactoryConfigurationError e) { throw new UnsupportedOperationException(e); } catch (final JAXBException | XMLStreamException e) { throw new SAXException(e); } }
From source file:org.lilyproject.indexer.model.indexerconf.LilyIndexerConfBuilder.java
private void validate(Document document) throws IndexerConfException { try {/*from w w w . j a va 2 s .com*/ SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); URL url = getClass().getClassLoader() .getResource("org/lilyproject/indexer/model/indexerconf/indexerconf.xsd"); Schema schema = factory.newSchema(url); Validator validator = schema.newValidator(); validator.validate(new DOMSource(document)); } catch (Exception e) { throw new IndexerConfException("Error validating indexer configuration against XML Schema.", e); } }
From source file:org.lilyproject.indexer.model.indexerconf.LilyIndexerConfBuilder.java
public static void validate(InputStream is) throws IndexerConfException { MyErrorHandler errorHandler = new MyErrorHandler(); try {/* w ww . j a va 2 s.c o m*/ SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); URL url = LilyIndexerConfBuilder.class.getClassLoader() .getResource("org/lilyproject/indexer/model/indexerconf/indexerconf.xsd"); Schema schema = factory.newSchema(url); Validator validator = schema.newValidator(); validator.setErrorHandler(errorHandler); validator.validate(new StreamSource(is)); } catch (Exception e) { if (!errorHandler.hasErrors()) { throw new IndexerConfException("Error validating indexer configuration.", e); } // else it will be reported below } if (errorHandler.hasErrors()) { throw new IndexerConfException("The following errors occurred validating the indexer configuration:\n" + errorHandler.getMessage()); } }
From source file:org.lsc.configuration.JaxbXmlConfigurationHelper.java
/** * Load an XML file to the object/*from www. ja v a 2 s. c om*/ * * @param filename * filename to read from * @return the completed configuration object * @throws FileNotFoundException * thrown if the file can not be accessed (either because of a * misconfiguration or due to a rights issue) * @throws LscConfigurationException */ public Lsc getConfiguration(String filename) throws LscConfigurationException { LOGGER.debug("Loading XML configuration from: " + filename); try { Unmarshaller unmarshaller = jaxbc.createUnmarshaller(); SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema lscSchema = null; try { int i = 0; Set<URL> urls = new HashSet<URL>(); urls.addAll(ClasspathHelper.forPackage("org.lsc")); if (System.getProperty("LSC.PLUGINS.PACKAGEPATH") != null) { String[] pathElements = System.getProperty("LSC.PLUGINS.PACKAGEPATH") .split(System.getProperty("path.separator")); for (String pathElement : pathElements) { urls.addAll(ClasspathHelper.forPackage(pathElement)); } } Reflections reflections = new Reflections(new ConfigurationBuilder().addUrls(urls) .setScanners(new ResourcesScanner(), new SubTypesScanner())); Set<String> xsdFiles = reflections.getResources(Pattern.compile(".*\\.xsd")); Source[] schemasSource = new Source[xsdFiles.size()]; List<String> xsdFilesList = new ArrayList<String>(xsdFiles); Collections.reverse(xsdFilesList); for (String schemaFile : xsdFilesList) { LOGGER.debug("Importing XML schema file: " + schemaFile); InputStream schemaStream = this.getClass().getClassLoader().getResourceAsStream(schemaFile); schemasSource[i++] = new StreamSource(schemaStream); } lscSchema = schemaFactory.newSchema(schemasSource); unmarshaller.setSchema(lscSchema); } catch (VerifyError e) { throw new LscConfigurationException(e.toString(), e); } catch (SAXException e) { throw new LscConfigurationException(e); } return (Lsc) unmarshaller.unmarshal(new File(filename)); } catch (JAXBException e) { throw new LscConfigurationException(e); } }
From source file:org.mifos.framework.components.mifosmenu.MenuParser.java
/** * Method to parse xml and return crude menu * * @return array of crude Menu objects/*from ww w . jav a 2 s .c o m*/ */ public static Menu[] parse() throws SystemException { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); SchemaFactory schfactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); schfactory.setErrorHandler(null); Schema schema = schfactory.newSchema( new StreamSource(MifosResourceUtil.getClassPathResourceAsStream(FilePaths.MENUSCHEMA))); factory.setNamespaceAware(false); factory.setValidating(false); factory.setSchema(schema); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(MifosResourceUtil.getClassPathResourceAsStream(FilePaths.MENUPATH)); NodeList tabNodeList = document.getElementsByTagName(MenuConstants.TOPMENUTAB); Menu leftMenus[] = new Menu[tabNodeList.getLength()]; for (int i = 0; i < tabNodeList.getLength(); i++) { leftMenus[i] = new Menu(); leftMenus[i].setTopMenuTabName(((Element) tabNodeList.item(i)).getAttribute(MenuConstants.NAME)); leftMenus[i].setMenuGroups(createMenuGroup(tabNodeList.item(i))); String menuHeading = ((Element) tabNodeList.item(i)) .getElementsByTagName(MenuConstants.LEFTMENULABEL).item(0).getFirstChild().getTextContent() .trim(); leftMenus[i].setMenuHeading(menuHeading); } return leftMenus; } catch (SAXParseException spe) { throw new MenuParseException(spe); } catch (SAXException sxe) { throw new MenuParseException(sxe); } catch (ParserConfigurationException pce) { throw new MenuParseException(pce); } catch (IOException ioe) { throw new MenuParseException(ioe); } }