abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r567446 - in /incubator/abdera/java/trunk/extensions/src: main/java/org/apache/abdera/ext/features/ test/java/org/apache/abdera/test/ext/features/
Date Sun, 19 Aug 2007 17:52:47 GMT
Author: jmsnell
Date: Sun Aug 19 10:52:45 2007
New Revision: 567446

URL: http://svn.apache.org/viewvc?view=rev&rev=567446
Log:
Updates to the feature package based on a pending update of the spec 
(has been sent off to the I-D publisher, should show up in a couple of days).

This also adds a number of Abdera-specific features and a new Selector mechanism that makes
it easy to select a Collection from a service document based on a variety of criteria. 

Added:
    incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/AbstractSelector.java
    incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/AcceptSelector.java
    incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/FeatureSelector.java
    incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/Selector.java
    incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/XPathSelector.java
Modified:
    incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/Feature.java
    incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/FeaturesHelper.java
    incubator/abdera/java/trunk/extensions/src/test/java/org/apache/abdera/test/ext/features/FeatureTest.java

Added: incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/AbstractSelector.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/AbstractSelector.java?view=auto&rev=567446
==============================================================================
--- incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/AbstractSelector.java
(added)
+++ incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/AbstractSelector.java
Sun Aug 19 10:52:45 2007
@@ -0,0 +1,36 @@
+/*
+* 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;
+
+public abstract class AbstractSelector
+    implements Selector {
+
+  public Selector clone() {
+    try {
+      return (Selector) super.clone();
+    } catch (CloneNotSupportedException e) {
+      return copy();
+    }
+  }
+
+  protected Selector copy() {
+    throw new RuntimeException(
+      new CloneNotSupportedException());
+  }
+  
+}

Added: incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/AcceptSelector.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/AcceptSelector.java?view=auto&rev=567446
==============================================================================
--- incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/AcceptSelector.java
(added)
+++ incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/AcceptSelector.java
Sun Aug 19 10:52:45 2007
@@ -0,0 +1,43 @@
+/*
+* 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 org.apache.abdera.model.Collection;
+
+public class AcceptSelector
+    extends AbstractSelector
+    implements Selector {
+
+  private static final long serialVersionUID = -1289924342084041384L;
+  private final List<String> accepts = new ArrayList<String>();
+  
+  public AcceptSelector(String... accepts) {
+    for (String accept : accepts) this.accepts.add(accept);
+  }
+  
+  public boolean select(Collection collection) {
+    for (String accept : accepts) {
+      if (collection.accepts(accept)) return true;
+    }
+    return false;
+  }
+
+}

Modified: incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/Feature.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/Feature.java?view=diff&rev=567446&r1=567445&r2=567446
==============================================================================
--- incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/Feature.java
(original)
+++ incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/Feature.java
Sun Aug 19 10:52:45 2007
@@ -27,6 +27,13 @@
 public class Feature 
   extends ExtensibleElementWrapper {
   
+  public enum Status {
+    UNSUPPORTED,
+    UNSPECIFIED,
+    SUPPORTED, 
+    REQUIRED,  
+  }
+  
   public Feature(Element internal) {
     super(internal);
   }
@@ -40,10 +47,22 @@
       return (ref != null) ? new IRI(ref) : null;
   }
   
-  public boolean isRequired() {
-    String req = getAttributeValue("required");
-    if ("yes".equals(req)) return true;
-    return false;
+  public Status getStatus() {
+    String status = getAttributeValue("status");
+    return status != null ? Status.valueOf(status.toUpperCase()) : Status.SUPPORTED;
+  }
+  
+  public void setStatus(Status status) {
+    if (status != null && status != Status.SUPPORTED) {
+      if (status != Status.UNSPECIFIED) {
+        setAttributeValue("status",status.name().toLowerCase());
+      } else {
+        throw new IllegalArgumentException(
+          "Cannot set the status to unspecified");
+      }
+    } else {
+      removeAttribute(new QName("status"));
+    }
   }
   
   public IRI getHref()  {
@@ -60,14 +79,6 @@
       setAttributeValue("ref", (new IRI(ref)).toString());
   }
   
-  public void setRequired(boolean required) {
-    if (required) {
-      setAttributeValue("required","yes");
-    } else {
-      removeAttribute(new QName("required"));
-    }
-  }
-  
   public void setHref(String href)  {
       if (href != null)
         setAttributeValue("href", (new IRI(href)).toString());
@@ -81,4 +92,5 @@
     else 
       removeAttribute(new QName("label"));
   }
+  
 }

Added: incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/FeatureSelector.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/FeatureSelector.java?view=auto&rev=567446
==============================================================================
--- incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/FeatureSelector.java
(added)
+++ incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/FeatureSelector.java
Sun Aug 19 10:52:45 2007
@@ -0,0 +1,84 @@
+/*
+* 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 org.apache.abdera.ext.features.Feature.Status;
+import org.apache.abdera.model.Collection;
+
+public class FeatureSelector
+  extends AbstractSelector
+  implements Selector {
+
+  private static final long serialVersionUID = -8943638085557912175L;
+  private final Status minimumStatus;
+  private final List<String> features = new ArrayList<String>();
+  
+  public FeatureSelector(String... features) {
+    this(Status.SUPPORTED,features);
+  }
+  
+  public FeatureSelector(Status minimumStatus, String... features) {
+    this.minimumStatus = minimumStatus;
+    for (String feature : features) this.features.add(feature);
+  }
+  
+  public boolean select(Collection collection) {
+    for (String feature : features) {
+      Status status = FeaturesHelper.getFeatureStatus(collection, feature);
+      if (status != null && status.ordinal() >= minimumStatus.ordinal()) 
+        return true;
+    }
+    return false;
+  }
+
+  public String[] getFeatures() {
+    return features.toArray(new String[features.size()]);
+  }
+
+  public Status getMinimumStatus() {
+    return minimumStatus;
+  }
+
+  @Override
+  public int hashCode() {
+    final int PRIME = 31;
+    int result = 1;
+    result = PRIME * result + ((features == null) ? 0 : features.hashCode());
+    result = PRIME * result + ((minimumStatus == null) ? 0 : minimumStatus.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 FeatureSelector other = (FeatureSelector) obj;
+    if (features == null) {
+      if (other.features != null) return false;
+    } else if (!features.equals(other.features)) return false;
+    if (minimumStatus == null) {
+      if (other.minimumStatus != null) return false;
+    } else if (!minimumStatus.equals(other.minimumStatus)) return false;
+    return true;
+  }
+
+}

Modified: incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/FeaturesHelper.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/FeaturesHelper.java?view=diff&rev=567446&r1=567445&r2=567446
==============================================================================
--- incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/FeaturesHelper.java
(original)
+++ incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/FeaturesHelper.java
Sun Aug 19 10:52:45 2007
@@ -17,15 +17,22 @@
 */
 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.ext.features.Feature.Status;
 import org.apache.abdera.ext.thread.ThreadConstants;
 import org.apache.abdera.factory.Factory;
 import org.apache.abdera.i18n.iri.IRI;
 import org.apache.abdera.model.Collection;
 import org.apache.abdera.model.Element;
