abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r560436 - in /incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo: ./ Box.java Coordinate.java Coordinates.java GeoHelper.java Line.java Multiple.java Point.java Polygon.java Position.java
Date Sat, 28 Jul 2007 01:34:30 GMT
Author: jmsnell
Date: Fri Jul 27 18:34:29 2007
New Revision: 560436

URL: http://svn.apache.org/viewvc?view=rev&rev=560436
Log:
Basic support for geotagging extensions http://georss.org/1

  public static void main(String... args) throws Exception {
   
    Abdera abdera = new Abdera();
    Entry entry = abdera.newEntry();
   
    Point point = new Point(10.0,12.0);
    point.setElevation(12.0d);
    point.setRadius(5d);
    
    GeoHelper.addPosition(entry, point);

    System.out.println(GeoHelper.isGeotagged(entry));
    
    Iterator<Position> i = GeoHelper.listPositions(entry);
    
    while(i.hasNext()) {
      Position p = i.next();
      if (p instanceof Point) {
        System.out.println(((Point)p).getCoordinate());
      } else if (p instanceof Multiple) {
        System.out.println(((Multiple)p).getCoordinates());
      }
      System.out.println(p.getElevation());
      System.out.println(p.getRadius());
      System.out.println(p.getFloor());
      System.out.println(p.getRelationshipTag());
      System.out.println(p.getFeatureTypeTag());
    }
  }

Added:
    incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/
    incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Box.java
    incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Coordinate.java
    incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Coordinates.java
    incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/GeoHelper.java
    incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Line.java
    incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Multiple.java
    incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Point.java
    incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Polygon.java
    incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Position.java

Added: incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Box.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Box.java?view=auto&rev=560436
==============================================================================
--- incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Box.java
(added)
+++ incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Box.java
Fri Jul 27 18:34:29 2007
@@ -0,0 +1,98 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.ext.geo;
+
+public class Box 
+  extends Multiple {
+
+  private static final String TWO_COORDINATES = "A box must have two coordinates";
+  private static final long serialVersionUID = 3994252648307511152L;
+
+  public Box() {
+    super();
+  }
+ 
+  public Box(Multiple multiple) {
+    super(multiple);
+    if (this.coordinates.size() != 2)
+      throw new IllegalArgumentException(
+        TWO_COORDINATES);
+  }
+  
+  public Box(Point lowerCorner, Point upperCorner) {
+    super(lowerCorner,upperCorner);
+  }
+  
+  public Box(Coordinate... coordinates) {
+    super(coordinates);
+    if (this.coordinates.size() != 2)
+      throw new IllegalArgumentException(
+        TWO_COORDINATES);
+  }
+
+  public Box(Coordinates coordinates) {
+    super(coordinates);
+    if (this.coordinates.size() != 2)
+      throw new IllegalArgumentException(
+        TWO_COORDINATES);
+  }
+
+  public Box(String value) {
+    super(value);
+    if (this.coordinates.size() != 2)
+      throw new IllegalArgumentException(
+        TWO_COORDINATES);
+  }
+
+  public Box(Multiple... multiples) {
+    super(multiples);
+    if (this.coordinates.size() != 2)
+      throw new IllegalArgumentException(
+        TWO_COORDINATES);
+  }
+
+  public Box(Point... points) {
+    super(points);
+    if (this.coordinates.size() != 2)
+      throw new IllegalArgumentException(
+        TWO_COORDINATES);
+  }
+
+  public Box(double... values) {
+    super(values);
+    if (this.coordinates.size() != 2)
+      throw new IllegalArgumentException(
+        TWO_COORDINATES);
+  }  
+  
+  @Override
+  public void setCoordinates(Coordinates coordinates) {
+    super.setCoordinates(coordinates);
+    if (this.coordinates.size() > 2)
+      throw new IllegalArgumentException(
+        TWO_COORDINATES);
+  }
+
+  public Coordinate getUpperCorner() {
+    return coordinates.size() > 1 ? coordinates.get(1) : null;
+  }
+  
+  public Coordinate getLowerCorner() {
+    return coordinates.size() > 0 ? coordinates.get(0) : null;
+  }
+}

Added: incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Coordinate.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Coordinate.java?view=auto&rev=560436
==============================================================================
--- incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Coordinate.java
(added)
+++ incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Coordinate.java
Fri Jul 27 18:34:29 2007
@@ -0,0 +1,130 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.ext.geo;
+
+import java.io.Serializable;
+
+public class Coordinate 
+  implements Serializable, 
+             Cloneable,
+             Comparable<Coordinate> {
+
+  private static final long serialVersionUID = -916272885213668761L;
+  
+  private double latitude = 0.0f;
+  private double longitude = 0.0f;
+  
+  public Coordinate() {}
+  
+  public Coordinate(double latitude, double longitude) {
+    setLatitude(latitude);
+    setLongitude(longitude);
+  }
+  
+  public Coordinate(String value) {
+    Coordinate c = parse(value);
+    setLatitude(c.latitude);
+    setLongitude(c.longitude);
+  }
+  
+  public double getLatitude() {
+    return latitude;
+  }
+  
+  public void setLatitude(double latitude) {
+    if (Double.compare(longitude, 90.0d) > 0)
+      throw new IllegalArgumentException("Latitude > 90.0 degrees");
+    if (Double.compare(longitude, -90.0d) < 0)
+      throw new IllegalArgumentException("Latitude < 90.0 degrees");
+    this.latitude = latitude;
+  }
+  
+  public double getLongitude() {
+    return longitude;
+  }
+  
+  public void setLongitude(double longitude) {
+    if (Double.compare(longitude, 180.0d) > 0)
+      throw new IllegalArgumentException("Longitude > 180.0 degrees");
+    if (Double.compare(longitude, -180.0d) < 0)
+      throw new IllegalArgumentException("Longitude < 180.0 degrees");
+    this.longitude = longitude;
+  }
+  
+  public String toString() {
+    return Double.toString(latitude) + " " + Double.toString(longitude);
+  }
+
+  public Coordinate clone() {
+    try {
+      return (Coordinate) super.clone();
+    } catch (CloneNotSupportedException e) {
+      return new Coordinate(latitude,longitude);  // not going to happen
+    }
+  }
+
+  @Override
+  public int hashCode() {
+    final int PRIME = 31;
+    int result = 1;
+    long temp;
+    temp = Double.doubleToLongBits(latitude);
+    result = PRIME * result + (int) (temp ^ (temp >>> 32));
+    temp = Double.doubleToLongBits(longitude);
+    result = PRIME * result + (int) (temp ^ (temp >>> 32));
+    return result;
+  }
+
+  @Override
+  public boolean equals(Object obj) {
+    if (this == obj)
+      return true;
+    if (obj == null)
+      return false;
+    if (getClass() != obj.getClass())
+      return false;
+    final Coordinate other = (Coordinate) obj;
+    if (Double.doubleToLongBits(latitude) != Double.doubleToLongBits(other.latitude))
+      return false;
+    if (Double.doubleToLongBits(longitude) != Double.doubleToLongBits(other.longitude))
+      return false;
+    return true;
+  }
+
+  public static Coordinate parse(String value) {
+    try {
+      String[] points = value.trim().split("\\s+", 2);
+      double latitude = Double.parseDouble(points[0].trim());
+      double longitude = Double.parseDouble(points[1].trim());
+      return new Coordinate(latitude,longitude);
+    } catch (Throwable t) {
+      throw new RuntimeException("Error parsing coordinate pair",t);
+    }
+  }
+
+  public int compareTo(Coordinate o) {
+    if (o == null || equals(o)) return 0;
+    int l1 = Double.compare(latitude, o.latitude);
+    int l2 = Double.compare(longitude, o.longitude);
+    if (l1 < 0) return -1;
+    if (l1 == 0 && l2 < -1) return -1;
+    if (l1 == 0 && l2 == 0) return  0;
+    return 1;
+  }
+  
+}

