Search in sources :

Example 11 with Parser

use of org.ccil.cowan.tagsoup.Parser in project acs-aem-commons by Adobe-Consulting-Services.

the class SlingHtmlParser method parse.

/**
 * @see org.apache.sling.commons.html.HtmlParser#parse(java.lang.String, java.io.InputStream, java.lang.String)
 */
public Document parse(String systemId, InputStream stream, String encoding) throws IOException {
    final Parser parser = new Parser();
    final DOMBuilder builder = new DOMBuilder();
    final InputSource source = new InputSource(stream);
    source.setEncoding(encoding);
    source.setSystemId(systemId);
    try {
        parser.setProperty("http://xml.org/sax/properties/lexical-handler", builder);
        parser.setContentHandler(builder);
        parser.parse(source);
    } catch (SAXException se) {
        if (se.getCause() instanceof IOException) {
            throw (IOException) se.getCause();
        }
        throw (IOException) new IOException("Unable to parse xml.").initCause(se);
    }
    return builder.getDocument();
}
Also used : InputSource(org.xml.sax.InputSource) IOException(java.io.IOException) DOMBuilder(org.apache.sling.commons.html.impl.DOMBuilder) HtmlParser(org.apache.sling.commons.html.HtmlParser) Parser(org.ccil.cowan.tagsoup.Parser) SAXException(org.xml.sax.SAXException)

Example 12 with Parser

use of org.ccil.cowan.tagsoup.Parser in project WordPress-Android by wordpress-mobile.

the class WPHtml method fromHtml.

/**
     * Returns displayable styled text from the provided HTML string. Any
     * <img> tags in the HTML will use the specified ImageGetter to
     * request a representation of the image (use null if you don't want this)
     * and the specified TagHandler to handle unknown tags (specify null if you
     * don't want this).
     *
     * <p>
     * This uses TagSoup to handle real HTML, including all of the brokenness
     * found in the wild.
     */
public static Spanned fromHtml(String source, ImageGetter imageGetter, TagHandler tagHandler, Context ctx, PostModel post, int maxImageWidth) {
    Parser parser = new Parser();
    try {
        parser.setProperty(Parser.schemaProperty, HtmlParser.schema);
    } catch (org.xml.sax.SAXNotRecognizedException e) {
        // Should not happen.
        throw new RuntimeException(e);
    } catch (org.xml.sax.SAXNotSupportedException e) {
        // Should not happen.
        throw new RuntimeException(e);
    }
    HtmlToSpannedConverter converter = new HtmlToSpannedConverter(source, imageGetter, tagHandler, parser, ctx, post, maxImageWidth);
    return converter.convert();
}
Also used : Parser(org.ccil.cowan.tagsoup.Parser)

Example 13 with Parser

use of org.ccil.cowan.tagsoup.Parser in project android_frameworks_base by ResurrectionRemix.

the class HtmlToSpannedConverter method fromHtml.

/**
     * Returns displayable styled text from the provided HTML string. Any &lt;img&gt; tags in the
     * HTML will use the specified ImageGetter to request a representation of the image (use null
     * if you don't want this) and the specified TagHandler to handle unknown tags (specify null if
     * you don't want this).
     *
     * <p>This uses TagSoup to handle real HTML, including all of the brokenness found in the wild.
     */
public static Spanned fromHtml(String source, int flags, ImageGetter imageGetter, TagHandler tagHandler) {
    Parser parser = new Parser();
    try {
        parser.setProperty(Parser.schemaProperty, HtmlParser.schema);
    } catch (org.xml.sax.SAXNotRecognizedException e) {
        // Should not happen.
        throw new RuntimeException(e);
    } catch (org.xml.sax.SAXNotSupportedException e) {
        // Should not happen.
        throw new RuntimeException(e);
    }
    HtmlToSpannedConverter converter = new HtmlToSpannedConverter(source, imageGetter, tagHandler, parser, flags);
    return converter.convert();
}
Also used : Parser(org.ccil.cowan.tagsoup.Parser)

Example 14 with Parser

use of org.ccil.cowan.tagsoup.Parser in project android_frameworks_base by DirtyUnicorns.

the class HtmlToSpannedConverter method fromHtml.

/**
     * Returns displayable styled text from the provided HTML string. Any &lt;img&gt; tags in the
     * HTML will use the specified ImageGetter to request a representation of the image (use null
     * if you don't want this) and the specified TagHandler to handle unknown tags (specify null if
     * you don't want this).
     *
     * <p>This uses TagSoup to handle real HTML, including all of the brokenness found in the wild.
     */
public static Spanned fromHtml(String source, int flags, ImageGetter imageGetter, TagHandler tagHandler) {
    Parser parser = new Parser();
    try {
        parser.setProperty(Parser.schemaProperty, HtmlParser.schema);
    } catch (org.xml.sax.SAXNotRecognizedException e) {
        // Should not happen.
        throw new RuntimeException(e);
    } catch (org.xml.sax.SAXNotSupportedException e) {
        // Should not happen.
        throw new RuntimeException(e);
    }
    HtmlToSpannedConverter converter = new HtmlToSpannedConverter(source, imageGetter, tagHandler, parser, flags);
    return converter.convert();
}
Also used : Parser(org.ccil.cowan.tagsoup.Parser)

Example 15 with Parser

use of org.ccil.cowan.tagsoup.Parser in project sling by apache.

the class HtmlParserImpl method parse.

/**
     * @see org.apache.sling.commons.html.HtmlParser#parse(java.io.InputStream, java.lang.String, org.xml.sax.ContentHandler)
     */
public void parse(InputStream stream, String encoding, ContentHandler ch) throws SAXException {
    final Parser parser = new Parser();
    if (ch instanceof LexicalHandler) {
        parser.setProperty("http://xml.org/sax/properties/lexical-handler", ch);
    }
    for (String feature : features.keySet()) {
        parser.setProperty(feature, features.get(feature));
    }
    parser.setContentHandler(ch);
    final InputSource source = new InputSource(stream);
    source.setEncoding(encoding);
    try {
        parser.parse(source);
    } catch (IOException ioe) {
        throw new SAXException(ioe);
    }
}
Also used : InputSource(org.xml.sax.InputSource) LexicalHandler(org.xml.sax.ext.LexicalHandler) IOException(java.io.IOException) HtmlParser(org.apache.sling.commons.html.HtmlParser) Parser(org.ccil.cowan.tagsoup.Parser) SAXException(org.xml.sax.SAXException)

Aggregations

Parser (org.ccil.cowan.tagsoup.Parser)18 InputSource (org.xml.sax.InputSource)5 IOException (java.io.IOException)4 SAXException (org.xml.sax.SAXException)4 XMLReader (org.xml.sax.XMLReader)4 HtmlParser (org.apache.sling.commons.html.HtmlParser)3 HttpURLConnection (java.net.HttpURLConnection)2 Transformer (javax.xml.transform.Transformer)2 DOMResult (javax.xml.transform.dom.DOMResult)2 SAXSource (javax.xml.transform.sax.SAXSource)2 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 StringWriter (java.io.StringWriter)1 URL (java.net.URL)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 TransformerException (javax.xml.transform.TransformerException)1 XPath (javax.xml.xpath.XPath)1