List of usage examples for org.dom4j.io SAXReader setValidation
public void setValidation(boolean validation)
From source file:org.nuxeo.ecm.platform.webdav.request.tests.FakeResponse.java
License:Open Source License
public Element getXMLOutput() throws DocumentException, IOException { SAXReader saxReader = new SAXReader(); saxReader.setMergeAdjacentText(true); saxReader.setEncoding("UTF-8"); saxReader.setValidation(false); saxReader.setIncludeExternalDTDDeclarations(false); saxReader.setIncludeInternalDTDDeclarations(false); String xml = getOutput();/*from w w w . j a v a 2s.c om*/ InputStream in = new StringBlob(xml).getStream(); return saxReader.read(in).getRootElement(); }
From source file:org.olat.core.util.xml.XMLParser.java
License:Apache License
/** * @param in//from ww w .j a va 2s. c om * @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, ""); } catch (Exception e) { throw new OLATRuntimeException(XMLParser.class, "Exception reading XML", e); } return document; }
From source file:org.olat.ims.qti.qpool.ItemFileResourceValidator.java
License:Apache License
private Document readDocument(InputStream in) { try {// w w w .jav a 2 s .c o m SAXReader reader = new SAXReader(); reader.setEntityResolver(new IMSEntityResolver()); reader.setValidation(false); return reader.read(in, ""); } catch (Exception e) { return null; } }
From source file:org.opencms.importexport.CmsImportExportManager.java
License:Open Source License
/** * Returns an instance of an import/export handler implementation that is able to import * a specified resource.<p>/* w w w . ja va 2 s. co m*/ * * @param parameters the import parameters * * @return an instance of an import/export handler implementation * * @throws CmsImportExportException if something goes wrong */ public I_CmsImportExportHandler getImportExportHandler(CmsImportParameters parameters) throws CmsImportExportException { Document manifest; InputStream stream = null; CmsImportHelper helper = new CmsImportHelper(parameters); try { helper.openFile(); stream = helper.getFileStream(CmsImportExportManager.EXPORT_MANIFEST); SAXReader reader = new SAXReader(false); reader.setValidation(false); reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); manifest = reader.read(stream); } catch (Throwable e) { throw new CmsImportExportException( Messages.get().container(Messages.ERR_IMPORTEXPORT_FILE_NOT_FOUND_1, EXPORT_MANIFEST), e); } finally { try { if (stream != null) { stream.close(); } } catch (Exception e) { // noop } helper.closeFile(); } for (int i = 0; i < m_importExportHandlers.size(); i++) { I_CmsImportExportHandler handler = m_importExportHandlers.get(i); if (handler.matches(manifest)) { return handler; } } CmsMessageContainer message = Messages.get().container(Messages.ERR_IMPORTEXPORT_ERROR_NO_HANDLER_FOUND_1, EXPORT_MANIFEST); if (LOG.isDebugEnabled()) { LOG.debug(message.key()); } throw new CmsImportExportException(message); }
From source file:org.opencms.setup.comptest.CmsSetupTestXmlAPI.java
License:Open Source License
/** * @see org.opencms.setup.comptest.I_CmsSetupTest#execute(org.opencms.setup.CmsSetupBean) */// ww w .ja v a 2 s . co m public CmsSetupTestResult execute(CmsSetupBean setupBean) throws Exception { CmsSetupTestResult testResult = new CmsSetupTestResult(this); SAXReader reader = new SAXReader(); Throwable exc = null; try { reader.setValidation(false); reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); } catch (Throwable e) { exc = e; } if (exc == null) { testResult.setResult("passed"); testResult.setHelp( "OpenCms requires Xerces version 2 to run. Usually this should be available as part of the servlet environment."); testResult.setGreen(); } else { testResult.setResult("not passed"); testResult.setRed(); testResult.setInfo("OpenCms requires Xerces XML API to be configured. " + "Be sure to set following Java System parameters:<br><pre>" + "javax.xml.parsers.DocumentBuilderFactory=org.apache.xerces.jaxp.DocumentBuilderFactoryImpl\n" + "javax.xml.parsers.SAXParserFactory=org.apache.xerces.jaxp.SAXParserFactoryImpl\n" + "javax.xml.transform.TransformerFactory=org.apache.xalan.processor.TransformerFactoryImpl\n" + "org.xml.sax.driver=org.apache.xerces.parsers.SAXParser</pre>"); testResult.setHelp(exc.getClass().getName() + ": " + exc.getMessage()); } return testResult; }
From source file:org.opencms.util.ant.CmsXmlUtils.java
License:Open Source License
/** * Helper to unmarshal (read) xml contents from an input source into a document.<p> * //w w w . j ava2 s . c o m * Using this method ensures that the OpenCms XML entity resolver is used.<p> * * Important: The encoding provided will NOT be used during unmarshalling, * the XML parser will do this on the base of the information in the source String. * The encoding is used for initializing the created instance of the document, * which means it will be used when marshalling the document again later.<p> * * @param source the XML input source to use * @param resolver the XML entity resolver to use * @param validate if the reader should try to validate the xml code * * @return the unmarshalled XML document * * @throws Exception if something goes wrong */ public static Document unmarshalHelper(InputSource source, EntityResolver resolver, boolean validate) throws Exception { SAXReader reader = new SAXReader(); if (resolver != null) { reader.setEntityResolver(resolver); } reader.setMergeAdjacentText(true); reader.setStripWhitespaceText(true); if (!validate) { reader.setValidation(false); reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); } return reader.read(source); }
From source file:org.opencms.xml.CmsXmlUtils.java
License:Open Source License
/** * Helper to unmarshal (read) xml contents from an input source into a document.<p> * //from w ww .j a v a 2 s .c om * Using this method ensures that the OpenCms XML entity resolver is used.<p> * * Important: The encoding provided will NOT be used during unmarshalling, * the XML parser will do this on the base of the information in the source String. * The encoding is used for initializing the created instance of the document, * which means it will be used when marshalling the document again later.<p> * * @param source the XML input source to use * @param resolver the XML entity resolver to use * @param validate if the reader should try to validate the xml code * * @return the unmarshalled XML document * * @throws CmsXmlException if something goes wrong */ public static Document unmarshalHelper(InputSource source, EntityResolver resolver, boolean validate) throws CmsXmlException { try { SAXReader reader = new SAXReader(); if (resolver != null) { reader.setEntityResolver(resolver); } reader.setMergeAdjacentText(true); reader.setStripWhitespaceText(true); if (!validate) { reader.setValidation(false); reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); } return reader.read(source); } catch (DocumentException e) { throw new CmsXmlException(Messages.get().container(Messages.ERR_UNMARSHALLING_XML_DOC_0), e); } catch (SAXException e) { throw new CmsXmlException(Messages.get().container(Messages.ERR_UNMARSHALLING_XML_DOC_0), e); } }
From source file:org.pentaho.di.trans.steps.getxmldata.GetXMLData.java
License:Apache License
protected boolean setDocument(String StringXML, FileObject file, boolean IsInXMLField, boolean readurl) throws KettleException { this.prevRow = buildEmptyRow(); // pre-allocate previous row try {// www .ja v a 2 s . c om SAXReader reader = XMLParserFactoryProducer.getSAXReader(null); data.stopPruning = false; // Validate XML against specified schema? if (meta.isValidating()) { reader.setValidation(true); reader.setFeature("http://apache.org/xml/features/validation/schema", true); } else { // Ignore DTD declarations reader.setEntityResolver(new IgnoreDTDEntityResolver()); } // Ignore comments? if (meta.isIgnoreComments()) { reader.setIgnoreComments(true); } if (data.prunePath != null) { // when pruning is on: reader.read() below will wait until all is processed in the handler if (log.isDetailed()) { logDetailed(BaseMessages.getString(PKG, "GetXMLData.Log.StreamingMode.Activated")); } if (data.PathValue.equals(data.prunePath)) { // Edge case, but if true, there will only ever be one item in the list data.an = new ArrayList<>(1); // pre-allocate array and sizes data.an.add(null); } reader.addHandler(data.prunePath, new ElementHandler() { public void onStart(ElementPath path) { // do nothing here... } public void onEnd(ElementPath path) { if (isStopped()) { // when a large file is processed and it should be stopped it is still reading the hole thing // the only solution I see is to prune / detach the document and this will lead into a // NPE or other errors depending on the parsing location - this will be treated in the catch part below // any better idea is welcome if (log.isBasic()) { logBasic(BaseMessages.getString(PKG, "GetXMLData.Log.StreamingMode.Stopped")); } data.stopPruning = true; path.getCurrent().getDocument().detach(); // trick to stop reader return; } // process a ROW element if (log.isDebug()) { logDebug(BaseMessages.getString(PKG, "GetXMLData.Log.StreamingMode.StartProcessing")); } Element row = path.getCurrent(); try { // Pass over the row instead of just the document. If // if there's only one row, there's no need to // go back to the whole document. processStreaming(row); } catch (Exception e) { // catch the KettleException or others and forward to caller, e.g. when applyXPath() has a problem throw new RuntimeException(e); } // prune the tree row.detach(); if (log.isDebug()) { logDebug(BaseMessages.getString(PKG, "GetXMLData.Log.StreamingMode.EndProcessing")); } } }); } if (IsInXMLField) { // read string to parse data.document = reader.read(new StringReader(StringXML)); } else if (readurl && KettleVFS.startsWithScheme(StringXML)) { data.document = reader.read(KettleVFS.getInputStream(StringXML)); } else if (readurl) { // read url as source HttpClient client = HttpClientManager.getInstance().createDefaultClient(); HttpGet method = new HttpGet(StringXML); method.addHeader("Accept-Encoding", "gzip"); HttpResponse response = client.execute(method); Header contentEncoding = response.getFirstHeader("Content-Encoding"); HttpEntity responseEntity = response.getEntity(); if (responseEntity != null) { if (contentEncoding != null) { String acceptEncodingValue = contentEncoding.getValue(); if (acceptEncodingValue.contains("gzip")) { GZIPInputStream in = new GZIPInputStream(responseEntity.getContent()); data.document = reader.read(in); } } else { data.document = reader.read(responseEntity.getContent()); } } } else { // get encoding. By default UTF-8 String encoding = "UTF-8"; if (!Utils.isEmpty(meta.getEncoding())) { encoding = meta.getEncoding(); } InputStream is = KettleVFS.getInputStream(file); try { data.document = reader.read(is, encoding); } finally { BaseStep.closeQuietly(is); } } if (meta.isNamespaceAware()) { prepareNSMap(data.document.getRootElement()); } } catch (Exception e) { if (data.stopPruning) { // ignore error when pruning return false; } else { throw new KettleException(e); } } return true; }
From source file:org.pentaho.di.ui.trans.steps.getxmldata.LoopNodesImportProgressDialog.java
License:Apache License
@SuppressWarnings("unchecked") private String[] doScan(IProgressMonitor monitor) throws Exception { monitor.beginTask(//from ww w. j av a 2 s . c o m BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.ScanningFile", filename), 1); SAXReader reader = XMLParserFactoryProducer.getSAXReader(null); monitor.worked(1); if (monitor.isCanceled()) { return null; } // Validate XML against specified schema? if (meta.isValidating()) { reader.setValidation(true); reader.setFeature("http://apache.org/xml/features/validation/schema", true); } else { // Ignore DTD reader.setEntityResolver(new IgnoreDTDEntityResolver()); } monitor.worked(1); monitor.beginTask( BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.ReadingDocument"), 1); if (monitor.isCanceled()) { return null; } InputStream is = null; try { Document document = null; if (!Utils.isEmpty(filename)) { is = KettleVFS.getInputStream(filename); document = reader.read(is, encoding); } else { if (!Utils.isEmpty(xml)) { document = reader.read(new StringReader(xml)); } else { document = reader.read(new URL(url)); } } monitor.worked(1); monitor.beginTask( BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.DocumentOpened"), 1); monitor.worked(1); monitor.beginTask( BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.ReadingNode"), 1); if (monitor.isCanceled()) { return null; } List<Node> nodes = document.selectNodes(document.getRootElement().getName()); monitor.worked(1); monitor.subTask(BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.FetchNodes")); if (monitor.isCanceled()) { return null; } for (Node node : nodes) { if (monitor.isCanceled()) { return null; } if (!listpath.contains(node.getPath())) { nr++; monitor.subTask(BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.FetchNodes", String.valueOf(nr))); monitor.subTask(BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.AddingNode", node.getPath())); listpath.add(node.getPath()); addLoopXPath(node, monitor); } } monitor.worked(1); } finally { try { if (is != null) { is.close(); } } catch (Exception e) { /* Ignore */ } } String[] list_xpath = listpath.toArray(new String[listpath.size()]); monitor.setTaskName( BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.NodesReturned")); monitor.done(); return list_xpath; }
From source file:org.pentaho.di.ui.trans.steps.getxmldata.XMLInputFieldsImportProgressDialog.java
License:Apache License
@SuppressWarnings("unchecked") private RowMetaAndData[] doScan(IProgressMonitor monitor) throws Exception { monitor.beginTask(// w w w .j av a 2 s .c o m BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.ScanningFile", filename), 1); SAXReader reader = XMLParserFactoryProducer.getSAXReader(null); monitor.worked(1); if (monitor.isCanceled()) { return null; } // Validate XML against specified schema? if (meta.isValidating()) { reader.setValidation(true); reader.setFeature("http://apache.org/xml/features/validation/schema", true); } else { // Ignore DTD reader.setEntityResolver(new IgnoreDTDEntityResolver()); } monitor.worked(1); monitor.beginTask( BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.ReadingDocument"), 1); if (monitor.isCanceled()) { return null; } InputStream is = null; try { Document document = null; if (!Utils.isEmpty(filename)) { is = KettleVFS.getInputStream(filename); document = reader.read(is, encoding); } else { if (!Utils.isEmpty(xml)) { document = reader.read(new StringReader(xml)); } else { document = reader.read(new URL(url)); } } monitor.worked(1); monitor.beginTask( BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.DocumentOpened"), 1); monitor.worked(1); monitor.beginTask( BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.ReadingNode"), 1); if (monitor.isCanceled()) { return null; } List<Node> nodes = document.selectNodes(this.loopXPath); monitor.worked(1); monitor.subTask(BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.FetchNodes")); if (monitor.isCanceled()) { return null; } for (Node node : nodes) { if (monitor.isCanceled()) { return null; } nr++; monitor.subTask(BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.FetchNodes", String.valueOf(nr))); monitor.subTask(BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.FetchNodes", node.getPath())); setNodeField(node, monitor); childNode(node, monitor); } monitor.worked(1); } finally { try { if (is != null) { is.close(); } } catch (Exception e) { /* Ignore */ } } RowMetaAndData[] listFields = fieldsList.toArray(new RowMetaAndData[fieldsList.size()]); monitor.setTaskName( BaseMessages.getString(PKG, "GetXMLDateLoopNodesImportProgressDialog.Task.NodesReturned")); monitor.done(); return listFields; }