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 import org.util.html.objects.*;
016
017 import static org.gui.GUITool.alert;
018 import static org.gui.GUITool.showFrame;
019
020 /**
021 *
022 * @author masaru
023 */
024 public class Main6 {
025
026 private JPanel panel_;
027 private JTextArea text_area_;
028 private String doctype_;
029 private JTextField text_;
030
031 private ParserPolicy keep_all_parser_;
032 private ParserPolicy body_tag_parser_;
033 private ParserPolicy html_tag_parser_;
034 private ParserPolicy html_document_parser_;
035
036
037
038 public static void main(String[] args) throws Exception {
039
040 final Main6 main = new Main6();
041 main.start();
042 // main.openInOtherThread("http://sakura.meijo-u.ac.jp/index.html");
043 //main.openInOtherThread("http://sakura.meijo-u.ac.jp/~tatsunootoshigo2008/");
044 }
045
046 public Main6() {
047
048 }
049
050 public void start() throws Exception {
051 //org.gui.GUITool.tryToSetSystemLookAndFeel();
052
053 SwingUtilities.invokeAndWait(new Runnable(){public void run() {
054 text_ = new JTextField();
055 text_.addActionListener(new ActionListener(){
056 public void actionPerformed(ActionEvent e) {
057 String url_text = text_.getText();
058 openInOtherThread(url_text);
059 }
060 });
061
062 JPanel menu = new JPanel();
063 menu.setLayout(new BoxLayout(menu, BoxLayout.X_AXIS));
064 menu.setPreferredSize(new Dimension(500, 30));
065 menu.add(text_);
066
067 JPanel contentpane = new JPanel(new BorderLayout());
068 contentpane.setPreferredSize(new Dimension(500, 400));
069 //text_area_ = new JTextArea();
070 //contentpane.add(new JScrollPane(text_area_), BorderLayout.CENTER);
071 panel_ = new JPanel();
072 panel_.setLayout(new BoxLayout(panel_, BoxLayout.Y_AXIS));
073 //panel_.setPreferredSize(new Dimension(500, 400));
074 contentpane.add(menu, BorderLayout.NORTH);
075 contentpane.add(new JScrollPane(panel_), BorderLayout.CENTER);
076 showFrame(contentpane, JFrame.EXIT_ON_CLOSE);
077 }});
078
079
080
081 keep_all_parser_ = new HTMLParserPolicy() {
082 @Override
083 public boolean throwExceptionIfDocumentHasError() {
084 return false;
085 }
086 public ParserPolicy getInnerPolicy(Element element) {
087 return this;
088 }
089 public Element allowElement(Element element) {
090 return element;
091 }
092 };
093
094
095 body_tag_parser_ = new HTMLParserPolicy() {
096 @Override
097 public boolean throwExceptionIfDocumentHasError() {
098 return false;
099 }
100 public ParserPolicy getInnerPolicy(Element element) {
101 TagElement tag = (TagElement)element;
102 /*
103 if(tag.getKey().toLowerCase().equals("html"))
104 return html_tag_parser;
105 */
106 return this;
107 }
108 public Element allowElement(Element element) {
109 //panel_.add(new JButton(element.toString()));
110 if(!element.isTagElement()) {
111 HTMLText text = new HTMLText(root_);
112 text.setText(((TextElement)element).getValue());
113 panel_.add(text.getPanel());
114 }else{
115 TagElement tag = (TagElement)element;
116 String key = tag.getKey().toLowerCase();
117 if(key.equals("a")) {
118
119 }else if(key.equals("img")) {
120 try{
121 HTMLImg img = new HTMLImg(root_);
122 img.setURL(root_.getDocumentBase().toURI().resolve(tag.getAttributeValue("src", "")).toURL());
123 panel_.add(img.getPanel());
124 }catch(Exception e) {e.printStackTrace();}
125 }else{
126 JLabel label = new JLabel("<html>"+tag.toString()+"</html>");
127 label.setBorder(BorderFactory.createLineBorder(Color.blue));
128 label.setToolTipText(tag.toString());
129 panel_.add(label);
130 }
131 }
132 panel_.revalidate();
133 return null;
134 }
135 };
136
137
138
139 html_tag_parser_ = new HTMLParserPolicy() {
140 @Override
141 public boolean throwExceptionIfDocumentHasError() {
142 return false;
143 }
144 public ParserPolicy getInnerPolicy(Element element) {
145 if(!element.isTagElement()) return keep_all_parser_;
146 TagElement tag = ((TagElement)element);
147 if(tag.getKey().toLowerCase().equals("body"))
148 return body_tag_parser_;
149 return keep_all_parser_;
150 }
151 public Element allowElement(Element element) {
152 //panel_.add(new JButton(element.toString()));
153 return element;
154 }
155 };
156
157 html_document_parser_ = new HTMLParserPolicy() {
158 @Override
159 public boolean throwExceptionIfDocumentHasError() {
160 return false;
161 }
162 public String selectEncoding(String last_tag_key) {
163 return "utf-8";
164 }
165 public ParserPolicy getInnerPolicy(Element element) {
166 TagElement tag = (TagElement)element;
167 if(tag.getKey().toLowerCase().equals("html"))
168 return html_tag_parser_;
169 return keep_all_parser_;
170 }
171 public Element allowElement(Element element) {
172 if(element.isTextElement()) {
173 TextElement text_tag = (TextElement)element;
174 String text = text_tag.getValue();
175 if(text.toLowerCase().startsWith("doctype")) {
176 doctype_ = text;
177 return null;
178 }
179 }
180 return null;
181 }
182 };
183
184
185 }
186
187 public void openInOtherThread(final String url_text) {
188 new Thread(new Runnable(){public void run(){
189 try{
190 open(url_text);
191 }catch(Exception e){e.printStackTrace();}
192 }}, "open url").start();
193 }
194
195 HTMLDocument root_ = new HTMLDocument();
196
197 public void open(String url_text) throws Exception {
198 panel_.removeAll();
199
200 URL url = new URL(url_text);
201 root_.setDocumentBase(url);
202
203 ElementParser parser;
204 /*
205 parser= new ElementParser(url.openStream());
206 Element[] all = parser.parse();
207 for(Element a : all)
208 panel_.add(new JLabel(a.toString()));
209 panel_.revalidate();
210
211 alert("now starting parse");
212 */
213 parser = new ElementParser(url.openStream());
214 parser.setDocumentBase(url.toURI());
215
216 parser.setPolicy(html_document_parser_);
217 parser.parse();
218
219 panel_.revalidate();
220 }
221
222
223
224
225
226
227
228
229 }