List of usage examples for javax.xml.parsers DocumentBuilder setErrorHandler
public abstract void setErrorHandler(ErrorHandler eh);
From source file:psidev.psi.mi.filemakers.xmlFlattener.structure.XsdTreeStructImpl.java
/** * Open a frame to choose an XML document and load it. * //w w w . ja v a 2 s. c o m */ public void loadDocument(URL url) throws FileNotFoundException, NullPointerException, MalformedURLException, IOException, SAXException { maxCounts = new HashMap<XsdNode, Integer>(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); factory.setValidating(validateDocument); factory.setAttribute(SCHEMA_LANGUAGE, XML_SCHEMA); factory.setAttribute(SCHEMA_SOURCE, schemaURL); try { DocumentBuilder builder = factory.newDocumentBuilder(); log.debug("XML document url: " + url.toString()); builder.setErrorHandler(xmlErrorHandler); document = builder.parse(url.toString()); this.documentURL = url; /* get all references */ log.debug("get keys/keyRefs"); buidKeyMaps(); for (String refer : refType2referedType.keySet()) { String refered = refType2referedType.get(refer); log.debug("found reftype: " + refer + " refers " + refered); } log.debug("done"); log.debug("document parsed ... get elements"); setLineNode(lineXsdNode); Utils.lastVisitedDirectory = url.getPath(); Utils.lastVisitedDocumentDirectory = url.getPath(); } catch (ParserConfigurationException e) { /** TODO: manage excepton */ } }
From source file:psidev.psi.mi.filemakers.xmlMaker.structure.XsdTreeStructImpl.java
public void validateXml(File xmlFile, Writer log) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);/* w ww. j a v a 2 s. co m*/ factory.setValidating(true); factory.setAttribute(SCHEMA_LANGUAGE, XML_SCHEMA); factory.setAttribute(SCHEMA_SOURCE, schemaURL); try { DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler(xmlErrorHandler); /** * TODO: check if it is necessary */ builder.parse(xmlFile); } catch (ParserConfigurationException e) { /** TODO: manage exception */ } catch (SAXException e2) { /** TODO: manage exception */ } catch (OutOfMemoryError ooo) { try { log.write("ERROR: not enougth memory to perform XML validation"); } catch (IOException e3) { /** TODO: manage exception */ } getMessageManager().sendMessage("ERROR: not enougth memory to perform XML validation", MessageManagerInt.errorMessage); } catch (IOException e3) { /** TODO: manage exception */ } }
From source file:ru.medved.json.wssoap.WebServiceSampler.java
/** * Open the file and create a Document./*from w w w .j av a2 s. c om*/ * * @param file - input filename or empty if using data from tesplan * @return Document * @throws IOException * @throws SAXException */ private Document openDocument(String file) throws SAXException, IOException { /* * Consider using Apache commons pool to create a pool of document * builders or make sure XMLParserUtils creates builders efficiently. */ DocumentBuilder XDB = XMLParserUtils.getXMLDocBuilder(); XDB.setErrorHandler(null);//Suppress messages to stdout Document doc = null; // if either a file or path location is given, // get the file object. if (file.length() > 0) {// we have a file if (this.getReadResponse()) { TextFile tfile = new TextFile(file); fileContents = tfile.getText(); } FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(file); doc = XDB.parse(fileInputStream); } finally { IOUtils.closeQuietly(fileInputStream); } } else {// must be a "here" document fileContents = getXmlData(); if (fileContents != null && fileContents.length() > 0) { doc = XDB.parse(new InputSource(new StringReader(fileContents))); } else { log.warn("No post data provided!"); } } return doc; }
From source file:schemacrawler.test.utility.TestUtility.java
private static void validateXML(final Path testOutputFile, final List<String> failures) throws Exception { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false);//from ww w. j a v a2s . c o m factory.setNamespaceAware(true); final DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler(new ErrorHandler() { @Override public void error(final SAXParseException e) throws SAXException { failures.add(e.getMessage()); } @Override public void fatalError(final SAXParseException e) throws SAXException { failures.add(e.getMessage()); } @Override public void warning(final SAXParseException e) throws SAXException { failures.add(e.getMessage()); } }); builder.parse(new InputSource(readerForFile(testOutputFile))); }
From source file:semlink.apps.uvig.Generator.java
/** * Completes the filling of the HTML output directory of all files, pre-made ones * and generated ones. This method and supporting {@link uvi.Generator#generateOneFile(DocumentBuilder, File)} * method make use of JDOM to parse the incoming XML files. Essentially this just means * that {@link javax.xml.parsers.DocumentBuilderFactory}, {@link javax.xml.parsers.DocumentBuilder}, * and {@link org.w3c.dom.Document} are utilized. After the HTML files for * each class are generated, this method reads in PropBank and FrameNet information, * adds it to the growing index, and finally generates all the HTML files needed * for the index/indices.// ww w .j av a 2s. com * * @see uvi.Generator#main(String[]) * @see uvi.Generator#generateOneFile(DocumentBuilder, File) * @see uvi.Generator#generateIndexFiles() * @see uvi.Generator#addOthers(int) */ private static void generateHTMLFiles() { // Create the subdirectories that the UVI uses createSubdirectories(); //Additional reference pages by K. Stowe ReferencePages.generateThemRolesReference(outDir); ReferencePages.generateSelectionalReference(outDir); ReferencePages.generatePredicateReference(outDir); ReferencePages.generateSyntacticReference(outDir); ReferencePages.generateVerbFeatureReference(outDir); loadThemRoleLinks(); loadPredicateLinks(); // List all the supplemental files. if (flVerbose && !flQuiet) { println("Supplemental files valid"); for (int s = 0; s < sNames.length; s++) { if (!sNames[s].equals("vn-cyc.s") || flWithCyc) { println(" " + filePath((File) sFiles.get(sNames[s]))); } } } // Place all required supporting files in the HTML output directory. copySupplementalFiles(); if (flVerbose && !flQuiet) { System.out.println("Properties defined:"); for (Object o : properties.keySet()) { String key = (String) o; String val = (String) properties.get(o); System.out.println(" " + key + " = " + val); } } // If the user only wanted to copy/construct the supplemental files, stop here. if (flCopyOnly) { return; } if (!flNoVnFnMap) { if (flVerbose && !flQuiet) { println("Additional Source: Extracting VerbNet-FrameNet mapping info for members from \"vn-fn.s\"..."); } // Load VerbNet-FrameNet mapping links for the VerbNet classes. This one // happens before the loop since the verb classes depend on these being // loaded (see Sweeper.printMembers). addOthers(DS_VN_FN); } // COMMENT if (flWithCyc) { if (flVerbose && !flQuiet) { println("Additional Source: Extracting VerbNet-Cyc mapping info for members from \"vn-cyc.s\"..."); } addOthers(DS_VN_CYC); } if (!flNoVnWnLinks) { if (flVerbose && !flQuiet) { println("Additional Source: Extracting WordNet sense numbers for members from \"wordnet.s\"..."); } // Load WordNet sense number info for the VerbNet classes. This one // happens before the loop since the verb classes depend on these being // loaded (see Sweeper.printMembers). addOthers(DS_WORDNET); } if (!flNoIndexVn) { try { // Create essential JDOM objects. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // So XML files are checked against their DTD. dbf.setValidating(true); DocumentBuilder db = dbf.newDocumentBuilder(); XMLErrorHandler xmlErrH = new XMLErrorHandler(); // So custom error messages can be printed (see XMLErrorHandler). db.setErrorHandler(xmlErrH); for (int x = 0; x < xmlFiles.length; x++) { String xmlFileName = xmlFiles[x].getName(); String htmlFileName = "vn/" + xmlFileName.substring(0, xmlFileName.lastIndexOf(".")) + ".php"; // Print the name of the current file being processed. if (flVerbose && !flQuiet) { println("Processing file \"" + xmlFileName + "\" into \"" + htmlFileName + "\"..."); } // Create the stream for the HTML file and give it to the 'Q class'. Q.setWriter(createHTMLOutStream(htmlFileName)); // Tell the 'Sweeper' class what file is currently being processed. It // will use this information to add the appropriate links to the index // as verbs are discovered in the XML files and to place in error messages // for location-of-error info. Sweeper.setCurFile(htmlFileName); // If everything with the stream was good, generated the HTML file. if (Q.getWriter() != null) { // Let the JDOM XML error handler know what input file is currently // being processed so it can report which file contained the error. xmlErrH.setActiveFile(xmlFileName); // Perform translation from XML->HTML. generateOneFile(db, xmlFiles[x]); // Close the stream to the HTML file. closeHTMLOutStream(Q.getWriter(), htmlFileName); } } } catch (ParserConfigurationException e) { eprintln("ERROR: Parser configuration error."); } } if (!flNoVnFnMap) { // Print warnings for all matches not utilized in the VN-FN mapping. VN_FN_Map.printUnused(); } if (!flNoIndexPb) { if (flVerbose && !flQuiet) { println("Additional Source: Extracting PropBank info for index from \"propbank.s\"..."); } // Add PropBank links to index. addOthers(DS_PROPBANK); } if (!flNoIndexFn) { if (flVerbose && !flQuiet) { println("Additional Source: Extracting FrameNet info for index from \"framenet.s\"..."); } // Add FrameNet links to index. addOthers(DS_FRAMENET); } if (!flNoIndexSg) { if (flVerbose && !flQuiet) { println("Additional Source: Extracting Grouping info for index from \"grouping.s\"..."); } // Add Grouping links to index. addOthers(DS_GROUPING); } // Sort the verbs for each index page (for each letter). Index.sort(); // Generate index.php and A.php through Z.php. generateIndexFiles(); if (!flNoIndexVn) { // Generate the reference page, reference.php. generateReferencePage(); // Generate the class hierarchy, class-h.php. generateClassHierarchy(); } }
From source file:semlink.apps.uvig.Generator.java
/** * Reads in data from an alternate data source (one other than VerbNet). * Based on the source requested via the argument, a file name is chosen * and a its data is extracted into the appropriate internal data structure. * Each file has its own format and is accounted for herein. * * @param type which file to read in and add to the index. This should be * one of the DS_* constants./*from w ww.jav a2 s . c o m*/ * @see uvi.Generator#generateIndexFiles() * @see uvi.Index#addLink(String, int, String, String) * @see uvi.VN_FN_Map#addMapPair(String, String, String) * @see uvi.WordNet */ private static void addOthers(int type) { String fileName = ""; // Decide which file name should be used, based on the requested source. switch (type) { case DS_PROPBANK: fileName = "propbank.s"; break; case DS_FRAMENET: fileName = "framenet.s"; break; case DS_GROUPING: fileName = "grouping.s"; break; case DS_WORDNET: fileName = "wordnet.s"; break; case DS_VN_FN: fileName = "vn-fn.s"; break; case DS_VN_CYC: fileName = "vn-cyc.s"; break; default: eprintln("ERROR: Invalid Generator.DS_* constant."); return; } try { // If the data source is an XML file... if (type == DS_FRAMENET || type == DS_VN_FN || type == DS_VN_CYC) { // Create essential JDOM objects. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // So XML files are checked against their DTD. This assumes that // there is an inline DTD inside the file, or that the XML file // references an external DTD file. dbf.setValidating(true); DocumentBuilder db = dbf.newDocumentBuilder(); XMLErrorHandler xmlErrH = new XMLErrorHandler(); // So custom error messages can be printed (see XMLErrorHandler). db.setErrorHandler(xmlErrH); // Let the JDOM XML error handler know what input file is currently // being processed so it can report which file contained the error. xmlErrH.setActiveFile(fileName); Document doc = db.parse((File) sFiles.get(fileName)); // Scan the FrameNet XML tree. if (type == DS_FRAMENET) { NodeList members = doc.getElementsByTagName("lexical-entry"); for (int m = 0; m < members.getLength(); m++) { Element e = (Element) members.item(m); String vb = e.getAttribute("name").replaceAll(" ", "_"); // Disallow spaces for searchable index reasons. String fm = e.getAttribute("frame"); vb = vb.substring(0, vb.length() - 2); // Strip '.v' String fnUrlPattern = getProperty("fnUrlPattern", "ErrorNoFnUrlPatternDefined-{1}"); fnUrlPattern = fnUrlPattern.replaceAll("\\{1\\}", fm); // Add the link to the index. Index.addLink(vb, DS_FRAMENET, "(fn " + fm + ")", fnUrlPattern); } } // Scan the VN-FN XML tree. else if (type == DS_VN_FN) { NodeList members = doc.getElementsByTagName("vncls"); for (int m = 0; m < members.getLength(); m++) { Element e = (Element) members.item(m); String cl = e.getAttribute("class"); String vn = e.getAttribute("vnmember"); String fn = e.getAttribute("fnframe"); // Add the class-verb-frame tuple to a special data structure // designed to hold the VN-FN information. Only add those // entries that aren't 'not available' or 'different sense'. if (!fn.equals("NA") && !fn.equals("DS") && !fn.equals("")) { VN_FN_Map.addMapPair(cl, vn, fn); } if (fn.equals("")) { eprintln("WARNING: VN-FN mapping should not have a blank fnframe attribute (" + cl + "/" + vn + ")."); } } } // Scan the VN-CYC XML tree. (never finished) else if (type == DS_VN_CYC) { NodeList members = doc.getElementsByTagName("tuple"); for (int m = 0; m < members.getLength(); m++) { Element e = (Element) members.item(m); String vnc = e.getAttribute("vnc"); String vnm = e.getAttribute("vnm"); String vnf = e.getAttribute("vnf"); String crl = e.getAttribute("crule"); // finish... } } } // Scan the PropBank file. else if (type == DS_PROPBANK) { BufferedReader in = new BufferedReader(new FileReader((File) sFiles.get(fileName))); String line; // Read the desired file line-by-line. while ((line = in.readLine()) != null) { // Split the 'verb,url' pair into its constituent parts. String[] parts = line.split(","); // Add a PropBank entry to the index. Index.addLink(parts[0], DS_PROPBANK, "(PropBank)", parts[1]); } in.close(); } // Scan the Grouping file. else if (type == DS_GROUPING) { BufferedReader in = new BufferedReader(new FileReader((File) sFiles.get(fileName))); String line; // Read the desired file line-by-line. while ((line = in.readLine()) != null) { // Split the 'verb,url' pair into its constituent parts. String[] parts = line.split(","); // Add a Grouping entry to the index. Index.addLink(parts[0], DS_GROUPING, "(Grouping)", parts[1]); } in.close(); } // Scan the WordNet file (wordnet.s should be the index.sense file). else if (type == DS_WORDNET) { if (flVerbose && !flQuiet) { println(" --> Step 1: VerbNet pre-scan for just members..."); } // Look for all the sense keys that will need sense numbers looked up. WordNet.preScan(xmlFiles); if (flVerbose && !flQuiet) { println(" --> Step 2: Scan index.sense (wordnet.s) for required sense numbers..."); } // Look up the sense numbers for the sense keys that exist in VerbNet. WordNet.loadSenseNumbers((File) sFiles.get("wordnet.s")); } } catch (Exception e) { eprintln("ERROR: Cannot read from file \"" + fileName + "\". " + e.getMessage()); } }
From source file:simpleserver.config.DTDEntityResolver.java
private XMLConfiguration initConf(boolean validate) { XMLConfiguration conf = new XMLConfiguration(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = null; try {// w ww .j a v a2 s. c o m dbf.setValidating(validate); db = dbf.newDocumentBuilder(); } catch (ParserConfigurationException e) { } if (db != null) { db.setEntityResolver(new DTDEntityResolver(this)); db.setErrorHandler(new DTDErrorHandler(this)); conf.setDocumentBuilder(db); } conf.setExpressionEngine(new XPathExpressionEngine()); conf.setDelimiterParsingDisabled(true); return conf; }
From source file:uk.sipperfly.utils.CommonUtil.java
/** * Import xml file. Truncate all info in db and add new info from xml. * * @param path input xml file location/*w w w . j a v a 2 s . c o m*/ * @return message string whether import is successful or not */ public String importXML(String path, String defaultTemplate) { String message = ""; try { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); dbFactory.setValidating(true); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); dBuilder.setErrorHandler(new SimpleErrorHandler()); Document doc; String EMAIL_REGEX = "^[\\w-_\\.+]*[\\w-_\\.]\\@([\\w-]+\\.)+[\\w]+[\\w]$"; if (!defaultTemplate.equals("")) { doc = dBuilder.parse(new InputSource(new StringReader(defaultTemplate))); } else { File fXmlFile = new File(path); doc = dBuilder.parse(fXmlFile); } doc.getDocumentElement().normalize(); NodeList nList = doc.getElementsByTagName("Metadata").item(0).getChildNodes(); this.bagInfoRepo.truncate(); for (int temp = 0; temp < nList.getLength(); temp++) { Node nNode = nList.item(temp); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; BagInfo bagInfo = new BagInfo(); if (!eElement.getNodeName().isEmpty()) { if (eElement.getAttribute("label").isEmpty()) { bagInfo.setLabel(eElement.getNodeName().replace("-", " ")); } else { bagInfo.setLabel(eElement.getAttribute("label")); } if (!defaultTemplate.equals("")) { bagInfo.setValue(""); } else { bagInfo.setValue(eElement.getTextContent().replaceAll("\\s+", " ").trim()); } this.bagInfoRepo.save(bagInfo); } } } NodeList recList = doc.getElementsByTagName("Recipients").item(0).getChildNodes(); this.recipientsRepo.truncate(); for (int temp = 0; temp < recList.getLength(); temp++) { Recipients recipient = this.recipientsRepo.getOneOrCreateOne(""); Node nNode = recList.item(temp); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; if (!eElement.getTextContent().isEmpty()) { if (eElement.getTextContent().matches(EMAIL_REGEX)) { recipient.setEmail(eElement.getTextContent()); this.recipientsRepo.save(recipient); } else { message = message + "Can't save " + eElement.getTextContent() + "because of invalid email format. "; } } } } this.FTPRepo.truncate(); NodeList ftpList = doc.getElementsByTagName("FTP"); for (int temp = 0; temp < ftpList.getLength(); temp++) { FTP ftp = this.FTPRepo.getOneOrCreateOne(); Node nNode = ftpList.item(temp); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; if (eElement.getElementsByTagName("Host").getLength() == 1) { ftp.setHostName(eElement.getElementsByTagName("Host").item(0).getTextContent()); } if (eElement.getElementsByTagName("Username").getLength() == 1) { ftp.setUsername(eElement.getElementsByTagName("Username").item(0).getTextContent()); } if (eElement.getElementsByTagName("Password").getLength() == 1) { ftp.setPassword(EncryptDecryptUtil .decrypt(eElement.getElementsByTagName("Password").item(0).getTextContent())); } if (eElement.getElementsByTagName("Port").getLength() == 1) { ftp.setPort( Integer.parseInt(eElement.getElementsByTagName("Port").item(0).getTextContent())); } if (eElement.getElementsByTagName("Mode").getLength() == 1) { ftp.setMode(eElement.getElementsByTagName("Mode").item(0).getTextContent()); } if (eElement.getElementsByTagName("Destination").getLength() == 1) { ftp.setDestination(eElement.getElementsByTagName("Destination").item(0).getTextContent()); } ftp.setSecurityType("FTPES"); this.FTPRepo.save(ftp); } } NodeList confList = doc.getElementsByTagName("configurations"); this.configurationsRepo.truncate(); for (int temp = 0; temp < confList.getLength(); temp++) { Configurations configurations = this.configurationsRepo.getOneOrCreateOne(); Node nNode = confList.item(temp); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; if (eElement.getElementsByTagName("Server-Name").getLength() == 1) { configurations.setServerName( eElement.getElementsByTagName("Server-Name").item(0).getTextContent()); } if (eElement.getElementsByTagName("Username").getLength() == 1) { configurations .setUsername(eElement.getElementsByTagName("Username").item(0).getTextContent()); } if (eElement.getElementsByTagName("Password").getLength() == 1) { configurations.setPassword(EncryptDecryptUtil .decrypt(eElement.getElementsByTagName("Password").item(0).getTextContent())); } if (eElement.getElementsByTagName("Port").getLength() == 1) { configurations .setServerPort(eElement.getElementsByTagName("Port").item(0).getTextContent()); } if (eElement.getElementsByTagName("Protocol").getLength() == 1) { configurations.setServerProtocol( eElement.getElementsByTagName("Protocol").item(0).getTextContent()); } if (eElement.getElementsByTagName("Email-Notification").getLength() == 1) { configurations.setEmailNotifications(Boolean.valueOf( eElement.getElementsByTagName("Email-Notification").item(0).getTextContent())); } } this.configurationsRepo.save(configurations); } message = message + "Successfully imported xml"; } catch (ParserConfigurationException ex) { Logger.getLogger(CommonUtil.class.getName()).log(Level.SEVERE, null, ex); return ""; } catch (SAXParseException ex) { Logger.getLogger(CommonUtil.class.getName()).log(Level.SEVERE, null, ex); return ""; } catch (SAXException ex) { Logger.getLogger(CommonUtil.class.getName()).log(Level.SEVERE, null, ex); return ""; } catch (IOException ex) { Logger.getLogger(CommonUtil.class.getName()).log(Level.SEVERE, null, ex); return ""; } catch (Exception ex) { Logger.getLogger(CommonUtil.class.getName()).log(Level.SEVERE, null, ex); return ""; } return message; }