Search in sources :

Example 1 with WritableLineReader

use of org.loboevolution.html.io.WritableLineReader in project LoboEvolution by LoboEvolution.

the class LoboWebDriver method loadHtml.

/**
 * <p>loadHtml.</p>
 *
 * @param in a {@link java.io.InputStream} object.
 * @return a {@link org.loboevolution.html.dom.domimpl.HTMLDocumentImpl} object.
 */
protected static HTMLDocumentImpl loadHtml(InputStream in) {
    HTMLDocumentImpl doc = null;
    try {
        WritableLineReader wis = new WritableLineReader(new InputStreamReader(in, StandardCharsets.UTF_8));
        final UserAgentContext ucontext = new UserAgentContext(true);
        HtmlPanel panel = new HtmlPanel();
        panel.setPreferredSize(new Dimension(800, 400));
        final HtmlRendererContext rendererContext = new HtmlRendererContext(panel, ucontext);
        rendererContext.setTest(true);
        ucontext.setUserAgentEnabled(true);
        doc = new HTMLDocumentImpl(ucontext, rendererContext, wis, "http://www.example.com/xhtml/htmlsample.html");
        doc.load();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return doc;
}
Also used : WritableLineReader(org.loboevolution.html.io.WritableLineReader) HtmlRendererContext(org.loboevolution.http.HtmlRendererContext) HTMLDocumentImpl(org.loboevolution.html.dom.domimpl.HTMLDocumentImpl) InputStreamReader(java.io.InputStreamReader) UserAgentContext(org.loboevolution.http.UserAgentContext) HtmlPanel(org.loboevolution.html.gui.HtmlPanel)

Example 2 with WritableLineReader

use of org.loboevolution.html.io.WritableLineReader in project LoboEvolution by LoboEvolution.

the class LoboWebDriver method loadHtml.

/**
 * <p>loadHtml.</p>
 *
 * @param html a {@link java.lang.String} object.
 * @return a {@link org.loboevolution.html.dom.domimpl.HTMLDocumentImpl} object.
 */
protected HTMLDocumentImpl loadHtml(String html) {
    HTMLDocumentImpl doc = null;
    try {
        WritableLineReader wis = new WritableLineReader(new StringReader(html));
        final UserAgentContext ucontext = new UserAgentContext(true);
        HtmlPanel panel = new HtmlPanel();
        panel.setPreferredSize(new Dimension(800, 400));
        final HtmlRendererContext rendererContext = new HtmlRendererContext(panel, ucontext);
        rendererContext.setTest(true);
        ucontext.setUserAgentEnabled(true);
        doc = new HTMLDocumentImpl(ucontext, rendererContext, wis, "http://www.example.com/xhtml/htmlsample.html");
        doc.load();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return doc;
}
Also used : WritableLineReader(org.loboevolution.html.io.WritableLineReader) HtmlRendererContext(org.loboevolution.http.HtmlRendererContext) HTMLDocumentImpl(org.loboevolution.html.dom.domimpl.HTMLDocumentImpl) UserAgentContext(org.loboevolution.http.UserAgentContext) HtmlPanel(org.loboevolution.html.gui.HtmlPanel) StringReader(java.io.StringReader)

Example 3 with WritableLineReader

use of org.loboevolution.html.io.WritableLineReader in project LoboEvolution by LoboEvolution.

the class HTMLDocumentImpl method load.

/**
 * <p>load.</p>
 *
 * @param closeReader a boolean.
 * @throws java.io.IOException if any.
 * @throws org.xml.sax.SAXException if any.
 * @throws java.io.UnsupportedEncodingException if any.
 */
public void load(boolean closeReader) throws IOException, SAXException, UnsupportedEncodingException {
    WritableLineReader reader;
    synchronized (this) {
        removeAllChildrenImpl();
        setTitle(null);
        setBaseURI(null);
        setDefaultTarget(null);
        this.styleSheets.clear();
        this.styleSheetAggregator = null;
        reader = this.reader;
    }
    if (reader != null) {
        try {
            final ErrorHandler errorHandler = new LocalErrorHandler();
            final HtmlParser parser = new HtmlParser(getUcontext(), document, errorHandler, true);
            parser.parse(reader);
        } finally {
            if (closeReader) {
                try {
                    reader.close();
                } catch (final Exception err) {
                    logger.log(Level.WARNING, "load(): Unable to close stream", err);
                }
                synchronized (this) {
                    this.reader = null;
                }
            }
        }
    }
}
Also used : WritableLineReader(org.loboevolution.html.io.WritableLineReader) ErrorHandler(org.xml.sax.ErrorHandler) LocalErrorHandler(org.loboevolution.html.io.LocalErrorHandler) HtmlParser(org.loboevolution.html.parser.HtmlParser) LocalErrorHandler(org.loboevolution.html.io.LocalErrorHandler) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 4 with WritableLineReader

use of org.loboevolution.html.io.WritableLineReader in project LoboEvolution by LoboEvolution.

the class DocumentBuilderImpl method createDocument.

/**
 * Creates a document without parsing the input provided, so the document object
 * can be used for incremental rendering.
 *
 * @param is The input source, which may be an instance of
 *           {@link org.loboevolution.html.parser.InputSourceImpl}. The input
 *           source must provide either an input stream or a reader.
 * @see HTMLDocumentImpl#load()
 * @return a {@link org.loboevolution.html.node.Document} object.
 * @throws org.xml.sax.SAXException if any.
 * @throws java.io.IOException if any.
 */
public Document createDocument(InputSource is) throws SAXException, IOException {
    final String encoding = is.getEncoding();
    String charset = encoding;
    if (charset == null) {
        charset = "US-ASCII";
    }
    final String uri = is.getSystemId();
    if (uri == null) {
        logger.warning("parse(): InputSource has no SystemId (URI); document item URLs will not be resolvable.");
    }
    WritableLineReader wis;
    final Reader reader = is.getCharacterStream();
    if (reader != null) {
        wis = new WritableLineReader(reader);
    } else {
        InputStream in = is.getByteStream();
        if (in != null) {
            wis = new WritableLineReader(new InputStreamReader(in, charset));
        } else if (uri != null) {
            final URLConnection connection = new URL(uri).openConnection();
            in = connection.getInputStream();
            if (encoding == null) {
                charset = Urls.getCharset(connection);
            }
            wis = new WritableLineReader(new InputStreamReader(in, charset));
        } else {
            throw new IllegalArgumentException("The InputSource must have either a reader, an input stream or a URI.");
        }
    }
    return new HTMLDocumentImpl(this.bcontext, this.rcontext, wis, uri);
}
Also used : WritableLineReader(org.loboevolution.html.io.WritableLineReader) HTMLDocumentImpl(org.loboevolution.html.dom.domimpl.HTMLDocumentImpl) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) WritableLineReader(org.loboevolution.html.io.WritableLineReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) URLConnection(java.net.URLConnection) URL(java.net.URL)

Aggregations

WritableLineReader (org.loboevolution.html.io.WritableLineReader)4 HTMLDocumentImpl (org.loboevolution.html.dom.domimpl.HTMLDocumentImpl)3 InputStreamReader (java.io.InputStreamReader)2 HtmlPanel (org.loboevolution.html.gui.HtmlPanel)2 HtmlRendererContext (org.loboevolution.http.HtmlRendererContext)2 UserAgentContext (org.loboevolution.http.UserAgentContext)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 LocalErrorHandler (org.loboevolution.html.io.LocalErrorHandler)1 HtmlParser (org.loboevolution.html.parser.HtmlParser)1 ErrorHandler (org.xml.sax.ErrorHandler)1 SAXException (org.xml.sax.SAXException)1