📄 rssfeedxmldatasource.java
字号:
/**
*
*/
package RssFeeds;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Set;
import java.util.List;
import java.util.ArrayList;
import java.util.TreeSet;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/**
* XML file data operation class
*
* @author Jack
*
*/
public class RssFeedXMLDataSource implements RssFeedDataSource {
private String inputFile;
private static Document document;
// top level element
private Element rss;
private Element rssAdd;
/**
* constructor to read the saved feed channels
*/
public RssFeedXMLDataSource() {
GetRssFeedFromFile("RssFeed.xml");
}
/**
* constructor of the class with xml file input
* using DOM parser to read xml file
* @param input
* xml file input
*/
public RssFeedXMLDataSource(String input) {
this();
inputFile = input;
DocumentBuilderFactory factory;
DocumentBuilder builder;
try {
factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
builder.setErrorHandler(new RssFeedErrorHandler());
if(new File(inputFile).exists()) {
InputSource inputSource = new InputSource(inputFile);
document = builder.parse(inputSource);
}
else {
DOMImplementation impl = builder.getDOMImplementation();
document = impl.createDocument(null, "rssAdd", null);
}
}catch(ParserConfigurationException e) {
e.printStackTrace();
}catch(SAXException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
rssAdd = document.getDocumentElement();
}
/**
* read xml file to the document using DOM parser
* @param input
* xml file input
*/
public void GetRssFeedFromFile(String input) {
document = null;
DocumentBuilderFactory savedFactory;
DocumentBuilder savedBuilder;
try {
savedFactory = DocumentBuilderFactory.newInstance();
savedBuilder = savedFactory.newDocumentBuilder();
savedBuilder.setErrorHandler(new RssFeedErrorHandler());
if(new File(input).exists()) {
InputSource savedInputSource = new InputSource(input);
document = savedBuilder.parse(savedInputSource);
}
else {
DOMImplementation savedImpl = savedBuilder.getDOMImplementation();
document = savedImpl.createDocument(null, "rss", null);
}
}catch(ParserConfigurationException e) {
e.printStackTrace();
}catch(SAXException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
rss = document.getDocumentElement();
}
/**
* obtain feed title from saved feed channels
* @return
* feed title
*/
public String getFeedTitle() {
String feedtitle;
NodeList feedNodeList = rss.getElementsByTagName("channelTitle");
// the first node title is the feed title
if( feedNodeList.getLength() < 1 )
feedtitle = "";
else
feedtitle = feedNodeList.item(0).getTextContent();
return feedtitle;
}
/**
* reading document to obtain channel title by tag of title
* @return
* channel title
*/
public String getChanTitle() {
String chanTitle;
NodeList feedNodeList = rssAdd.getElementsByTagName("title");
if( feedNodeList.getLength() < 1 )
chanTitle = "";
else
chanTitle = feedNodeList.item(0).getTextContent();
return chanTitle;
}
/**
* reading document to obtain channel number by tag of title
* @return
* channel number
*/
public int getChanNum() {
NodeList feedNodeList = rssAdd.getElementsByTagName("title");
int num = feedNodeList.getLength();
return num;
}
/**
* obtain all feed items and save them in the RssFeedItem object array list
* @return
* RssFeedItem array list
*/
public List<RssFeedItem> getItemOnebyOne() {
List<RssFeedItem> itemList = new ArrayList<RssFeedItem>();
NodeList firstList = rssAdd.getElementsByTagName("item");
for(int i = 0; i < firstList.getLength(); i++) {
RssFeedItem feedItem = new RssFeedItem();
Node firstNode = firstList.item(i);
NodeList secondList = firstNode.getChildNodes();
int secondLen = secondList.getLength();
for(int j=0; j<secondLen; j++) {
Node secondNode = secondList.item(j);
String tag = secondNode.getNodeName();
if(tag.equals("title")) {
feedItem.setItemTitle(secondNode.getTextContent());
}
else if(tag.equals("link")) {
feedItem.setItemlink(secondNode.getTextContent());
}
else if(tag.equals("pubDate")) {
feedItem.setItemPubDate(secondNode.getTextContent());
}
else if(tag.equals("description")) {
feedItem.setItemDescription(secondNode.getTextContent());
}
else
continue;
}
itemList.add(feedItem);
}
return itemList;
}
/**
* add a new feed
*
* @param f
* XML feed source
*/
public void addFeed(RssFeedXMLDataSource f) {
RssFeedItem feedItem = new RssFeedItem();
List<RssFeedItem> itemList;
f.GetRssFeedFromFile("RssFeed.xml");
// obtain all item list
itemList = getItemOnebyOne();
// second level element
Element channel = document.createElement("channel");
Element c_title = document.createElement("channelTitle");
c_title.appendChild(document.createTextNode(getChanTitle()));
channel.appendChild(c_title);
for(int i = 0; i < itemList.size(); i++ )
{
Element item = document.createElement("item");
// create item' subelements
Element title = document.createElement("title");
Element link = document.createElement("link");
Element description = document.createElement("description");
Element pubDate = document.createElement("pubDate");
feedItem = itemList.get(i);
// append text to elements
title.appendChild(document.createTextNode(feedItem.getItemTitle()));
link.appendChild(document.createTextNode(feedItem.getItemlink()));
description.appendChild(document.createTextNode(feedItem.getItemDescription()));
pubDate.appendChild(document.createTextNode(feedItem.getItemPubDate()));
// append elements to item
item.appendChild(title);
item.appendChild(link);
item.appendChild(description);
item.appendChild(pubDate);
// add two layer elements
rss.appendChild(channel).appendChild(item);
}
f.saveDocumentToFile();
}
/**
* delete feed name by the selected feed name
* @param f
* saved XML feed source
* @param feedsel
* the selected feed name
*/
public void deletFeedName(RssFeedXMLDataSource f, String feedsel) {
f.GetRssFeedFromFile("RssFeed.xml");
NodeList nodes = rss.getElementsByTagName("channel");
for(int i = 0; i < nodes.getLength(); i++) {
Node channel = nodes.item(i);
NodeList items = channel.getChildNodes();
for(int j = 0; j < items.getLength(); j++) {
Node item = items.item(j);
if(item.getNodeName().equals("channelTitle")) {
if(feedsel.equals(item.getTextContent())) {
rss.removeChild(channel);
}
else
break;
}
}
}
saveDocumentToFile();
}
/**
* delete feed item by the selected feed name and feed item name
* @param f
* the saved XML feed source
* @param feedname
* the selected feed title
* @param itemsel
* the selected feed item title
*/
public void deletFeedItem(RssFeedXMLDataSource f, String feedname, String itemsel) {
f.GetRssFeedFromFile("RssFeed.xml");
NodeList nodes = rss.getElementsByTagName("channel");
for(int i = 0; i < nodes.getLength(); i++) {
Node channel = nodes.item(i);
NodeList items = channel.getChildNodes();
for(int j = 0; j < items.getLength(); j++) {
Node item = items.item(j);
if(item.getNodeName().equals("channelTitle")) {
if(feedname.equals(item.getTextContent())) {
//obtain item object
for(int n = j+1; n < items.getLength(); n++) {
Node next = items.item(n);
NodeList nexts = next.getChildNodes();
Node last0 = nexts.item(0);
if(itemsel.equals(last0.getTextContent())) {
channel.removeChild(next);
}
}
}
else
break;
}
else
break;
}
}
saveDocumentToFile();
}
/**
* when window close save the document to file
* @see RssFeeds.RssFeedDataSource#close()
*/
@Override
public void close() {
GetRssFeedFromFile("RssFeed.xml");
// normalize is called to remove any blank spaces in the document
document.normalize();
saveDocumentToFile();
}
/**
* obtain all channel title from the saved xml file
* @see RssFeeds.RssFeedDataSource#feedSet()
*/
@Override
public Set<String> feedSet() {
Set<String> feeds = new TreeSet<String>();
NodeList feedNodeList = rss.getElementsByTagName("channelTitle");
for (int i = 0; i < feedNodeList.getLength(); i++) {
String feed = feedNodeList.item(i).getTextContent();
feeds.add(feed);
}
return feeds;
}
/**
* obtain all item title from the saved xml file and show them
* @see RssFeeds.RssFeedDataSource#itemSet()
*/
@Override
public Set<String> itemSet(String str) {
return searchAndShowItemList(str);
}
/**
* search and show item list by the feed title
* @param feedname
* the selected feed title
* @return
* a set of feed item title
*/
public Set<String> searchAndShowItemList(String feedname) {
Set <String>itemTitleSet = new TreeSet<String>();
NodeList nodes = rss.getElementsByTagName("channel");
for(int i = 0; i < nodes.getLength(); i++) {
Node channel = nodes.item(i);
NodeList items = channel.getChildNodes();
for(int j = 0; j < items.getLength(); j++) {
Node item = items.item(j);
if(item.getNodeName().equals("channelTitle")) {
if(feedname.equals(item.getTextContent())) {
// obtain item title
for(int n = j+1; n < items.getLength(); n++) {
Node next = items.item(n);
NodeList nexts = next.getChildNodes();
Node last = nexts.item(0);
String t = last.getTextContent();
itemTitleSet.add(t);
}
}
else
break;
}
else
break;
}
}
return itemTitleSet;
}
/**
* obtain one feed item from the saved xml file
* by selected feed title and the item title
* @param feedsel
* the selected feed title
* @param itemsel
* the selected item title
* @return
* the RssFeedItem object
*/
public RssFeedItem searchAndShowOneItem(String feedsel, String itemsel) {
RssFeedItem itemData = new RssFeedItem();
NodeList nodes = rss.getElementsByTagName("channel");
for(int i = 0; i < nodes.getLength(); i++) {
Node channel = nodes.item(i);
NodeList items = channel.getChildNodes();
for(int j = 0; j < items.getLength(); j++) {
Node item = items.item(j);
if(item.getNodeName().equals("channelTitle")) {
if(feedsel.equals(item.getTextContent())) {
// obtain item object
for(int n = j+1; n < items.getLength(); n++) {
Node next = items.item(n);
NodeList nexts = next.getChildNodes();
Node last0 = nexts.item(0);
if(itemsel.equals(last0.getTextContent())) {
Node last1 = nexts.item(1);
Node last2 = nexts.item(2);
Node last3 = nexts.item(3);
itemData.setItemTitle(last0.getTextContent());
itemData.setItemlink(last1.getTextContent());
itemData.setItemDescription(last2.getTextContent());
itemData.setItemPubDate(last3.getTextContent());
}
}
}
else
break;
}
else
break;
}
}
return itemData;
}
/**
* save the document to the xml when add new or delete feeds and items operation
*/
private void saveDocumentToFile() {
inputFile = "RssFeed.xml";
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer;
try {
transformer = factory.newTransformer();
Source source = new DOMSource(document);
FileOutputStream out = new FileOutputStream(inputFile);
Result output = new StreamResult(out);
transformer.transform(source, output);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -