Skip to content

Instantly share code, notes, and snippets.

@beppu
Created June 9, 2010 13:30
Show Gist options
  • Select an option

  • Save beppu/431467 to your computer and use it in GitHub Desktop.

Select an option

Save beppu/431467 to your computer and use it in GitHub Desktop.
/**
*
*/
package com.nugporn.xxx;
import java.io.IOException;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
* @author beppu
*
*/
public class Feed {
private ArrayList<Item> items;
/**
* @return
*/
public ArrayList<Item> getItems() {
return items;
}
/**
* @param item
*/
public void addItem(Item item) {
this.items.add(item);
}
/**
* @param doc
* @throws XPathExpressionException
*/
Feed(Document doc) throws XPathExpressionException {
this.items = new ArrayList<Item>();
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression xx = xpath.compile("//channel/item");
NodeList nodes = (NodeList) xx.evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++) {
Item item = new Item(nodes.item(i));
this.addItem(item);
}
}
/**
* @param filename
* @return
* @throws ParserConfigurationException
* @throws SAXException
* @throws IOException
* @throws XPathExpressionException
*/
public static Feed loadFromFile(String filename) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
// load document into doc
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse(filename);
Feed f = new Feed(doc);
return f;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
StringBuffer b = new StringBuffer();
String __ = " ";
for (Item i: items) {
b.append(i.getTitle() + "\n");
b.append(__ + i.getPubDate().toString() + "\n");
b.append(__ + i.getLink().toString() + "\n");
b.append(__ + i.getComments().toString() + "\n");
b.append(__ + i.getCreator() + "\n");
b.append(__ + i.getCategories().toString() + "\n");
b.append("\n");
}
return b.toString();
}
}
package com.nugporn.xxx;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* @author beppu
*
*/
public class Item {
// item attributes from the feed
private String title;
private URL link;
private URL comments;
private String pubDate;
private String creator;
private String description;
private String content;
private ArrayList<String> categories;
// objects for internal use
private Node itemNode;
private XPath xpath;
/**
* @param node
* @throws XPathExpressionException
*/
public Item(Node node) throws XPathExpressionException {
this.itemNode = node;
this.xpath = XPathFactory.newInstance().newXPath();
// http://www.ibm.com/developerworks/xml/library/x-nmspccontext/index.html?ca=drs-
// It's so unfortunately retarded that I have to do this.
this.xpath.setNamespaceContext(new NamespaceContext() {
public String getNamespaceURI(String prefix) {
if (prefix == null) {
throw new IllegalArgumentException("No prefix provided");
} else if ("dc".equals(prefix)) {
return "http://purl.org/dc/elements/1.1/";
} else {
return XMLConstants.XML_NS_URI;
}
}
@Override
public String getPrefix(String namespaceURI) {
return null;
}
@Override
public Iterator<String> getPrefixes(String namespaceURI) {
return null;
}
});
try {
this.link = new URL(x("link"));
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
this.comments = new URL(x("comments"));
} catch (MalformedURLException e) {
e.printStackTrace();
}
this.title = x("title");
this.pubDate = x("pubDate");
this.creator = x("dc:creator");
this.description = x("description");
this.content = x("content:encoded");
this.categories = xx("category");
}
/**
* @return the title
*/
public String getTitle() {
return title;
}
/**
* @return the link
*/
public URL getLink() {
return link;
}
/**
* @return the comments
*/
public URL getComments() {
return comments;
}
/**
* @return the pubDate
*/
public String getPubDate() {
return pubDate;
}
/**
* @return the creator
*/
public String getCreator() {
return creator;
}
/**
* @return the description
*/
public String getDescription() {
return description;
}
/**
* @return the content
*/
public String getContent() {
return content;
}
/**
* @return the categories
*/
public ArrayList<String> getCategories() {
return categories;
}
/**
* @param path
* @return
* @throws XPathExpressionException
*/
private String x(String path) throws XPathExpressionException {
XPathExpression expr = this.xpath.compile(path);
NodeList nodes = (NodeList) expr.evaluate(this.itemNode, XPathConstants.NODE);
if (nodes == null) {
return "";
}
Node n1 = nodes.item(0);
if (n1 != null) {
return n1.getNodeValue();
} else {
return "";
}
}
/**
* @param path
* @return
* @throws XPathExpressionException
*/
private ArrayList<String> xx(String path) throws XPathExpressionException {
// System.out.println(path);
ArrayList<String> strings = new ArrayList<String>();
XPathExpression expr = this.xpath.compile(path);
NodeList nodes = (NodeList) expr.evaluate(this.itemNode, XPathConstants.NODESET);
if (nodes == null) {
return strings;
}
for (int i = 0; i < nodes.getLength(); i++) {
strings.add(nodes.item(i).getTextContent());
}
return strings;
}
}
/**
*
*/
package com.nugporn.xxx;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;
import org.xml.sax.SAXException;
/**
* @author beppu
*
*/
public class NugFeed {
/**
*
*/
public NugFeed() {
}
/**
* @param args
*/
public static void main(String[] args) {
Feed f = null;
if (args.length == 0) {
System.out.println("Usage: nugporn <FILE>\n");
}
try {
f = Feed.loadFromFile(args[0]);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XPathExpressionException e) {
e.printStackTrace();
}
if (f != null) {
System.out.println(f.toString());
} else {
System.out.println("Could not load feed: " + args[0] + "\n");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment