// /home/jean/Área de Trabalho/Link para Dropbox/workspace/GSPAnalyzer/grails app // examples/rgms-master/grails-app/views/bookChapter/show.gsp private void process(String uri) { try { URL url; try { url = new URL(uri); } catch (java.net.MalformedURLException mfu) { int idx = uri.indexOf(':'); if (idx == -1 || idx == 1) { // try file url = new URL("file:" + uri); } else { throw mfu; } } // Call SimpleHtmlRendererContext.navigate() // which implements incremental rendering. this.rcontext.navigate(url, null); } catch (Exception err) { logger.log(Level.SEVERE, "Error trying to load URI=[" + uri + "].", err); } }
/** A Swing frame that can be used to test the Cobra HTML rendering engine. */ public class TestFrame extends JFrame { private static final Logger logger = Logger.getLogger(TestFrame.class.getName()); private final SimpleHtmlRendererContext rcontext; private final JTree tree; private final HtmlPanel htmlPanel; private final JTextArea textArea; private final JTextField addressField; public TestFrame() throws HeadlessException { this(""); } public TestFrame(String title) throws HeadlessException { super(title); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = this.getContentPane(); contentPane.setLayout(new BorderLayout()); JPanel topPanel = new JPanel(); topPanel.setLayout(new BorderLayout()); JPanel bottomPanel = new JPanel(); bottomPanel.setLayout(new BorderLayout()); final JTextField textField = new JTextField(); this.addressField = textField; JButton button = new JButton("Parse & Render"); final JTabbedPane tabbedPane = new JTabbedPane(); final JTree tree = new JTree(); final JScrollPane scrollPane = new JScrollPane(tree); this.tree = tree; contentPane.add(topPanel, BorderLayout.NORTH); contentPane.add(bottomPanel, BorderLayout.CENTER); topPanel.add(new JLabel("URL: "), BorderLayout.WEST); topPanel.add(textField, BorderLayout.CENTER); topPanel.add(button, BorderLayout.EAST); bottomPanel.add(tabbedPane, BorderLayout.CENTER); final HtmlPanel panel = new HtmlPanel(); panel.addSelectionChangeListener( new SelectionChangeListener() { public void selectionChanged(SelectionChangeEvent event) { if (logger.isLoggable(Level.INFO)) { logger.info("selectionChanged(): selection node: " + panel.getSelectionNode()); } } }); this.htmlPanel = panel; UserAgentContext ucontext = new SimpleUserAgentContext(); this.rcontext = new LocalHtmlRendererContext(panel, ucontext); final JTextArea textArea = new JTextArea(); this.textArea = textArea; textArea.setEditable(false); final JScrollPane textAreaSp = new JScrollPane(textArea); tabbedPane.addTab("HTML", panel); tabbedPane.addTab("Tree", scrollPane); tabbedPane.addTab("Source", textAreaSp); tabbedPane.addChangeListener( new ChangeListener() { public void stateChanged(ChangeEvent e) { Component component = tabbedPane.getSelectedComponent(); if (component == scrollPane) { tree.setModel(new NodeTreeModel(panel.getRootNode())); } else if (component == textAreaSp) { textArea.setText(rcontext.getSourceCode()); } } }); button.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent event) { process(textField.getText()); } }); } public HtmlRendererContext getHtmlRendererContext() { return this.rcontext; } public void navigate(String uri) { this.addressField.setText(uri); this.process(uri); } // /home/jean/Área de Trabalho/Link para Dropbox/workspace/GSPAnalyzer/grails app // examples/rgms-master/grails-app/views/bookChapter/show.gsp private void process(String uri) { try { URL url; try { url = new URL(uri); } catch (java.net.MalformedURLException mfu) { int idx = uri.indexOf(':'); if (idx == -1 || idx == 1) { // try file url = new URL("file:" + uri); } else { throw mfu; } } // Call SimpleHtmlRendererContext.navigate() // which implements incremental rendering. this.rcontext.navigate(url, null); } catch (Exception err) { logger.log(Level.SEVERE, "Error trying to load URI=[" + uri + "].", err); } } private class LocalHtmlRendererContext extends SimpleHtmlRendererContext { public LocalHtmlRendererContext(HtmlPanel contextComponent, UserAgentContext ucontext) { super(contextComponent, ucontext); } public HtmlRendererContext open( URL url, String windowName, String windowFeatures, boolean replace) { TestFrame frame = new TestFrame("Cobra Test Tool"); frame.setSize(600, 400); frame.setExtendedState(TestFrame.NORMAL); frame.setVisible(true); HtmlRendererContext ctx = frame.getHtmlRendererContext(); ctx.setOpener(this); frame.navigate(url.toExternalForm()); return ctx; } } }
public static void main(String[] args) throws Exception { // Initialize logging so only Cobra warnings are seen. Logger.getLogger("org.lobobrowser").setLevel(Level.WARNING); // Open a connection on the URL we want to render first. String uri = "http://www.google.com"; URL url = new URL(uri); URLConnection connection = url.openConnection(); InputStream in = connection.getInputStream(); // A Reader should be created with the correct charset, // which may be obtained from the Content-Type header // of an HTTP response. Reader reader = new InputStreamReader(in); // InputSourceImpl constructor with URI recommended // so the renderer can resolve page component URLs. InputSource is = new InputSourceImpl(reader, uri); HtmlPanel htmlPanel = new HtmlPanel(); UserAgentContext ucontext = new LocalUserAgentContext(); HtmlRendererContext rendererContext = new LocalHtmlRendererContext(htmlPanel, ucontext); // Set a preferred width for the HtmlPanel, // which will allow getPreferredSize() to // be calculated according to block content. // We do this here to illustrate the // feature, but is generally not // recommended for performance reasons. htmlPanel.setPreferredWidth(800); // Note: This example does not perform incremental // rendering while loading the initial document. DocumentBuilderImpl builder = new DocumentBuilderImpl(rendererContext.getUserAgentContext(), rendererContext); Document document = builder.parse(is); in.close(); // Set the document in the HtmlPanel. This method // schedules the document to be rendered in the // GUI thread. htmlPanel.setDocument(document, rendererContext); // Create a JFrame and add the HtmlPanel to it. final JFrame frame = new JFrame(); frame.getContentPane().add(htmlPanel); // We pack the JFrame to demonstrate the // validity of HtmlPanel's preferred size. // Normally you would want to set a specific // JFrame size instead. // pack() should be called in the GUI dispatch // thread since the document is scheduled to // be rendered in that thread, and is required // for the preferred size determination. EventQueue.invokeLater( new Runnable() { public void run() { frame.pack(); frame.setVisible(true); } }); }