[OpenLayers-Commits] r4198 - sandbox/tschaub/xml/lib/OpenLayers/Format

commits at openlayers.org commits at openlayers.org
Fri Sep 7 18:22:00 EDT 2007


Author: crschmidt
Date: 2007-09-07 18:21:56 -0400 (Fri, 07 Sep 2007)
New Revision: 4198

Added:
   sandbox/tschaub/xml/lib/OpenLayers/Format/APP.js
Log:
Stick APP.js in SVN, now that it's subclassed from GeoRSS, and hence 
slightly less cargo-cult.


Added: sandbox/tschaub/xml/lib/OpenLayers/Format/APP.js
===================================================================
--- sandbox/tschaub/xml/lib/OpenLayers/Format/APP.js	                        (rev 0)
+++ sandbox/tschaub/xml/lib/OpenLayers/Format/APP.js	2007-09-07 22:21:56 UTC (rev 4198)
@@ -0,0 +1,128 @@
+/* Copyright (c) 2006-2007 MetaCarta, Inc., published under a modified BSD license.
+ * See http://svn.openlayers.org/trunk/openlayers/repository-license.txt 
+ * for the full text of the license. */
+
+/**
+ * @requires OpenLayers/Format.js
+ * @requires OpenLayers/Format/XML.js
+ * @requires OpenLayers/Format/GeoRSS.js
+ *
+ * Class: OpenLayers.Format.APP
+ * APP Format. Create a new instance with the 
+ *     <OpenLayers.Format.APP> constructor.
+ *
+ * Inherits from:
+ *  - <OpenLayers.Format.GeoRSS>
+ */
+OpenLayers.Format.APP = OpenLayers.Class(OpenLayers.Format.GeoRSS, {
+    
+    
+    /**
+     * Constructor: OpenLayers.Format.APP
+     * Create a new parser for APP
+     *
+     * Parameters:
+     * options - {Object} An optional object whose properties will be set on
+     *                    this instance.
+     */
+    initialize: function(options) {
+        OpenLayers.Format.GeoRSS.prototype.initialize.apply(this, [options]);
+    },
+    
+    createFeatureFromItem: function(item) {
+            var geometry = this.createGeometryFromItem(item);
+            /* Provide defaults for title and description */
+            var title = "Untitled";
+            try {
+                title = OpenLayers.Util.getNodes(item, 
+                        "title")[0].firstChild.nodeValue;
+            }
+            catch (e) { title="Untitled"; }
+           
+            /* First try RSS descriptions, then Atom summaries */
+            var descr_nodes = this.getElementsByTagNameNS(item,
+                                                          "*",
+                                                          "description");
+            if (descr_nodes.length == 0) {
+                descr_nodes = this.getElementsByTagNameNS(item,
+                                                          "*",
+                                                          "summary");
+            }
+            var description = "No description.";
+            try {
+              description = descr_nodes[0].firstChild.nodeValue;
+            }
+            catch (e) { description="No description."; }
+
+            /* If no link URL is found in the first child node, try the
+               href attribute */
+            var link = null;
+            var edit_link = null;
+            try {
+                var link = OpenLayers.Util.getNodes(item, "link")[0].firstChild.nodeValue;
+            }
+            catch (e) {
+                var link_list = OpenLayers.Util.getNodes(item, "link");
+                for (var i = 0; i < link_list.length; i++) {
+                    var this_link = link_list[i];
+                    if (this_link.getAttribute("href") && this_link.getAttribute("rel") == "edit") {
+                        edit_link = this_link.getAttribute("href");
+                    }  else {
+                        link = this_link.getAttribute("href");
+                    }    
+                }    
+            }
+            var id = null;
+            try {
+                id = OpenLayers.Util.getNodes(item, "id")[0].firstChild.nodeValue;
+            } catch (e) {
+            }
+            
+            var data = {
+                "title": title,
+                "description": description,
+                "link": link,
+                "edit_link": edit_link
+            };
+            var feature = new OpenLayers.Feature.Vector(geometry, data);
+            feature.fid = id;
+            return feature;
+    },        
+    
+
+    /**
+     * Method: createFeatureXML
+     * Accept an <OpenLayers.Feature.Vector>, and build a geometry for it.
+     * 
+     * Parameters:
+     * feature - {<OpenLayers.Feature.Vector>} 
+     *
+     * Returns:
+     * {DOMElement}
+     */
+    createFeatureXML: function(feature) {
+        var geometryNode = this.buildGeometryNode(feature.geometry);
+        var featureNode = this.createElementNS(this.rssns, "item");
+        var titleNode = this.createElementNS(this.rssns, "title");
+        titleNode.appendChild(this.createTextNode(feature.attributes.title ? feature.attributes.title : ""));
+        var descNode = this.createElementNS(this.rssns, "description");
+        descNode.appendChild(this.createTextNode(feature.attributes.description ? feature.attributes.description : ""));
+        featureNode.appendChild(titleNode);
+        featureNode.appendChild(descNode);
+        for(var attr in feature.attributes) {
+            if (attr == "edit_link" || attr == "title" || attr == "description" ) { continue; }
+            var attrText = this.createTextNode(feature.attributes[attr]); 
+            var nodename = attr;
+            if (attr.search(":") != -1) {
+                nodename = attr.split(":")[1];
+            }    
+            var attrContainer = this.createElementNS(this.featureNS, "feature:"+nodename);
+            attrContainer.appendChild(attrText);
+            featureNode.appendChild(attrContainer);
+        }    
+        featureNode.appendChild(geometryNode);
+        return featureNode;
+    },    
+    
+    CLASS_NAME: "OpenLayers.Format.APP" 
+});     



More information about the Commits mailing list