1 17 package com.sun.syndication.io.impl; 18 19 import com.sun.syndication.feed.WireFeed; 20 import com.sun.syndication.feed.atom.*; 21 import com.sun.syndication.io.FeedException; 22 import org.jdom.Attribute; 23 import org.jdom.Document; 24 import org.jdom.Element; 25 import org.jdom.Namespace; 26 import org.jdom.input.SAXBuilder; 27 28 import java.io.StringReader ; 29 import java.util.List ; 30 31 38 39 public class Atom03Generator extends BaseWireFeedGenerator { 40 private static final String ATOM_URI = "http://purl.org/atom/ns#"; 41 private static final Namespace ATOM_NS = Namespace.getNamespace(ATOM_URI); 42 43 private String _version; 44 45 public Atom03Generator() { 46 this("atom_0.3","0.3"); 47 } 48 49 protected Atom03Generator(String type,String version) { 50 super(type); 51 _version = version; 52 } 53 54 protected String getVersion() { 55 return _version; 56 } 57 58 protected Namespace getFeedNamespace() { 59 return ATOM_NS; 60 } 61 62 public Document generate(WireFeed wFeed) throws FeedException { 63 Feed feed = (Feed) wFeed; 64 Element root = createRootElement(feed); 65 populateFeed(feed,root); 66 return createDocument(root); 67 } 68 69 protected Document createDocument(Element root) { 70 return new Document(root); 71 } 72 73 protected Element createRootElement(Feed feed) { 74 Element root = new Element("feed",getFeedNamespace()); 75 root.addNamespaceDeclaration(getFeedNamespace()); 76 Attribute version = new Attribute("version", getVersion()); 77 root.setAttribute(version); 78 generateModuleNamespaceDefs(root); 79 return root; 80 } 81 82 protected void populateFeed(Feed feed,Element parent) throws FeedException { 83 addFeed(feed,parent); 84 addEntries(feed,parent); 85 } 86 87 protected void addFeed(Feed feed,Element parent) throws FeedException { 88 Element eFeed = parent; 89 populateFeedHeader(feed,eFeed); 90 checkFeedHeaderConstraints(eFeed); 91 generateFeedModules(feed.getModules(),eFeed); 92 } 93 94 protected void addEntries(Feed feed,Element parent) throws FeedException { 95 List items = feed.getEntries(); 96 for (int i=0;i<items.size();i++) { 97 addEntry((Entry)items.get(i),parent); 98 } 99 checkEntriesConstraints(parent); 100 } 101 102 protected void addEntry(Entry entry,Element parent) throws FeedException { 103 Element eEntry = new Element("entry", getFeedNamespace()); 104 populateEntry(entry,eEntry); 105 checkEntryConstraints(eEntry); 106 generateItemModules(entry.getModules(),eEntry); 107 parent.addContent(eEntry); 108 } 109 110 protected void populateFeedHeader(Feed feed,Element eFeed) throws FeedException { 111 if (feed.getTitle() != null) { 112 eFeed.addContent(generateSimpleElement("title", feed.getTitle())); 113 } 114 115 List links = feed.getAlternateLinks(); 116 for (int i = 0; i < links.size(); i++) { 117 eFeed.addContent(generateLinkElement((Link)links.get(i))); 118 } 119 120 links = feed.getOtherLinks(); 121 for (int i = 0; i < links.size(); i++) { 122 eFeed.addContent(generateLinkElement((Link)links.get(i))); 123 } 124 125 if (feed.getAuthor() != null) { 126 Element authorElement = new Element("author", getFeedNamespace()); 127 fillPersonElement(authorElement, feed.getAuthor()); 128 eFeed.addContent(authorElement); 129 } 130 131 List contributors = feed.getContributors(); 132 for (int i = 0; i < contributors.size(); i++) { 133 Element contributorElement = new Element("contributor", getFeedNamespace()); 134 fillPersonElement(contributorElement, (Person)contributors.get(i)); 135 eFeed.addContent(contributorElement); 136 } 137 138 if (feed.getTagline() != null) { 139 Element taglineElement = new Element("tagline", getFeedNamespace()); 140 fillContentElement(taglineElement, feed.getTagline()); 141 eFeed.addContent(taglineElement); 142 } 143 144 if (feed.getId() != null) { 145 eFeed.addContent(generateSimpleElement("id", feed.getId())); 146 } 147 148 if (feed.getGenerator() != null) { 149 eFeed.addContent(generateGeneratorElement(feed.getGenerator())); 150 } 151 152 if (feed.getCopyright() != null) { 153 eFeed.addContent(generateSimpleElement("copyright", feed.getCopyright())); 154 } 155 156 if (feed.getInfo() != null) { 157 Element infoElement = new Element("info", getFeedNamespace()); 158 fillContentElement(infoElement, feed.getInfo()); 159 eFeed.addContent(infoElement); 160 } 161 162 if (feed.getModified() != null) { 163 Element modifiedElement = new Element("modified", getFeedNamespace()); 164 modifiedElement.addContent(DateParser.formatW3CDateTime(feed.getModified())); 165 eFeed.addContent(modifiedElement); 166 } 167 } 168 169 protected void populateEntry(Entry entry,Element eEntry) throws FeedException { 170 if (entry.getTitle() != null) { 171 eEntry.addContent(generateSimpleElement("title", entry.getTitle())); 172 } 173 List links = entry.getAlternateLinks(); 174 for (int i = 0; i < links.size(); i++) { 175 eEntry.addContent(generateLinkElement((Link)links.get(i))); 176 } 177 178 links = entry.getOtherLinks(); 179 for (int i = 0; i < links.size(); i++) { 180 eEntry.addContent(generateLinkElement((Link)links.get(i))); 181 } 182 183 if (entry.getAuthor() != null) { 184 Element authorElement = new Element("author", getFeedNamespace()); 185 fillPersonElement(authorElement, entry.getAuthor()); 186 eEntry.addContent(authorElement); 187 } 188 189 List contributors = entry.getContributors(); 190 for (int i = 0; i < contributors.size(); i++) { 191 Element contributorElement = new Element("contributor", getFeedNamespace()); 192 fillPersonElement(contributorElement, (Person)contributors.get(i)); 193 eEntry.addContent(contributorElement); 194 } 195 if (entry.getId() != null) { 196 eEntry.addContent(generateSimpleElement("id", entry.getId())); 197 } 198 199 if (entry.getModified() != null) { 200 Element modifiedElement = new Element("modified", getFeedNamespace()); 201 modifiedElement.addContent(DateParser.formatW3CDateTime(entry.getModified())); 202 eEntry.addContent(modifiedElement); 203 } 204 205 if (entry.getIssued() != null) { 206 Element issuedElement = new Element("issued", getFeedNamespace()); 207 issuedElement.addContent(DateParser.formatW3CDateTime(entry.getIssued())); 208 eEntry.addContent(issuedElement); 209 } 210 211 if (entry.getCreated() != null) { 212 Element createdElement = new Element("created", getFeedNamespace()); 213 createdElement.addContent(DateParser.formatW3CDateTime(entry.getCreated())); 214 eEntry.addContent(createdElement); 215 } 216 217 if (entry.getSummary() != null) { 218 Element summaryElement = new Element("summary", getFeedNamespace()); 219 fillContentElement(summaryElement, entry.getSummary()); 220 eEntry.addContent(summaryElement); 221 } 222 223 List contents = entry.getContents(); 224 for (int i = 0; i < contents.size(); i++) { 225 Element contentElement = new Element("content", getFeedNamespace()); 226 fillContentElement(contentElement, (Content)contents.get(i)); 227 eEntry.addContent(contentElement); 228 } 229 } 230 231 protected void checkFeedHeaderConstraints(Element eFeed) throws FeedException { 232 } 233 234 protected void checkEntriesConstraints(Element parent) throws FeedException { 235 } 236 237 protected void checkEntryConstraints(Element eEntry) throws FeedException { 238 } 239 240 241 protected Element generateLinkElement(Link link) { 242 Element linkElement = new Element("link", getFeedNamespace()); 243 244 if (link.getRel() != null) { 245 Attribute relAttribute = new Attribute("rel", link.getRel().toString()); 246 linkElement.setAttribute(relAttribute); 247 } 248 249 if (link.getType() != null) { 250 Attribute typeAttribute = new Attribute("type", link.getType()); 251 linkElement.setAttribute(typeAttribute); 252 } 253 254 if (link.getHref() != null) { 255 Attribute hrefAttribute = new Attribute("href", link.getHref()); 256 linkElement.setAttribute(hrefAttribute); 257 } 258 return linkElement; 259 } 260 261 262 protected void fillPersonElement(Element element, Person person) { 263 if (person.getName() != null) { 264 element.addContent(generateSimpleElement("name", person.getName())); 265 } 266 if (person.getUrl() != null) { 267 element.addContent(generateSimpleElement("url", person.getUrl())); 268 } 269 270 if (person.getEmail() != null) { 271 element.addContent(generateSimpleElement("email", person.getEmail())); 272 } 273 } 274 275 protected Element generateTagLineElement(Content tagline) { 276 Element taglineElement = new Element("tagline", getFeedNamespace()); 277 278 if (tagline.getType() != null) { 279 Attribute typeAttribute = new Attribute("type", tagline.getType()); 280 taglineElement.setAttribute(typeAttribute); 281 } 282 283 if (tagline.getValue() != null) { 284 taglineElement.addContent(tagline.getValue()); 285 } 286 return taglineElement; 287 } 288 289 protected void fillContentElement(Element contentElement, Content content) 290 throws FeedException { 291 292 if (content.getType() != null) { 293 Attribute typeAttribute = new Attribute("type", content.getType()); 294 contentElement.setAttribute(typeAttribute); 295 } 296 297 String mode = content.getMode(); 298 if (mode != null) { 299 Attribute modeAttribute = new Attribute("mode", content.getMode().toString()); 300 contentElement.setAttribute(modeAttribute); 301 } 302 303 if (content.getValue() != null) { 304 305 if (mode == null || mode.equals(Content.ESCAPED)) { 306 contentElement.addContent(content.getValue()); 307 } else if (mode.equals(Content.BASE64)) { 308 contentElement.addContent(Base64.encode(content.getValue())); 309 } else if (mode.equals(Content.XML)) { 310 311 StringBuffer tmpDocString = new StringBuffer ("<tmpdoc>"); 312 tmpDocString.append(content.getValue()); 313 tmpDocString.append("</tmpdoc>"); 314 StringReader tmpDocReader = new StringReader (tmpDocString.toString()); 315 Document tmpDoc; 316 317 try { 318 SAXBuilder saxBuilder = new SAXBuilder(); 319 tmpDoc = saxBuilder.build(tmpDocReader); 320 } 321 catch (Exception ex) { 322 throw new FeedException("Invalid XML",ex); 323 } 324 325 List children = tmpDoc.getRootElement().removeContent(); 326 contentElement.addContent(children); 327 } 328 } 329 } 330 331 protected Element generateGeneratorElement(Generator generator) { 332 Element generatorElement = new Element("generator", getFeedNamespace()); 333 334 if (generator.getUrl() != null) { 335 Attribute urlAttribute = new Attribute("url", generator.getUrl()); 336 generatorElement.setAttribute(urlAttribute); 337 } 338 339 if (generator.getVersion() != null) { 340 Attribute versionAttribute = new Attribute("version", generator.getVersion()); 341 generatorElement.setAttribute(versionAttribute); 342 } 343 344 if (generator.getValue() != null) { 345 generatorElement.addContent(generator.getValue()); 346 } 347 348 return generatorElement; 349 350 } 351 352 protected Element generateSimpleElement(String name, String value) { 353 Element element = new Element(name, getFeedNamespace()); 354 element.addContent(value); 355 return element; 356 } 357 358 } 359 | Popular Tags |