~sword-devel/jsword/trunk

« back to all changes in this revision

Viewing changes to jsword/java/historic/org/crosswire/net/WebPage.java

  • Committer: joe
  • Date: 2002-10-08 21:36:18 UTC
  • Revision ID: svn-v4:a88caf3b-7e0a-0410-8d0d-cecb45342206:trunk:80
big config and comment update

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
package org.crosswire.net;
3
 
 
4
 
import java.io.InputStreamReader;
5
 
import java.io.Reader;
6
 
import java.net.HttpURLConnection;
7
 
import java.net.URL;
8
 
import java.net.URLConnection;
9
 
import java.util.Enumeration;
10
 
import java.util.Vector;
11
 
 
12
 
// import java.a*t.*;
13
 
// import java.a*t.event.*;
14
 
// import javax.sw*ng.*;
15
 
// import javax.sw*ng.text.*;
16
 
// import javax.sw*ng.text.html.*;
17
 
// import sunw.hotjava.bean.*;
18
 
import org.crosswire.util.Logger;
19
 
import org.crosswire.util.StringUtil;
20
 
 
21
 
/**
22
 
* Represents a single web page.
23
 
* @author Joe Walker
24
 
*/
25
 
public class WebPage
26
 
{
27
 
    /**
28
 
    * Create a URLData object from a URL string
29
 
    * @param browser The browser that created us, from which we get headers
30
 
    * @param url_str The URL to visit
31
 
    */
32
 
    public WebPage(WebBrowser browser, String url_str)
33
 
    {
34
 
        try
35
 
        {
36
 
            this.url = new URL(url_str);
37
 
            this.browser = browser;
38
 
 
39
 
            // Create a connection
40
 
            URLConnection cnx = url.openConnection();
41
 
            cnx.setDoOutput(true);
42
 
            cnx.setUseCaches(false);
43
 
            cnx.setAllowUserInteraction(false);
44
 
 
45
 
            // Add custom headers
46
 
            for (Enumeration en=browser.headers(); en.hasMoreElements(); )
47
 
            {
48
 
                Header header = (Header) en.nextElement();
49
 
                cnx.setRequestProperty(header.getName(), header.getValue());
50
 
            }
51
 
 
52
 
            // They all should be http connections ...
53
 
            if (cnx instanceof HttpURLConnection)
54
 
            {
55
 
                HttpURLConnection hcnx = (HttpURLConnection) cnx;
56
 
 
57
 
                code = hcnx.getResponseCode();
58
 
 
59
 
                // Read the headers
60
 
                int i = 0;
61
 
                while (true)
62
 
                {
63
 
                    Header header = new Header(cnx.getHeaderFieldKey(i), cnx.getHeaderField(i));
64
 
                    if (!header.isValid()) break;
65
 
 
66
 
                    // Add new cookies to the browser
67
 
                    if (header.isCookie())
68
 
                    {
69
 
                        Cookie cookie = header.getCookie();
70
 
                        browser.addCookie(cookie);
71
 
                        cookies.addElement(cookie);
72
 
                    }
73
 
 
74
 
                    headers.addElement(header);
75
 
                    i++;
76
 
                }
77
 
            }
78
 
 
79
 
            // Read the actual page data
80
 
            Reader in = new InputStreamReader(cnx.getInputStream());
81
 
            data = StringUtil.read(in);
82
 
        }
83
 
        catch (Throwable ex)
84
 
        {
85
 
            ex.printStackTrace();
86
 
            if (ex instanceof ThreadDeath) throw (ThreadDeath) ex;
87
 
            data = null;
88
 
        }
89
 
    }
90
 
 
91
 
    /**
92
 
    * Check that the given string exists in the data
93
 
    */
94
 
    public boolean contains(String test)
95
 
    {
96
 
        return data.indexOf(test) != -1;
97
 
    }
98
 
 
99
 
    /**
100
 
    * The page headers
101
 
    */
102
 
    public void printHeaders()
103
 
    {
104
 
        for (Enumeration en=headers.elements(); en.hasMoreElements(); )
105
 
        {
106
 
            System.out.println(""+en.nextElement());
107
 
        }
108
 
    }
109
 
 
110
 
    /**
111
 
    * The page data
112
 
    */
113
 
    public void printContent()
114
 
    {
115
 
        System.out.println(data);
116
 
    }
117
 
 
118
 
    /*
119
 
    * The page data
120
 
    *
121
 
    public void displayContent()
122
 
    {
123
 
        try
124
 
        {
125
 
            if (mode == 0)
126
 
            {
127
 
                HTMLDocument html = new HTMLDocument();
128
 
                HTMLEditorKit editor = new HTMLEditorKit();
129
 
 
130
 
                try
131
 
                {
132
 
                    // Shove the page data into a stream
133
 
                    // And the read it into a Document
134
 
                    editor.read(new StringReader(data), html, 0);
135
 
                }
136
 
                catch (Throwable ex)
137
 
                {
138
 
                    if (ex instanceof ThreadDeath) throw (ThreadDeath) ex;
139
 
                    // The HTML component regularly throws up without any good reason
140
 
                    // ignore him and he will go away.
141
 
                }
142
 
 
143
 
                // Create the GUI components
144
 
                JFrame frame = new JFrame(url.toString());
145
 
                JTextPane text = new JTextPane(html);
146
 
                JScrollPane scroll = new JScrollPane(text);
147
 
 
148
 
                // Display them
149
 
                scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
150
 
                scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
151
 
 
152
 
                frame.getContentPane().setLayout(new BorderLayout());
153
 
                frame.getContentPane().add("Center", scroll);
154
 
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
155
 
 
156
 
                try
157
 
                {
158
 
                    frame.pack();
159
 
                }
160
 
                catch (Throwable ex)
161
 
                {
162
 
                    if (ex instanceof ThreadDeath) throw (ThreadDeath) ex;
163
 
                    // The HTML component regularly throws up without any good reason
164
 
                    // ignore him and he will go away.
165
 
                }
166
 
 
167
 
                frame.setVisible(true);
168
 
            }
169
 
            else
170
 
            {
171
 
                /*
172
 
                HotJavaBrowserBean hjb = new HotJavaBrowserBean();
173
 
                StringReader sin = new StringReader(data);
174
 
                hjb.setDocumentSource(sin);
175
 
 
176
 
                final Frame frame = new Frame(url.toString());
177
 
 
178
 
                // Display them
179
 
                frame.setLayout(new BorderLayout());
180
 
                frame.add("Center", hjb);
181
 
 
182
 
                frame.addWindowListener(new WindowAdapter() {
183
 
                    public void windowClosing(WindowEvent ev)
184
 
                    {
185
 
                        frame.setVisible(false);
186
 
                        frame.dispose();
187
 
                    }
188
 
                });
189
 
 
190
 
                //frame.pack();
191
 
                frame.setVisible(true);
192
 
                *
193
 
            }
194
 
        }
195
 
        catch (Throwable ex)
196
 
        {
197
 
            if (ex instanceof ThreadDeath) throw (ThreadDeath) ex;
198
 
            // System.out.println(Level.INFO, "Failure", ex);
199
 
        }
200
 
    }
201
 
    */
202
 
 
203
 
    /**
204
 
    * The page headers
205
 
    */
206
 
    public void printNewCookies()
207
 
    {
208
 
        for (Enumeration en=cookies.elements(); en.hasMoreElements(); )
209
 
        {
210
 
            System.out.println(en.nextElement().toString());
211
 
        }
212
 
    }
213
 
 
214
 
    /**
215
 
    * Works out the reply number 404 or something
216
 
    * @return The reply status
217
 
    */
218
 
    public int getStatus()
219
 
    {
220
 
        return code;
221
 
    }
222
 
 
223
 
    /**
224
 
    * Gets the page contents
225
 
    * @return The page contents
226
 
    */
227
 
    public String getPageContents()
228
 
    {
229
 
        return data;
230
 
    }
231
 
 
232
 
    /** How are the pages displayed */
233
 
    public static int mode = 1;
234
 
 
235
 
    /** The page headers */
236
 
    private Vector headers = new Vector();
237
 
 
238
 
    /** The new cookies delivered with this page */
239
 
    private Vector cookies = new Vector();
240
 
 
241
 
    /** The page source */
242
 
    private String data;
243
 
 
244
 
    /** The response code */
245
 
    private int code = -1;
246
 
 
247
 
    /** The URL we went after in the first place */
248
 
    private URL url;
249
 
 
250
 
    /** The Web browser we were grabbed from */
251
 
    private WebBrowser browser;
252
 
 
253
 
    /** The log stream */
254
 
    protected static Logger log = Logger.getLogger(WebPage.class);
255
 
}