Search in sources :

Example 76 with SAXReader

use of org.dom4j.io.SAXReader in project OpenOLAT by OpenOLAT.

the class XMLParser method parse.

/**
 * @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, "");
        document.normalize();
    } catch (Exception e) {
        throw new OLATRuntimeException(XMLParser.class, "Exception reading XML", e);
    }
    return document;
}
Also used : OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) SAXReader(org.dom4j.io.SAXReader) Document(org.dom4j.Document) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException)

Example 77 with SAXReader

use of org.dom4j.io.SAXReader in project core by craftercms.

the class XmlUtilsTest method setUpTestDocument.

private void setUpTestDocument() throws SAXException {
    SAXReader reader = new SAXReader();
    reader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
    reader.setFeature("http://xml.org/sax/features/external-general-entities", false);
    reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
    try {
        document = reader.read(new StringReader(XML));
    } catch (DocumentException e) {
    }
}
Also used : SAXReader(org.dom4j.io.SAXReader) DocumentException(org.dom4j.DocumentException) StringReader(java.io.StringReader)

Example 78 with SAXReader

use of org.dom4j.io.SAXReader in project core by craftercms.

the class AttributeAddingProcessorTest method readInputXml.

private Document readInputXml() throws DocumentException, SAXException {
    SAXReader reader = new SAXReader();
    reader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
    reader.setFeature("http://xml.org/sax/features/external-general-entities", false);
    reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
    return reader.read(new StringReader(INPUT_XML));
}
Also used : SAXReader(org.dom4j.io.SAXReader) StringReader(java.io.StringReader)

Example 79 with SAXReader

use of org.dom4j.io.SAXReader in project maven-archetype by apache.

the class DefaultOldArchetype method addModuleToParentPom.

static boolean addModuleToParentPom(String artifactId, Reader fileReader, Writer fileWriter) throws DocumentException, IOException, ArchetypeTemplateProcessingException {
    SAXReader reader = new SAXReader();
    Document document = reader.read(fileReader);
    Element project = document.getRootElement();
    String packaging = null;
    Element packagingElement = project.element("packaging");
    if (packagingElement != null) {
        packaging = packagingElement.getStringValue();
    }
    if (!"pom".equals(packaging)) {
        throw new ArchetypeTemplateProcessingException("Unable to add module to the current project as it is not of packaging type 'pom'");
    }
    Element modules = project.element("modules");
    if (modules == null) {
        modules = project.addText("  ").addElement("modules");
        modules.setText("\n  ");
        project.addText("\n");
    }
    boolean found = false;
    for (Iterator<?> i = modules.elementIterator("module"); i.hasNext() && !found; ) {
        Element module = (Element) i.next();
        if (module.getText().equals(artifactId)) {
            found = true;
        }
    }
    if (!found) {
        Node lastTextNode = null;
        for (Iterator<?> i = modules.nodeIterator(); i.hasNext(); ) {
            Node node = (Node) i.next();
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                lastTextNode = null;
            } else if (node.getNodeType() == Node.TEXT_NODE) {
                lastTextNode = node;
            }
        }
        if (lastTextNode != null) {
            modules.remove(lastTextNode);
        }
        modules.addText("\n    ");
        modules.addElement("module").setText(artifactId);
        modules.addText("\n  ");
        XMLWriter writer = new XMLWriter(fileWriter);
        writer.write(document);
    }
    return !found;
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) Node(org.dom4j.Node) Document(org.dom4j.Document) XMLWriter(org.dom4j.io.XMLWriter)

Example 80 with SAXReader

use of org.dom4j.io.SAXReader in project spring-framework by spring-projects.

the class XsltViewTests method assertHtmlOutput.

@SuppressWarnings("rawtypes")
private void assertHtmlOutput(String output) throws Exception {
    SAXReader reader = new SAXReader();
    Document document = reader.read(new StringReader(output));
    List nodes = document.getRootElement().selectNodes("/html/body/table/tr");
    Element tr1 = (Element) nodes.get(0);
    assertRowElement(tr1, "1", "Whatsit", "12.99");
    Element tr2 = (Element) nodes.get(1);
    assertRowElement(tr2, "2", "Thingy", "13.99");
    Element tr3 = (Element) nodes.get(2);
    assertRowElement(tr3, "3", "Gizmo", "14.99");
    Element tr4 = (Element) nodes.get(3);
    assertRowElement(tr4, "4", "Cranktoggle", "11.99");
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) StringReader(java.io.StringReader) List(java.util.List) Document(org.dom4j.Document)

Aggregations

SAXReader (org.dom4j.io.SAXReader)322 Document (org.dom4j.Document)256 Element (org.dom4j.Element)195 StringReader (java.io.StringReader)120 DocumentException (org.dom4j.DocumentException)74 Test (org.junit.jupiter.api.Test)74 File (java.io.File)54 List (java.util.List)49 IOException (java.io.IOException)48 InputStream (java.io.InputStream)48 ArrayList (java.util.ArrayList)47 Node (org.dom4j.Node)28 FileInputStream (java.io.FileInputStream)25 HashMap (java.util.HashMap)24 XMLWriter (org.dom4j.io.XMLWriter)22 ByteArrayInputStream (java.io.ByteArrayInputStream)20 URL (java.net.URL)18 OutputFormat (org.dom4j.io.OutputFormat)18 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)17 Test (org.junit.Test)14