Added: incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Coordinates.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Coordinates.java?view=auto&rev=560436
==============================================================================
--- incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Coordinates.java
(added)
+++ incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Coordinates.java
Fri Jul 27 18:34:29 2007
@@ -0,0 +1,202 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.ext.geo;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+
+public class Coordinates 
+  implements Iterable<Coordinate>, 
+             Serializable, 
+             Cloneable,
+             Comparable<Coordinates>{
+
+  private static final long serialVersionUID = 1640788106113796699L;
+  protected List<Coordinate> coords = new ArrayList<Coordinate>();
+  
+  public Coordinates() {}
+  
+  public Coordinates(Coordinate... coordinates) {
+    for (Coordinate coord : coordinates) add(coord);
+  }
+  
+  public Coordinates(Coordinates coordinates) {
+    coords.addAll(coordinates.coords);
+  }
+  
+  public Coordinates(String value) {
+    Coordinates cs = parse(value);
+    this.coords = cs.coords;
+  }
+  
+  public synchronized Coordinate get(int n) {
+    return coords.get(n);
+  }
+  
+  public synchronized void add(Coordinates coordinates) {
+    coords.addAll(coordinates.coords);
+  }
+  
+  public synchronized void add(Coordinate coordinate) {
+    coords.add(coordinate);
+  }
+  
+  public synchronized void add(Coordinate... coordinates) {
+    for (Coordinate c : coordinates) add(c);
+  }
+  
+  public synchronized void add(double latitude, double longitude) {
+    coords.add(new Coordinate(latitude,longitude));
+  }
+  
+  public synchronized void add(String value) {
+    coords.add(new Coordinate(value));
+  }
+  
+  public synchronized void remove(Coordinate coordinate) {
+    if (coords.contains(coordinate)) coords.remove(coordinate);
+  }
+  
+  public synchronized void remove(Coordinate... coordinates) {
+    for (Coordinate c : coordinates) remove(c);
+  }
+  
+  public synchronized void remove(double latitude, double longitude) {
+    remove(new Coordinate(latitude,longitude));
+  }
+  
+  public synchronized void remove(String value) {
+    remove(new Coordinate(value));
+  }
+  
+  public synchronized boolean contains(double latitude, double longitude) {
+    return contains(new Coordinate(latitude,longitude));
+  }
+  
+  public synchronized boolean contains(String value) {
+    return contains(new Coordinate(value));
+  }
+  
+  public synchronized boolean contains(Coordinate coordinate) {
+    return coords.contains(coordinate);
+  }
+  
+  public synchronized boolean contains(Coordinate... coordinates) {
+    for (Coordinate c : coordinates) 
+      if (!coords.contains(c)) return false;
+    return true;
+  }
+  
+  public Iterator<Coordinate> iterator() {
+    return coords.iterator();
+  }
+
+  public String toString() {
+    StringBuffer buf = new StringBuffer();
+    for (Coordinate coord : coords) {
+      if (buf.length() > 0) buf.append(" ");
+      buf.append(coord);
+    }
+    return buf.toString();
+  }
+
+  @Override
+  public int hashCode() {
+    final int PRIME = 31;
+    int result = 1;
+    result = PRIME * result + ((coords == null) ? 0 : coords.hashCode());
+    return result;
+  }
+
+  @Override
+  public boolean equals(Object obj) {
+    if (this == obj)
+      return true;
+    if (obj == null)
+      return false;
+    if (getClass() != obj.getClass())
+      return false;
+    final Coordinates other = (Coordinates) obj;
+    if (coords == null) {
+      if (other.coords != null)
+        return false;
+    } else if (!coords.equals(other.coords))
+      return false;
+    return true;
+  }
+  
+  public static Coordinates parse(String value) {
+    Coordinates cs = new Coordinates();
+    try {
+      String[] points = value.trim().split("\\s+");
+      for (int n = 0; n < points.length; n = n + 2) {
+        double lat = Double.parseDouble(points[n]);
+        double lon = Double.parseDouble(points[n+1]);
+        Coordinate c = new Coordinate(lat,lon);
+        cs.add(c);
+      }
+      return cs;
+    } catch (Throwable t) {
+      throw new RuntimeException("Error parsing coordinate pairs",t);
+    }
+  }
+  
+  public int size() {
+    return coords.size();
+  }
+  
+  public void clear() {
+    coords.clear();
+  }
+  
+  public Coordinates clone() {
+    try {
+      return (Coordinates) super.clone();
+    } catch (CloneNotSupportedException e) {
+      return new Coordinates(this);  // Not going to happen
+    }
+  }
+  
+  public Coordinates sort() {
+    return sort(false);
+  }
+  
+  public Coordinates sort(boolean reverse) {
+    Coordinates c = clone();
+    if (reverse) Collections.sort(c.coords,Collections.reverseOrder());
+    else Collections.sort(c.coords);
+    return c;
+  }
+
+  public int compareTo(Coordinates o) {
+    if (o == null || equals(o)) return 0;
+    if (size() < o.size()) return -1;
+    if (o.size() < size()) return 1;
+    for (int n = 0; n < size(); n++) {
+      Coordinate c1 = coords.get(n);
+      Coordinate c2 = o.coords.get(n);
+      int c = c1.compareTo(c2);
+      if (c != 0) return c;
+    }
+    return 0;
+  }
+  
+}

