1 37 package net.sourceforge.cruisecontrol.publishers.rss; 38 39 import java.io.BufferedWriter ; 40 import java.io.File ; 41 import java.io.IOException ; 42 import java.io.Writer ; 43 44 import java.util.ArrayList ; 45 import java.util.Collections ; 46 import java.util.List ; 47 48 import org.jdom.Document; 49 import org.jdom.Element; 50 import org.jdom.JDOMException; 51 import org.jdom.input.SAXBuilder; 52 53 import org.apache.log4j.Logger; 54 55 62 public class Feed { 63 64 private static final Logger LOG = Logger.getLogger(Feed.class); 65 66 private String channelTitle; 67 private String channelLink; 68 private String channelDescription; 69 private String channelLanguage = "en-US"; 70 71 private int maxLength = 20; 72 private final List items = new ArrayList (); 73 74 79 public Feed(File publishToFile) { 80 81 if (publishToFile.exists() && publishToFile.canRead()) { 83 try { 84 85 SAXBuilder builder = new SAXBuilder(); 86 Document doc = builder.build(publishToFile); 87 88 if (doc.getRootElement() != null 89 && doc.getRootElement().getChild(RSS.NODE_CHANNEL) != null) { 90 91 Element channelElement = doc.getRootElement().getChild(RSS.NODE_CHANNEL); 92 if (channelElement.getChild(RSS.NODE_CHANNEL_TITLE) != null) { 93 this.channelTitle = 94 channelElement.getChild(RSS.NODE_CHANNEL_TITLE).getText().trim(); 95 } 96 if (channelElement.getChild(RSS.NODE_CHANNEL_LINK) != null) { 97 this.channelLink = 98 channelElement.getChild(RSS.NODE_CHANNEL_LINK).getText().trim(); 99 } 100 if (channelElement.getChild(RSS.NODE_CHANNEL_DESCRIPTION) != null) { 101 this.channelDescription = 102 channelElement.getChild(RSS.NODE_CHANNEL_DESCRIPTION).getText().trim(); 103 } 104 105 if (channelElement.getChildren(RSS.NODE_ITEM) != null) { 106 List itemNodes = channelElement.getChildren(RSS.NODE_ITEM); 107 for (int i = 0; i < itemNodes.size(); i++) { 108 this.items.add(new Item((Element) itemNodes.get(i))); 109 } 110 } 111 } else { 112 LOG.info("existing RSS file doesn't appear to be valid. Missnig root element or channel node."); 113 } 114 115 Collections.sort(this.items); 117 while (this.items.size() > this.maxLength) { 118 this.items.remove(this.items.size() - 1); 119 } 120 } catch (JDOMException jex) { 121 LOG.error("jdom exception while parsing existing RSS file " + publishToFile.getPath() 122 + "; deleting file and starting over...", jex); 123 publishToFile.delete(); 124 } catch (IOException ioe) { 125 LOG.error("IOException while reading existing RSS file " + publishToFile.getPath() 126 + "; deleting file and starting over...", ioe); 127 publishToFile.delete(); 128 } 129 } else { 130 LOG.info("Unable to locate or read the existing RSS feed file."); 131 } 132 } 133 134 135 136 142 public void setTitle(String title) { 143 this.channelTitle = title; 144 } 145 146 152 public String getTitle() { 153 return this.channelTitle; 154 } 155 public void setLink(String link) { 156 this.channelLink = link; 157 } 158 public String getLink() { 159 return this.channelLink; 160 } 161 public void setDescription(String description) { 162 this.channelDescription = description; 163 } 164 public String getDescription() { 165 return this.channelDescription; 166 } 167 168 public void setMaxLength(int max) { 169 this.maxLength = max; 170 } 171 public int getMaxLength() { 172 return this.maxLength; 173 } 174 175 public void addItem(Item item) { 176 synchronized (this.items) { 177 if (this.items.size() == this.maxLength) { 178 this.items.remove(this.items.size() - 1); 179 } 180 this.items.add(0, item); 181 } 182 } 183 184 public List getItems() { 185 return this.items; 186 } 187 188 public void write(Writer wr) throws IOException { 189 190 BufferedWriter br = new BufferedWriter (wr); 191 br.write("<?xml version=\"1.0\" ?>\n"); 192 br.write("<rss version=\"2.0\">\n"); 193 br.write(" <channel>\n"); 194 br.write(" <title>"); 195 if (this.getTitle() != null) { 196 br.write(this.getTitle()); 197 } 198 br.write("</title>\n"); 199 br.write(" <link>"); 200 if (this.getLink() != null) { 201 br.write(this.getLink()); 202 } 203 br.write("</link>\n"); 204 br.write(" <description>"); 205 if (this.getDescription() != null) { 206 br.write(this.getDescription()); 207 } 208 br.write("</description>\n"); 209 br.write(" <language>"); 210 br.write(this.channelLanguage); 211 br.write("</language>\n"); 212 213 for (int i = 0; i < this.items.size(); i++) { 214 if (this.items.get(i) != null) { 216 Item item = (Item) this.items.get(i); 217 br.write(item.toXml()); 218 } 219 } 220 br.write(" </channel>\n"); 221 br.write("</rss>\n"); 222 br.flush(); 223 } 224 } 225 | Popular Tags |