use of org.jdom2.input.SAXBuilder in project jspwiki by apache.
the class HtmlStringToWikiTranslator method htmlStringToElement.
/**
* Use NekoHtml to parse HTML like well formed XHTML
*
* @param html
* @return xhtml jdom root element (node "HTML")
* @throws JDOMException
* @throws IOException
*/
private Element htmlStringToElement(String html) throws JDOMException, IOException {
SAXBuilder builder = new SAXBuilder(new XMLReaderSAX2Factory(true, CYBERNEKO_PARSER), null, null);
Document doc = builder.build(new StringReader(html));
Element element = doc.getRootElement();
return element;
}
use of org.jdom2.input.SAXBuilder in project jspwiki by apache.
the class XmlUtil method parse.
/**
* Parses the given XML file and returns the requested nodes. If there's an error accessing or parsing the file, an
* empty list is returned.
*
* @param xml file to parse; matches all resources from classpath, filters repeated items.
* @param requestedNodes requestd nodes on the xml file
* @return the requested nodes of the XML file.
*/
public static List<Element> parse(String xml, String requestedNodes) {
if (StringUtils.isNotEmpty(xml) && StringUtils.isNotEmpty(requestedNodes)) {
Set<Element> readed = new HashSet<Element>();
SAXBuilder builder = new SAXBuilder();
try {
Enumeration<URL> resources = XmlUtil.class.getClassLoader().getResources(xml);
while (resources.hasMoreElements()) {
URL resource = resources.nextElement();
log.debug("reading " + resource.toString());
Document doc = builder.build(resource);
XPathFactory xpfac = XPathFactory.instance();
XPathExpression<Element> xp = xpfac.compile(requestedNodes, Filters.element());
// filter out repeated items
readed.addAll(xp.evaluate(doc));
}
return new ArrayList<Element>(readed);
} catch (IOException ioe) {
log.error("Couldn't load all " + xml + " resources", ioe);
} catch (JDOMException jdome) {
log.error("error parsing " + xml + " resources", jdome);
}
}
return Collections.<Element>emptyList();
}
use of org.jdom2.input.SAXBuilder in project jspwiki by apache.
the class WebContainerAuthorizer method getWebXml.
/**
* Returns an {@link org.jdom2.Document} representing JSPWiki's web
* application deployment descriptor. The document is obtained by calling
* the servlet context's <code>getResource()</code> method and requesting
* <code>/WEB-INF/web.xml</code>. For non-servlet applications, this
* method calls this class'
* {@link ClassLoader#getResource(java.lang.String)} and requesting
* <code>WEB-INF/web.xml</code>.
* @return the descriptor
* @throws IOException if the deployment descriptor cannot be found or opened
* @throws JDOMException if the deployment descriptor cannot be parsed correctly
*/
protected Document getWebXml() throws JDOMException, IOException {
URL url;
SAXBuilder builder = new SAXBuilder();
builder.setValidation(false);
builder.setEntityResolver(new LocalEntityResolver());
Document doc = null;
if (m_engine.getServletContext() == null) {
ClassLoader cl = WebContainerAuthorizer.class.getClassLoader();
url = cl.getResource("WEB-INF/web.xml");
if (url != null)
log.info("Examining " + url.toExternalForm());
} else {
url = m_engine.getServletContext().getResource("/WEB-INF/web.xml");
if (url != null)
log.info("Examining " + url.toExternalForm());
}
if (url == null)
throw new IOException("Unable to find web.xml for processing.");
log.debug("Processing web.xml at " + url.toExternalForm());
doc = builder.build(url);
return doc;
}
use of org.jdom2.input.SAXBuilder in project estatio by estatio.
the class LocationLookupService method extractLocation.
private Location extractLocation(final String xml) throws JDOMException, IOException {
final SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new StringReader(xml));
Element root = doc.getRootElement();
String lat = root.getChild("result").getChild("geometry").getChild("location").getChildTextTrim("lat");
String lon = root.getChild("result").getChild("geometry").getChild("location").getChildTextTrim("lng");
return Location.fromString(lat + ";" + lon);
}
use of org.jdom2.input.SAXBuilder in project pwm by pwm-project.
the class XmlUtil method getBuilder.
private static SAXBuilder getBuilder() {
final SAXBuilder builder = new SAXBuilder();
builder.setExpandEntities(false);
builder.setValidation(false);
builder.setFeature("http://xml.org/sax/features/resolve-dtd-uris", false);
return builder;
}
Aggregations