View Javadoc

1   package com.sun.syndication.samples;
2   
3   import com.sun.syndication.feed.synd.SyndFeed;
4   import com.sun.syndication.io.SyndFeedInput;
5   
6   import java.applet.Applet;
7   import java.awt.*;
8   import java.awt.event.ActionEvent;
9   import java.awt.event.ActionListener;
10  import java.io.InputStreamReader;
11  import java.net.MalformedURLException;
12  import java.net.URL;
13  
14  /*
15   * Created on Sep 5, 2004
16   * Updated on Sep 13 to remove inner classes
17   *
18   * This applet accepts a local file name or a URL, fetches the XML feed and
19   * displays it using SyndFeed.toString()
20   *
21   * The applet adheres to the security policy of the appletviewer
22   */
23  /***
24   * @author Ken Kast
25   *
26   */
27  public class RomeApplet extends Applet implements ActionListener {
28  
29  	private static final int fieldWidth = 80;
30  	private URL home = null;
31  	private Label feedLabel;
32  	private TextArea feedText;
33  	private Button fetchButton;
34  	private Checkbox fileButton;
35  	private CheckboxGroup fileURLButtons;
36  	private TextField urlField;
37  	private Checkbox urlButton;
38  	private GridBagLayout layout = new GridBagLayout();
39  	private GridBagConstraints constraints = new GridBagConstraints();
40  
41  	public void init() {
42  
43  		//Determine where applet lives
44  		//Used as base directory for fetching files
45  		home = getCodeBase();
46  
47  		//Layout GUI.
48  		//It consists of
49  		//	two radio buttons to choose URL or file as input
50  		//	a text field to enter file name or URL
51  		//	a text box to display the feed
52  		//	a button to fetch the feed
53  		setSize(10 * fieldWidth, 450);
54  		setBackground(Color.lightGray);
55  		setLayout(layout);
56  		fileURLButtons = new CheckboxGroup();
57  		urlButton = new Checkbox("URL", fileURLButtons, true);
58  		add(urlButton);
59  		fileButton = new Checkbox("File", fileURLButtons, false);
60  		add(fileButton);
61  		urlField = new TextField("", fieldWidth);
62  		urlField.setEditable(true);
63  		add(urlField);
64  		feedLabel = new Label("Feed");
65  		add(feedLabel);
66  		feedText = new TextArea(20, fieldWidth);
67  		add(feedText);
68  		fetchButton = new Button("Fetch");
69  		fetchButton.addActionListener(this); //Event handler
70  		add(fetchButton);
71  		//Place widgets in window
72  		locateWidget(urlButton, 0, 0, 1, 1, 0, 0, GridBagConstraints.CENTER);
73  		locateWidget(fileButton, 0, 1, 1, 1, 2, 0, GridBagConstraints.WEST);
74  		locateWidget(urlField, 1, 0, 1, 2, 2, 0, GridBagConstraints.WEST);
75  		locateWidget(feedLabel, 0, 2, 1, 1, 10, 10,
76  				GridBagConstraints.NORTHEAST);
77  		locateWidget(feedText, 1, 2, 1, 1, 10, 10, GridBagConstraints.NORTHWEST);
78  		locateWidget(fetchButton, 1, 3, 1, 1, 10, 10, GridBagConstraints.CENTER);
79  		validate();
80  		urlField.requestFocus();
81  	}
82  
83  	private void locateWidget(Component widget, int gridx, int gridy,
84  			int gridwidth, int gridheight, int top, int bottom, int anchor) {
85  		constraints.gridx = gridx;
86  		constraints.gridy = gridy;
87  		constraints.gridwidth = gridwidth;
88  		constraints.gridheight = gridheight;
89  		constraints.insets.top = top;
90  		constraints.insets.bottom = bottom;
91  		constraints.anchor = anchor;
92  		layout.setConstraints(widget, constraints);
93  	}
94  
95  	/*
96  	 * Event handler for "Fetch" button
97  	 */
98  	public void actionPerformed(ActionEvent e) {
99  		Checkbox selChkbx = fileURLButtons.getSelectedCheckbox();
100 		URL url;
101 		if (e.getSource() == fetchButton) {
102 			boolean isSourceURL = (selChkbx == urlButton);
103 			try { // Crash if there is a problem with the URL
104 				if (isSourceURL)
105 					url = new URL(urlField.getText().toString());
106 				else
107 					url = new URL(home, urlField.getText().toString());
108 				feedText.setText(getRSSFeed(url).toString());
109 			}
110 			catch (MalformedURLException ex) {
111 				System.err.println("Malformed URL " + ex.getMessage());
112 			}
113 			catch (Exception ex) {
114 				ex.printStackTrace();
115 				System.err.println("ERROR: " + ex.getMessage());
116 			}
117 		}
118 	}
119 
120 	private SyndFeed getRSSFeed(URL url) throws Exception {
121 		SyndFeed feed;
122 		SyndFeedInput input = new SyndFeedInput();
123 		feed = input.build(new InputStreamReader(url.openStream()));
124 		return feed;
125 	}
126 }