abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r1174962 - in /abdera/abdera2: core/src/main/java/org/apache/abdera2/parser/axiom/ ext/src/main/java/org/apache/abdera2/ext/activities/
Date Fri, 23 Sep 2011 19:18:25 GMT
Author: jmsnell
Date: Fri Sep 23 19:18:25 2011
New Revision: 1174962

URL: http://svn.apache.org/viewvc?rev=1174962&view=rev
Log:
bug fix + rudimentary Activity Stream to Feed conversion

Added:
    abdera/abdera2/ext/src/main/java/org/apache/abdera2/ext/activities/ActivityToFeedConverter.java
  (with props)
Modified:
    abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/StaxStreamWriter.java
    abdera/abdera2/ext/src/main/java/org/apache/abdera2/ext/activities/FeedToActivityConverter.java

Modified: abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/StaxStreamWriter.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/StaxStreamWriter.java?rev=1174962&r1=1174961&r2=1174962&view=diff
==============================================================================
--- abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/StaxStreamWriter.java
(original)
+++ abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/StaxStreamWriter.java
Fri Sep 23 19:18:25 2011
@@ -120,7 +120,8 @@ public class StaxStreamWriter extends Ab
 
     public StreamWriter startDocument(String xmlversion, String charset) {
         try {
-            writer.writeStartDocument(xmlversion, charset);
+            
+            writer.writeStartDocument(charset,xmlversion);
         } catch (XMLStreamException e) {
             throw new RuntimeException(e);
         }

Added: abdera/abdera2/ext/src/main/java/org/apache/abdera2/ext/activities/ActivityToFeedConverter.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/ext/src/main/java/org/apache/abdera2/ext/activities/ActivityToFeedConverter.java?rev=1174962&view=auto
==============================================================================
--- abdera/abdera2/ext/src/main/java/org/apache/abdera2/ext/activities/ActivityToFeedConverter.java
(added)
+++ abdera/abdera2/ext/src/main/java/org/apache/abdera2/ext/activities/ActivityToFeedConverter.java
Fri Sep 23 19:18:25 2011
@@ -0,0 +1,172 @@
+package org.apache.abdera2.ext.activities;
+
+import java.util.Date;
+
+import javax.xml.namespace.QName;
+
+import org.apache.abdera2.activities.model.ASObject;
+import org.apache.abdera2.activities.model.Activity;
+import org.apache.abdera2.activities.model.Collection;
+import org.apache.abdera2.common.xml.XMLVersion;
+import org.apache.abdera2.model.Text;
+import org.apache.abdera2.writer.StreamWriter;
+
+public class ActivityToFeedConverter {
+
+  protected void id(ASObject obj, StreamWriter writer) {
+    String id = obj.getId();
+    if (id != null) writer.writeId(id);
+    else writer.writeId();
+  }
+  
+  protected void title(ASObject obj, StreamWriter writer) {
+    writer.writeTitle(
+      titleType(obj),
+      obj.has("title") ? 
+        (String)obj.getProperty("title") :
+        obj.has("displayName") ? 
+          obj.getDisplayName() :
+          "");
+  }
+  
+  protected Text.Type titleType(ASObject obj) {
+    return Text.Type.HTML;
+  }
+  
+  protected void summary(ASObject obj, StreamWriter writer) {
+    writer.writeSummary(
+      summaryType(obj),
+      obj.has("summary") ? 
+        (String)obj.getSummary() :
+        obj.has("displayName") ? 
+          obj.getDisplayName() :
+          "");
+  }
+  
+  @SuppressWarnings("unchecked")
+  protected void actor(Activity activity, StreamWriter writer) {
+    ASObject obj = activity.getActor();
+    if (obj instanceof Collection) {
+      Collection<ASObject> col = 
+        (Collection<ASObject>)obj;
+      for (ASObject item : col.getItems())
+        actor(item,writer);
+    } else {
+      actor((ASObject)obj,writer);
+    }
+  }
+  
+  @SuppressWarnings("unchecked")
+  protected void actor(ASObject obj, StreamWriter writer) {
+    writer.startAuthor();
+    if (obj.getDisplayName() != null)
+      writer.writePersonName(obj.getDisplayName());
+    if (obj.getUrl() != null)
+      writer.writePersonUri(obj.getUrl().toString());
+    if (obj.has("emails")) {
+      Object emails = obj.getProperty("emails");
+      if (emails instanceof Iterable) {
+        Iterable<String> i = (Iterable<String>) emails;
+        for (String email : i)
+          writer.writePersonEmail(email);
+      } else {
+        writer.writePersonEmail(emails.toString()); // take a chance
+      }
+    }
+    writer.endAuthor();
+  }
+
+  protected void object(QName qname, ASObject object, StreamWriter writer) {
+    if (object != null) {
+      writer.startElement(qname);
+      if (object.getObjectType() != null)
+        writer.startElement(FeedToActivityConverter.OBJECTTYPE)
+              .writeElementText(object.getObjectType())
+              .endElement();
+      if (object.getId() != null)
+        writer.writeId(object.getId());
+      if (object.getDisplayName() != null)
+        writer.writeTitle(object.getDisplayName());
+      if (object.getSummary() != null)
+        writer.writeSummary(object.getSummary());
+      if (object.getUrl() != null)
+        writer.writeLink(object.getUrl().toString());
+      if (object.getImage() != null) {
+        writer.writeLink(object.getImage().getUrl().toString(), "preview");
+      }
+      writer.endElement();
+    }
+  }
+  
+  protected void object(Activity activity, StreamWriter writer) {
+    ASObject object = activity.getObject();
+    object(FeedToActivityConverter.OBJECT,object,writer);
+  }
+  
+  protected void target(Activity activity, StreamWriter writer) {
+    ASObject object = activity.getTarget();
+    object(FeedToActivityConverter.TARGET,object,writer);
+  }
+  
+  protected Text.Type summaryType(ASObject obj) {
+    return Text.Type.HTML;
+  }
+  
+  protected void writeFeedHeader(
+    Collection<Activity> stream, 
+    StreamWriter writer) {
+    id(stream,writer);
+    title(stream,writer);
+    Date updated = stream.getUpdated();
+    if (updated != null) 
+      writer.writeUpdated(updated);    
+  }
+  
+  private void writeEntry(
+    Activity activity,
+    StreamWriter writer) {
+      writer.startEntry();
+      writeEntryDetail(activity,writer);
+      writer.endEntry();
+  }
+  
+  protected void writeEntryDetail(
+    Activity activity, 
+    StreamWriter writer) {
+      id(activity,writer);
+      title(activity,writer);
+      summary(activity,writer);
+      if (activity.getUpdated() != null)
+        writer.writeUpdated(activity.getUpdated());
+      else writer.writeUpdated(new Date());
+      if (activity.getPublished() != null)
+        writer.writePublished(activity.getPublished());
+      else writer.writePublished(new Date());
+      if (activity.getUrl() != null)
+        writer.writeLink(activity.getUrl().toString());
+      actor(activity,writer);
+      object(activity,writer);
+      target(activity,writer);
+  }
+  
+  public void convert(
+    Collection<Activity> stream, 
+    StreamWriter writer) {
+      writer.startDocument(XMLVersion.XML11, "UTF-8");
+      writer.startFeed();
+      writeFeedHeader(stream,writer);
+      for (Activity activity : stream.getItems())
+        writeEntry(activity,writer);
+      writer.endFeed();
+      writer.endDocument();
+  }
+  
+  public void convert(
+    Activity activity, 
+    StreamWriter writer) {
+      writer.startDocument(XMLVersion.XML11, "UTF-8");
+      writeEntry(activity,writer);
+      writer.endDocument();
+  }
+  
+}

Propchange: abdera/abdera2/ext/src/main/java/org/apache/abdera2/ext/activities/ActivityToFeedConverter.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: abdera/abdera2/ext/src/main/java/org/apache/abdera2/ext/activities/FeedToActivityConverter.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/ext/src/main/java/org/apache/abdera2/ext/activities/FeedToActivityConverter.java?rev=1174962&r1=1174961&r2=1174962&view=diff
==============================================================================
--- abdera/abdera2/ext/src/main/java/org/apache/abdera2/ext/activities/FeedToActivityConverter.java
(original)
+++ abdera/abdera2/ext/src/main/java/org/apache/abdera2/ext/activities/FeedToActivityConverter.java
Fri Sep 23 19:18:25 2011
@@ -47,6 +47,10 @@ public class FeedToActivityConverter {
     convert(feed,writer,null);
   }
   
+  public Activity convert(Entry entry) {
+    return item(entry);
+  }
+  
   public void convert(
     Feed feed, 
     CollectionWriter writer, 



Mime
View raw message