abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r531297 - in /incubator/abdera/java/trunk: core/src/main/java/org/apache/abdera/model/ parser/src/main/java/org/apache/abdera/parser/stax/
Date Sun, 22 Apr 2007 22:49:52 GMT
Author: jmsnell
Date: Sun Apr 22 15:49:50 2007
New Revision: 531297

URL: http://svn.apache.org/viewvc?view=rev&rev=531297
Log:
 * Additional modifications supporting the new accept element definition.
 * Utility methods to make it easier to work with entry and null accepts
 * A utility method for sorting entries by app:edited
 * A utility method for creating an app:control element on demand
 * Edit text and content elements in place rather than discarding and recreating them
 * Smarter declaration of namespaces when dealing with extensions

Modified:
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Collection.java
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Entry.java
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Feed.java
    incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMCollection.java
    incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMElement.java
    incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMEntry.java
    incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMExtensibleElement.java
    incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMFeed.java

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Collection.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Collection.java?view=diff&rev=531297&r1=531296&r2=531297
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Collection.java
(original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Collection.java
Sun Apr 22 15:49:50 2007
@@ -124,6 +124,39 @@
   boolean accepts(String mediaType);
   
   /**
+   * Returns true if the collections accepts Atom entry documents 
+   * (equivalent to calling accepts("application/atom+xml;type=entry");)
+   */
+  boolean acceptsEntry();
+  
+  /**
+   * Sets the appropriate accept element to indicate that entries are accepted
+   * (equivalent to calling setAccept("application/atom+xml;type=entry");)
+   */
+  void setAcceptsEntry();
+  
+  /**
+   * Sets the collection so that nothing is accepted
+   * (equivalent to calling setAccept(""); )
+   */
+  void setAcceptsNothing();
+  
+  /**
+   * Adds a new accept element to the collection
+   */
+  void addAccepts(String mediaRange);
+  
+  /**
+   * Adds new accept elements to the collection
+   */
+  void addAccepts(String... mediaRanges);
+  
+  /**
+   * Same as setAcceptsEntry except the existing accepts are not discarded
+   */
+  void addAcceptsEntry();
+  
+  /**
    * Returns true if the collection accepts the given media-type
    * @param mediaType The media-type to check
    * @return True if the media-type is acceptable

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Entry.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Entry.java?view=diff&rev=531297&r1=531296&r2=531297
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Entry.java (original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Entry.java Sun
Apr 22 15:49:50 2007
@@ -785,6 +785,13 @@
   DateTime setEdited(String value);
   
   /**
+   * Returns this entries Atom Publishing Protocol control element.
+   * A new control element will be created if one currently does not exist
+   * @return The app:control element
+   */
+  Control getControl(boolean create);
+  
+  /**
    * Returns this entries Atom Publishing Protocol control element
    * @return The app:control element
    */

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Feed.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Feed.java?view=diff&rev=531297&r1=531296&r2=531297
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Feed.java (original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/model/Feed.java Sun Apr
22 15:49:50 2007
@@ -142,6 +142,11 @@
   void sortEntriesByUpdated(boolean new_first);
   
   /**
+   * Sorts entries by the app:edited property. if app:edited is null, use app:updated
+   */
+  void sortEntriesByEdited(boolean new_first);
+  
+  /**
    * Sorts entries using the given comparator
    * @param comparator Sort the entries using the comparator
    */

Modified: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMCollection.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMCollection.java?view=diff&rev=531297&r1=531296&r2=531297
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMCollection.java
(original)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMCollection.java
Sun Apr 22 15:49:50 2007
@@ -162,19 +162,58 @@
     }
   }
 
