Java Code Examples for org.dom4j.io.SAXReader#setValidation()

The following examples show how to use org.dom4j.io.SAXReader#setValidation() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultDocumentFormater.java    From moql with Apache License 2.0 6 votes vote down vote up
public T importObject(InputStream is) throws XmlAccessException {
	SAXReader reader = new SAXReader();
	if (encoding != null)
		reader.setEncoding(encoding);
	Document document;
	try {
		if (validation) {
			// specify the schema to use
	        reader.setValidation(true);
	        reader.setFeature("http://apache.org/xml/features/validation/schema",true);
	        // add error handler which turns any errors into XML
	        ErrorChecker checker = new ErrorChecker();
	        reader.setErrorHandler(checker);
		}
		document = reader.read(is);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		throw new XmlAccessException("Read failed!", e);
	}
	return formater.importObjectFromElement(document.getRootElement());
}
 
Example 2
Source File: FileUtilities.java    From CogniCrypt with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Validate an XML file.
 *
 * @param helpLocation
 * @return
 */
private boolean validateXMLFile(final File helpLocation) {
	final SAXReader reader = new SAXReader();
	reader.setValidation(false);
	try {
		reader.read(helpLocation);
	}
	catch (final DocumentException e) {
		Activator.getDefault().logError(e);
		appendFileErrors(helpLocation.getName());
		return false;
	}
	return true;
}
 
Example 3
Source File: FileUtilitiesTest.java    From CogniCrypt with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Test method for {@link de.cognicrypt.codegenerator.taskintegrator.controllers.FileUtilities#updateThePluginXMLFileWithHelpData(java.lang.String)}.
 *
 * @throws DocumentException
 * @throws MalformedURLException
 */
@Test
public void testUpdateThePluginXMLFileWithHelpData() throws DocumentException, MalformedURLException {
	final FileUtilities fileUtilities = new FileUtilities(this.tempTaskName);
	fileUtilities.updateThePluginXMLFileWithHelpData(this.tempTaskName);

	final File pluginXMLFile = CodeGenUtils.getResourceFromWithin("src" + Constants.innerFileSeparator + ".." + Constants.innerFileSeparator + Constants.PLUGIN_XML_FILE);
	final SAXReader reader = new SAXReader();
	Document pluginXMLDocument = null;
	reader.setValidation(false);
	pluginXMLDocument = reader.read(pluginXMLFile);
	boolean isSuccessfulWrite = false;
	if (pluginXMLDocument != null) {
		final Element root = pluginXMLDocument.getRootElement();
		for (final Iterator<Element> extensionElement = root.elementIterator("extension"); extensionElement.hasNext();) {
			final Element currentExtensionElement = extensionElement.next();
			final Attribute point = currentExtensionElement.attribute("point");
			if (point != null && point.getValue().equals("org.eclipse.help.contexts")) {
				for (final Iterator<Element> helpFileContext = currentExtensionElement.elementIterator("contexts"); helpFileContext.hasNext();) {
					final Element currentHelpFileContext = helpFileContext.next();
					for (final Iterator<Attribute> it = currentHelpFileContext.attributeIterator(); it.hasNext();) {
						final Attribute file = it.next();
						if (file.getName().equals("file") && file.getValue().equals("src" + Constants.innerFileSeparator + "main" + Constants.innerFileSeparator + "resources"
								+ Constants.innerFileSeparator + "Help" + Constants.innerFileSeparator + this.tempTaskName + ".xml")) {
							isSuccessfulWrite = true;
						}
					}
				}
			}
		}

	}

	assertTrue(isSuccessfulWrite);
}
 
Example 4
Source File: XMLHelper.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public SAXReader createSAXReader(ErrorLogger errorLogger, EntityResolver entityResolver) {
	SAXReader saxReader = new SAXReader();
	saxReader.setMergeAdjacentText( true );
	saxReader.setValidation( true );
	saxReader.setErrorHandler( errorLogger );
	saxReader.setEntityResolver( entityResolver );

	return saxReader;
}
 
Example 5
Source File: XMLParser.java    From olat with Apache License 2.0 5 votes vote down vote up
/**
 * @param in
 * @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) {
        log.error("Exception reading XML", e);
        throw new OLATRuntimeException(XMLParser.class, "Exception reading XML", e);
    }
    return document;
}
 
Example 6
Source File: XMLParser.java    From olat with Apache License 2.0 5 votes vote down vote up
/**
 * @param in
 * @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) {
        log.error("Exception reading XML", e);
        throw new OLATRuntimeException(XMLParser.class, "Exception reading XML", e);
    }
    return document;
}
 
Example 7
Source File: XmlInputFieldsImportProgressDialog.java    From hop with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings( "unchecked" )
private RowMetaAndData[] doScan( IProgressMonitor monitor ) throws Exception {
  monitor.beginTask( 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 = HopVfs.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;

}
 
Example 8
Source File: LoopNodesImportProgressDialog.java    From hop with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings( "unchecked" )
private String[] doScan( IProgressMonitor monitor ) throws Exception {
  monitor.beginTask( 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 = HopVfs.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;

}
 
Example 9
Source File: DOM4JHandleExample.java    From java-client-api with Apache License 2.0 4 votes vote down vote up
public static void runShortcut(ExampleProperties props)
  throws IOException, DocumentException
{
  String filename = "flipper.xml";

  // register the handle from the extra library
  DatabaseClientFactory.getHandleRegistry().register(
    DOM4JHandle.newFactory()
  );

  // create the client
  DatabaseClient client = DatabaseClientFactory.newClient(
    props.host, props.port, props.writerUser, props.writerPassword,
    props.authType);

  // create a manager for documents of any format
  XMLDocumentManager docMgr = client.newXMLDocumentManager();

  // read the example file
  InputStream docStream = Util.openStream("data"+File.separator+filename);
  if (docStream == null)
    throw new IOException("Could not read document example");

  // create an identifier for the document
  String docId = "/example/"+filename;

  // parse the example file with dom4j
  SAXReader reader = new SAXReader();
  reader.setValidation(false);
  Document writeDocument =
    reader.read(new InputStreamReader(docStream, "UTF-8"));

  // write the document
  docMgr.writeAs(docId, writeDocument);

  // ... at some other time ...

  // read the document content
  Document readDocument = docMgr.readAs(docId, Document.class);

  String rootName = readDocument.getRootElement().getName();

  // delete the document
  docMgr.delete(docId);

  System.out.println("(Shortcut) Wrote and read /example/"+filename+
    " content with the <"+rootName+"/> root element using dom4j");

  // release the client
  client.release();
}
 
Example 10
Source File: LoopNodesImportProgressDialog.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings( "unchecked" )
private String[] doScan( IProgressMonitor monitor ) throws Exception {
  monitor.beginTask( 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;

}
 
Example 11
Source File: XMLInputFieldsImportProgressDialog.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings( "unchecked" )
private RowMetaAndData[] doScan( IProgressMonitor monitor ) throws Exception {
  monitor.beginTask( 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;

}
 
Example 12
Source File: XSLStringGenerationAndManipulationTests.java    From CogniCrypt with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * Generate a Document object from the given file location.
 *
 * @param file
 * @return
 * @throws DocumentException
 */
public Document readDocFomFile(final File file) throws DocumentException {
	final SAXReader reader = new SAXReader();
	reader.setValidation(false);
	return reader.read(file.getAbsolutePath());
}