001 /*
002 * To change this template, choose Tools | Templates
003 * and open the template in the editor.
004 */
005
006 package org.util.xml;
007
008 import java.awt.BorderLayout;
009 import java.awt.Desktop;
010 import java.awt.FlowLayout;
011 import java.awt.event.ActionEvent;
012 import java.awt.event.ActionListener;
013 import java.net.MalformedURLException;
014 import java.net.URI;
015 import java.net.URL;
016 import javax.swing.AbstractAction;
017 import javax.swing.BorderFactory;
018 import javax.swing.JButton;
019 import javax.swing.JFrame;
020 import javax.swing.JOptionPane;
021 import javax.swing.JPanel;
022 import javax.swing.JTextField;
023 import org.util.xml.element.Element;
024 import org.util.xml.element.TagElement;
025 import org.util.xml.element.TextElement;
026 import org.util.xml.parse.policy.HTMLParserPolicy;
027 import org.util.xml.parse.ElementParser;
028
029 /**
030 *
031 * @author masaru
032 */
033 public class Main2 {
034
035 JPanel panel;
036
037 public static void main(String[] args) {
038 new Main2();
039 }
040
041 public Main2() {
042 JTextField textfield = new JTextField("http://www.google.com/");
043 textfield.addActionListener(new ActionListener(){
044 public void actionPerformed(final ActionEvent e) {
045 new Thread(new Runnable(){public void run(){
046 openURL(((JTextField)e.getSource()).getText());
047 }}).start();
048 }
049 });
050 panel = new JPanel(new FlowLayout());
051
052 JPanel contentpane = new JPanel(new BorderLayout());
053 contentpane.add(textfield,BorderLayout.NORTH);
054 contentpane.add(panel,BorderLayout.CENTER);
055 contentpane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
056
057 JFrame frame = new JFrame();
058 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
059 frame.setContentPane(contentpane);
060 frame.setSize(200,200);
061 frame.setLocation(100,100);
062 frame.setVisible(true);
063 }
064 public void openURL(String urltext) {
065 try{
066 URL url = new URL(urltext);
067 ElementParser parser = new ElementParser(url.openStream());
068 parser.setPolicy(new HTMLParserPolicy(){
069
070 @Override
071 public Element allowElement(Element element) {
072 super.allowElement(element);
073
074 if(element.isTextElement()) {
075
076 TextElement text = (TextElement)element;
077 if(true) {
078 String doc = text.getValue();
079 //JOptionPane.showMessageDialog(null, doc);
080 int num = doc.indexOf("embedUrl");
081 if(num != -1) {
082 int start = doc.indexOf("\'", num) + 1;
083 int end = doc.indexOf("\'", start);
084 String value = doc.substring(start, end);
085 //JOptionPane.showMessageDialog(null, value);
086 final String url = value;
087 String comment = "";
088 JButton button = new JButton(new AbstractAction(url){
089 public void actionPerformed(ActionEvent arg0) {
090 try{
091 Desktop.getDesktop().browse(new URI(url));
092 }catch(Exception e){
093 JOptionPane.showMessageDialog(null, "Error:"+e.getMessage()+"\n"+url);
094 }
095 }
096 });
097 button.setToolTipText("<html><body>"+comment+"</body></html>");
098 panel.add(button);
099 panel.doLayout();
100 }
101 }
102 }
103
104 return null;
105 }
106
107 });
108 parser.parse();
109 } catch(Exception e) {
110 e.printStackTrace();
111 JOptionPane.showMessageDialog(null, e);
112 }
113 }
114 }