Added: incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/GeoHelper.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/GeoHelper.java?view=auto&rev=560436
==============================================================================
--- incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/GeoHelper.java
(added)
+++ incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/GeoHelper.java
Fri Jul 27 18:34:29 2007
@@ -0,0 +1,308 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.ext.geo;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.xml.namespace.QName;
+
+import org.apache.abdera.model.Element;
+import org.apache.abdera.model.Entry;
+import org.apache.abdera.model.ExtensibleElement;
+
+/**
+ * Basic support for the GeoRSS extensions to Atom: http://georss.org/1
+ */
+public class GeoHelper {
+
+  public static final String W3C_GEO_NS = "http://www.w3.org/2003/01/geo/wgs84_pos#";
+  public static final String SIMPLE_GEO_NS = "http://www.georss.org/georss/10";
+  public static final String GML_NS = "http://www.opengis.net/gml";
+  
+  public static final QName QNAME_W3C_LAT = new QName(W3C_GEO_NS, "lat", "geo");
+  public static final QName QNAME_W3C_LONG = new QName(W3C_GEO_NS, "long", "geo");
+  
+  public static final QName QNAME_SIMPLE_POINT = new QName(SIMPLE_GEO_NS, "point", "georss");
+  public static final QName QNAME_SIMPLE_LINE = new QName(SIMPLE_GEO_NS, "line", "georss");
+  public static final QName QNAME_SIMPLE_POLYGON = new QName(SIMPLE_GEO_NS, "polygon", "georss");
+  public static final QName QNAME_SIMPLE_BOX = new QName(SIMPLE_GEO_NS, "box", "georss");
+  public static final QName QNAME_WHERE = new QName(SIMPLE_GEO_NS, "where", "georss");
+  
+  public static final QName QNAME_GML_POINT = new QName(GML_NS, "Point", "gml");
+  public static final QName QNAME_GML_POS = new QName(GML_NS, "pos", "gml");
+  public static final QName QNAME_GML_LINESTRING  = new QName(GML_NS, "LineString", "gml");
+  public static final QName QNAME_GML_POSLIST = new QName(GML_NS, "posList", "gml");
+  public static final QName QNAME_GML_POLYGON = new QName(GML_NS, "Polygon", "gml");
+  public static final QName QNAME_GML_EXTERIOR = new QName(GML_NS, "exterior", "gml");
+  public static final QName QNAME_GML_LINEARRING = new QName(GML_NS, "LinearRing", "gml");
+  public static final QName QNAME_GML_ENVELOPE = new QName(GML_NS, "Envelope", "gml");
+  public static final QName QNAME_GML_LOWERCORNER = new QName(GML_NS, "lowerCorner", "gml");
+  public static final QName QNAME_GML_UPPERCORNER = new QName(GML_NS, "upperCorner", "gml");
+  
+  public enum Encoding {
+    SIMPLE, W3C, GML
+  }
+  
+  public static void addPosition(
+    Entry entry, 
+    Position position) {
+      addPosition(entry,position,Encoding.SIMPLE);
+  }
+  
+  public static void addPosition(
+    Entry entry, 
+    Position position, 
+    Encoding encoding) {
+      switch(encoding) {
+        case SIMPLE:
+          addSimplePosition(entry, position);
+          break;
+        case GML:
+          addGmlPosition(entry, position);
+          break;
+        case W3C: {
+          addW3CPosition(entry, position); 
+          break;
+        }
+      }
+  }
+  
+  private static void setPositionAttributes(Element pos, Position position) {
+    if (pos != null) {
+      if (position.getFeatureTypeTag() != null)
+        pos.setAttributeValue("featuretypetag", position.getFeatureTypeTag());
+      if (position.getRelationshipTag() != null)
+        pos.setAttributeValue("relationshiptag", position.getRelationshipTag());
+      if (position.getElevation() != null)
+        pos.setAttributeValue("elev", position.getElevation().toString());
+      if (position.getFloor() != null)
+        pos.setAttributeValue("floor", position.getFloor().toString());
+      if (position.getRadius() != null)
+        pos.setAttributeValue("radius", position.getRadius().toString());
+    }
+  }
+  
+  private static void addGmlPosition(Entry entry, Position position) {
+    ExtensibleElement pos = entry.addExtension(QNAME_WHERE);
+    if (position instanceof Point) {
+      Point point = (Point) position;
+      ExtensibleElement p = pos.addExtension(QNAME_GML_POINT);
+      p.addSimpleExtension(QNAME_GML_POS, point.getCoordinate().toString());
+    } else if (position instanceof Line) {
+      Multiple m = (Multiple) position;
+      ExtensibleElement p = pos.addExtension(QNAME_GML_LINESTRING);
+      p.addSimpleExtension(QNAME_GML_POSLIST, m.getCoordinates().toString());
+    } else if (position instanceof Polygon) {
+      Multiple m = (Multiple) position;
+      ExtensibleElement p = pos.addExtension(QNAME_GML_POLYGON);
+      p = p.addExtension(QNAME_GML_EXTERIOR);
+      p = p.addExtension(QNAME_GML_LINEARRING);
+      p.addSimpleExtension(QNAME_GML_POSLIST, m.getCoordinates().toString());
+    } else if (position instanceof Box) {
+      Box m = (Box) position;
+      ExtensibleElement p = pos.addExtension(QNAME_GML_ENVELOPE);
+      if (m.getLowerCorner() != null)
+        p.addSimpleExtension(QNAME_GML_LOWERCORNER,m.getLowerCorner().toString());
+      if (m.getUpperCorner() != null)
+        p.addSimpleExtension(QNAME_GML_UPPERCORNER, m.getUpperCorner().toString());
+    }
+    setPositionAttributes(pos,position);
+  }
+  
+  private static void addSimplePosition(Entry entry, Position position) {
+    Element pos = null;
+    if (position instanceof Point) {
+      Point point = (Point) position;
+      pos = 
+        entry.addSimpleExtension(
+          QNAME_SIMPLE_POINT, 
+          point.getCoordinate().toString());
+    } else if (position instanceof Multiple) {
+      Multiple line = (Multiple) position;
+      QName qname = position instanceof Line ? QNAME_SIMPLE_LINE :
+                    position instanceof Box  ? QNAME_SIMPLE_BOX :
+                    position instanceof Polygon ? QNAME_SIMPLE_POLYGON : null;
+      if (qname != null) {
+        pos = 
+          entry.addSimpleExtension(
+            qname, 
+            line.getCoordinates().toString());
+      }
+    }
+    setPositionAttributes(pos, position);
+  }
+  
+  private static void addW3CPosition(Entry entry, Position position) {
+    if (!(position instanceof Point)) 
+      throw new IllegalArgumentException("The W3C Encoding only supports Points");
+    Element el = entry.getExtension(QNAME_W3C_LAT);
+    if (el != null) el.discard();
+    el = entry.getExtension(QNAME_W3C_LONG);
+    if (el != null) el.discard();    
+    Point point = (Point) position;
+    entry.addSimpleExtension(QNAME_W3C_LAT, Double.toString(point.getCoordinate().getLatitude()));
+    entry.addSimpleExtension(QNAME_W3C_LONG, Double.toString(point.getCoordinate().getLongitude()));
+  }
+  
+  private static List<Position> _getPositions(Entry entry) {
+    List<Position> list = new ArrayList<Position>();
+    getW3CPosition(entry,list);
+    getSimplePosition(entry,list);
+    getGMLPosition(entry, list);
+    return list;
+  }
+  
+  public static boolean isGeotagged(Entry entry) {
+    if (entry.getExtensions(QNAME_SIMPLE_POINT).size() > 0) return true;
+    if (entry.getExtensions(QNAME_SIMPLE_LINE).size() > 0) return true;
+    if (entry.getExtensions(QNAME_SIMPLE_BOX).size() > 0) return true;
+    if (entry.getExtensions(QNAME_SIMPLE_POLYGON).size() > 0) return true;
+    if (entry.getExtensions(QNAME_WHERE).size() > 0) return true;
+    if (entry.getExtensions(QNAME_W3C_LAT).size() > 0 && 
+        entry.getExtensions(QNAME_W3C_LONG).size() > 0) return true;
+    return false;
+  }
+  
+  public static Iterator<Position> listPositions(Entry entry) {
+    return _getPositions(entry).iterator();
+  }
+  
+  public static Position[] getPositions(Entry entry) {
+    List<Position> positions = _getPositions(entry);
+    return positions.toArray(new Position[positions.size()]);
+  }
+  
+  private static void getSimplePosition(Entry entry, List<Position> list) {
+    List<Element> elements = entry.getExtensions(SIMPLE_GEO_NS);
+    for (Element element : elements) {
+      Position pos = null;
+      QName qname = element.getQName();
+      String text = element.getText();
+      if (text != null) {
+        if (qname.equals(QNAME_SIMPLE_POINT)) {
+          pos = new Point(text.trim());
+        } else if (qname.equals(QNAME_SIMPLE_LINE)) {
+          pos = new Line(text.trim());
+        } else if (qname.equals(QNAME_SIMPLE_BOX)) {
+          pos = new Box(text.trim());
+        } else if (qname.equals(QNAME_SIMPLE_POLYGON)) {
+          pos = new Polygon(text.trim());
+        }
+      }
+      if (pos != null) {
+        getPositionAttributes(element, pos);
+        list.add(pos);
+      }
+    }
+  }
+  
+  private static void getGMLPosition(Entry entry, List<Position> list) {
+    List<ExtensibleElement> elements = entry.getExtensions(QNAME_WHERE);
+    for (ExtensibleElement where : elements) {
+      Position pos = null;
+      List<ExtensibleElement> children = where.getElements();
+      for (ExtensibleElement element : children) {
+        QName qname = element.getQName();
+        if (qname.equals(QNAME_GML_POINT)) {
+          element = traverse(
+            element, 
+            QNAME_GML_POS);
+          if (element != null) {
+            String text = element.getText();
+            if (text != null) {
+              pos = new Point(text.trim());
+            }
+          }
+        } else if (qname.equals(QNAME_GML_LINESTRING)) {
+          element = traverse(
+            element,
+            QNAME_GML_POSLIST);
+          if (element != null) {
+            String text = element.getText();
+            if (text != null) {
+              pos = new Line(text.trim());
+            }
+          }
+        } else if (qname.equals(QNAME_GML_POLYGON)) {
+          element = traverse(
+            element, 
+            QNAME_GML_EXTERIOR, 
+            QNAME_GML_LINEARRING, 
+            QNAME_GML_POSLIST);
+          if (element != null) {
+            String text = element.getText();
+            if (text != null) {
+              pos = new Polygon(text.trim());
+            }
+          }
+        } else if (qname.equals(QNAME_GML_ENVELOPE)) {
+          String lc = element.getSimpleExtension(QNAME_GML_LOWERCORNER);
+          String uc = element.getSimpleExtension(QNAME_GML_UPPERCORNER);
+          if (lc != null && uc != null) {
+            Coordinate c1 = new Coordinate(lc);
+            Coordinate c2 = new Coordinate(uc);
+            pos = new Box(c1,c2);
+          }
+        }
+        if (pos != null) {
+          getPositionAttributes(element, pos);
+          list.add(pos);
+        }
+      }
+    }
+  }
+  
+  private static ExtensibleElement traverse(ExtensibleElement element, QName... qnames) {
+    for (QName qname : qnames) {
+      element = element.getExtension(qname);
+      if (element == null) break;
+    }
+    return element;
+  }
+  
+  private static void getPositionAttributes(Element pos, Position position) {
+    if (position != null) {
+      String featuretypetag = pos.getAttributeValue("featuretypetag");
+      String relationshiptag = pos.getAttributeValue("relationshiptag");
+      String elevation = pos.getAttributeValue("elev");
+      String floor = pos.getAttributeValue("floor");
+      String radius = pos.getAttributeValue("radius");
+      if (featuretypetag != null)
+        position.setFeatureTypeTag(featuretypetag);
+      if (featuretypetag != null)
+        position.setRelationshipTag(relationshiptag);
+      if (elevation != null)
+        position.setElevation(Double.valueOf(elevation));
+      if (floor != null)
+        position.setFloor(Double.valueOf(floor));
+      if (radius != null)
+        position.setRadius(Double.valueOf(radius));
+    }
+  }
+  
+  private static void getW3CPosition(Entry entry, List<Position> list) {
+    String slat = entry.getSimpleExtension(QNAME_W3C_LAT);
+    String slong = entry.getSimpleExtension(QNAME_W3C_LONG);
+    if (slat != null && slong != null) {
+      Point point = new Point(slat.trim() + " " + slong.trim());
+      list.add(point);
+    }
+  }
+}
\ No newline at end of file

