Search in sources :

Example 1 with GeoRSSModule

use of com.sun.syndication.feed.module.georss.GeoRSSModule in project pentaho-kettle by pentaho.

the class RssOutput method createEntry.

/**
 * @param author
 *          : The author of the event
 * @param title
 *          : The title of the event
 * @param link
 *          : The link to the element in RES
 * @param date
 *          : The event's date
 * @param desc
 *          : The event's description
 */
@SuppressWarnings("unchecked")
public boolean createEntry(String author, String title, String link, Date date, String desc, String geopointLat, String geopointLong) {
    boolean retval = false;
    try {
        // Add entry to the feed
        SyndEntry entry = new SyndEntryImpl();
        SyndContent description;
        entry = new SyndEntryImpl();
        if (title != null) {
            entry.setTitle(title);
        }
        if (link != null) {
            entry.setLink(link);
        }
        if (date != null) {
            entry.setPublishedDate(date);
        }
        if (author != null) {
            entry.setAuthor(author);
        }
        if (desc != null) {
            description = new SyndContentImpl();
            description.setType("text/plain");
            description.setValue(desc);
            entry.setDescription(description);
        }
        if (meta.AddGeoRSS() && geopointLat != null && geopointLong != null) {
            // Add GeoRSS?
            GeoRSSModule geoRSSModule = new SimpleModuleImpl();
            if (meta.useGeoRSSGML()) {
                geoRSSModule = new W3CGeoModuleImpl();
            }
            geoRSSModule.setPosition(new Position(Const.toDouble(geopointLat.replace(',', '.'), 0), Const.toDouble(geopointLong.replace(',', '.'), 0)));
            entry.getModules().add(geoRSSModule);
        }
        data.entries.add(entry);
        retval = true;
    } catch (Exception e) {
        logError(BaseMessages.getString(PKG, "RssOutput.Log.ErrorAddingEntry", e.getMessage()));
        setErrors(1);
        retval = false;
    }
    return retval;
}
Also used : SyndContent(com.sun.syndication.feed.synd.SyndContent) Position(com.sun.syndication.feed.module.georss.geometries.Position) SyndEntry(com.sun.syndication.feed.synd.SyndEntry) SyndContentImpl(com.sun.syndication.feed.synd.SyndContentImpl) SyndEntryImpl(com.sun.syndication.feed.synd.SyndEntryImpl) GeoRSSModule(com.sun.syndication.feed.module.georss.GeoRSSModule) SimpleModuleImpl(com.sun.syndication.feed.module.georss.SimpleModuleImpl) W3CGeoModuleImpl(com.sun.syndication.feed.module.georss.W3CGeoModuleImpl) KettleException(org.pentaho.di.core.exception.KettleException) KettleStepException(org.pentaho.di.core.exception.KettleStepException)

Example 2 with GeoRSSModule

use of com.sun.syndication.feed.module.georss.GeoRSSModule in project sis by apache.

the class LocationServlet method init.

/**
 * Read GeoRSS data (location information provide sis-location-config.xml )
 * and build quad-tree.
 *
 * @param config
 *          Servlet configuration file
 * @exception ServletException
 *              General exception for servlet
 */
