BrightSide Workbench Full Report + Source Code
RssReader.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.elephant.impl.feed;
19 
20 import com.sun.syndication.feed.synd.SyndFeed;
21 import com.sun.syndication.io.FeedException;
22 import com.sun.syndication.io.SyndFeedInput;
23 import com.sun.syndication.io.XmlReader;
24 import java.io.IOException;
25 import java.io.PrintWriter;
26 import java.net.MalformedURLException;
27 import java.net.URL;
28 import javax.servlet.ServletException;
29 import org.turro.elephant.context.IElement;
30 import org.turro.elephant.impl.abstracts.AbstractElement;
31 import org.turro.elephant.impl.util.Localizer;
32 import org.turro.elephant.layout.IManageable;
33 import org.turro.elephant.layout.IRenderable;
34 import org.turro.marker.ElephantMarker;
35 
40 public class RssReader extends AbstractElement implements IElement, IRenderable, IManageable {
41  protected Localizer localizer;
42 
46  public RssReader() {
47  super();
48  }
49 
50  @Override
51  public void startConstruction() throws ServletException, IOException {
53  PrintWriter out = getConstructor().getOut();
54  out.print(getStart());
55  out.print(getStartBody());
56  String url[] = getAttributes().getAttributeValue("attrib:feed", "").split(",");
57  SyndFeedInput input = new SyndFeedInput();
58  try {
59  SyndFeed feed;
60  for(int i = 0; i < url.length; i++) {
61  feed = input.build(new XmlReader(new URL(url[i])));
62  renderFeed(out, feed);
63  }
64  } catch (MalformedURLException | IllegalArgumentException | FeedException ex) {
65  throw new ServletException(ex);
66  }
67  out.print(getEndBody());
68  out.print(getEnd());
69  }
70 
71  @Override
72  public String getStart() {
73  return "<div class='rssreader'>";
74  }
75 
76  @Override
77  public String getEnd() {
78  return "</div>";
79  }
80 
81  private void renderFeed(PrintWriter out, SyndFeed feed) {
83  marker.put("feed", feed);
84  marker.process("rss", "feed");
85  }
86 }
void process(String rootTmpl, String tmpl)
Object put(Object key, Object value)