Added: incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Line.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Line.java?view=auto&rev=560436
==============================================================================
--- incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Line.java
(added)
+++ incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Line.java
Fri Jul 27 18:34:29 2007
@@ -0,0 +1,77 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.ext.geo;
+
+public class Line 
+  extends Multiple {
+  
+  private static final long serialVersionUID = 2852975001208906285L;
+
+  public Line() {
+  }
+
+  public Line(Multiple multiple) {
+    super(multiple);
+    verify();
+  }
+  
+  public Line(Point point) {
+    super(point);
+    verify();
+  }
+  
+  public Line(Coordinate... coordinates) {
+    super(coordinates);
+    verify();
+  }
+
+  public Line(Coordinates coordinates) {
+    super(coordinates);
+    verify();
+  }
+
+  public Line(String value) {
+    super(value);
+    verify();
+  }
+
+  public Line(Multiple... multiples) {
+    super(multiples);
+    verify();
+  }
+
+  public Line(Point... points) {
+    super(points);
+    verify();
+  }
+
+  public Line(double... values) {
+    super(values);
+    verify();
+  }
+
+  @Override
+  public void setCoordinates(Coordinates coordinates) {
+    super.setCoordinates(coordinates);
+    verify();
+  }
+  
+  public void verify() {
+    super.verify179Rule();
+  }
+}