+import org.apache.abdera.model.Service;
+import org.apache.abdera.model.Workspace;
+import org.apache.abdera.util.MimeTypeHelper;
 
 /**
  * Implementation of the current APP Features Draft
@@ -35,6 +42,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 TYPE = new QName(FNS, "type", "f");
   
   private static final String FEATURE_BASE                 = "http://www.w3.org/2007/app/";
   public static final String FEATURE_DRAFTS                = FEATURE_BASE + "drafts";
@@ -43,7 +51,7 @@
   public static final String FEATURE_TEXT_CONTENT          = FEATURE_BASE + "text-content";
   public static final String FEATURE_XML_CONTENT           = FEATURE_BASE + "xml-content";
   public static final String FEATURE_BINARY_CONTENT        = FEATURE_BASE + "binary-content";
-  public static final String FEATURE_REF_CONTENT           = FEATURE_BASE + "src-content";
+  public static final String FEATURE_REF_CONTENT           = FEATURE_BASE + "ref-content";
   public static final String FEATURE_XHTML_TITLE           = FEATURE_BASE + "xhtml-title";
   public static final String FEATURE_HTML_TITLE            = FEATURE_BASE + "html-title";
   public static final String FEATURE_TEXT_TITLE            = FEATURE_BASE + "text-title";
@@ -65,9 +73,82 @@
   public static final String FEATURE_PRESERVE_EXTENSIONS   = FEATURE_BASE + "preserve-extensions";
   public static final String FEATURE_PRESERVE_LINKS        = FEATURE_BASE + "preserve-links";
   public static final String FEATURE_PRESERVE_RIGHTS       = FEATURE_BASE + "preserve-rights";
+  public static final String FEATURE_SCHEDULED_PUBLISHING  = FEATURE_BASE + "scheduled-publishing";
   public static final String FEATURE_THREADING             = ThreadConstants.THR_NS;
 
   
+  private static final String ABDERA_FEATURE_BASE = "http://incubator.apache.org/abdera/features/";
+  
+  /**
+   * Indicates that the collection will accept digitally signed entries
+   * If marked as "required", the collection will only accept digitally signed entries
+   */
+  public static final String ABDERA_FEATURE_SIGNATURE = ABDERA_FEATURE_BASE + "signature";
+  
+  /**
+   * Indicates that the collection will preserve XML digital signatures contained
+   * in member resources 
+   */
+  public static final String ABDERA_FEATURE_PRESERVE_SIGNATURE = ABDERA_FEATURE_BASE + "preserve-signature";
+  
+  /**
+   * Indicates that the collection supports the use of the Atom Bidi Attribute.
+   * If marked as "required", the collection will only accept entries that contain the bidi
attribute
+   */
+  public static final String ABDERA_FEATURE_BIDI = ABDERA_FEATURE_BASE + "bidi";
+  
+  /**
+   * Indicates that the collection supports the use of Diffie-Hellman key exchange
+   * for XML encrypted requests
+   */
+  public static final String ABDERA_FEATURE_DHENCREQUEST = ABDERA_FEATURE_BASE + "dhenc-request";
+  
+  /**
+   * Indicates that the collection supports the use of Diffie-Hellman key exchange
+   * for XML encrypted responses
+   */
+  public static final String ABDERA_FEATURE_DHENCRESPONSE = ABDERA_FEATURE_BASE + "dhenc-response";
+  
+  /**
+   * Indicates that the collection will add it's own digital signature to the 
+   * collection feed and member resources
+   */
+  public static final String ABDERA_FEATURE_SIGNED_RESPONSE = ABDERA_FEATURE_BASE + "response-signature";
+  
+  /**
+   * Indicates that the collection supports the use of Geo extensions (see the
+   * org.apache.abdera.ext.geo Package)
+   */
+  public static final String ABDERA_FEATURE_GEO = ABDERA_FEATURE_BASE + "geo";
+  
+  /**
+   * Indicates that the collection supports the use of the Feed paging standard.
+   * (ftp://ftp.rfc-editor.org/in-notes/internet-drafts/draft-nottingham-atompub-feed-history-11.txt)
+   * See the org.apache.abdera.ext.history Package)
+   */
+  public static final String ABDERA_FEATURE_PAGING = ABDERA_FEATURE_BASE + "paging";
+  
+  /**
+   * Indicates that the collection supports the use of the Simple Sharing Extensions
+   * (see the org.apache.abdera.ext.sharing Package)
+   */
+  public static final String ABDERA_FEATURE_SHARING = ABDERA_FEATURE_BASE + "sharing";
+  
+  /**
+   * Indicates that the collection supports the GoogleLogin auth scheme
+   * (see the org.apache.abdera.ext.gdata Package)
+   */
+  public static final String ABDERA_FEATURE_GOOGLELOGIN = ABDERA_FEATURE_BASE + "googlelogin";
+  
+  /**
+   * Indicates that the collection supports the WSSE auth scheme
+   * (see the org.apache.abdera.ext.wsse Package)
+   */
+  public static final String ABDERA_FEATURE_WSSE = ABDERA_FEATURE_BASE + "wsse";
+  
+  
+  
+  
   private FeaturesHelper() {}
   
   /**
@@ -84,38 +165,58 @@
       return null;
   }
   
-  /**
-   * Returns true if the collection contains the specified feature element
-   */
-  public static boolean supportsFeature(
-    Collection collection,
-    String feature) { 
-      return supportsFeature(collection, new String[] {feature});
+  public static Status getFeatureStatus(Collection collection, String feature) {
+    Feature f = getFeature(collection,feature);
+    return f != null ? f.getStatus() : Status.UNSPECIFIED;
+  }
+  
+  public static Feature[] getSupportedFeatures(Collection collection) {
+    return getFeatures(collection, Status.SUPPORTED);
+  }
+  
+  public static Feature[] getUnsupportedFeatures(Collection collection) {
+    return getFeatures(collection, Status.UNSUPPORTED);
+  }
+  
+  public static Feature[] getRequiredFeatures(Collection collection) {
+    return getFeatures(collection, Status.REQUIRED);
+  }
+  
+  public static Feature[] getFeatures(Collection collection, Status status) {
+    if (status == null) status = Status.SUPPORTED;
+    List<Feature> list = new ArrayList<Feature>();
+    List<Feature> features = collection.getExtensions(FEATURE);
+    for (Feature feature : features) {
+      if (status == feature.getStatus()) {
+        list.add(feature);
+      }
+    }
+    return list.toArray(new Feature[list.size()]);
   }
   
   /**
-   * Returns true if the collection contains the specified feature element(s)
+   * Add the specified features to the collection
    */
-  public static boolean supportsFeature(
+  public static Feature[] addFeatures(
     Collection collection, 
     String... features) {
-      List<Element> list = collection.getExtensions(FEATURE);
-      return check(list,features) == features.length;
+      List<Feature> list = new ArrayList<Feature>();
+      for (String feature : features)
+        list.add(addFeature(collection,feature));
+      return list.toArray(new Feature[list.size()]);
   }
   
-  
-  private static int check(List<Element> exts, String... refvals) {
-    int c = 0;
-    for (String refval : refvals) {
-      for (Element el : exts) {
-        String ref = el.getAttributeValue("ref");
-        if (ref.equals(refval)) {
-          c++;
-          break;
-        }
-      }
-    }
-    return c;
+  /**
+   * Add the specified features to the collection
+   */
+  public static Feature[] addFeatures(
+    Collection collection,
+    Status status,
+    String... features) {
+      List<Feature> list = new ArrayList<Feature>();
+      for (String feature : features)
+        list.add(addFeature(collection,feature, status));
+      return list.toArray(new Feature[list.size()]);
   }
   
   /**
@@ -129,7 +230,7 @@
       return addFeature(
         collection, 
         feature, 
-        false, null, null);
+        null, null, null);
   }
   
   /**
@@ -140,11 +241,11 @@
   public static Feature addFeature(
     Collection collection, 
     String feature, 
-    boolean required) {
+    Status status) {
       return addFeature(
         collection, 
         feature, 
-        required, 
+        status, 
         null, null);
   }
   
@@ -159,21 +260,93 @@
   public static Feature addFeature(
     Collection collection, 
     String feature,
-    boolean required,
+    Status status,
     String href,
     String label) {
-    if (supportsFeature(collection, feature)) 
-      throw new IllegalArgumentException("Feature already supported");
+    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.setAttributeValue("ref", (new IRI(feature)).toString());
-    if (required) el.setAttributeValue("required", "yes");
-    if (href != null) el.setAttributeValue("href", (new IRI(href)).toString());
-    if (label != null) el.setAttributeValue("label", label);
+    el.setRef(new IRI(feature).toString());
+    el.setStatus(status);
+    if (href != null) el.setHref(new IRI(href).toString());
+    if (label != null) el.setLabel(label);
     return el;
+  }
+  
+  /**
+   * Select a Collection from the service document
+   */
+  public static Collection[] select(Service service, Selector selector) {
+    return select(service, new Selector[] {selector});
+  }
+  
+  /**
+   * Select a Collection from the service document
+   */
+  public static Collection[] select(Service service, Selector... selectors) {
+    List<Collection> list = new ArrayList<Collection>();
+    for (Workspace workspace : service.getWorkspaces()) {
+      Collection[] collections = select(workspace, selectors);
+      for (Collection collection : collections)
+        list.add(collection);
+    }
+    return list.toArray(new Collection[list.size()]);
+  }
+  
+  /**
+   * Select a Collection from the Workspace
+   */
+  public static Collection[] select(Workspace workspace, Selector selector) {
+    return select(workspace, new Selector[] {selector});
+  }
+  
+  /**
+   * Select a Collection from the Workspace
+   */
+  public static Collection[] select(Workspace workspace, Selector... selectors) {
+    List<Collection> list = new ArrayList<Collection>();
+    for (Collection collection : workspace.getCollections()) {
+      boolean accept = true;
+      for (Selector selector : selectors) {
+        if (!selector.select(collection)) {
+          accept = false;
+          break;
+        }
+      }
+      if (accept) list.add(collection);
+    }
+    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()]);
   }
   
 }

Added: incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/Selector.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/Selector.java?view=auto&rev=567446
==============================================================================
--- incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/Selector.java
(added)
+++ incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/Selector.java
Sun Aug 19 10:52:45 2007
@@ -0,0 +1,31 @@
+/*
+* 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.io.Serializable;
+
+import org.apache.abdera.model.Collection;
+
+public interface Selector
+  extends Cloneable, Serializable {
+
+  boolean select(Collection collection);
+  
+  Selector clone(); 
+  
+}

Added: incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/XPathSelector.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/XPathSelector.java?view=auto&rev=567446
==============================================================================
--- incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/XPathSelector.java
(added)
+++ incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/XPathSelector.java
Sun Aug 19 10:52:45 2007
@@ -0,0 +1,89 @@
+/*
+* 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.Map;
+
+import org.apache.abdera.Abdera;
+import org.apache.abdera.model.Collection;
+import org.apache.abdera.xpath.XPath;
+
+/**
+ * Selects a collection based on a boolean XPath expression
+ */
+public class XPathSelector
+    extends AbstractSelector
+    implements Selector {
+
+  private static final long serialVersionUID = 7751803876821166591L;
+  
+  private final XPath xpath;
+  private final Map<String,String> namespaces;
+  private final String path;
+  
+  public XPathSelector(
+    String path) {
+      this(path,(new Abdera()).getXPath());
+  }
+  
+  public XPathSelector(
+    String path, 
+    XPath xpath) {
+      this(path,xpath,xpath.getDefaultNamespaces());
+  }
+  
+  public XPathSelector(
+    String path, 
+    XPath xpath, 
+    Map<String,String> namespaces) {
+      this.path = path;
+      this.xpath = xpath;
+      this.namespaces = namespaces;
+      if (!this.namespaces.containsValue(FeaturesHelper.FNS)) {
+        int c = 0;
+        String p = "f";
+        if (!this.namespaces.containsKey(p)) {
+          this.namespaces.put(p, FeaturesHelper.FNS);
+        } else {
+          String s = p + c;
+          while (this.namespaces.containsKey(s)) {
+            c++; s = p + c;
+          }
+          this.namespaces.put(s, FeaturesHelper.FNS);
+        }
+      }
+  }
+  
+  public String getFeaturesPrefix() {
+    for (Map.Entry<String,String> entry : namespaces.entrySet()) {
+      if (entry.getValue().equals(FeaturesHelper.FNS)) return entry.getKey();
+    }
+    return null;
+  }
+  
+  public boolean select(Collection collection) {
+    if (xpath.booleanValueOf(path, collection, namespaces)) {
+      return true;
+    }
+    return false;
+  }
+
+  public void addNamespace(String prefix, String uri) {
+    namespaces.put(prefix, uri);
+  }
+}

