Example usage for org.jdom2.input SAXBuilder setEntityResolver

List of usage examples for org.jdom2.input SAXBuilder setEntityResolver

Introduction

In this page you can find the example usage for org.jdom2.input SAXBuilder setEntityResolver.

Prototype

public void setEntityResolver(final EntityResolver entityResolver) 

Source Link

Document

This sets custom EntityResolver for the Builder.

Usage

From source file:com.rometools.rome.io.WireFeedInput.java

License:Open Source License

/**
 * Creates and sets up a org.jdom2.input.SAXBuilder for parsing.
 *
 * @return a new org.jdom2.input.SAXBuilder object
 *///from  ww w.  ja  v  a 2  s.  co  m
protected SAXBuilder createSAXBuilder() {
    SAXBuilder saxBuilder;
    if (validate) {
        saxBuilder = new SAXBuilder(XMLReaders.DTDVALIDATING);
    } else {
        saxBuilder = new SAXBuilder(XMLReaders.NONVALIDATING);
    }
    saxBuilder.setEntityResolver(RESOLVER);

    //
    // This code is needed to fix the security problem outlined in
    // http://www.securityfocus.com/archive/1/297714
    //
    // Unfortunately there isn't an easy way to check if an XML parser
    // supports a particular feature, so
    // we need to set it and catch the exception if it fails. We also need
    // to subclass the JDom SAXBuilder
    // class in order to get access to the underlying SAX parser - otherwise
    // the features don't get set until
    // we are already building the document, by which time it's too late to
    // fix the problem.
    //
    // Crimson is one parser which is known not to support these features.
    try {

        final XMLReader parser = saxBuilder.createParser();

        setFeature(saxBuilder, parser, "http://xml.org/sax/features/external-general-entities", false);
        setFeature(saxBuilder, parser, "http://xml.org/sax/features/external-parameter-entities", false);
        setFeature(saxBuilder, parser, "http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

        if (!allowDoctypes) {
            setFeature(saxBuilder, parser, "http://apache.org/xml/features/disallow-doctype-decl", true);
        }

    } catch (final JDOMException e) {
        throw new IllegalStateException("JDOM could not create a SAX parser");
    }

    saxBuilder.setExpandEntities(false);

    return saxBuilder;

}

From source file:com.sun.syndication.io.WireFeedInput.java

License:Open Source License

/**
 * Creates and sets up a org.jdom2.input.SAXBuilder for parsing.
 * /*from  w w  w.  j a va  2s. c o  m*/
 * @return a new org.jdom2.input.SAXBuilder object
 */
protected SAXBuilder createSAXBuilder() {
    SAXBuilder saxBuilder = new SAXBuilder(_validate);
    saxBuilder.setEntityResolver(RESOLVER);

    //
    // This code is needed to fix the security problem outlined in http://www.securityfocus.com/archive/1/297714
    //
    // Unfortunately there isn't an easy way to check if an XML parser supports a particular feature, so
    // we need to set it and catch the exception if it fails. We also need to subclass the JDom SAXBuilder 
    // class in order to get access to the underlying SAX parser - otherwise the features don't get set until
    // we are already building the document, by which time it's too late to fix the problem.
    //
    // Crimson is one parser which is known not to support these features.
    try {
        XMLReader parser = saxBuilder.createParser();
        try {
            parser.setFeature("http://xml.org/sax/features/external-general-entities", false);
            saxBuilder.setFeature("http://xml.org/sax/features/external-general-entities", false);
        } catch (SAXNotRecognizedException e) {
            // ignore
        } catch (SAXNotSupportedException e) {
            // ignore
        }

        try {
            parser.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
            saxBuilder.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        } catch (SAXNotRecognizedException e) {
            // ignore
        } catch (SAXNotSupportedException e) {
            // ignore
        }

        try {
            parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
            saxBuilder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        } catch (SAXNotRecognizedException e) {
            // ignore
        } catch (SAXNotSupportedException e) {
            // ignore
        }

    } catch (JDOMException e) {
        throw new IllegalStateException("JDOM could not create a SAX parser");
    }

    saxBuilder.setExpandEntities(false);
    return saxBuilder;
}

From source file:cz.muni.fi.mir.mathmlcanonicalization.Settings.java

License:Apache License

/**
 * Sets properties desired for MathML normalization purpose
 *
 * @return initialized SAXBuilder instance
 *///from   w  ww  .  ja v  a 2 s. c  om
public static SAXBuilder setupSAXBuilder() {
    final SAXBuilder builder = new SAXBuilder();
    builder.setXMLReaderFactory(XMLReaders.NONVALIDATING);
    builder.setFeature("http://xml.org/sax/features/validation", false);
    //builder.setFeature("http://xml.org/sax/features/external-general-entities", true);
    builder.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", true);
    builder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", true);
    builder.setEntityResolver(new EntityResolver() {
        @Override
        public InputSource resolveEntity(String publicId, String systemId) {
            if (systemId.endsWith("dtd")) {
                String dtdLocation = Settings.getProperty(Settings.MATHMLDTD);
                return new InputSource(Settings.class.getResourceAsStream(dtdLocation));
            }
            return null;
        }
    });

    return builder;
}

From source file:de.nava.informa.parsers.FeedParser.java

License:Open Source License

/**
 * Parse feed from input source with base location set and create channel.
 *
 * @param cBuilder     specific channel builder to use.
 * @param inpSource    input source of data.
 * @param baseLocation base location of feed.
 * @return parsed channel.//  www  . j av a  2 s . c o m
 * @throws IOException    if IO errors occur.
 * @throws ParseException if parsing is not possible.
 */
public static ChannelIF parse(ChannelBuilderIF cBuilder, InputSource inpSource, URL baseLocation)
        throws IOException, ParseException {
    // document reading without validation
    SAXBuilder saxBuilder = new SAXBuilder();

    // turn off DTD loading
    saxBuilder.setEntityResolver(new NoOpEntityResolver());

    try {
        Document doc = saxBuilder.build(inpSource);
        ChannelIF channel = parse(cBuilder, doc);
        channel.setLocation(baseLocation);
        return channel;
    } catch (JDOMException e) {
        throw new ParseException("Problem parsing " + inpSource + ": " + e);
    }
}

From source file:de.nava.informa.parsers.OPMLParser.java

License:Open Source License

public static Collection<FeedIF> parse(InputSource inpSource, URL baseLocation)
        throws IOException, ParseException {
    // document reading without validation
    SAXBuilder saxBuilder = new SAXBuilder(false);
    // turn off DTD loading
    saxBuilder.setEntityResolver(new NoOpEntityResolver());
    try {/*from   w w  w . j  a v a  2s  . com*/
        Document doc = saxBuilder.build(inpSource);
        return parse(doc);
    } catch (JDOMException e) {
        throw new ParseException(e);
    }
}

From source file:ilarkesto.integration.jdom.JDom.java

License:Open Source License

private static SAXBuilder createSaxBuilder() {
    SAXBuilder builder = new SAXBuilder(false);
    builder.setExpandEntities(false);//from w  ww  .j av  a  2  s .co  m
    builder.setValidation(false);
    builder.setEntityResolver(DUMMY_ENTITY_RESOLVER);
    return builder;
}

From source file:io.smartspaces.workbench.project.jdom.JdomReader.java

License:Apache License

/**
 * Get the root element for a given input file.
 *
 * @param inputFile//w w w .j  a v a2s.c  o  m
 *          input project file
 *
 * @return top-level element
 */
Element getRootElement(File inputFile) {
    Document doc;
    try {
        SAXBuilder builder = new SAXBuilder();
        builder.setJDOMFactory(new LocatedJDOMFactory());
        builder.setFeature(XML_PARSER_FEATURE_XINCLUDE, true);
        builder.setEntityResolver(new MyEntityResolver());
        doc = builder.build(inputFile);
    } catch (Exception e) {
        throw new SmartSpacesException(
                String.format("Exception while processing %s", inputFile.getAbsolutePath()), e);
    }

    return doc.getRootElement();
}

From source file:io.wcm.handler.richtext.util.RichTextUtil.java

License:Apache License

/**
 * Parses XHTML text string. Adds a wrapping "root" element before parsing and returns this root element.
 * @param text XHTML text string (root element not needed)
 * @param xhtmlEntities If set to true, Resolving of XHtml entities in XHtml fragment is supported.
 * @return Root element with parsed xhtml content
 * @throws JDOMException Is thrown if the text could not be parsed as XHTML
 *//*from   www . j a  va  2 s  .  c  om*/
public static Element parseText(String text, boolean xhtmlEntities) throws JDOMException {

    // add root element
    String xhtmlString = (xhtmlEntities ? "<!DOCTYPE root [" + XHTML_ENTITY_DEF + "]>" : "") + "<root>" + text
            + "</root>";

    try {
        SAXBuilder saxBuilder = new SAXBuilder();

        if (xhtmlEntities) {
            saxBuilder.setEntityResolver(XHtmlEntityResolver.getInstance());
        }

        Document doc = saxBuilder.build(new StringReader(xhtmlString));
        return doc.getRootElement();
    } catch (IOException ex) {
        throw new RuntimeException("Error parsing XHTML fragment.", ex);
    }

}

From source file:jetbrains.buildServer.tools.XmlUtil.java

License:Apache License

@NotNull
private static SAXBuilder getBuilder(final boolean validate) {
    SAXBuilder builder = new SAXBuilder(validate);
    builder.setFeature("http://xml.org/sax/features/namespaces", true);
    builder.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
    builder.setEntityResolver(new DefaultHandler() {
        @Override//  ww  w.jav  a2  s  .  c  o m
        public InputSource resolveEntity(String publicId, String systemId) throws IOException, SAXException {
            String dtdFileName = new File(systemId).getName();
            InputStream dtdStream = getClass().getClassLoader().getResourceAsStream(dtdFileName);
            if (dtdStream != null) {
                return new InputSource(dtdStream);
            }

            return super.resolveEntity(publicId, systemId);
        }
    });

    return builder;
}

From source file:nl.colorize.util.xml.XMLHelper.java

License:Apache License

private static SAXBuilder getSaxBuilder() {
    SAXBuilder saxBuilder = new SAXBuilder();
    saxBuilder.setEntityResolver(new NoOpEntityResolver());
    return saxBuilder;
}