Added: incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Multiple.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Multiple.java?view=auto&rev=560436
==============================================================================
--- incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Multiple.java
(added)
+++ incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Multiple.java
Fri Jul 27 18:34:29 2007
@@ -0,0 +1,125 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.ext.geo;
+
+import java.util.Iterator;
+
+public abstract class Multiple 
+  extends Position 
+  implements Iterable<Coordinate> {
+
+  protected Coordinates coordinates;
+  
+  public Multiple() {}
+  
+  public Multiple(Multiple... multiples) {
+    this.coordinates = new Coordinates();
+    for (Multiple m : multiples) 
+      coordinates.add(m.getCoordinates());
+  }
+
+  public Multiple(Multiple multiple) {
+    this(multiple.getCoordinates().clone());
+  }
+  
+  public Multiple(Point point) {
+    this(point.getCoordinate().clone());
+  }
+  
+  public Multiple(Point... points) {
+    this.coordinates = new Coordinates();
+    for (Point p : points) 
+      coordinates.add(p.getCoordinate());
+  }
+  
+  public Multiple(Coordinates coordinates) {
+    this.coordinates = coordinates;
+  }
+
+  public Multiple(Coordinate... coordinates) {
+    this.coordinates = new Coordinates(coordinates);
+  }
+  
+  public Multiple(String value) {
+    this.coordinates = new Coordinates(value);
+  }
+  
+  public Multiple(double... values) {
+    this.coordinates = new Coordinates();
+    for (int n = 0; n < values.length; n = n + 2) {
+      Coordinate c = new Coordinate(values[n],values[n+1]);
+      this.coordinates.add(c);
+    }
+  }
+  
+  public Coordinates getCoordinates() {
+    return coordinates;
+  }
+
+  public void setCoordinates(Coordinates coordinates) {
+    this.coordinates = coordinates;
+  }
+
+  public Iterator<Coordinate> iterator() {
+    return coordinates.iterator();
+  }
+
+  @Override
+  public int hashCode() {
+    final int PRIME = 31;
+    int result = super.hashCode();
+    result = PRIME * result + ((coordinates == null) ? 0 : coordinates.hashCode());
+    return result;
+  }
+
+  @Override
+  public boolean equals(Object obj) {
+    if (this == obj)
+      return true;
+    if (!super.equals(obj))
+      return false;
+    if (getClass() != obj.getClass())
+      return false;
+    final Multiple other = (Multiple) obj;
+    if (coordinates == null) {
+      if (other.coordinates != null)
+        return false;
+    } else if (!coordinates.equals(other.coordinates))
+      return false;
+    return true;
+  }
+  
+  public int compareTo(Position o) {
+    if (o == null || !this.getClass().isInstance(o) || equals(o)) return 0;
+    return coordinates.compareTo(((Multiple)o).coordinates);
+  }
+
+  protected void verify179Rule() {
+    for (Coordinate c1 : getCoordinates()) {
+      for (Coordinate c2 : getCoordinates()) {
+        check179(c1.getLatitude(),c2.getLatitude());
+        check179(c1.getLongitude(),c2.getLongitude());
+      }
+    }
+  }
+  
+  private void check179(double d1, double d2) {
+    if (Math.max(d1, d2)-Math.min(d1,d2) > 179) 
+      throw new RuntimeException("Values are greater than 179 degrees");
+  }
+}

