Search in sources :

Example 56 with SAXReader

use of org.dom4j.io.SAXReader in project archiva by apache.

the class XMLReader method init.

private void init(String type, URL url) throws XMLException {
    this.documentType = type;
    this.xmlUrl = url;
    SAXReader reader = new SAXReader();
    try (InputStream in = url.openStream()) {
        InputStreamReader inReader = new InputStreamReader(in, Charset.forName("UTF-8"));
        LatinEntityResolutionReader latinReader = new LatinEntityResolutionReader(inReader);
        this.document = reader.read(latinReader);
    } catch (DocumentException e) {
        throw new XMLException("Unable to parse " + documentType + " xml " + xmlUrl + ": " + e.getMessage(), e);
    } catch (IOException e) {
        throw new XMLException("Unable to open stream to " + url + ": " + e.getMessage(), e);
    }
    Element root = this.document.getRootElement();
    if (root == null) {
        throw new XMLException("Invalid " + documentType + " xml: root element is null.");
    }
    if (!StringUtils.equals(root.getName(), documentType)) {
        throw new XMLException("Invalid " + documentType + " xml: Unexpected root element <" + root.getName() + ">, expected <" + documentType + ">");
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) SAXReader(org.dom4j.io.SAXReader) InputStream(java.io.InputStream) DocumentException(org.dom4j.DocumentException) Element(org.dom4j.Element) IOException(java.io.IOException)

Example 57 with SAXReader

use of org.dom4j.io.SAXReader in project archiva by apache.

the class LatinEntityResolutionReaderTest method testReaderLeftOver.

@Test
public void testReaderLeftOver() throws IOException {
    Path inputFile = getExampleXml("maven-metadata-leftover.xml");
    // Bits from RepositoryMetadataReader.read
    InputStream in = null;
    SAXReader reader = new SAXReader();
    URL url = inputFile.toUri().toURL();
    in = url.openStream();
    InputStreamReader inReader = new InputStreamReader(in, Charset.forName("UTF-8"));
    LatinEntityResolutionReader latinReader = new LatinEntityResolutionReader(inReader);
    try {
        reader.read(latinReader);
    } catch (DocumentException e) {
        Assert.fail("Should not have failed here." + e);
        IOException ioe = new IOException();
        ioe.initCause(e);
        throw ioe;
    }
}
Also used : Path(java.nio.file.Path) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) SAXReader(org.dom4j.io.SAXReader) DocumentException(org.dom4j.DocumentException) IOException(java.io.IOException) URL(java.net.URL) Test(org.junit.Test)

Example 58 with SAXReader

use of org.dom4j.io.SAXReader in project tutorials by eugenp.

the class Dom4JParser method getNodeById.

public Node getNodeById(String id) {
    try {
        SAXReader reader = new SAXReader();
        Document document = reader.read(file);
        List<Node> elements = document.selectNodes("//*[@tutId='" + id + "']");
        return elements.get(0);
    } catch (DocumentException e) {
        e.printStackTrace();
        return null;
    }
}
Also used : SAXReader(org.dom4j.io.SAXReader) Node(org.dom4j.Node) DocumentException(org.dom4j.DocumentException) Document(org.dom4j.Document)

Example 59 with SAXReader

use of org.dom4j.io.SAXReader in project tutorials by eugenp.

the class Dom4JParser method getFirstElementList.

public List<Element> getFirstElementList() {
    try {
        SAXReader reader = new SAXReader();
        Document document = reader.read(file);
        return document.getRootElement().elements();
    } catch (DocumentException e) {
        e.printStackTrace();
        return null;
    }
}
Also used : SAXReader(org.dom4j.io.SAXReader) DocumentException(org.dom4j.DocumentException) Document(org.dom4j.Document)

Example 60 with SAXReader

use of org.dom4j.io.SAXReader in project application by collectionspace.

the class ReturnedDocument method setResponse.

@Override
public boolean setResponse(HttpMethod method, int status) throws IOException, DocumentException {
    // it's ok to release the parent connection since we consume the entire response stream here
    boolean result = true;
    this.status = status;
    InputStream stream = method.getResponseBodyAsStream();
    SAXReader reader = new SAXReader();
    if (status >= 400) {
        log.debug("Got error : " + IOUtils.toString(stream));
    }
    // TODO errorhandling
    Document out = null;
    Header content_type = method.getResponseHeader("Content-Type");
    if (content_type != null && "application/xml".equals(content_type.getValue())) {
        if (log.isDebugEnabled()) {
            ByteArrayOutputStream dump = new ByteArrayOutputStream();
            // TODO CSPACE-2552 add ,"UTF-8" to reader.read()?
            out = reader.read(new TeeInputStream(stream, dump));
            log.debug(dump.toString("UTF-8"));
        } else {
            // TODO CSPACE-2552 add ,"UTF-8" to reader.read()?
            out = reader.read(stream);
        }
    }
    stream.close();
    doc = out;
    return result;
}
Also used : Header(org.apache.commons.httpclient.Header) TeeInputStream(org.apache.commons.io.input.TeeInputStream) InputStream(java.io.InputStream) SAXReader(org.dom4j.io.SAXReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.dom4j.Document) TeeInputStream(org.apache.commons.io.input.TeeInputStream)

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