001 package org.util.xml; 002 003 import java.awt.*; 004 import java.awt.event.*; 005 import java.awt.geom.*; 006 import java.io.*; 007 import java.net.*; 008 import javax.swing.*; 009 import javax.swing.event.*; 010 011 import org.util.xml.element.*; 012 import org.util.xml.parse.*; 013 import org.util.xml.parse.policy.*; 014 import org.util.xml.renderer.html.*; 015 016 import static org.gui.GUITool.alert; 017 import static org.gui.GUITool.showFrame; 018 019 /** 020 * 021 * @author masaru 022 */ 023 public class Main5 { 024 025 private JPanel panel_; 026 private JTextArea text_area_; 027 private String doctype_; 028 private JTextField text_; 029 030 private ParserPolicy keep_all_parser_; 031 private ParserPolicy body_tag_parser_; 032 private ParserPolicy html_tag_parser_; 033 private ParserPolicy html_document_parser_; 034 035 036 037 public static void main(String[] args) throws Exception { 038 039 final Main5 main = new Main5(); 040 main.start(); 041 main.openInOtherThread("http://sakura.meijo-u.ac.jp/index.html"); 042 } 043 044 public Main5() { 045 046 } 047 048 public void start() throws Exception { 049 //org.gui.GUITool.tryToSetSystemLookAndFeel(); 050 051 SwingUtilities.invokeAndWait(new Runnable(){public void run() { 052 text_ = new JTextField(); 053 text_.addActionListener(new ActionListener(){ 054 public void actionPerformed(ActionEvent e) { 055 String url_text = text_.getText(); 056 openInOtherThread(url_text); 057 } 058 }); 059 060 JPanel menu = new JPanel(); 061 menu.setLayout(new BoxLayout(menu, BoxLayout.X_AXIS)); 062 menu.setPreferredSize(new Dimension(500, 30)); 063 menu.add(text_); 064 065 JPanel contentpane = new JPanel(new BorderLayout()); 066 contentpane.setPreferredSize(new Dimension(500, 400)); 067 //text_area_ = new JTextArea(); 068 //contentpane.add(new JScrollPane(text_area_), BorderLayout.CENTER); 069 panel_ = new JPanel(); 070 panel_.setLayout(new BoxLayout(panel_, BoxLayout.Y_AXIS)); 071 //panel_.setPreferredSize(new Dimension(500, 400)); 072 contentpane.add(menu, BorderLayout.NORTH); 073 contentpane.add(new JScrollPane(panel_), BorderLayout.CENTER); 074 showFrame(contentpane, JFrame.EXIT_ON_CLOSE); 075 }}); 076 077 078 079 keep_all_parser_ = new HTMLParserPolicy() { 080 public ParserPolicy getInnerPolicy(Element element) { 081 return this; 082 } 083 public Element allowElement(Element element) { 084 return element; 085 } 086 }; 087 088 089 body_tag_parser_ = new HTMLParserPolicy() { 090 public ParserPolicy getInnerPolicy(Element element) { 091 TagElement tag = (TagElement)element; 092 /* 093 if(tag.getKey().toLowerCase().equals("html")) 094 return html_tag_parser; 095 */ 096 return this; 097 } 098 public Element allowElement(Element element) { 099 //panel_.add(new JButton(element.toString())); 100 if(!element.isTagElement()) { 101 JLabel label = new JLabel(((TextElement)element).getValue()); 102 label.setBackground(Color.green); 103 panel_.add(label); 104 }else{ 105 TagElement tag = (TagElement)element; 106 String key = tag.getKey().toLowerCase(); 107 if(key.equals("a")) { 108 109 }else{ 110 JLabel label = new JLabel("<html>"+tag.toString()+"</html>"); 111 label.setBorder(BorderFactory.createLineBorder(Color.blue)); 112 label.setToolTipText(tag.toString()); 113 panel_.add(label); 114 } 115 } 116 panel_.revalidate(); 117 return null; 118 } 119 }; 120 121 122 123 html_tag_parser_ = new HTMLParserPolicy() { 124 public ParserPolicy getInnerPolicy(Element element) { 125 if(!element.isTagElement()) return keep_all_parser_; 126 TagElement tag = ((TagElement)element); 127 if(tag.getKey().toLowerCase().equals("body")) 128 return body_tag_parser_; 129 return keep_all_parser_; 130 } 131 public Element allowElement(Element element) { 132 //panel_.add(new JButton(element.toString())); 133 return element; 134 } 135 }; 136 137 html_document_parser_ = new HTMLParserPolicy() { 138 public ParserPolicy getInnerPolicy(Element element) { 139 TagElement tag = (TagElement)element; 140 if(tag.getKey().toLowerCase().equals("html")) 141 return html_tag_parser_; 142 return keep_all_parser_; 143 } 144 public Element allowElement(Element element) { 145 if(element.isTextElement()) { 146 TextElement text_tag = (TextElement)element; 147 String text = text_tag.getValue(); 148 if(text.toLowerCase().startsWith("doctype")) { 149 doctype_ = text; 150 return null; 151 } 152 } 153 return null; 154 } 155 }; 156 157 158 } 159 160 public void openInOtherThread(final String url_text) { 161 new Thread(new Runnable(){public void run(){ 162 try{ 163 open(url_text); 164 }catch(Exception e){e.printStackTrace();} 165 }}, "open url").start(); 166 } 167 168 public void open(String url_text) throws Exception { 169 panel_.removeAll(); 170 171 URL url = new URL(url_text); 172 ElementParser parser = new ElementParser(url.openStream()); 173 parser.setDocumentBase(url.toURI()); 174 175 alert("now starting parse"); 176 177 parser.setPolicy(html_document_parser_); 178 parser.parse(); 179 180 panel_.revalidate(); 181 } 182 183 184 185 186 187 188 189 190 191 192 193 public void oldMain5() throws Exception { 194 String text = "<img src=\"http://www.google.com/intl/en_ALL/images/logo.gif\"/><div>aaabbbccc</div>"; 195 // ElementParser parser = new ElementParser(new StringReader(text)); 196 // ElementParser parser = new ElementParser(new URL("http://sakura.meijo-u.ac.jp/index.html").openStream()); 197 URL url = new URL("http://www.google.com/index.html"); 198 // URL url = new URL("http://video.google.com/?hl=ja&tab=wv"); 199 ElementParser parser = new ElementParser(url.openStream()); 200 parser.setDocumentBase(url.toURI()); 201 202 final HTMLObject htmlo = new HTMLObject(); 203 parser.setPolicy(htmlo); 204 parser.parse(); 205 206 final JComponent comp = new JComponent() { 207 public void paint(Graphics g) { 208 htmlo.paint(g); 209 } 210 }; 211 comp.addMouseListener(new MouseListener(){ 212 public void mousePressed(MouseEvent e) { 213 if(htmlo.hit(e.getX(), e.getY())) 214 htmlo.mousePressed(e); 215 } 216 public void mouseReleased(MouseEvent e) { 217 if(htmlo.hit(e.getX(), e.getY())) 218 htmlo.mouseReleased(e); 219 } 220 public void mouseClicked(MouseEvent e) { 221 if(htmlo.hit(e.getX(), e.getY())) 222 htmlo.mouseClicked(e); 223 } 224 public void mouseEntered(MouseEvent e) { 225 if(htmlo.hit(e.getX(), e.getY())) 226 htmlo.mouseEntered(e); 227 } 228 public void mouseExited(MouseEvent e) { 229 if(htmlo.hit(e.getX(), e.getY())) 230 htmlo.mouseExited(e); 231 } 232 }); 233 comp.addMouseMotionListener(new MouseMotionListener(){ 234 public void mouseMoved(MouseEvent e) { 235 if(htmlo.hit(e.getX(), e.getY())) { 236 htmlo.mouseMoved(e); 237 if(!htmlo.is_mouse_over_) { 238 htmlo.is_mouse_over_ = true; 239 htmlo.mouseEntered(e); 240 } 241 } else { 242 if(htmlo.is_mouse_over_) { 243 htmlo.is_mouse_over_ = false; 244 htmlo.mouseExited(e); 245 } 246 } 247 } 248 public void mouseDragged(MouseEvent e) { 249 if(htmlo.hit(e.getX(), e.getY())) 250 htmlo.mouseDragged(e); 251 } 252 }); 253 comp.addMouseWheelListener(new MouseWheelListener(){ 254 public void mouseWheelMoved(MouseWheelEvent e) { 255 htmlo.mouseWheelMoved(e); 256 } 257 }); 258 259 htmlo.addChangeListener(new ChangeListener(){ 260 public void stateChanged(ChangeEvent e) { 261 comp.repaint(); 262 } 263 }); 264 265 JPanel panel = new JPanel(new BorderLayout()); 266 panel.add(comp, BorderLayout.CENTER); 267 268 JFrame frame = new JFrame(); 269 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 270 frame.setContentPane(panel); 271 frame.pack(); 272 frame.setBounds(100,100, 500, 500); 273 frame.setVisible(true); 274 275 } 276 }