Added: incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Point.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Point.java?view=auto&rev=560436
==============================================================================
--- incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Point.java
(added)
+++ incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Point.java
Fri Jul 27 18:34:29 2007
@@ -0,0 +1,91 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.ext.geo;
+
+public class Point 
+  extends Position {
+
+  private static final long serialVersionUID = 7540202474168797239L;
+  
+  private Coordinate coordinate;
+
+  public Point() {}
+  
+  public Point(Point point) {
+    this.coordinate = point.getCoordinate().clone();
+  }
+  
+  public Point(Coordinate coordinate) {
+    this.coordinate = coordinate;
+  }
+  
+  public Point(double latitude, double longitude) {
+    this.coordinate = new Coordinate(latitude,longitude);
+  }
+
+  public Point(String value) {
+    this.coordinate = new Coordinate(value);
+  }
+
+  public Coordinate getCoordinate() {
+    return coordinate;
+  }
+
+  public void setCoordinate(Coordinate coordinate) {
+    this.coordinate = coordinate;
+  }
+  
+  public void setCoordinate(double latitude, double longitude) {
+    this.coordinate = new Coordinate(latitude,longitude);
+  }
+  
+  public void setCoordinate(String value) {
+    this.coordinate = new Coordinate(value);
+  }
+
+  @Override
+  public int hashCode() {
+    final int PRIME = 31;
+    int result = super.hashCode();
+    result = PRIME * result + ((coordinate == null) ? 0 : coordinate.hashCode());
+    return result;
+  }
+
+  @Override
+  public boolean equals(Object obj) {
+    if (this == obj)
+      return true;
+    if (!super.equals(obj))
+      return false;
+    if (getClass() != obj.getClass())
+      return false;
+    final Point other = (Point) obj;
+    if (coordinate == null) {
+      if (other.coordinate != null)
+        return false;
+    } else if (!coordinate.equals(other.coordinate))
+      return false;
+    return true;
+  }
+
+  public int compareTo(Position o) {
+    if (o == null || !(o instanceof Point) || equals(o)) return 0;
+    return coordinate.compareTo(((Point)o).coordinate);
+  }
+  
+}

