1 package org.lobobrowser.html.test; 2 3 import java.awt.BorderLayout ; 4 import java.awt.Component ; 5 import java.awt.Container ; 6 import java.awt.HeadlessException ; 7 import java.awt.event.ActionEvent ; 8 import java.awt.event.ActionListener ; 9 import java.net.*; 10 import java.io.*; 11 import java.util.logging.*; 12 13 import javax.swing.JButton ; 14 import javax.swing.JFrame ; 15 import javax.swing.JLabel ; 16 import javax.swing.JPanel ; 17 import javax.swing.JScrollPane ; 18 import javax.swing.JTabbedPane ; 19 import javax.swing.JTextArea ; 20 import javax.swing.JTextField ; 21 import javax.swing.JTree ; 22 23 24 import org.lobobrowser.html.*; 25 import org.lobobrowser.html.parser.*; 26 import org.lobobrowser.util.io.*; 27 import org.w3c.dom.*; 28 29 32 public class ParserTest extends JFrame { 33 private static final Logger logger = Logger.getLogger(ParserTest.class.getName()); 34 private final JTree tree; 35 private final JTextArea textArea; 36 37 public ParserTest() throws HeadlessException { 38 this("HTML Parser-Only Test Tool"); 39 } 40 41 public ParserTest(String title) throws HeadlessException { 42 super(title); 43 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 44 Container contentPane = this.getContentPane(); 45 contentPane.setLayout(new BorderLayout ()); 46 JPanel topPanel = new JPanel (); 47 topPanel.setLayout(new BorderLayout ()); 48 JPanel bottomPanel = new JPanel (); 49 bottomPanel.setLayout(new BorderLayout ()); 50 final JTextField textField = new JTextField (); 51 JButton button = new JButton ("Parse & Render"); 52 final JTabbedPane tabbedPane = new JTabbedPane (); 53 final JTree tree = new JTree (); 54 tree.setModel(null); 55 final JScrollPane scrollPane = new JScrollPane (tree); 56 57 this.tree = tree; 58 59 contentPane.add(topPanel, BorderLayout.NORTH); 60 contentPane.add(bottomPanel, BorderLayout.CENTER); 61 62 topPanel.add(new JLabel ("URL: "), BorderLayout.WEST); 63 topPanel.add(textField, BorderLayout.CENTER); 64 topPanel.add(button, BorderLayout.EAST); 65 66 bottomPanel.add(tabbedPane, BorderLayout.CENTER); 67 68 final JTextArea textArea = new JTextArea (); 69 textArea.setEditable(false); 70 this.textArea = textArea; 71 final JScrollPane textAreaSp = new JScrollPane (textArea); 72 73 tabbedPane.addTab("HTML DOM", scrollPane); 74 tabbedPane.addTab("Source Code", textAreaSp); 75 76 button.addActionListener(new ActionListener () { 77 public void actionPerformed(ActionEvent event) { 78 process(textField.getText()); 79 } 80 }); 81 } 82 83 84 private void process(String uri) { 85 try { 86 URL url; 87 try { 88 url = new URL(uri); 89 } catch(java.net.MalformedURLException mfu) { 90 int idx = uri.indexOf(':'); 91 if(idx == -1 || idx == 1) { 92 url = new URL("file:" + uri); 94 } 95 else { 96 throw mfu; 97 } 98 } 99 logger.info("process(): Loading URI=[" + uri + "]."); 100 long time0 = System.currentTimeMillis(); 101 URLConnection connection = url.openConnection(); 102 connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible;) Cobra/0.96.1+"); 103 connection.setRequestProperty("Cookie", ""); 104 if(connection instanceof HttpURLConnection) { 105 HttpURLConnection hc = (HttpURLConnection) connection; 106 hc.setInstanceFollowRedirects(true); 107 int responseCode = hc.getResponseCode(); 108 logger.info("process(): HTTP response code: " + responseCode); 109 } 110 InputStream in = connection.getInputStream(); 111 byte[] content; 112 try { 113 content = IORoutines.load(in, 8192); 114 } finally { 115 in.close(); 116 } 117 String source = new String (content, "ISO-8859-1"); 118 this.textArea.setText(source); 119 long time1 = System.currentTimeMillis(); 120 InputStream bin = new ByteArrayInputStream(content); 121 HtmlParserContext context = new SimpleHtmlParserContext(); 122 DocumentBuilderImpl builder = new DocumentBuilderImpl(context); 123 String actualURI = url.toExternalForm(); 125 Document document = builder.parse(new InputSourceImpl(bin, actualURI, "ISO-8859-1")); 127 long time2 = System.currentTimeMillis(); 128 logger.info("Parsed URI=[" + uri + "]: Parse elapsed: " + (time2 - time1) + " ms. Load elapsed: " + (time1 - time0) + " ms."); 129 this.tree.setModel(new NodeTreeModel(document)); 130 } catch(Exception err) { 131 logger.log(Level.SEVERE, "Error trying to load URI=[" + uri + "].", err); 132 } 133 } 134 135 public static void main(String [] args) { 136 ParserTest frame = new ParserTest(); 137 frame.setSize(800, 400); 138 frame.setExtendedState(TestFrame.MAXIMIZED_BOTH); 139 frame.setVisible(true); 140 } 141 } 142 | Popular Tags |