📄 rssfeedgui.java
字号:
return addr.substring(lastSlashIndex + 1, lastQuestionIndex);
else
return null;
}
}
/**
* obtain file name from URL format input
* @param addr
* URL format string
* @return
* web file
*/
public String getFilenameFromURLFormat(String addr) {
int lastSlashIndex = addr.lastIndexOf('/');
if (lastSlashIndex >= 0 &&
lastSlashIndex < addr.length() - 1) {
return addr.substring(lastSlashIndex + 1);
}
else
return null;
}
/**
* Implements a ListSelectionListener for making the UI respond when a
* different feed is selected from the list.
*/
private class FeedListListener implements ListSelectionListener {
/**
* @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
*/
public void valueChanged(ListSelectionEvent e) {
if (feedNameList.getSelectedValue() != null
&& !feedNameList.getSelectedValue().equals("")) {
clearLinkPaneElements();
getItemList((String)feedNameList.getSelectedValue());
}
}
/**
* obtain feed item list by feed name
* @param feedname
* the selected feed name
*/
public void getItemList( String feedname ) {
RssFeedXMLDataSource getfeedXml = new RssFeedXMLDataSource();
data.get(getfeedXml, feedname);
}
}
/**
* Implements a ListSelectionListener for making the UI respond when a
* different item is selected from the list.
*/
private class ItemListListener implements ListSelectionListener {
/**
* @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
*/
public void valueChanged(ListSelectionEvent e) {
if (feedItemList.getSelectedValue() != null
&& !feedItemList.getSelectedValue().equals(""))
getOneItem((String)feedItemList.getSelectedValue());
}
/**
* obtain one item details by item name
* @param itemname
* the selected one item
*/
public void getOneItem( String itemname ) {
String feedname;
RssFeedItem feedItem;
feedname = (String)feedNameList.getSelectedValue();
RssFeedXMLDataSource getItemXml = new RssFeedXMLDataSource();
feedItem = data.getItem(getItemXml, feedname, itemname);
showItem( feedItem );
}
}
/**
* show the item title, link, pubDate, description and article of the item
* @param feedItem
* feed item object
*/
public void showItem(RssFeedItem feedItem) {
String file;
feedTitleTxt.setText(feedItem.getItemTitle());
feedLinkTxt.setText(feedItem.getItemlink());
feedDateTxt.setText(feedItem.getItemPubDate());
feedDescTxt.setText(feedItem.getItemDescription());
feedDescTxt.setLineWrap(true);
feedDescTxt.setWrapStyleWord(true);
file = getFilenameFromLinkField(feedLinkTxt.getText());
/**
* check the item is cached, if yes get the content from cache,or
* Start a thread to obtain item description form downloaded file
* and cache the content.
*/
if(saveArticleMap.containsKey(feedLinkTxt.getText()))
htmlLabel.setText(saveArticleMap.get(feedLinkTxt.getText()));
else {
htmlLabel.setText("");
new Thread(new RssFeedItemReader(feedLinkTxt.getText(), file)).start();
}
}
/**
* check the source string include the pattern string
* @param source
* the source string
* @param pattern
* the pattern string
* @return
* true/false
*/
public boolean patternMatch(String source, String pattern) {
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(source);
if(m.find())
return true;
else
return false;
}
/**
* Rss feed file download runnable class
*
* @author Jack
*
*/
public class RssFeedDownload implements Runnable {
private String addr;
/**
* @param addr
*/
public RssFeedDownload(String addr){
this.addr = addr;
}
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
String file;
String xml;
OutputStream out = null;
URLConnection conn = null;
InputStream in = null;
file = getFilenameFromURLFormat(addr);
if(file == null) {
JOptionPane.showMessageDialog(frameDialog,
"Input format error !\n( Example: http://feeds.news.com.au/rss/news.xml )",
"Add new feed",
JOptionPane.ERROR_MESSAGE);
return;
}
try {
URL url = new URL(addr);
out = new BufferedOutputStream(
new FileOutputStream(file));
conn = url.openConnection();
in = conn.getInputStream();
byte[] buffer = new byte[1024];
int numRead;
long numWritten = 0;
while ((numRead = in.read(buffer)) != -1) {
out.write(buffer, 0, numRead);
numWritten += numRead;
}
} catch (Exception exception) {
JOptionPane.showMessageDialog(frameDialog,
"NetWork connection error !",
"Add new feed",
JOptionPane.ERROR_MESSAGE);
exception.printStackTrace();
return;
}finally {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException ioe) {
}
}
xml = getFilenameFromURLFormat(addr);
if(xml != null) {
RssFeedXMLDataSource addfeedmxl = new RssFeedXMLDataSource(xml);
data.add(addfeedmxl);
}
else {
return;
}
//delete temporary file
File temFile = new File(xml);
if(temFile.exists())
temFile.delete();
feedNameTxt.setText("");
}
}
/**
* Rss feed item web page download and the article pick up runnable class
*
* @author Jack
*
*/
public class RssFeedItemReader implements Runnable {
private String urlPara;
private String localname;
public RssFeedItemReader() {
}
/**
* constructor for the class
* @param urlPara
* URL format string
* @param localname
* web file name
*/
public RssFeedItemReader(String urlPara, String localname) {
super();
this.urlPara = urlPara;
this.localname = localname;
}
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
String file;
OutputStream out = null;
URLConnection conn = null;
InputStream in = null;
file = getFilenameFromURLFormat(urlPara);
if(file == null) {
JOptionPane.showMessageDialog(null,
"Input format error !\n( Example: http://feeds.news.com.au/rss/news.xml )",
"Add new feed",
JOptionPane.ERROR_MESSAGE);
}
try {
URL url = new URL(urlPara);
out = new BufferedOutputStream(new FileOutputStream(localname));
conn = url.openConnection();
in = conn.getInputStream();
byte[] buffer = new byte[1024];
int numRead;
long numWritten = 0;
while ((numRead = in.read(buffer)) != -1) {
out.write(buffer, 0, numRead);
numWritten += numRead;
}
} catch (Exception exception) {
JOptionPane.showMessageDialog(null,
"NetWork connection error !",
"Add new feed",
JOptionPane.ERROR_MESSAGE);
exception.printStackTrace();
return;
}finally {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException ioe) {
}
}
readTheFilePageContent(localname);
}
/**
* pick up the related content from the web file
* @param file
* the downloaded web file
*/
public void readTheFilePageContent(String file) {
BufferedReader reader = null;
String patternPage = "<p";
String patternScriptStart = "<script";
String patternScriptEnd = "/script>";
String patternScript = "<script />";
String patternTableStart = "<table";
String patternTableEnd = "/table>";
String patternUlStart = "<ul";
String patternUlEnd = "/ul>";
String patternOlStart = "<ol";
String patternOlEnd = "/ol>";
String patternNSptStart = "<noscript";
String patternNSptEnd = "/noscript>";
String patternParam = "<param";
String patternImage = "<img";
String line = null;
StringBuffer strbf = new StringBuffer();
try{
reader = new BufferedReader(new FileReader(file));
line = reader.readLine();
strbf.append("<html>\n");
while(line != null) {
if(patternMatch(line, patternScript)) {
line = reader.readLine();
continue;
}// this case ignoring the 'script' lines
else if(patternMatch(line, patternScriptStart)) {
if(!patternMatch(line, patternScriptEnd)){
line = reader.readLine();
while(line != null) {
if(patternMatch(line, patternScriptEnd)){
break;
}
line = reader.readLine();
}
}
}// this case ignoring the 'table' tag lines
else if(patternMatch(line, patternTableStart)) {
if(!patternMatch(line, patternTableEnd)){
line = reader.readLine();
while(line != null) {
if(patternMatch(line, patternTableEnd)){
break;
}
line = reader.readLine();
}
}
}// this case ignoring the 'ul' tag lines
else if(patternMatch(line, patternUlStart)) {
if(!patternMatch(line, patternUlEnd)){
line = reader.readLine();
while(line != null) {
if(patternMatch(line, patternUlEnd)){
break;
}
line = reader.readLine();
}
}
}// this case ignoring the 'ol' tag lines
else if(patternMatch(line, patternOlStart)) {
if(!patternMatch(line, patternOlEnd)){
line = reader.readLine();
while(line != null) {
if(patternMatch(line, patternOlEnd)){
break;
}
line = reader.readLine();
}
}
}// this case ignoring the 'noscript' tag lines
else if(patternMatch(line, patternNSptStart)){
if(!patternMatch(line, patternNSptEnd)) {
line = reader.readLine();
while(line != null) {
if(patternMatch(line, patternNSptEnd)){
break;
}
line = reader.readLine();
}
}
}// this case ignoring the 'param' tag lines
else if(patternMatch(line, patternParam)){
line = reader.readLine();
continue;
}// this case obtaining the 'p' tag lines without image
else if(patternMatch(line, patternPage)) {
if(!patternMatch(line, patternImage))
strbf.append(line);
}// this case obtaining the page content but no tag signed lines
else if(isPageNoPageTag(line)){
strbf.append(line);
}
line = reader.readLine();
}
}catch(IOException ioe) {
ioe.printStackTrace();
}finally {
try {
if(reader != null)
reader.close();
}catch(IOException ioe) {
}
}
// delete the temp file
File temFile = new File(file);
if(temFile.exists()) {
temFile.delete();
}
// save the content to cache
saveArticleMap.put(urlPara, strbf.toString());
// show the content on the article field
if(feedTitleTxt.getText() == null || feedTitleTxt.getText().equals(""))
htmlLabel.setText("");
else
htmlLabel.setText(strbf.toString());
}
}
/**
* check the read line is page but no page tag
*
* @param str
* the read line from web file
* @return
* true/false
*/
public boolean isPageNoPageTag(String str) {
boolean isPage = false;
char c;
for(int i = 0; i < str.length(); i++) {
c = str.charAt(i);
if(c == '\t') {
continue;
}
else if(c == ' ') {
continue;
}
if(((c >= 'a')&&(c <= 'z'))||((c >= 'A')&&(c <= 'Z'))) {
isPage = true;
break;
}
else
break;
}
return isPage;
}
/**
* clear the item panel's related fields
*/
public void clearLinkPaneElements() {
feedTitleTxt.setText("");
feedLinkTxt.setText("");
feedDateTxt.setText("");
feedDescTxt.setText("");
htmlLabel.setText("");
}
/**
* Implements the windowClosing method from WindowAdapter/WindowListener to
* persist the contents of the data/model.
*/
private class ExitListener extends WindowAdapter {
/**
* @see java.awt.event.WindowAdapter#windowClosing(java.awt.event.WindowEvent)
*/
public void windowClosing(WindowEvent e) {
data.persist();
System.exit(0);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -