abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r589181 - in /incubator/abdera/java/trunk: examples/src/main/java/org/apache/abdera/examples/ext/ extensions/main/src/main/java/org/apache/abdera/ext/features/ extensions/main/src/test/java/org/apache/abdera/test/ext/features/
Date Sat, 27 Oct 2007 19:00:48 GMT
Author: jmsnell
Date: Sat Oct 27 12:00:47 2007
New Revision: 589181

URL: http://svn.apache.org/viewvc?rev=589181&view=rev
Log:
Updates the features impl to match the current draft of the spec

Added:
    incubator/abdera/java/trunk/extensions/main/src/main/java/org/apache/abdera/ext/features/Features.java
Modified:
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/ext/Features.java
    incubator/abdera/java/trunk/extensions/main/src/main/java/org/apache/abdera/ext/features/Feature.java
    incubator/abdera/java/trunk/extensions/main/src/main/java/org/apache/abdera/ext/features/FeaturesExtensionFactory.java
    incubator/abdera/java/trunk/extensions/main/src/main/java/org/apache/abdera/ext/features/FeaturesHelper.java
    incubator/abdera/java/trunk/extensions/main/src/test/java/org/apache/abdera/test/ext/features/FeatureTest.java

Modified: incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/ext/Features.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/ext/Features.java?rev=589181&r1=589180&r2=589181&view=diff
==============================================================================
--- incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/ext/Features.java
(original)
+++ incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/ext/Features.java
Sat Oct 27 12:00:47 2007
@@ -40,10 +40,11 @@
     Collection collection = workspace.addCollection("My collection", "foo");
     
     // Specify which features are supported by the collection
-    FeaturesHelper.addFeature(collection, FeaturesHelper.FEATURE_SUPPORTS_DRAFTS);
-    FeaturesHelper.addFeature(collection, FeaturesHelper.FEATURE_REQUIRES_TEXT_TEXT);
-    FeaturesHelper.addFeature(collection, FeaturesHelper.FEATURE_IGNORES_SLUG);
-    FeaturesHelper.addFeature(collection, FeaturesHelper.FEATURE_SUPPORTS_BIDI);
+    org.apache.abdera.ext.features.Features features = FeaturesHelper.addFeaturesElement(collection);
+    features.addFeature(FeaturesHelper.FEATURE_SUPPORTS_DRAFTS);
+    features.addFeature(FeaturesHelper.FEATURE_REQUIRES_TEXT_TEXT);
+    features.addFeature(FeaturesHelper.FEATURE_IGNORES_SLUG);
+    features.addFeature(FeaturesHelper.FEATURE_SUPPORTS_BIDI);
     
     // Get the support status of a specific feature
     System.out.println(FeaturesHelper.getFeatureStatus(collection, FeaturesHelper.FEATURE_SUPPORTS_DRAFTS));
@@ -53,9 +54,8 @@
     System.out.println(FeaturesHelper.getFeatureStatus(collection, FeaturesHelper.FEATURE_SUPPORTS_GEO));
     
     
-    Feature[] features = null;
-    features = FeaturesHelper.getFeatures(collection);
-    for (Feature feature : features) {
+    Feature[] fs = FeaturesHelper.getFeatures(collection);
+    for (Feature feature : fs) {
       System.out.println("\t" + feature.getRef());
     }
     
@@ -73,3 +73,4 @@
   }
   
 }
+

Modified: incubator/abdera/java/trunk/extensions/main/src/main/java/org/apache/abdera/ext/features/Feature.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/main/src/main/java/org/apache/abdera/ext/features/Feature.java?rev=589181&r1=589180&r2=589181&view=diff
==============================================================================
--- incubator/abdera/java/trunk/extensions/main/src/main/java/org/apache/abdera/ext/features/Feature.java
(original)
+++ incubator/abdera/java/trunk/extensions/main/src/main/java/org/apache/abdera/ext/features/Feature.java
Sat Oct 27 12:00:47 2007
@@ -17,12 +17,18 @@
 */
 package org.apache.abdera.ext.features;
 
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.activation.MimeType;
+import javax.activation.MimeTypeParseException;
 import javax.xml.namespace.QName;
 
 import org.apache.abdera.factory.Factory;
 import org.apache.abdera.i18n.iri.IRI;
 import org.apache.abdera.model.Element;
 import org.apache.abdera.model.ExtensibleElementWrapper;
+import org.apache.abdera.util.MimeTypeHelper;
 
 public class Feature 
   extends ExtensibleElementWrapper {
@@ -68,4 +74,32 @@
       removeAttribute(new QName("label"));
   }
   
+  
+  public void addType(String mediaRange) {
+    addType(new String[] {mediaRange});
+  }
+  
+  public void addType(String... mediaRanges) {
+    mediaRanges = MimeTypeHelper.condense(mediaRanges);
+    for (String mediaRange : mediaRanges) {
+      try {
+        addSimpleExtension(FeaturesHelper.TYPE, new MimeType(mediaRange).toString());
+      } catch (MimeTypeParseException e) {}
+    }
+  }
+  
+  public String[] getTypes() {
+    List<String> list = new ArrayList<String>();
+    for (Element type : getExtensions(FeaturesHelper.TYPE)) {
+      String value = type.getText();
+      if (value != null) {
+        value = value.trim();
+        try {
+          list.add(new MimeType(value).toString());
+        } catch (MimeTypeParseException e) {}
+      }
+    }
+    return list.toArray(new String[list.size()]);
+  }
+
 }

Added: incubator/abdera/java/trunk/extensions/main/src/main/java/org/apache/abdera/ext/features/Features.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/main/src/main/java/org/apache/abdera/ext/features/Features.java?rev=589181&view=auto
==============================================================================
--- incubator/abdera/java/trunk/extensions/main/src/main/java/org/apache/abdera/ext/features/Features.java
(added)
+++ incubator/abdera/java/trunk/extensions/main/src/main/java/org/apache/abdera/ext/features/Features.java
Sat Oct 27 12:00:47 2007
@@ -0,0 +1,97 @@
+/*
+* 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.features;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.xml.namespace.QName;
+
+import org.apache.abdera.factory.Factory;
+import org.apache.abdera.i18n.iri.IRI;
+import org.apache.abdera.model.Element;
+import org.apache.abdera.model.ExtensibleElementWrapper;
+import org.apache.abdera.util.URIHelper;
+
+public class Features 
+  extends ExtensibleElementWrapper {
+  
+  public Features(
+    Element internal) {
+      super(internal);
+  }
+  
+  public Features(
+    Factory factory, 
+    QName qname) {
+      super(factory, qname);
+  }
+
+  public IRI getResolvedHref() {
+    return URIHelper.resolve(getResolvedBaseUri(), getHref());
+  }
+  
+  public IRI getHref() {
+    String href = getAttributeValue("href");
+    return href != null ? new IRI(href) : null;
+  }
+  
+  public void setHref(String href) {
+    setAttributeValue("href", (new IRI(href)).toString());
+  }
+  
+  public String getName() {
+    return getAttributeValue("name");
+  }
+  
+  public void setName(String name) {
+    setAttributeValue("name",name);
+  }
+  
+  public void addFeature(Feature feature) {
+    addExtension(feature);
+  }
+  
+  public void addFeature(Feature... features) {
+    for (Feature feature : features) addFeature(feature);
+  }
+  
+  public Feature addFeature(String feature) {
+    Feature f = addExtension(FeaturesHelper.FEATURE);
+    f.setRef(feature);
+    return f;
+  }
+
+  public Feature addFeature(String feature, String href, String label) {
+    Feature f = addExtension(FeaturesHelper.FEATURE);
+    f.setRef(feature);
+    f.setHref(href);
+    f.setLabel(label);
+    return f;
+  }
+  
+  public Feature[] addFeatures(String... features) {
+    List<Feature> list = new ArrayList<Feature>();
+    for (String feature : features) list.add(addFeature(feature));
+    return list.toArray(new Feature[list.size()]);
+  }
+  
+  public List<Feature> getFeatures() {
+    return getExtensions(FeaturesHelper.FEATURE);
+  }
+}

Modified: incubator/abdera/java/trunk/extensions/main/src/main/java/org/apache/abdera/ext/features/FeaturesExtensionFactory.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/main/src/main/java/org/apache/abdera/ext/features/FeaturesExtensionFactory.java?rev=589181&r1=589180&r2=589181&view=diff
==============================================================================
--- incubator/abdera/java/trunk/extensions/main/src/main/java/org/apache/abdera/ext/features/FeaturesExtensionFactory.java
(original)
+++ incubator/abdera/java/trunk/extensions/main/src/main/java/org/apache/abdera/ext/features/FeaturesExtensionFactory.java
Sat Oct 27 12:00:47 2007
@@ -25,6 +25,7 @@
   public FeaturesExtensionFactory() {
     super(FeaturesHelper.FNS);
     addImpl(FeaturesHelper.FEATURE,Feature.class);
+    addImpl(FeaturesHelper.FEATURES,Features.class);
   }
   
 }

Modified: incubator/abdera/java/trunk/extensions/main/src/main/java/org/apache/abdera/ext/features/FeaturesHelper.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/main/src/main/java/org/apache/abdera/ext/features/FeaturesHelper.java?rev=589181&r1=589180&r2=589181&view=diff
==============================================================================
--- incubator/abdera/java/trunk/extensions/main/src/main/java/org/apache/abdera/ext/features/FeaturesHelper.java
(original)
+++ incubator/abdera/java/trunk/extensions/main/src/main/java/org/apache/abdera/ext/features/FeaturesHelper.java
Sat Oct 27 12:00:47 2007
@@ -18,19 +18,23 @@
 package org.apache.abdera.ext.features;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
+import java.util.Map;
+import java.util.WeakHashMap;
 
-import javax.activation.MimeType;
-import javax.activation.MimeTypeParseException;
 import javax.xml.namespace.QName;
 
+import org.apache.abdera.Abdera;
 import org.apache.abdera.factory.Factory;
-import org.apache.abdera.i18n.iri.IRI;
 import org.apache.abdera.model.Collection;
+import org.apache.abdera.model.Document;
 import org.apache.abdera.model.Element;
 import org.apache.abdera.model.Service;
 import org.apache.abdera.model.Workspace;
-import org.apache.abdera.util.MimeTypeHelper;
+import org.apache.abdera.protocol.Response.ResponseType;
+import org.apache.abdera.protocol.client.AbderaClient;
+import org.apache.abdera.protocol.client.ClientResponse;
 
 /**
  * Implementation of the current APP Features Draft
@@ -45,6 +49,7 @@
   
   public static final String FNS = "http://purl.org/atompub/features/1.0";
   public static final QName FEATURE = new QName(FNS, "feature","f");
+  public static final QName FEATURES = new QName(FNS, "features","f");
   public static final QName TYPE = new QName(FNS, "type", "f");
   
   private static final String FEATURE_BASE                   = "http://www.w3.org/2007/app/";
@@ -215,17 +220,73 @@
    * examples of the type of markup that would be removed include scripts and embed 
    */
   public static final String FEATURE_FILTERS_MARKUP = BLOG_FEATURE_BASE + "filtersUnsafeMarkup";
+    
+  private FeaturesHelper() {}
   
+  private static Map<String,Features> featuresCache = 
+    Collections.synchronizedMap(new WeakHashMap<String,Features>());
   
-  private FeaturesHelper() {}
+  private static Features getCachedFeatures(String iri) {
+    return featuresCache.get(iri);
+  }
+  
+  private static void setCachedFeatures(String iri, Features features) {
+    featuresCache.put(iri,features);
+  }
+  
+  public static void flushCachedFeatures() {
+    featuresCache.clear();
+  }
+  
+  public static Features newFeatures(Abdera abdera) {
+    Factory factory = abdera.getFactory();
+    Document<Features> doc = factory.newDocument();
+    Features features = factory.newElement(FEATURES,doc);
+    doc.setRoot(features);
+    return features;
+  }
+  
+  public static Features getFeaturesElement(Collection collection) {
+    return getFeaturesElement(collection,true);
+  }
+  
+  public static Features getFeaturesElement(Collection collection, boolean outofline) {
+    Features features = collection.getExtension(FEATURES);
+    if (features != null && outofline) {
+      if (features.getHref() != null) {
+        String iri = features.getResolvedHref().toASCIIString();
+        features = getCachedFeatures(iri);
+        if (features == null) {
+          Abdera abdera = collection.getFactory().getAbdera();
+          AbderaClient client = new AbderaClient(abdera);
+          ClientResponse resp = client.get(iri);
+          if(resp.getType() == ResponseType.SUCCESS) {
+            Document<Features> doc = resp.getDocument();
+            features = doc.getRoot();
+            setCachedFeatures(iri,features);
+          } else {
+            features = null;
+          }
+        }
+      }
+    }
+    return features;
+  }
+  
+  public static Feature getFeature(
+    Collection collection,
+    String feature) {
+      return getFeature(getFeaturesElement(collection), feature);
+  }
   
   /**
    * Returns the specified feature element or null
    */
   public static Feature getFeature(
-    Collection collection,
+    Features features,
     String feature) {
-      List<Element> list = collection.getExtensions(FEATURE);
+      if (features == null) return null;
+      List<Element> list = features.getExtensions(FEATURE);
       for (Element el : list) {
         if (el.getAttributeValue("ref").equals(feature))
           return (Feature) el;
@@ -234,66 +295,28 @@
   }
   
   public static Status getFeatureStatus(Collection collection, String feature) {
-    Feature f = getFeature(collection,feature);
-    return f != null ? Status.SPECIFIED : Status.UNSPECIFIED;
+    return getFeatureStatus(getFeaturesElement(collection), feature);
   }
   
-  public static Feature[] getFeatures(Collection collection) {    
-    List<Feature> features = collection.getExtensions(FEATURE);
-    return features.toArray(new Feature[features.size()]);
+  public static Status getFeatureStatus(Features features, String feature) {
+    if (features == null) return Status.UNSPECIFIED;
+    Feature f = getFeature(features,feature);
+    return f != null ? Status.SPECIFIED : Status.UNSPECIFIED;
   }
   
-  /**
-   * Add the specified features to the collection
-   */
-  public static Feature[] addFeatures(
-    Collection collection, 
-    String... features) {
-      List<Feature> list = new ArrayList<Feature>();
-      for (String feature : features)
-        list.add(addFeature(collection,feature));
-      return list.toArray(new Feature[list.size()]);
+  public static Feature[] getFeatures(Collection collection) {
+    Features features = getFeaturesElement(collection);
+    if (features == null) return null;
+    List<Feature> list = features.getExtensions(FEATURE);
+    return list.toArray(new Feature[list.size()]);
   }
   
-  /**
-   * Add the specified feature to the collection
-   * @param collection The collection
-   * @param feature The IRI of the feature to add 
-   */
-  public static Feature addFeature(
-    Collection collection, 
-    String feature) {
-      return addFeature(
-        collection, 
-        feature, 
-        null, null);
-  }
-    
-  /**
-   * Add the specified feature to the collection
-   * @param collection The collection
-   * @param feature The IRI of the feature to add 
-   * @param required True if the feature is required
-   * @param href An IRI pointing to a human readable resource describing the feature
-   * @param label A human readable label for the feature
-   */
-  public static Feature addFeature(
-    Collection collection, 
-    String feature,
-    String href,
-    String label) {
-    if (getFeature(collection, feature) != null) 
-      throw new IllegalArgumentException("Feature already specified");
-    Factory factory = collection.getFactory();
-    Feature el = 
-      (Feature)factory.newExtensionElement(
-        FeaturesHelper.FEATURE, collection);
-    collection.declareNS(FNS, "f");
-    el.setRef(new IRI(feature).toString());
-    if (href != null) el.setHref(new IRI(href).toString());
-    if (label != null) el.setLabel(label);
-    return el;
-  }
+  public static Features addFeaturesElement(Collection collection) {
+    if (getFeaturesElement(collection,false) != null) 
+      throw new IllegalArgumentException(
+        "A collection element can only contain one features element");
+    return collection.addExtension(FEATURES);
+  }  
   
   /**
    * Select a Collection from the service document
@@ -339,32 +362,5 @@
     }
     return list.toArray(new Collection[list.size()]);
   }
-  
-  public static void addType(Feature feature, String mediaRange) {
-    addType(feature, new String[] {mediaRange});
-  }
-  
-  public static void addType(Feature feature, String... mediaRanges) {
-    mediaRanges = MimeTypeHelper.condense(mediaRanges);
-    for (String mediaRange : mediaRanges) {
-      try {
-        feature.addSimpleExtension(TYPE, new MimeType(mediaRange).toString());
-      } catch (MimeTypeParseException e) {}
-    }
-  }
-  
-  public static String[] getTypes(Feature feature) {
-    List<String> list = new ArrayList<String>();
-    for (Element type : feature.getExtensions(TYPE)) {
-      String value = type.getText();
-      if (value != null) {
-        value = value.trim();
-        try {
-          list.add(new MimeType(value).toString());
-        } catch (MimeTypeParseException e) {}
-      }
-    }
-    return list.toArray(new String[list.size()]);
-  }
-  
+   
 }

Modified: incubator/abdera/java/trunk/extensions/main/src/test/java/org/apache/abdera/test/ext/features/FeatureTest.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/main/src/test/java/org/apache/abdera/test/ext/features/FeatureTest.java?rev=589181&r1=589180&r2=589181&view=diff
==============================================================================
--- incubator/abdera/java/trunk/extensions/main/src/test/java/org/apache/abdera/test/ext/features/FeatureTest.java
(original)
+++ incubator/abdera/java/trunk/extensions/main/src/test/java/org/apache/abdera/test/ext/features/FeatureTest.java
Sat Oct 27 12:00:47 2007
@@ -23,24 +23,36 @@
 import org.apache.abdera.ext.features.AcceptSelector;
 import org.apache.abdera.ext.features.Feature;
 import org.apache.abdera.ext.features.FeatureSelector;
+import org.apache.abdera.ext.features.Features;
 import org.apache.abdera.ext.features.FeaturesHelper;
 import org.apache.abdera.ext.features.Selector;
 import org.apache.abdera.ext.features.XPathSelector;
 import org.apache.abdera.ext.features.FeaturesHelper.Status;
 import org.apache.abdera.model.Collection;
+import org.apache.abdera.model.Document;
 import org.apache.abdera.model.Service;
 import org.apache.abdera.model.Workspace;
 
 public class FeatureTest extends TestCase {
 
+  public static void testFeaturesDocument() throws Exception {
+    Abdera abdera = Abdera.getInstance();
+    Features features = FeaturesHelper.newFeatures(abdera);
+    assertNotNull(features);
+    assertNotNull(features.getDocument());
+    Document<Features> doc = features.getDocument();
+    assertTrue(doc.getRoot() instanceof Features);
+  }
+  
   public static void testFeatures() throws Exception {
-    Abdera abdera = new Abdera();
+    Abdera abdera = Abdera.getInstance();
     Collection coll = abdera.getFactory().newCollection();
-    FeaturesHelper.addFeature(
-      coll, "http://example.com/features/foo", 
+    Features features = FeaturesHelper.addFeaturesElement(coll);
+    features.addFeature(
+      "http://example.com/features/foo", 
       null, "foo & here");
-    FeaturesHelper.addFeature(
-      coll, "http://example.com/features/bar", 
+    features.addFeature(
+      "http://example.com/features/bar", 
       null, null);
     
     assertEquals(Status.SPECIFIED,FeaturesHelper.getFeatureStatus(
@@ -56,12 +68,13 @@
   
   public static void testSelectors() throws Exception {
     
-    Abdera abdera = new Abdera();
+    Abdera abdera = Abdera.getInstance();
     Service service = abdera.newService();
     Workspace workspace = service.addWorkspace("a");
     Collection collection1 = workspace.addCollection("a1","a1");
     collection1.setAcceptsEntry();
-    FeaturesHelper.addFeature(collection1, FeaturesHelper.FEATURE_SUPPORTS_DRAFTS);
+    Features features = FeaturesHelper.addFeaturesElement(collection1);
+    features.addFeature(FeaturesHelper.FEATURE_SUPPORTS_DRAFTS);
     Collection collection2 = workspace.addCollection("a2","a2");
     collection2.setAccept("image/*");
     
@@ -80,7 +93,7 @@
     assertEquals(collections[0],collection2);
     
     Selector s3 = new XPathSelector(
-      "f:feature[@ref='" + FeaturesHelper.FEATURE_SUPPORTS_DRAFTS + "']");
+      "f:features/f:feature[@ref='" + FeaturesHelper.FEATURE_SUPPORTS_DRAFTS + "']");
     
     collections = FeaturesHelper.select(service,s3);
     
@@ -90,10 +103,10 @@
   
 
   public static void testType() throws Exception {
-     Abdera abdera = new Abdera();
+    Abdera abdera = Abdera.getInstance();
      Feature feature = abdera.getFactory().newElement(FeaturesHelper.FEATURE);
-     FeaturesHelper.addType(feature, "image/jpg","image/gif","image/png","image/*");
-     String[] types = FeaturesHelper.getTypes(feature);
+     feature.addType("image/jpg","image/gif","image/png","image/*");
+     String[] types = feature.getTypes();
      assertEquals(1,types.length);
      assertEquals("image/*", types[0]);
   }



Mime
View raw message