List of usage examples for org.dom4j.io SAXReader setValidation
public void setValidation(boolean validation)
From source file:BlastResultXMLsplit.BlastXMLsplit.java
public BlastXMLsplit(String filepath, int seqnumber) throws FileNotFoundException, IOException, ParserConfigurationException { SAXReader reader = new SAXReader(); reader.setValidation(false); try {//from w w w. j ava 2 s.co m System.out.println("Xlmfile reading"); Document document = reader.read(new FileInputStream(filepath));//XMLdocument? System.out.println("Xlmfile read done!"); org.dom4j.DocumentFactory DocumentFactory = new org.dom4j.DocumentFactory(); ArrayList str = new ArrayList();//??? String fileoutpath; org.dom4j.Element rootElm = document.getRootElement();// File f = new File(filepath); int count = 1; List<org.dom4j.Element> blastOutput_iterations = rootElm.element("BlastOutput_iterations").elements();//? org.dom4j.Element BlastOutput_program = rootElm.element("BlastOutput_program"); org.dom4j.Element BlastOutput_version = rootElm.element("BlastOutput_version"); org.dom4j.Element BlastOutput_reference = rootElm.element("BlastOutput_reference"); org.dom4j.Element BlastOutput_db = rootElm.element("BlastOutput_db"); //? BlastOutput_program.getParent().remove(BlastOutput_program); BlastOutput_version.getParent().remove(BlastOutput_version); BlastOutput_reference.getParent().remove(BlastOutput_reference); BlastOutput_db.getParent().remove(BlastOutput_db); // org.dom4j.Element BlastOutput_query_def=rootElm.element("BlastOutput_query-def"); //org.dom4j.Element BBlastOutput_query_len=rootElm.element("BlastOutput_query-len"); org.dom4j.Element BlastOutput_param = rootElm.element("BlastOutput_param"); rootElm.remove(BlastOutput_param); String Iteration_queryIDstr, Iteration_queryDefstr, Iteration_query_len; List<org.dom4j.Element> Iterationlist = null; int size = blastOutput_iterations.size(); System.out.println("Your query seqcount is " + size + "\r\n Start dividing your file"); for (int i = 0; i < size; i = i + seqnumber) { //?? fileoutpath = f.getParent() + System.getProperty("file.separator") + count + ".xml"; count++; System.out.println("The " + count + " is located in " + fileoutpath); FileOutputStream fos = new FileOutputStream(fileoutpath); OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(fos, format); // XMLWriter writer = new XMLWriter(new FileOutputStream(fileoutpath)); //w?? org.dom4j.Element firstelement = blastOutput_iterations.get(i); Iteration_queryIDstr = firstelement.element("Iteration_query-ID").getText(); Iteration_queryDefstr = firstelement.element("Iteration_query-def").getText(); Iteration_query_len = firstelement.element("Iteration_query-len").getText(); //org.dom4j.DocumentFactory DocumentFactory = new org.dom4j.DocumentFactory(); Document document2 = DocumentHelper.createDocument(); ; org.dom4j.Element BlastOutputElement = document2.addElement("BlastOutput"); //doc.setRootElement(BlastOutputElement); //; BlastOutputElement.add(BlastOutput_program); BlastOutputElement.add(BlastOutput_version); BlastOutputElement.add(BlastOutput_reference); BlastOutputElement.add(BlastOutput_db); BlastOutputElement.addElement("BlastOutput_query-ID"); BlastOutputElement.element("BlastOutput_query-ID").setText(Iteration_queryIDstr); BlastOutputElement.addElement("BlastOutput_query-def"); BlastOutputElement.element("BlastOutput_query-def").setText(Iteration_queryDefstr); BlastOutputElement.addElement("BlastOutput_query-len"); BlastOutputElement.element("BlastOutput_query-len").setText(Iteration_query_len); // Element BlastOutput_param_new=DocumentFactory.createElement("BlastOutput_param"); // for (Iterator it = BlastOutput_param.elementIterator(); it.hasNext();) { // Element tempele=(Element) it.next(); // tempele.getParent().remove(tempele); // BlastOutput_param_new.add(tempele); // } BlastOutputElement.add(BlastOutput_param); //BlastOutputElement.add(BlastOutput_param); if (i + seqnumber < blastOutput_iterations.size()) { Iterationlist = blastOutput_iterations.subList(i, i + seqnumber); } else { Iterationlist = blastOutput_iterations.subList(i, blastOutput_iterations.size() - 1); } //System.out.println(Iterationlist.size()); //?query resetIterationlist(Iterationlist); //?BlastOutput_iterations Element BlastOutput_iterations = DocumentFactory.createElement("BlastOutput_iterations"); //BlastOutputElement.addAttribute("BlastOutput_iterations"); //org.dom4j.Element BlastOutput_iterations = new org.dom4j.Element("BlastOutput_iterations"); for (int j = 0; j < Iterationlist.size(); j++) { Iterationlist.get(j).getParent().remove(Iterationlist.get(j)); //System.out.println(j); BlastOutput_iterations.add(Iterationlist.get(j)); } BlastOutputElement.add(BlastOutput_iterations); // writer.write(document2); writer.close(); BlastOutput_program.getParent().remove(BlastOutput_program); BlastOutput_version.getParent().remove(BlastOutput_version); BlastOutput_reference.getParent().remove(BlastOutput_reference); BlastOutput_db.getParent().remove(BlastOutput_db); BlastOutput_param.getParent().remove(BlastOutput_param); } } catch (DocumentException ex) { Logger.getLogger(BlastXMLsplit.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:ca.coder2000.recipes.RecipeFile.java
License:Mozilla Public License
/** * Creates a new instance of RecipeFile/*www .j a va 2s.c o m*/ * @param file the file we want this class to load and manage. */ public RecipeFile(File file) { SAXReader reader = new SAXReader(true); reader.setValidation(true); try { reader.setFeature("http://xml.org/sax/features/validation", true); reader.setFeature("http://apache.org/xml/features/validation/schema", true); reader.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", "schemas/recipe.xsd"); reader.setErrorHandler(new RecipeErrorHandler()); doc = reader.read(file); checkVersion(); } catch (DocumentException de) { JOptionPane.showMessageDialog(null, de.getMessage(), java.util.ResourceBundle.getBundle("recipe").getString("Error_Parsing_Recipe"), JOptionPane.ERROR_MESSAGE); } catch (SAXException sxe) { JOptionPane.showMessageDialog(null, sxe.getMessage(), java.util.ResourceBundle.getBundle("recipe").getString("Error_Parsing_Recipe"), JOptionPane.ERROR_MESSAGE); } catch (Exception e) { JOptionPane.showMessageDialog(null, e.getMessage(), java.util.ResourceBundle.getBundle("recipe").getString("Error_Parsing_Recipe"), JOptionPane.ERROR_MESSAGE); } }
From source file:cn.kee.engine.common.SystemInitServlet.java
private Document readerDom(File flie) { Document doc = null;/*from w w w .ja va 2 s . c om*/ SAXReader reader = new SAXReader(); reader.setValidation(false); reader.setEntityResolver(new NoOpEntityResolver()); try { doc = reader.read(flie); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } return doc; }
From source file:com.blocks.framework.utils.date.XMLDom4jUtils.java
License:Open Source License
/** * Create dom4j document from xmlSource/* w w w . j a va 2 s . c o m*/ * * @param xmlSource * URL?XML?XML?? Object * @param validate * boolean * @param encoding * String * @throws DocumentException * @throws IOException * @return Document * @throws BaseException */ public static Document createDocument(Object xmlSource, boolean validate, String encoding) throws BaseException { // Use xerces and validate XML file if (xmlSource instanceof Document) return (Document) xmlSource; Document doc = null; SAXReader saxReader = new SAXReader(true); saxReader.setValidation(validate); if (encoding == null || encoding.equals("")) { encoding = DEFAULT_ENCODING; } // Check input source type if (xmlSource instanceof StringBuffer) xmlSource = ((StringBuffer) xmlSource).toString(); try { if (xmlSource instanceof String) { if (((String) xmlSource).startsWith("<")) { // Treat the String as XML code StringReader reader = new StringReader(xmlSource.toString()); DocumentHelper.parseText(xmlSource.toString()); doc = saxReader.read(reader, encoding); } else { doc = saxReader.read(new File((String) xmlSource)); } } else if (xmlSource instanceof File) { doc = saxReader.read((File) xmlSource); } else if (xmlSource instanceof InputStream) { doc = saxReader.read((InputStream) xmlSource); } else if (xmlSource instanceof Reader) { doc = saxReader.read((Reader) xmlSource); } else if (xmlSource instanceof URL) { doc = saxReader.read((URL) xmlSource); } } catch (Exception ex) { ex.printStackTrace(); throw new BaseException("UTIL-0001", ex); } return doc; }
From source file:com.digiaplus.lms.core.xml.XMLParser.java
License:Apache License
/** * @param in/*ww w.jav a 2s . co m*/ * @param validateXML * @return parsed document */ public Document parse(InputStream in, boolean validateXML) { Document document; try { SAXReader reader = new SAXReader(); reader.setEntityResolver(er); reader.setValidation(validateXML); document = reader.read(in, ""); document.normalize(); } catch (Exception e) { throw new DAPRuntimeException(XMLParser.class, "Exception reading XML", e); } return document; }
From source file:com.genericworkflownodes.knime.config.impl.GalaxyNodeConfigurationReader.java
License:Open Source License
@Override public INodeConfiguration read(InputStream in) throws Exception { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); SAXReader reader = new SAXReader(parser.getXMLReader()); reader.setValidation(false); SimpleErrorHandler errorHandler = new SimpleErrorHandler(); reader.setErrorHandler(errorHandler); doc = reader.read(in);/* w w w . j a v a 2s. c om*/ if (!errorHandler.isValid()) { System.err.println(errorHandler.getErrorReport()); throw new Exception("Galaxy tool xml file is not valid !"); } readPorts(); readParameters(); readDescription(); config.setXml(doc.asXML()); return config; }
From source file:com.genericworkflownodes.knime.schemas.SchemaValidator.java
License:Open Source License
/** * Validate the given xml stream against the stored schemata. * // w ww. j av a 2 s .co m * @param xmlstream * The stream to validate. * @return True if the file is valid, false otherwise. */ public boolean validates(InputStream xmlstream) { SAXParserFactory factory = SAXParserFactory.newInstance(); SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); SimpleErrorHandler errorHandler = new SimpleErrorHandler(); try { factory.setSchema(schemaFactory.newSchema(getSchemaSources())); SAXParser parser = factory.newSAXParser(); SAXReader reader = new SAXReader(parser.getXMLReader()); reader.setValidation(false); reader.setErrorHandler(errorHandler); reader.read(xmlstream); } catch (SAXException e) { throw new RuntimeException(e); } catch (DocumentException e) { throw new RuntimeException(e); } catch (ParserConfigurationException e) { throw new RuntimeException(e); } if (!errorHandler.isValid()) { errorReport = errorHandler.getErrorReport(); return false; } return true; }
From source file:com.globalsight.everest.tm.importer.TmxReader.java
License:Apache License
/** * Reads an XML file and checks its correctness by validating * against the TMX DTD. If there's any error in the file, an * exception is thrown./*from w ww . j av a2 s . c om*/ * * As a side effect, this method builds a list of source and * target locales found in the file, including the declared source * locale from the header. */ private void analyzeXml(String p_url) throws Exception { if (m_tmxLevel == ImportUtil.TMX_LEVEL_TRADOS_RTF || m_tmxLevel == ImportUtil.TMX_LEVEL_TRADOS_HTML || m_tmxLevel == ImportUtil.TMX_LEVEL_TRADOS_FM || m_tmxLevel == ImportUtil.TMX_LEVEL_TRADOS_FM_SGML || m_tmxLevel == ImportUtil.TMX_LEVEL_TRADOS_IL || m_tmxLevel == ImportUtil.TMX_LEVEL_TRADOS_XPTAG) { // Convert the Trados codes to native System4 codes by // converting the file to RTF, saving it as HTML and // extracting the resulting TUVs. CATEGORY.info("Converting Trados TMX to native TMX: " + p_url); p_url = convertTradosTmx(p_url, m_tmxLevel); // Now we have a new file that contains native content. m_options.setFileName(p_url); m_options.setFileType(com.globalsight.everest.tm.importer.ImportOptions.TYPE_XML); m_tmxLevel = ImportUtil.TMX_LEVEL_NATIVE; } CATEGORY.debug("Analyzing document: " + p_url); // Reset list of locales found in the file. m_sourceLocales = new HashSet(); m_targetLocales = new HashSet(); SAXReader reader = new SAXReader(); reader.setXMLReaderClassName("org.apache.xerces.parsers.SAXParser"); // Read the DTD and validate. reader.setEntityResolver(DtdResolver.getInstance()); reader.setValidation(true); // enable element complete notifications to conserve memory reader.addHandler("/tmx", new ElementHandler() { public void onStart(ElementPath path) { Element element = path.getCurrent(); m_tmxVersion = element.attributeValue(Tmx.VERSION); } public void onEnd(ElementPath path) { } }); reader.addHandler("/tmx/header", new ElementHandler() { public void onStart(ElementPath path) { } public void onEnd(ElementPath path) { Element element = path.getCurrent(); m_header = new Tmx(element); m_header.setTmxVersion(m_tmxVersion); element.detach(); } }); reader.addHandler("/tmx/body/tu", new ElementHandler() { public void onStart(ElementPath path) { ++m_entryCount; } public void onEnd(ElementPath path) { Element element = path.getCurrent(); // Record optional source language declared on TU. String srclang = element.attributeValue(Tmx.SRCLANG); if (srclang != null) { m_sourceLocales.add(ImportUtil.normalizeLocale(srclang)); } // Find target languages HashSet langs = new HashSet(); List tuvs = element.selectNodes("./tuv"); for (int i = 0, max = tuvs.size(); i < max; i++) { Element tuv = (Element) tuvs.get(i); String lang = tuv.attributeValue(Tmx.LANG); // Collect TUV locales langs.add(ImportUtil.normalizeLocale(lang)); } langs.remove(srclang); m_targetLocales.addAll(langs); // prune the current element to reduce memory element.detach(); } }); Document document = reader.read(p_url); // Add declared source language from header. String sourceLocale = ImportUtil.normalizeLocale(m_header.getSourceLang()); m_sourceLocales.add(sourceLocale); }
From source file:com.globalsight.everest.tm.importer.TmxReaderThread.java
License:Apache License
public void run() { try {/*from w w w .j a v a 2 s . c om*/ SAXReader reader = new SAXReader(); reader.setXMLReaderClassName("org.apache.xerces.parsers.SAXParser"); // Read the DTD and validate. reader.setEntityResolver(DtdResolver.getInstance()); reader.setValidation(true); reader.addHandler("/tmx", new ElementHandler() { public void onStart(ElementPath path) { Element element = path.getCurrent(); m_tmxVersion = element.attributeValue(Tmx.VERSION); } public void onEnd(ElementPath path) { } }); reader.addHandler("/tmx/header", new ElementHandler() { public void onStart(ElementPath path) { } public void onEnd(ElementPath path) { Element element = path.getCurrent(); element.detach(); m_tmx = new Tmx(element); m_tmx.setTmxVersion(m_tmxVersion); m_defaultSrcLang = ImportUtil.normalizeLocale(m_tmx.getSourceLang()); } }); // enable pruning to call me back as each Element is complete reader.addHandler("/tmx/body/tu", new ElementHandler() { public void onStart(ElementPath path) { m_count++; } public void onEnd(ElementPath path) { Element element = path.getCurrent(); element.detach(); m_result = m_results.hireResult(); try { // Normalize spelling of locales. normalizeTu(element); // Filter out targets not to be imported. filterTu(element); // Validate we have source and target. validateTu(element); // Create TU objects SegmentTmTu tu = createTu(element); if (CATEGORY.isDebugEnabled()) { CATEGORY.debug(tu.toDebugString(true)); } m_result.setResultObject(tu); } catch (Throwable ex) { String msg = "Entry " + m_count + ": " + ex.getMessage(); m_result.setError(msg); if (CATEGORY.isDebugEnabled()) { CATEGORY.debug(msg, ex); } else { CATEGORY.warn(msg); } } boolean done = m_results.put(m_result); m_result = null; // Stop reading the TMX file. if (done) { throw new ThreadDeath(); } } }); String url = m_options.getFileName(); Document document = reader.read(url); } catch (ThreadDeath ignore) { CATEGORY.info("ReaderThread: interrupted."); } catch (Throwable ignore) { // Should never happen, and I don't know how to handle // this case other than passing the exception in // m_results, which I won't do for now. CATEGORY.error("unexpected error", ignore); } finally { if (m_result != null) { m_results.fireResult(m_result); } m_results.producerDone(); m_results = null; CATEGORY.debug("ReaderThread: done."); } }
From source file:com.globalsight.everest.tm.util.TmxAnalyzer.java
License:Apache License
public void analyze(String p_url) throws Exception { m_tuCount = 0;/*from w ww . jav a 2s . c o m*/ m_tuvCount = 0; m_localeCount = 0; m_locales = new HashSet(); m_tmxVersion = ""; m_tmx = null; SAXReader reader = new SAXReader(); reader.setXMLReaderClassName("org.apache.xerces.parsers.SAXParser"); reader.setEntityResolver(DtdResolver.getInstance()); reader.setValidation(true); log("Analyzing document: " + p_url); reader.addHandler("/tmx", new ElementHandler() { public void onStart(ElementPath path) { Element element = path.getCurrent(); m_tmxVersion = element.attributeValue("version"); } public void onEnd(ElementPath path) { } }); reader.addHandler("/tmx/header", new ElementHandler() { public void onStart(ElementPath path) { } public void onEnd(ElementPath path) { Element element = path.getCurrent(); element.detach(); m_tmx = new Tmx(element); m_tmx.setTmxVersion(m_tmxVersion); } }); // enable element complete notifications to conserve memory reader.addHandler("/tmx/body/tu", new ElementHandler() { public void onStart(ElementPath path) { ++m_tuCount; if (m_tuCount % 1000 == 0) { log("TU " + m_tuCount); } } public void onEnd(ElementPath path) { Element element = path.getCurrent(); List tuvs = element.selectNodes("//tuv"); m_tuvCount += tuvs.size(); for (int i = 0, max = tuvs.size(); i < max; i++) { Element tuv = (Element) tuvs.get(i); String locale = tuv.attributeValue("lang"); m_locales.add(locale); } // prune the current element to reduce memory element.detach(); element = null; } }); Document document = reader.read(p_url); m_localeCount = m_locales.size(); log("File: " + p_url); log("TMX version: " + m_tmxVersion); log("Total TUs: " + m_tuCount); log("Total TUVs: " + m_tuvCount); log("Total Locales: " + m_localeCount); for (Iterator it = m_locales.iterator(); it.hasNext();) { String locale = (String) it.next(); log(locale); } // all done }