@SuppressWarnings("unchecked")
public void init(ServletConfig config) throws ServletException {
    this.context = config.getServletContext();
    long startTime = 0;
    long endTime = 0;
    int capacity = -1, depth = -1;
    this.qtreeIdxPath = this.context.getInitParameter("org.apache.sis.services.config.qIndexPath");
    this.georssStoragePath = this.context.getInitParameter("org.apache.sis.services.config.geodataPath");
    if (!this.qtreeIdxPath.endsWith("/"))
        this.qtreeIdxPath += "/";
    if (!this.georssStoragePath.endsWith("/"))
        this.georssStoragePath += "/";
    InputStream indexStream = null;
    try {
        indexStream = new FileInputStream(qtreeIdxPath + "node_0.txt");
    } catch (FileNotFoundException e) {
        System.out.println("[INFO] Existing qtree index at: [" + qtreeIdxPath + "] not found. Creating new index.");
    }
    if (indexStream != null) {
        startTime = System.currentTimeMillis();
        this.tree = QuadTreeReader.readFromFile(qtreeIdxPath, "tree_config.txt", "node_0.txt");
        try {
            indexStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        endTime = System.currentTimeMillis();
        this.timeToLoad = "Quad Tree fully loaded from index files in " + Double.toString((endTime - startTime) / 1000L) + " seconds";
        System.out.println("[INFO] Finished loading tree from stored index");
    } else {
        startTime = System.currentTimeMillis();
        WireFeedInput wf = new WireFeedInput(true);
        // read quad tree properties set in config xml file
        InputStream configStream = null;
        try {
            configStream = new FileInputStream(this.context.getInitParameter("org.apache.sis.services.config.filePath"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (configStream != null) {
            DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
            try {
                DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
                Document configDoc = docBuilder.parse(configStream);
                NodeList capacityNode = configDoc.getElementsByTagName("capacity");
                if (capacityNode.item(0) != null) {
                    capacity = Integer.parseInt(capacityNode.item(0).getFirstChild().getNodeValue());
                }
                NodeList depthNode = configDoc.getElementsByTagName("depth");
                if (depthNode.item(0) != null) {
                    depth = Integer.parseInt(depthNode.item(0).getFirstChild().getNodeValue());
                }
                // TODO make this
                this.tree = new QuadTree(capacity, depth);
                // configurable
                NodeList urlNodes = configDoc.getElementsByTagName("url");
                for (int i = 0; i < urlNodes.getLength(); i++) {
                    // read in georss and build tree
                    String georssUrlStr = urlNodes.item(i).getFirstChild().getNodeValue();
                    WireFeed feed = null;
                    try {
                        feed = wf.build(new XmlReader(new URL(georssUrlStr)));
                    } catch (Exception e) {
                        System.out.println("[ERROR] Error obtaining geodata url: [" + georssUrlStr + "]: Message: " + e.getMessage() + ": skipping and continuing");
                        continue;
                    }
                    Channel c = (Channel) feed;
                    List<Item> items = (List<Item>) c.getItems();
                    for (Item item : items) {
                        GeoRSSModule geoRSSModule = (GeoRSSModule) item.getModule(GeoRSSModule.GEORSS_GEORSS_URI);
                        if (geoRSSModule == null)
                            geoRSSModule = (GeoRSSModule) item.getModule(GeoRSSModule.GEORSS_GML_URI);
                        if (geoRSSModule == null)
                            geoRSSModule = (GeoRSSModule) item.getModule(GeoRSSModule.GEORSS_W3CGEO_URI);
                        // then discard it
                        if (geoRSSModule != null && geoRSSModule.getPosition() != null) {
                            String filename = "";
                            if (item.getGuid() != null)
                                filename = cleanStr(item.getGuid().getValue()) + ".txt";
                            else
                                filename = cleanStr(item.getLink()) + ".txt";
                            GeoRSSData data = new GeoRSSData(filename, new DirectPosition2D(geoRSSModule.getPosition().getLongitude(), geoRSSModule.getPosition().getLatitude()));
                            if (this.tree.insert(data)) {
                                data.saveToFile(item, geoRSSModule, georssStoragePath);
                            } else {
                                System.out.println("[INFO] Unable to store data at location " + data.getLatLon().y + ", " + data.getLatLon().x + " under filename " + data.getFileName());
                            }
                        }
                    }
                }
                configStream.close();
                endTime = System.currentTimeMillis();
                this.timeToLoad = "Quad Tree fully loaded from retrieving GeoRSS files over the network in " + Double.toString((endTime - startTime) / 1000L) + " seconds";
                QuadTreeWriter.writeTreeToFile(tree, qtreeIdxPath);
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
        } else {
            throw new ServletException("Unable to read location service XML config: null!");
        }
    }
}
Also used : QuadTree(org.apache.sis.index.tree.QuadTree) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) FileNotFoundException(java.io.FileNotFoundException) Document(org.w3c.dom.Document) DirectPosition2D(org.apache.sis.geometry.DirectPosition2D) WireFeed(com.sun.syndication.feed.WireFeed) URL(java.net.URL) SAXException(org.xml.sax.SAXException) ServletException(javax.servlet.ServletException) Item(com.sun.syndication.feed.rss.Item) WireFeedInput(com.sun.syndication.io.WireFeedInput) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) Channel(com.sun.syndication.feed.rss.Channel) IOException(java.io.IOException) XmlReader(com.sun.syndication.io.XmlReader) FileInputStream(java.io.FileInputStream) TransformerException(javax.xml.transform.TransformerException) ServletException(javax.servlet.ServletException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) GeoRSSModule(com.sun.syndication.feed.module.georss.GeoRSSModule) GeoRSSData(org.apache.sis.index.tree.GeoRSSData)

Aggregations

GeoRSSModule (com.sun.syndication.feed.module.georss.GeoRSSModule)2 WireFeed (com.sun.syndication.feed.WireFeed)1 SimpleModuleImpl (com.sun.syndication.feed.module.georss.SimpleModuleImpl)1 W3CGeoModuleImpl (com.sun.syndication.feed.module.georss.W3CGeoModuleImpl)1 Position (com.sun.syndication.feed.module.georss.geometries.Position)1 Channel (com.sun.syndication.feed.rss.Channel)1 Item (com.sun.syndication.feed.rss.Item)1 SyndContent (com.sun.syndication.feed.synd.SyndContent)1 SyndContentImpl (com.sun.syndication.feed.synd.SyndContentImpl)1 SyndEntry (com.sun.syndication.feed.synd.SyndEntry)1 SyndEntryImpl (com.sun.syndication.feed.synd.SyndEntryImpl)1 WireFeedInput (com.sun.syndication.io.WireFeedInput)1 XmlReader (com.sun.syndication.io.XmlReader)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1