1 package org.lobobrowser.html.test; 2 3 import java.awt.BorderLayout ; 4 import java.awt.Container ; 5 import java.awt.HeadlessException ; 6 import java.awt.event.ActionEvent ; 7 import java.awt.event.ActionListener ; 8 import java.net.*; 9 import java.io.*; 10 import java.util.logging.*; 11 12 import javax.swing.JButton ; 13 import javax.swing.JFrame ; 14 import javax.swing.JLabel ; 15 import javax.swing.JPanel ; 16 import javax.swing.JScrollPane ; 17 import javax.swing.JTabbedPane ; 18 import javax.swing.JTextArea ; 19 import javax.swing.JTextField ; 20 import javax.swing.JTree ; 21 import javax.swing.event.ChangeEvent ; 22 import javax.swing.event.ChangeListener ; 23 24 import org.lobobrowser.html.*; 25 import org.lobobrowser.html.gui.*; 26 import org.lobobrowser.html.parser.*; 27 import org.lobobrowser.html.style.*; 28 import org.lobobrowser.util.io.*; 29 import org.w3c.css.sac.*; 30 import org.w3c.css.*; 31 import org.w3c.dom.css.*; 32 33 import com.steadystate.css.parser.CSSOMParser; 34 35 38 public class CssParserTest extends JFrame { 39 private static final Logger logger = Logger.getLogger(CssParserTest.class.getName()); 40 private final HtmlPanel cssOutput; 41 private final JTextArea textArea; 42 43 public CssParserTest() throws HeadlessException { 44 this("CSS Parser Test Tool"); 45 } 46 47 public CssParserTest(String title) throws HeadlessException { 48 super(title); 49 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 50 Container contentPane = this.getContentPane(); 51 contentPane.setLayout(new BorderLayout ()); 52 JPanel topPanel = new JPanel (); 53 topPanel.setLayout(new BorderLayout ()); 54 JPanel bottomPanel = new JPanel (); 55 bottomPanel.setLayout(new BorderLayout ()); 56 final JTextField textField = new JTextField (); 57 JButton button = new JButton ("Parse & Render"); 58 final JTabbedPane tabbedPane = new JTabbedPane (); 59 60 HtmlPanel htmlPanel = new HtmlPanel(); 61 this.cssOutput = htmlPanel; 62 63 contentPane.add(topPanel, BorderLayout.NORTH); 64 contentPane.add(bottomPanel, BorderLayout.CENTER); 65 66 topPanel.add(new JLabel ("URL: "), BorderLayout.WEST); 67 topPanel.add(textField, BorderLayout.CENTER); 68 topPanel.add(button, BorderLayout.EAST); 69 70 bottomPanel.add(tabbedPane, BorderLayout.CENTER); 71 72 final JTextArea textArea = new JTextArea (); 73 this.textArea = textArea; 74 final JScrollPane textAreaSp = new JScrollPane (textArea); 75 76 tabbedPane.addTab("Parsed CSS", htmlPanel); 77 tabbedPane.addTab("Source Code", textAreaSp); 78 79 button.addActionListener(new ActionListener () { 80 public void actionPerformed(ActionEvent event) { 81 process(textField.getText()); 82 } 83 }); 84 } 85 86 87 private void process(String uri) { 88 try { 89 URL url; 90 try { 91 url = new URL(uri); 92 } catch(java.net.MalformedURLException mfu) { 93 int idx = uri.indexOf(':'); 94 if(idx == -1 || idx == 1) { 95 url = new URL("file:" + uri); 97 } 98 else { 99 throw mfu; 100 } 101 } 102 logger.info("process(): Loading URI=[" + uri + "]."); 103 long time0 = System.currentTimeMillis(); 104 URLConnection connection = url.openConnection(); 105 connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible;) Cobra/0.96.1+"); 106 connection.setRequestProperty("Cookie", ""); 107 if(connection instanceof HttpURLConnection) { 108 HttpURLConnection hc = (HttpURLConnection) connection; 109 hc.setInstanceFollowRedirects(true); 110 int responseCode = hc.getResponseCode(); 111 logger.info("process(): HTTP response code: " + responseCode); 112 } 113 InputStream in = connection.getInputStream(); 114 byte[] content; 115 try { 116 content = IORoutines.load(in, 8192); 117 } finally { 118 in.close(); 119 } 120 String source = new String (content, "ISO-8859-1"); 121 this.textArea.setText(source); 122 long time1 = System.currentTimeMillis(); 123 CSSOMParser parser = new CSSOMParser(); 124 InputSource is = CSSUtilities.getCssInputSourceForStyleSheet(source); 125 CSSStyleSheet styleSheet = parser.parseStyleSheet(is); 126 long time2 = System.currentTimeMillis(); 127 logger.info("Parsed URI=[" + uri + "]: Parse elapsed: " + (time2 - time1) + " ms. Load elapsed: " + (time1 - time0) + " ms."); 128 this.showStyleSheet(styleSheet); 129 } catch(Exception err) { 130 logger.log(Level.SEVERE, "Error trying to load URI=[" + uri + "].", err); 131 this.clearCssOutput(); 132 } 133 } 134 135 private void clearCssOutput() { 136 this.cssOutput.clearDocument(); 137 } 138 139 private void showStyleSheet(CSSStyleSheet styleSheet) { 140 StringWriter stringWriter = new StringWriter(); 141 PrintWriter writer = new PrintWriter(stringWriter); 142 writer.println("<DL>"); 143 CSSRuleList ruleList = styleSheet.getCssRules(); 144 int length = ruleList.getLength(); 145 for(int i = 0; i < length; i++) { 146 CSSRule rule = ruleList.item(i); 147 writer.println("<DT><strong>Rule: type=" + rule.getType() + ",class=" + rule.getClass().getName() + "</strong></DT>"); 148 writer.println("<DD>"); 149 this.writeRuleInfo(writer, rule); 150 writer.println("</DD>"); 151 } 152 writer.println("</DL>"); 153 writer.flush(); 154 String html = stringWriter.toString(); 155 HtmlRendererContext rcontext = new SimpleHtmlRendererContext(this.cssOutput); 156 this.cssOutput.setHtml(html, "about:css", rcontext); 157 } 158 159 private void writeRuleInfo(PrintWriter writer, CSSRule rule) { 160 if(rule instanceof CSSStyleRule) { 161 CSSStyleRule styleRule = (CSSStyleRule) rule; 162 writer.println("Selector: " + styleRule.getSelectorText()); 163 writer.println("<br>"); 164 writer.println("CSS Text: " + styleRule.getCssText()); 165 } 166 else if(rule instanceof CSSImportRule) { 167 CSSImportRule styleRule = (CSSImportRule) rule; 168 writer.println("HREF: " + styleRule.getHref()); 169 writer.println("<br>"); 170 writer.println("CSS Text: " + styleRule.getCssText()); 171 } 172 } 173 174 public static void main(String [] args) { 175 CssParserTest frame = new CssParserTest(); 176 frame.setSize(800, 400); 177 frame.setExtendedState(TestFrame.MAXIMIZED_BOTH); 178 frame.setVisible(true); 179 } 180 } 181 | Popular Tags |