Search in sources :

Example 81 with SAXBuilder

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;
}
Also used : XMLReaderSAX2Factory(org.jdom2.input.sax.XMLReaderSAX2Factory) SAXBuilder(org.jdom2.input.SAXBuilder) Element(org.jdom2.Element) StringReader(java.io.StringReader) Document(org.jdom2.Document)

Example 82 with SAXBuilder

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();
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException) URL(java.net.URL) XPathFactory(org.jdom2.xpath.XPathFactory) HashSet(java.util.HashSet)

Example 83 with SAXBuilder

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;
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) IOException(java.io.IOException) Document(org.jdom2.Document) URL(java.net.URL)

Example 84 with SAXBuilder

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);
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) Element(org.jdom2.Element) StringReader(java.io.StringReader) Document(org.jdom2.Document)

Example 85 with SAXBuilder

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;
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder)

Aggregations

SAXBuilder (org.jdom2.input.SAXBuilder)134 Document (org.jdom2.Document)94 Element (org.jdom2.Element)55 IOException (java.io.IOException)33 JDOMException (org.jdom2.JDOMException)27 File (java.io.File)20 StringReader (java.io.StringReader)20 Test (org.junit.jupiter.api.Test)18 InputStream (java.io.InputStream)14 ArrayList (java.util.ArrayList)13 Test (org.junit.Test)11 URL (java.net.URL)9 XMLOutputter (org.jdom2.output.XMLOutputter)8 Modification (com.thoughtworks.go.domain.materials.Modification)7 List (java.util.List)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputSource (org.xml.sax.InputSource)6 ParseException (java.text.ParseException)5 MCRPath (org.mycore.datamodel.niofs.MCRPath)5 BufferedInputStream (java.io.BufferedInputStream)4