From 1ece8330780ca49de1e71da0fb7bcf82cd73c5c0 Mon Sep 17 00:00:00 2001 From: oswetto Date: Sat, 25 Mar 2023 00:28:39 +0100 Subject: [PATCH] CobraHtmlPanel improve --- .../java/org/loboevolution/gui/HtmlPanel.java | 10 ++------- .../gui/LocalHtmlRendererConfig.java | 21 +++--------------- .../dom/domimpl/HTMLIFrameElementImpl.java | 8 ++++--- .../dom/domimpl/HTMLImageElementImpl.java | 17 +++++++++----- .../loboevolution/driver/CobraHtmlPanel.java | 22 +++++++++++++------ README.md | 14 +++++++++++- 6 files changed, 49 insertions(+), 43 deletions(-) diff --git a/LoboHTML/src/main/java/org/loboevolution/gui/HtmlPanel.java b/LoboHTML/src/main/java/org/loboevolution/gui/HtmlPanel.java index cbf305a..cfe7ded 100644 --- a/LoboHTML/src/main/java/org/loboevolution/gui/HtmlPanel.java +++ b/LoboHTML/src/main/java/org/loboevolution/gui/HtmlPanel.java @@ -506,17 +506,11 @@ public void setDefaultOverflowY(int overflow) { } } - public static HtmlPanel createlocalPanel(URLConnection connection, String uri) throws Exception { - final HtmlPanel panel = new HtmlPanel(); - panel.setBrowserPanel(null); + public static HtmlPanel createlocalPanel(URLConnection connection, HtmlPanel panel, HtmlRendererContext rendererContext, + HtmlRendererConfig config, String uri) throws Exception { try (InputStream in = HttpNetwork.openConnectionCheckRedirects(connection); Reader reader = new InputStreamReader(in, StandardCharsets.UTF_8)) { - final InputSource is = new InputSourceImpl(reader, uri); - final UserAgentContext ucontext = new UserAgentContext(new LocalHtmlRendererConfig()); - final HtmlRendererContext rendererContext = new LocalHtmlRendererContext(panel, ucontext); - final HtmlRendererConfig config = new LocalHtmlRendererConfig(); - panel.setPreferredSize(new Dimension(800, 400)); final DocumentBuilderImpl builder = new DocumentBuilderImpl(rendererContext.getUserAgentContext(),rendererContext, config); final Document document = builder.parse(is); panel.setDocument(document, rendererContext); diff --git a/LoboHTML/src/main/java/org/loboevolution/gui/LocalHtmlRendererConfig.java b/LoboHTML/src/main/java/org/loboevolution/gui/LocalHtmlRendererConfig.java index 629bd55..5c27da7 100644 --- a/LoboHTML/src/main/java/org/loboevolution/gui/LocalHtmlRendererConfig.java +++ b/LoboHTML/src/main/java/org/loboevolution/gui/LocalHtmlRendererConfig.java @@ -21,34 +21,19 @@ import lombok.AllArgsConstructor; import lombok.Data; -import org.loboevolution.common.Urls; -import org.loboevolution.component.IBrowserFrame; -import org.loboevolution.component.IBrowserPanel; -import org.loboevolution.component.IToolBar; import org.loboevolution.config.DesktopConfig; import org.loboevolution.config.HtmlRendererConfig; -import org.loboevolution.html.dom.HTMLElement; -import org.loboevolution.html.dom.HTMLLinkElement; -import org.loboevolution.html.dom.domimpl.HTMLDocumentImpl; -import org.loboevolution.html.dom.input.FormInput; -import org.loboevolution.http.UserAgentContext; import org.loboevolution.info.GeneralInfo; import org.loboevolution.info.TabInfo; import org.loboevolution.net.Cookie; import org.loboevolution.net.HttpNetwork; -import javax.swing.*; import java.awt.*; -import java.awt.event.MouseEvent; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.Proxy; import java.net.URL; -import java.net.URLConnection; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; +import java.util.Map; @Data @AllArgsConstructor diff --git a/LoboHTML/src/main/java/org/loboevolution/html/dom/domimpl/HTMLIFrameElementImpl.java b/LoboHTML/src/main/java/org/loboevolution/html/dom/domimpl/HTMLIFrameElementImpl.java index 20b4025..f3e7ecc 100644 --- a/LoboHTML/src/main/java/org/loboevolution/html/dom/domimpl/HTMLIFrameElementImpl.java +++ b/LoboHTML/src/main/java/org/loboevolution/html/dom/domimpl/HTMLIFrameElementImpl.java @@ -256,11 +256,13 @@ public void draw(FrameControl frameControl) { connection.setRequestProperty("User-Agent", UserAgent.getUserAgent()); connection.getHeaderField("Set-Cookie"); connection.connect(); - final HtmlPanel hpanel = HtmlPanel.createlocalPanel(connection, createURL.toString()); + HtmlPanel panel = new HtmlPanel(); + panel.setBrowserPanel(null); + panel = HtmlPanel.createlocalPanel(connection, panel, doc.getHtmlRendererContext(), doc.getHtmlRendererConfig(), createURL.toString()); if (Strings.isNotBlank(getWidth()) && Strings.isNotBlank(getHeight())) { - hpanel.setPreferredSize(new Dimension(Integer.parseInt(getWidth()), Integer.parseInt(getHeight()))); + panel.setPreferredSize(new Dimension(Integer.parseInt(getWidth()), Integer.parseInt(getHeight()))); } - frameControl.add(hpanel); + frameControl.add(panel); } } catch (Exception e) { logger.log(Level.SEVERE, e.getMessage(), e); diff --git a/LoboHTML/src/main/java/org/loboevolution/html/dom/domimpl/HTMLImageElementImpl.java b/LoboHTML/src/main/java/org/loboevolution/html/dom/domimpl/HTMLImageElementImpl.java index 2db5cd1..bd7b802 100644 --- a/LoboHTML/src/main/java/org/loboevolution/html/dom/domimpl/HTMLImageElementImpl.java +++ b/LoboHTML/src/main/java/org/loboevolution/html/dom/domimpl/HTMLImageElementImpl.java @@ -23,9 +23,11 @@ package org.loboevolution.html.dom.domimpl; import org.loboevolution.common.Urls; +import org.loboevolution.gui.HtmlRendererContext; import org.loboevolution.html.control.ImgSvgControl; import org.loboevolution.html.dom.HTMLImageElement; import org.loboevolution.gui.HtmlPanel; +import org.loboevolution.html.dom.nodeimpl.NodeImpl; import org.loboevolution.html.renderstate.ImageRenderState; import org.loboevolution.html.renderstate.RenderState; import org.loboevolution.net.UserAgent; @@ -410,12 +412,15 @@ public void draw(ImgSvgControl imgSvgControl) { connection.getHeaderField("Set-Cookie"); connection.connect(); - final HtmlPanel hpanel = HtmlPanel.createlocalPanel(connection, uri); - final Dimension dim = hpanel.getPreferredSize(); - final double height = getHeight() == -1 ? dim.getHeight() : getHeight(); - final double width = getWidth() == -1 ? dim.getWidth() : getWidth(); - hpanel.setPreferredSize(new Dimension((int) width, (int) height)); - imgSvgControl.add(hpanel); + NodeImpl mode = (NodeImpl) document; + HtmlPanel panel = new HtmlPanel(); + panel.setBrowserPanel(null); + HtmlRendererContext htmlRendererContext = mode.getHtmlRendererContext(); + panel = HtmlPanel.createlocalPanel(connection, panel, mode.getHtmlRendererContext(), mode.getHtmlRendererConfig(), uri); + final double height = getHeight() == -1 ? htmlRendererContext.getInnerWidth() : getHeight(); + final double width = getWidth() == -1 ? htmlRendererContext.getInnerWidth() : getWidth(); + panel.setPreferredSize(new Dimension((int) width, (int) height)); + imgSvgControl.add(panel); } catch (Exception e) { logger.log(Level.SEVERE, e.getMessage(), e); } diff --git a/LoboUnitTest/src/test/java/org/loboevolution/driver/CobraHtmlPanel.java b/LoboUnitTest/src/test/java/org/loboevolution/driver/CobraHtmlPanel.java index 042e0a4..b389248 100644 --- a/LoboUnitTest/src/test/java/org/loboevolution/driver/CobraHtmlPanel.java +++ b/LoboUnitTest/src/test/java/org/loboevolution/driver/CobraHtmlPanel.java @@ -1,7 +1,12 @@ package org.loboevolution.driver; +import org.loboevolution.config.HtmlRendererConfig; import org.loboevolution.gui.HtmlPanel; +import org.loboevolution.gui.HtmlRendererContext; +import org.loboevolution.gui.LocalHtmlRendererConfig; +import org.loboevolution.gui.LocalHtmlRendererContext; +import org.loboevolution.http.UserAgentContext; import org.loboevolution.net.UserAgent; import javax.swing.*; @@ -14,7 +19,7 @@ public class CobraHtmlPanel { public static void main(String[] args) throws Exception { JFrame window = new JFrame(); - window.setSize(600, 400); + window.setSize(800, 400); window.setVisible(true); URL createURL = new URL(Objects. @@ -23,11 +28,14 @@ public static void main(String[] args) throws Exception { connection.setRequestProperty("User-Agent", UserAgent.getUserAgent()); connection.getHeaderField("Set-Cookie"); connection.connect(); - final HtmlPanel hpanel = HtmlPanel.createlocalPanel(connection, createURL.toString()); - hpanel.setPreferredSize(new Dimension(500, 800)); - window.getContentPane().add(hpanel); + HtmlPanel panel = new HtmlPanel(); + panel.setBrowserPanel(null); + panel.setPreferredSize(new Dimension(800, 400)); + final HtmlRendererConfig config = new LocalHtmlRendererConfig(); + final UserAgentContext ucontext = new UserAgentContext(config); + final HtmlRendererContext rendererContext = new LocalHtmlRendererContext(panel, ucontext); + panel = HtmlPanel.createlocalPanel(connection, panel, rendererContext, config, createURL.toString()); + window.getContentPane().add(panel); } -} - -//https://codeberg.org/miurahr/LoboComponent \ No newline at end of file +} \ No newline at end of file diff --git a/README.md b/README.md index 9b432fa..6af2bcb 100644 --- a/README.md +++ b/README.md @@ -1 +1,13 @@ -# CobraEvolution \ No newline at end of file +# CobraEvolution + +CobraEvolution is HTML render and parser used from LoboEvolution [credits](https://github.com/LoboEvolution/LoboEvolution). + +### Maven Users +``` +mvn install +``` + +Code examples: +* Html Parser => [CobraParser](https://github.com/LoboEvolution/CobraEvolution/blob/main/LoboUnitTest/src/test/java/org/loboevolution/driver/CobraParser.java) +* Html Render and Parser => [CobraHtmlPanel](https://github.com/LoboEvolution/CobraEvolution/blob/main/LoboUnitTest/src/test/java/org/loboevolution/driver/CobraHtmlPanel.java) +* Java Browser => [LoboEvolution](https://github.com/LoboEvolution/LoboEvolution)