+  public void setAccept(String mediaRange) {
+    setAccept(new String[] {mediaRange});
+  }
+  
   public void setAccept(String... mediaRanges) {
-    if (mediaRanges != null || mediaRanges.length == 0) {
-      mediaRanges = MimeTypeHelper.condense(mediaRanges);
-      StringBuffer value = new StringBuffer();
-      for (String type : mediaRanges) {
-        if (value.length() > 0)
-          value.append(",");
-        value.append(type);
+    if (mediaRanges != null && mediaRanges.length > 0) {
+      _removeChildren(ACCEPT, true);
+      if (mediaRanges.length == 1 && mediaRanges[0].equals("")) {
+        addExtension(ACCEPT);
+      } else {
+        mediaRanges = MimeTypeHelper.condense(mediaRanges);
+        for (String type : mediaRanges) {
+          try {
+            addSimpleExtension(ACCEPT, new MimeType(type).toString());
+          } catch (Exception e) {}
+        }
       }
-      addSimpleExtension(ACCEPT, value.toString());
     } else {
       _removeChildren(ACCEPT, true);
     }
+  }
+  
+  public void addAccepts(String mediaRange) {
+    addAccepts(new String[] {mediaRange});
+  }
+  
+  public void addAccepts(String... mediaRanges) {
+    if (mediaRanges != null) {
+      for (String type : mediaRanges) {
+        if (!accepts(type)) {
+          try {
+            addSimpleExtension(ACCEPT, new MimeType(type).toString());
+          } catch (Exception e) {}
+        }
+      }
+    }
+  }
+  
+  public void addAcceptsEntry() {
+    addAccepts("application/atom+xml;type=entry");
+  }
+  
+  public void setAcceptsEntry() {
+    setAccept("application/atom+xml;type=entry");
+  }
+  
+  public void setAcceptsNothing() {
+    setAccept("");
+  }
+  
+  public boolean acceptsEntry() {
+    return accepts("application/atom+xml;type=entry");
   }
 
   public boolean accepts(String mediaType) {

Modified: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMElement.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMElement.java?view=diff&rev=531297&r1=531296&r2=531297
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMElement.java
(original)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMElement.java
Sun Apr 22 15:49:50 2007
@@ -403,10 +403,6 @@
 
   protected <T extends Text>void setTextElement(QName qname, T text, boolean many)
{
     if (text != null) {
-      if (!many) {
-        OMElement el = getFirstChildWithName(qname);
-        if (el != null) el.discard();
-      }
       _setChild(qname, (OMElement)text);
     } else _removeChildren(qname, false);
   }
@@ -672,6 +668,28 @@
   }
   
   public void declareNS(String uri, String prefix) {
-    super.declareNamespace(uri,prefix);
+    if (!isDeclared(uri,prefix)) {
+      super.declareNamespace(uri,prefix);
+    }
+  }
+  
+  protected boolean isDeclared(String ns, String prefix) {
+    for (Iterator i = this.getAllDeclaredNamespaces(); i.hasNext();) {
+      OMNamespace omn = (OMNamespace) i.next();
+      if (omn.getNamespaceURI().equals(ns) && 
+           (omn.getPrefix() != null && 
+             omn.getPrefix().equals(prefix))) 
+               return true;
+    }
+    Base parent = this.getParentElement();
+    if (parent != null && parent instanceof FOMElement) {
+      return ((FOMElement)parent).isDeclared(ns, prefix);
+    } else return false;
+  }
+  
+  protected void declareIfNecessary(String ns, String prefix) {
+    if (prefix != null && !isDeclared(ns, prefix)) {
+      declareNS(ns,prefix);
+    }
   }
 }

Modified: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMEntry.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMEntry.java?view=diff&rev=531297&r1=531296&r2=531297
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMEntry.java
(original)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMEntry.java
Sun Apr 22 15:49:50 2007
@@ -179,8 +179,6 @@
 
   public void setContentElement(Content content) {
     if (content != null) {
-      Content element = getContentElement();
-      if (element != null) element.discard();
       _setChild(CONTENT, (OMElement)content);
     } else {
       _removeChildren(CONTENT, false);
@@ -692,6 +690,15 @@
   
   public DateTime setEdited(String value) {
     return setUpdated((value != null) ? AtomDate.valueOf(value) : null);
+  }
+
+  public Control getControl(boolean create) {
+    Control control = getControl();
+    if (control == null && create) {
+      control = getFactory().newControl();
+      setControl(control);
+    }
+    return control;
   }
   
   public Control getControl() {

Modified: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMExtensibleElement.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMExtensibleElement.java?view=diff&rev=531297&r1=531296&r2=531297
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMExtensibleElement.java
(original)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMExtensibleElement.java
Sun Apr 22 15:49:50 2007
@@ -102,9 +102,7 @@
     }
     QName qname = extension.getQName();
     String prefix = qname.getPrefix();
-    if (prefix != null) {
-      declareNS(qname.getNamespaceURI(), prefix);
-    }
+    declareIfNecessary(qname.getNamespaceURI(), prefix);
     addChild((OMElement)extension);
   }
   
@@ -112,28 +110,22 @@
   public <T extends Element>T addExtension(QName qname) {
     FOMFactory fomfactory = (FOMFactory) factory;
     String prefix = qname.getPrefix();
-    if (prefix != null) {
-      declareNS(qname.getNamespaceURI(),prefix);
-    }
+    declareIfNecessary(qname.getNamespaceURI(), prefix);
     return (T)fomfactory.newExtensionElement(qname, this);
   }
   
   @SuppressWarnings("unchecked")
   public <T extends Element>T addExtension(String namespace, String localpart, String
prefix) {
-    if (prefix != null) {
-      declareNS(namespace, prefix);
-    }
+    declareIfNecessary(namespace, prefix);
     return (T)addExtension(new QName(namespace, localpart, prefix));
   }
-
+  
   public Element addSimpleExtension(QName qname, String value) {
     FOMFactory fomfactory = (FOMFactory) factory;
     Element el = fomfactory.newElement(qname, this);
     el.setText(value);
     String prefix = qname.getPrefix();
-    if (prefix != null) {
-      declareNS(qname.getNamespaceURI(),prefix);
-    }
+    declareIfNecessary(qname.getNamespaceURI(),prefix);
     return el;
   }
   
@@ -142,9 +134,7 @@
     String localPart, 
     String prefix, 
     String value) {
-      if (prefix != null) {
-        declareNS(namespace,prefix);
-      }
+      declareIfNecessary(namespace,prefix);
       return addSimpleExtension(
         new QName(
           namespace, 

Modified: incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMFeed.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMFeed.java?view=diff&rev=531297&r1=531296&r2=531297
==============================================================================
--- incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMFeed.java
(original)
+++ incubator/abdera/java/trunk/parser/src/main/java/org/apache/abdera/parser/stax/FOMFeed.java
Sun Apr 22 15:49:50 2007
@@ -151,6 +151,10 @@
     sortEntries(new UpdatedComparator(new_first));
   }
   
+  public void sortEntriesByEdited(boolean new_first) {
+    sortEntries(new EditedComparator(new_first));
+  }
+  
   public void sortEntries(Comparator<Entry> comparator) {
     if (comparator == null) return;
     List<Entry> entries = this.getEntries();
@@ -159,6 +163,24 @@
     for (Entry e: entries) { e.discard(); }
     for (Entry e: a) { addEntry(e); }
   }
+  
+  private static class EditedComparator implements Comparator<Entry> {
+    private boolean new_first = true;
+    EditedComparator(boolean new_first) {
+      this.new_first = new_first;
+    }
+    public int compare(Entry o1, Entry o2) {
+      Date d1 = o1.getEdited();
+      Date d2 = o2.getEdited();
+      if (d1 == null) d1 = o1.getUpdated();
+      if (d2 == null) d2 = o2.getUpdated();
+      if (d1 == null && d2 == null) return 0;
+      if (d1 == null && d2 != null) return -1;
+      if (d1 != null && d2 == null) return 1;
+      int r = d1.compareTo(d2);
+      return (new_first) ? -r : r;
+    }
+  };
   
   private static class UpdatedComparator implements Comparator<Entry> {
     private boolean new_first = true;



Mime
View raw message