/* * The MIT License (MIT) * * Copyright (c) 2014 México Abierto * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * * For more information visit https://github.com/mxabierto/avisos. */ package mx.org.cedn.avisosconagua.engine; import com.google.publicalerts.cap.Alert; import com.google.publicalerts.cap.CapUtil; import com.google.publicalerts.cap.CapXmlBuilder; import com.sun.syndication.feed.synd.SyndContent; import com.sun.syndication.feed.synd.SyndContentImpl; import com.sun.syndication.feed.synd.SyndEntry; import com.sun.syndication.feed.synd.SyndEntryImpl; import com.sun.syndication.feed.synd.SyndFeed; import com.sun.syndication.feed.synd.SyndFeedImpl; import com.sun.syndication.io.FeedException; import com.sun.syndication.io.SyndFeedOutput; import java.text.ParseException; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; import mx.org.cedn.avisosconagua.util.Utils; /** * Feed Writer implementation using Google's CAP library. * @author Hasdai Pacheco */ public class FeedWriter { private SyndFeed feed; private CapXmlBuilder builder; private List<Alert> alerts; /** * Creates a new instance of a FeedWriter. * @param feedType Type for the feed, must be in supported types, defaults to "atom_1.0". */ public FeedWriter(String feedType) { initFeed(feedType,"",""); } /** * Creates a new instance of a FeedWriter. * @param feedType Type for the feed, must be in supported types, defaults to "atom_1.0". * @param feedTitle Title for the feed. * @param feedAuthor Author of the feed. */ public FeedWriter (String feedType, String feedTitle, String feedAuthor) { initFeed(feedType, feedTitle, feedAuthor); } /** * Initializes feed writer. * @param feedType Type for the feed, must be in supported types, defaults to "atom_1.0". * @param feedTitle Title for the feed. * @param feedAuthor Author of the feed. */ private void initFeed(String feedType, String feedTitle, String feedAuthor) { builder = new CapXmlBuilder(2); alerts = new ArrayList<>(); feed = new SyndFeedImpl(); feed.setFeedType(feedType); feed.setTitle(feedTitle); feed.setAuthor(feedAuthor); feed.setUri("http://smn.cna.gob.mx/capalert"); } /** * Sets the title of the feed. * @param title Title. */ public void setTitle(String title) { feed.setTitle(title); } /** * Sets the URI of the feed. * @param uri The URI. */ public void setURI(String uri) { feed.setUri(uri); } /** * Sets the author of the feed. * @param author The autor. */ public void setAuthor(String author) { feed.setAuthor(author); } /** * Sets publish date of the feed. * @param date The publish date. */ public void setPubDate(Date date) { feed.setPublishedDate(date); } /** * Sets the list of alerts to generate the feed. * @param alertList List of alerts to be included in the feed. */ public void setAlertList(List<Alert> alertList) { alerts = alertList; } /** * Gets the list of alerts defined for this feed. * @return List of alerts in the feed. */ public List<Alert> getAlertList() { return alerts; } /** * Adds a CAP Alert to the list of alerts of this feed. * @param alert Alert to add to the feed. * @return true if alert was added */ public boolean addAlert(Alert alert) { return alerts.add(alert); } /** * Adds the provided alerts to the list of alerts for this feed. * @param allAlerts list of alerts to add * @return true if alerts were added */ public boolean addAllAlerts(List<Alert> allAlerts) { return alerts.addAll(allAlerts); } /** * Gets the XML for the feed. * @return String containing the XML code for the feed. */ public String getXML() { String ret = ""; List<SyndEntry> entries = new ArrayList<>(); for(Alert alert : alerts) { if (!alert.getInfoList().isEmpty()) { SyndEntry entry = new SyndEntryImpl(); entry.setUri(alert.getIdentifier()); entry.setTitle(alert.getInfo(0).getEvent()); try { entry.setUpdatedDate(Utils.isoformater.parse(alert.getSent())); } catch (ParseException ex) { } SyndContent cnt = new SyndContentImpl(); cnt.setType("text/xml"); cnt.setValue(CapUtil.stripXmlPreamble(builder.toXml(alert))); entry.setContents(Collections.singletonList(cnt)); entries.add(entry); } } feed.setEntries(entries); try { SyndFeedOutput sfo = new SyndFeedOutput(); ret = sfo.outputString(feed); } catch (FeedException ex) { System.out.println("There was an exception writing feed contents"); } return ret; } }