Modified: incubator/abdera/java/trunk/extensions/src/test/java/org/apache/abdera/test/ext/features/FeatureTest.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/src/test/java/org/apache/abdera/test/ext/features/FeatureTest.java?view=diff&rev=567446&r1=567445&r2=567446
==============================================================================
--- incubator/abdera/java/trunk/extensions/src/test/java/org/apache/abdera/test/ext/features/FeatureTest.java
(original)
+++ incubator/abdera/java/trunk/extensions/src/test/java/org/apache/abdera/test/ext/features/FeatureTest.java
Sun Aug 19 10:52:45 2007
@@ -20,9 +20,16 @@
 import junit.framework.TestCase;
 
 import org.apache.abdera.Abdera;
+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.FeaturesHelper;
+import org.apache.abdera.ext.features.Selector;
+import org.apache.abdera.ext.features.XPathSelector;
+import org.apache.abdera.ext.features.Feature.Status;
 import org.apache.abdera.model.Collection;
+import org.apache.abdera.model.Service;
+import org.apache.abdera.model.Workspace;
 
 public class FeatureTest extends TestCase {
 
@@ -31,28 +38,68 @@
     Collection coll = abdera.getFactory().newCollection();
     FeaturesHelper.addFeature(
       coll, "http://example.com/features/foo", 
-      true, null, "foo & here");
+      Status.REQUIRED, null, "foo & here");
     FeaturesHelper.addFeature(
       coll, "http://example.com/features/bar", 
-      false, null, null);
-    assertTrue(FeaturesHelper.supportsFeature(
+      null, null, null);
+    FeaturesHelper.addFeature(
+      coll, "http://example.com/features/baz",
+      Status.UNSUPPORTED);
+    
+    assertEquals(Status.REQUIRED,FeaturesHelper.getFeatureStatus(
       coll, "http://example.com/features/foo"));
-    assertTrue(FeaturesHelper.supportsFeature(
+    assertEquals(Status.SUPPORTED, FeaturesHelper.getFeatureStatus(
       coll, "http://example.com/features/bar"));
-    assertTrue(FeaturesHelper.supportsFeature(
-      coll, "http://example.com/features/foo",
-            "http://example.com/features/bar"));
-    assertFalse(FeaturesHelper.supportsFeature(
-      coll, "http://example.com/features/foo",
-            "http://example.com/features/pez"));
-    assertFalse(FeaturesHelper.supportsFeature(
-      coll, "http://example.com/features/pez",
-            "http://example.com/features/foo"));
-    assertFalse(FeaturesHelper.supportsFeature(
+    assertEquals(Status.UNSUPPORTED, FeaturesHelper.getFeatureStatus(
+      coll, "http://example.com/features/baz"));
+    assertEquals(Status.UNSPECIFIED,FeaturesHelper.getFeatureStatus(
       coll, "http://example.com/features/pez"));
-    Feature f = FeaturesHelper.getFeature(
-      coll, "http://example.com/features/foo");
-    assertTrue(f.isRequired());
+
   }
   
+  public static void testSelectors() throws Exception {
+    
+    Abdera abdera = new Abdera();
+    Service service = abdera.newService();
+    Workspace workspace = service.addWorkspace("a");
+    Collection collection1 = workspace.addCollection("a1","a1");
+    collection1.setAcceptsEntry();
+    FeaturesHelper.addFeature(collection1, FeaturesHelper.FEATURE_DRAFTS);
+    Collection collection2 = workspace.addCollection("a2","a2");
+    collection2.setAccept("image/*");
+    
+    Selector s1 = new FeatureSelector(FeaturesHelper.FEATURE_DRAFTS);
+    
+    Collection[] collections = FeaturesHelper.select(service, s1);
+    
+    assertEquals(1,collections.length);
+    assertEquals(collections[0],collection1);
+    
+    Selector s2 = new AcceptSelector("image/png");
+    
+    collections = FeaturesHelper.select(service,s2);
+    
+    assertEquals(1,collections.length);
+    assertEquals(collections[0],collection2);
+    
+    Selector s3 = new XPathSelector(
+      "f:feature[@ref='http://www.w3.org/2007/app/drafts']");
+    
+    collections = FeaturesHelper.select(service,s3);
+    
+    for (Collection c : collections) System.out.println(c);
+    
+    assertEquals(1,collections.length);
+    assertEquals(collections[0],collection1);
+  }
+  
+
+  public static void testType() throws Exception {
+     Abdera abdera = new Abdera();
+     Feature feature = abdera.getFactory().newElement(FeaturesHelper.FEATURE);
+     FeaturesHelper.addType(feature, "image/jpg","image/gif","image/png","image/*");
+     String[] types = FeaturesHelper.getTypes(feature);
+     assertEquals(1,types.length);
+     assertEquals("image/*", types[0]);
+  }
 }



Mime
View raw message