001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003: package fitnesse.responders;
004:
005: import fitnesse.*;
006: import fitnesse.components.XmlWriter;
007: import fitnesse.wiki.*;
008: import fitnesse.util.XmlUtil;
009: import fitnesse.http.*;
010: import org.w3c.dom.*;
011: import java.io.ByteArrayOutputStream;
012:
013: public class RssResponder implements Responder {
014: protected Element channelElement;
015:
016: private String resource;
017:
018: private WikiPage contextPage;
019:
020: public Response makeResponse(FitNesseContext context,
021: Request request) throws Exception {
022: Document rssDocument = buildRssHeader();
023:
024: resource = request.getResource();
025: contextPage = context.root.getPageCrawler().getPage(
026: context.root, PathParser.parse(resource));
027: XmlUtil.addTextNode(rssDocument, channelElement, "title",
028: "FitNesse:");
029: WikiPage page = context.root.getChildPage("RecentChanges");
030: buildItemReport(page, rssDocument, request.getResource());
031: SimpleResponse response = responseFrom(rssDocument);
032: return response;
033: }
034:
035: private void buildItemReport(WikiPage page, Document rssDocument,
036: String resource) throws Exception {
037: if (page != null) {
038: PageData data = page.getData();
039: String recentChanges = data.getContent();
040: String lines[] = recentChanges.split("\n");
041: for (int i = 0; i < lines.length; i++) {
042: String fields[] = lines[i].split("\\|");
043: String title = fields[1];
044: String author = fields[2];
045: String pubDate = fields[3];
046:
047: if (shouldReportItem(resource, title))
048: buildItem(rssDocument, title, author, pubDate);
049: }
050: }
051: }
052:
053: private boolean shouldReportItem(String resource, String title) {
054: return !exists(resource) || title.startsWith(resource);
055: }
056:
057: private void buildItem(Document rssDocument, String title,
058: String author, String pubDate) throws Exception {
059: Element itemElement1 = rssDocument.createElement("item");
060: XmlUtil.addTextNode(rssDocument, itemElement1, "title", title);
061: XmlUtil
062: .addTextNode(rssDocument, itemElement1, "author",
063: author);
064: XmlUtil.addTextNode(rssDocument, itemElement1, "pubDate",
065: pubDate);
066: buildLink(rssDocument, itemElement1, title);
067:
068: String description = makeDescription(author, pubDate);
069: XmlUtil.addTextNode(rssDocument, itemElement1, "description",
070: description);
071: Element itemElement = itemElement1;
072: channelElement.appendChild(itemElement);
073: }
074:
075: private void buildLink(Document rssDocument, Element itemElement1,
076: String pageName) throws Exception {
077: String prefix = "http://localhost/";
078: if (contextPage != null) {
079: PageData data = contextPage.getData();
080: String prefixVariable = data.getVariable("RSS_PREFIX");
081: prefix = prefixVariable == null ? prefix : prefixVariable;
082: }
083: String link = prefix + pageName;
084:
085: XmlUtil.addTextNode(rssDocument, itemElement1, "link", link);
086: }
087:
088: private String makeDescription(String author, String pubDate) {
089: String description;
090: String authoredBy = "";
091: if (exists(author))
092: authoredBy = author + ":";
093: description = authoredBy + pubDate;
094: return description;
095: }
096:
097: private boolean exists(String author) {
098: return author != null && author.length() > 0;
099: }
100:
101: private SimpleResponse responseFrom(Document rssDocument)
102: throws Exception {
103: byte[] bytes = toByteArray(rssDocument);
104: SimpleResponse response = new SimpleResponse();
105: response.setContent(bytes);
106: response.setContentType("text/xml");
107: return response;
108: }
109:
110: private byte[] toByteArray(Document rssDocument) throws Exception {
111: ByteArrayOutputStream os = new ByteArrayOutputStream();
112: XmlWriter writer = new XmlWriter(os);
113: writer.write(rssDocument);
114: writer.close();
115: byte[] bytes = os.toByteArray();
116: return bytes;
117: }
118:
119: private Document buildRssHeader() throws Exception {
120: Document rssDocument = XmlUtil.newDocument();
121: Element rssDocumentElement = rssDocument.createElement("rss");
122: rssDocument.appendChild(rssDocumentElement);
123: channelElement = rssDocument.createElement("channel");
124: rssDocumentElement.setAttribute("version", "2.0");
125: rssDocumentElement.appendChild(channelElement);
126: return rssDocument;
127: }
128:
129: }
|