List of usage examples for javax.xml.parsers DocumentBuilderFactory setValidating
public void setValidating(boolean validating)
From source file:Main.java
public static Document createDocumentFromFile(String pathToXmlFile) throws ParserConfigurationException, SAXException, IOException { // path to file is global String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema"; // String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource"; DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); // validate against XML Schema in dbsql2xml.xsd // documentBuilderFactory.setNamespaceAware(true); //INFO change validation to true. Someday when xsd file is complete... documentBuilderFactory.setValidating(false); documentBuilderFactory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); // documentBuilderFactory.setAttribute(JAXP_SCHEMA_SOURCE, new File(pathToXsdFile)); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.parse(new File(pathToXmlFile)); return document; }
From source file:fr.free.movierenamer.utils.URIRequest.java
private static Document getXmlDocument(InputSource source) throws IOException, SAXException { try {//w ww . java 2 s . co m DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setFeature("http://xml.org/sax/features/namespaces", false); factory.setFeature("http://xml.org/sax/features/validation", false); return factory.newDocumentBuilder().parse(source); } catch (ParserConfigurationException e) { // will never happen throw new RuntimeException(e); } }
From source file:com.hdsfed.cometapi.XMLHelper.java
public static Document StringToDoc(String xml_content) throws ParserConfigurationException, SAXException, IOException { if (xml_content == null || xml_content == "") { return null; }/*from w ww. jav a 2 s .c o m*/ if (xml_content.contains("& ")) { ScreenLog.out("WARNING: xml contains ampersand!"); xml_content = xml_content.replaceAll("&", "&"); } Document doc = null; byte[] xml_byte_array; xml_byte_array = xml_content.getBytes("UTF-8"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); DocumentBuilder builder = factory.newDocumentBuilder(); ByteArrayInputStream bastream = new ByteArrayInputStream(xml_byte_array); doc = builder.parse(bastream); return doc; }
From source file:com.l2jfree.sql.L2DatabaseInstaller.java
public static void check() throws SAXException, IOException, ParserConfigurationException { final TreeMap<String, String> tables = new TreeMap<String, String>(); final TreeMap<Double, String> updates = new TreeMap<Double, String>(); final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); // FIXME add validation factory.setIgnoringComments(true);// ww w . j av a2 s.c om final List<Document> documents = new ArrayList<Document>(); InputStream is = null; try { // load default database schema from resources is = L2DatabaseInstaller.class.getResourceAsStream("database_schema.xml"); documents.add(factory.newDocumentBuilder().parse(is)); } finally { IOUtils.closeQuietly(is); } final File f = new File("./config/database_schema.xml"); // load optional project specific database tables/updates (fails on already existing) if (f.exists()) documents.add(factory.newDocumentBuilder().parse(f)); for (Document doc : documents) { for (Node n1 : L2XML.listNodesByNodeName(doc, "database")) { for (Node n2 : L2XML.listNodesByNodeName(n1, "table")) { final String name = L2XML.getAttribute(n2, "name"); final String definition = L2XML.getAttribute(n2, "definition"); final String oldDefinition = tables.put(name, definition); if (oldDefinition != null) throw new RuntimeException("Found multiple tables with name " + name + "!"); } for (Node n2 : L2XML.listNodesByNodeName(n1, "update")) { final Double revision = Double.valueOf(L2XML.getAttribute(n2, "revision")); final String query = L2XML.getAttribute(n2, "query"); final String oldQuery = updates.put(revision, query); if (oldQuery != null) throw new RuntimeException("Found multiple updates with revision " + revision + "!"); } } } createRevisionTable(); final double databaseRevision = getDatabaseRevision(); if (databaseRevision == -1) // no table exists { for (Entry<String, String> table : tables.entrySet()) { final String tableName = table.getKey(); final String tableDefinition = table.getValue(); installTable(tableName, tableDefinition); } if (updates.isEmpty()) insertRevision(0); else insertRevision(updates.lastKey()); } else // check for possibly required updates { for (Entry<String, String> table : tables.entrySet()) { final String tableName = table.getKey(); final String tableDefinition = table.getValue(); if (L2Database.tableExists(tableName)) continue; System.err.println("Table '" + tableName + "' is missing, so the server attempts to install it."); System.err.println("WARNING! It's highly recommended to check the results manually."); installTable(tableName, tableDefinition); } for (Entry<Double, String> update : updates.entrySet()) { final double updateRevision = update.getKey(); final String updateQuery = update.getValue(); if (updateRevision > databaseRevision) { executeUpdate(updateQuery); insertRevision(updateRevision); } } } }
From source file:com.sinet.gage.dlap.utils.XMLUtils.java
/** * @param String//w w w . j ava 2 s. c o m * @param String * @return String */ public static String parseXML(String xml, String rootElementName) { try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(false); DocumentBuilder db; db = dbf.newDocumentBuilder(); Document newDoc = db.parse(new ByteArrayInputStream(xml.getBytes())); Element element = (Element) newDoc.getElementsByTagName(rootElementName).item(0); // Imports a node from another document to this document, // without altering Document newDoc2 = db.newDocument(); // or removing the source node from the original document Node copiedNode = newDoc2.importNode(element, true); // Adds the node to the end of the list of children of this node newDoc2.appendChild(copiedNode); Transformer tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); tf.setOutputProperty(OutputKeys.INDENT, "yes"); Writer out = new StringWriter(); tf.transform(new DOMSource(newDoc2), new StreamResult(out)); return out.toString(); } catch (TransformerException | ParserConfigurationException | SAXException | IOException e) { LOGGER.error("Exception in parsing xml from response: ", e); } return ""; }
From source file:gr.abiss.calipso.util.PdfUtils.java
public static void writePdf(String fontDir, String resourcesBasePath, OutputStream os, String html) { try {/*from w w w.j av a 2 s . c o m*/ // Create a buffer to hold the cleaned up HTML // Note: you can safely ignore both flush() and close(), // as they do nothing in ByteArrayOutputStream's implementation. // ByteArrayOutputStream out = new ByteArrayOutputStream(); // Clean up the HTML to be well formed HtmlCleaner cleaner = new HtmlCleaner(); CleanerProperties props = cleaner.getProperties(); props.setAdvancedXmlEscape(true); props.setRecognizeUnicodeChars(true); props.setTranslateSpecialEntities(true); props.setUseCdataForScriptAndStyle(false); props.setOmitXmlDeclaration(false); props.setOmitDoctypeDeclaration(false); TagNode node = cleaner.clean(html); // write to the ByteArray buffer html = new PrettyXmlSerializer(props).getAsString(node); // logger.info("CLEANED HTML: " + html); // update the html string using the cleaned-up version // html = new String(out.toByteArray()); ITextRenderer renderer = new ITextRenderer(); renderer.getFontResolver().addFont(fontDir + File.separator + "ARIALUNI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); if (StringUtils.isNotBlank(resourcesBasePath)) { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource is = new InputSource(new StringReader(html)); Document doc = builder.parse(is); String rsPath = new File(resourcesBasePath + File.separator).toURI().toURL().toString(); renderer.setDocument(doc, rsPath); } catch (Exception e) { throw new RuntimeException(e); } } else { renderer.setDocumentFromString(html); } renderer.layout(); renderer.createPDF(os); } catch (Exception e) { logger.error("Failed to creare PDF for item, html: \n" + html, e); } }
From source file:com.impetus.kundera.ejb.PersistenceXmlLoader.java
/** * Gets the document.// w w w .j a v a 2 s . co m * * @param configURL * the config url * @return the document * @throws Exception * the exception */ private static Document getDocument(URL configURL) throws Exception { InputStream is = null; if (configURL != null) { URLConnection conn = configURL.openConnection(); conn.setUseCaches(false); // avoid JAR locking on Windows and Tomcat is = conn.getInputStream(); } if (is == null) { throw new IOException("Failed to obtain InputStream from url: " + configURL); } DocumentBuilderFactory docBuilderFactory = null; docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setValidating(true); docBuilderFactory.setNamespaceAware(true); try { // otherwise Xerces fails in validation docBuilderFactory.setAttribute("http://apache.org/xml/features/validation/schema", true); } catch (IllegalArgumentException e) { docBuilderFactory.setValidating(false); docBuilderFactory.setNamespaceAware(false); } InputSource source = new InputSource(is); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); // docBuilder.setEntityResolver( resolver ); List errors = new ArrayList(); docBuilder.setErrorHandler(new ErrorLogger("XML InputStream", errors)); Document doc = docBuilder.parse(source); if (errors.size() != 0) { throw new PersistenceException("invalid persistence.xml", (Throwable) errors.get(0)); } is.close(); //Close input Stream return doc; }
From source file:org.eclipse.lyo.testsuite.server.util.OSLCUtils.java
public static Document createXMLDocFromResponseBody(String respBody) throws ParserConfigurationException, IOException, SAXException { //Create XML Doc out of response DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);// w w w. j ava 2 s. c o m dbf.setValidating(false); // Don't load external DTD dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); is.setCharacterStream(new StringReader(respBody)); return db.parse(is); }
From source file:Main.java
private static Document generate() throws ParserConfigurationException { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(false); documentBuilderFactory.setValidating(false); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.newDocument(); return document; }
From source file:Main.java
private static synchronized DocumentBuilderFactory getFactory(boolean validate, boolean namespaceAware) { DocumentBuilderFactory factory = doms[validate ? 0 : 1][namespaceAware ? 0 : 1]; if (factory == null) { factory = DocumentBuilderFactory.newInstance(); factory.setValidating(validate); factory.setNamespaceAware(namespaceAware); doms[validate ? 0 : 1][namespaceAware ? 0 : 1] = factory; }// w w w .j ava 2s . c o m return factory; }