Added: incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Polygon.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Polygon.java?view=auto&rev=560436
==============================================================================
--- incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Polygon.java
(added)
+++ incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Polygon.java
Fri Jul 27 18:34:29 2007
@@ -0,0 +1,78 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.ext.geo;
+
+public class Polygon 
+  extends Multiple {
+
+  private static final long serialVersionUID = 5387230171535985909L;
+
+  public Polygon() {
+    super();
+  }
+
+  public Polygon(Multiple multiple) {
+    super(multiple);
+    verify();
+  }
+  
+  public Polygon(Point point) {
+    super(point);
+    verify();
+  }
+  
+  public Polygon(Coordinate... coordinates) {
+    super(coordinates);
+    verify();
+  }
+
+  public Polygon(Coordinates coordinates) {
+    super(coordinates);
+    verify();
+  }
+
+  public Polygon(String value) {
+    super(value);
+    verify();
+  }
+
+  public Polygon(Multiple... multiples) {
+    super(multiples);
+    verify();
+  }
+
+  public Polygon(Point... points) {
+    super(points);
+    verify();
+  }
+
+  public Polygon(double... values) {
+    super(values);
+    verify();
+  }
+  
+  @Override
+  public void setCoordinates(Coordinates coordinates) {
+    super.setCoordinates(coordinates);
+    verify();
+  }
+
+  public void verify() {
+    super.verify179Rule();
+  }
+}

Added: incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Position.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Position.java?view=auto&rev=560436
==============================================================================
--- incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Position.java
(added)
+++ incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/geo/Position.java
Fri Jul 27 18:34:29 2007
@@ -0,0 +1,132 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.ext.geo;
+
+import java.io.Serializable;
+
+public abstract class Position 
+  implements Serializable,
+             Cloneable,
+             Comparable<Position> {
+
+  public static final String DEFAULT_FEATURE_TYPE_TAG = "location";
+  public static final String DEFAULT_RELATIONSHIP_TAG = "is-located-at";
+  
+  protected String featureTypeTag;
+  protected String relationshipTag;
+  protected Double elevation;
+  protected Double floor;
+  protected Double radius;
+  
+  public Double getElevation() {
+    return elevation;
+  }
+  public void setElevation(Double elevation) {
+    this.elevation = elevation;
+  }
+  public String getFeatureTypeTag() {
+    return featureTypeTag;
+  }
+  public void setFeatureTypeTag(String featureTypeTag) {
+    this.featureTypeTag = featureTypeTag;
+  }
+  public Double getFloor() {
+    return floor;
+  }
+  public void setFloor(Double floor) {
+    this.floor = floor;
+  }
+  public Double getRadius() {
+    return radius;
+  }
+  public void setRadius(Double radius) {
+    this.radius = radius;
+  }
+  public String getRelationshipTag() {
+    return relationshipTag;
+  }
+  public void setRelationshipTag(String relationshipTag) {
+    this.relationshipTag = relationshipTag;
+  }
+  
+  @Override
+  public int hashCode() {
+    final int PRIME = 31;
+    //int result = super.hashCode();
+    int result = this.getClass().hashCode();
+    result = PRIME * result + ((elevation == null) ? 0 : elevation.hashCode());
+    result = PRIME * result + ((featureTypeTag == null) ? DEFAULT_FEATURE_TYPE_TAG.hashCode()
: featureTypeTag.hashCode());
+    result = PRIME * result + ((floor == null) ? 0 : floor.hashCode());
+    result = PRIME * result + ((radius == null) ? 0 : radius.hashCode());
+    result = PRIME * result + ((relationshipTag == null) ? DEFAULT_RELATIONSHIP_TAG.hashCode()
: relationshipTag.hashCode());
+    return result;
+  }
+  @Override
+  public boolean equals(Object obj) {
+    if (this == obj)
+      return true;
+    if (getClass() != obj.getClass())
+      return false;
+    final Position other = (Position) obj;
+    if (elevation == null) {
+      if (other.elevation != null)
+        return false;
+    } else if (!elevation.equals(other.elevation))
+      return false;
+    if (featureTypeTag == null) {
+      if (other.featureTypeTag != null && 
+          !other.featureTypeTag.equalsIgnoreCase(DEFAULT_FEATURE_TYPE_TAG))
+        return false;
+    } else {
+      String s = other.featureTypeTag != null ? 
+        other.featureTypeTag : DEFAULT_FEATURE_TYPE_TAG;
+      if (!featureTypeTag.equalsIgnoreCase(s))
+        return false;
+    }
+    if (floor == null) {
+      if (other.floor != null)
+        return false;
+    } else if (!floor.equals(other.floor))
+      return false;
+    if (radius == null) {
+      if (other.radius != null)
+        return false;
+    } else if (!radius.equals(other.radius))
+      return false;
+    if (relationshipTag == null) {
+      if (other.relationshipTag != null && 
+          !other.relationshipTag.equalsIgnoreCase(DEFAULT_RELATIONSHIP_TAG))
+        return false;
+    } else {
+      String s = other.relationshipTag != null ? 
+          other.relationshipTag : DEFAULT_RELATIONSHIP_TAG;
+      if (!relationshipTag.equalsIgnoreCase(s))
+        return false;
+    }
+    return true;
+  }
+  
+  @SuppressWarnings("unchecked")
+  public <T extends Position>T clone() {
+    try {
+      return (T)super.clone();
+    } catch (CloneNotSupportedException e) {
+      return null; // should never happen
+    }
+  }
+}



Mime
View raw message