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 + ">");
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
Aggregations