abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r519850 - in /incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features: ./ Control.java Feature.java FeaturesExtensionFactory.java FeaturesHelper.java
Date Mon, 19 Mar 2007 06:25:12 GMT
Author: jmsnell
Date: Sun Mar 18 23:25:11 2007
New Revision: 519850

URL: http://svn.apache.org/viewvc?view=rev&rev=519850
Log:
Initial implementation of the in-progress http://www.ietf.org/internet-drafts/draft-snell-atompub-feature-01.txt

Added:
    incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/
    incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/Control.java
    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/FeaturesExtensionFactory.java
    incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/FeaturesHelper.java

Added: incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/Control.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/Control.java?view=auto&rev=519850
==============================================================================
--- incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/Control.java
(added)
+++ incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/Control.java
Sun Mar 18 23:25:11 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 javax.xml.namespace.QName;
+
+import org.apache.abdera.factory.Factory;
+import org.apache.abdera.g14n.iri.IRI;
+import org.apache.abdera.g14n.iri.IRISyntaxException;
+import org.apache.abdera.model.Element;
+import org.apache.abdera.model.ExtensibleElementWrapper;
+
+public class Control 
+  extends ExtensibleElementWrapper {
+  
+  public Control(Element internal) {
+    super(internal);
+  }
+  
+  public Control(Factory factory) {
+    super(factory, FeaturesHelper.CONTROL);
+  }
+  
+  public IRI getRef() 
+    throws IRISyntaxException {
+      String ref = getAttributeValue("ref");
+      return (ref != null) ? new IRI(ref) : null;
+  }
+  
+  public boolean getRequired() {
+    String req = getAttributeValue("required");
+    if ("yes".equals(req)) return true;
+    return false;
+  }
+  
+  public IRI getHref() 
+    throws IRISyntaxException {
+      String href = getAttributeValue("href");
+      return (href != null) ? new IRI(href) : null;
+  }
+  
+  public String getLabel() {
+    return getAttributeValue("label");
+  }
+  
+  public void setRef(String ref) 
+    throws IRISyntaxException {
+      if (ref == null) throw new IllegalArgumentException();
+      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) 
+    throws IRISyntaxException {
+      if (href != null)
+        setAttributeValue("href", (new IRI(href)).toString());
+      else 
+        removeAttribute(new QName("href"));
+  }
+  
+  public void setLabel(String label) {
+    if (label != null)
+      setAttributeValue("label", label);
+    else 
+      removeAttribute(new QName("label"));
+  }
+}

Added: 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=auto&rev=519850
==============================================================================
--- incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/Feature.java
(added)
+++ incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/Feature.java
Sun Mar 18 23:25:11 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 javax.xml.namespace.QName;
+
+import org.apache.abdera.factory.Factory;
+import org.apache.abdera.g14n.iri.IRI;
+import org.apache.abdera.g14n.iri.IRISyntaxException;
+import org.apache.abdera.model.Element;
+import org.apache.abdera.model.ExtensibleElementWrapper;
+
+public class Feature 
+  extends ExtensibleElementWrapper {
+  
+  public Feature(Element internal) {
+    super(internal);
+  }
+  
+  public Feature(Factory factory) {
+    super(factory, FeaturesHelper.FEATURE);
+  }
+  
+  public IRI getRef() 
+    throws IRISyntaxException {
+      String ref = getAttributeValue("ref");
+      return (ref != null) ? new IRI(ref) : null;
+  }
+  
+  public boolean getRequired() {
+    String req = getAttributeValue("required");
+    if ("yes".equals(req)) return true;
+    return false;
+  }
+  
+  public IRI getHref() 
+    throws IRISyntaxException {
+      String href = getAttributeValue("href");
+      return (href != null) ? new IRI(href) : null;
+  }
+  
+  public String getLabel() {
+    return getAttributeValue("label");
+  }
+  
+  public void setRef(String ref) 
+    throws IRISyntaxException {
+      if (ref == null) throw new IllegalArgumentException();
+      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) 
+    throws IRISyntaxException {
+      if (href != null)
+        setAttributeValue("href", (new IRI(href)).toString());
+      else 
+        removeAttribute(new QName("href"));
+  }
+  
+  public void setLabel(String label) {
+    if (label != null)
+      setAttributeValue("label", label);
+    else 
+      removeAttribute(new QName("label"));
+  }
+}

Added: incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/FeaturesExtensionFactory.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/FeaturesExtensionFactory.java?view=auto&rev=519850
==============================================================================
--- incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/FeaturesExtensionFactory.java
(added)
+++ incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/FeaturesExtensionFactory.java
Sun Mar 18 23:25:11 2007
@@ -0,0 +1,53 @@
+/*
+* 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.ExtensionFactory;
+import org.apache.abdera.model.Element;
+
+public final class FeaturesExtensionFactory 
+  implements ExtensionFactory {
+
+  public boolean handlesNamespace(String namespace) {
+    return (FeaturesHelper.FNS.equals(namespace));
+  }
+
+  public List<String> getNamespaces() {
+    List<String> lst = new ArrayList<String>();
+    lst.add(FeaturesHelper.FNS);
+    return lst;
+  }
+
+  @SuppressWarnings("unchecked")
+  public <T extends Element> T getElementWrapper(Element internal) {
+    QName qname = internal.getQName();
+    if (FeaturesHelper.FEATURE.equals(qname)) 
+      return (T)new Feature(internal);
+    else if (FeaturesHelper.CONTROL.equals(qname))
+      return (T)new Control(internal);
+    else return (T)internal;
+  }
+
+
+
+}

Added: 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=auto&rev=519850
==============================================================================
--- incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/FeaturesHelper.java
(added)
+++ incubator/abdera/java/trunk/extensions/src/main/java/org/apache/abdera/ext/features/FeaturesHelper.java
Sun Mar 18 23:25:11 2007
@@ -0,0 +1,179 @@
+/*
+* 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.List;
+
+import javax.xml.namespace.QName;
+
+import org.apache.abdera.factory.Factory;
+import org.apache.abdera.g14n.iri.IRI;
+import org.apache.abdera.g14n.iri.IRISyntaxException;
+import org.apache.abdera.model.Collection;
+import org.apache.abdera.model.Element;
+
+/**
+ * Implementation of the current APP Features Draft
+ * (http://www.ietf.org/internet-drafts/draft-snell-atompub-feature-01.txt)
+ */
+public final class FeaturesHelper {
+
+  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 CONTROL = new QName(FNS, "control","f");
+  
+  private FeaturesHelper() {}
+  
+  /**
+   * Returns the specified feature element or null
+   */
+  public static Feature getFeature(
+    Collection collection,
+    String feature) {
+      List<Element> list = collection.getExtensions(FEATURE);
+      for (Element el : list) {
+        if (el.getAttributeValue("ref").equals(feature))
+          return (Feature) el;
+      }
+      return null;
+  }
+  
+  /**
+   * Returns the specified control element or null
+   */
+  public static Control getControl(
+    Collection collection,
+    String control) {
+      List<Element> list = collection.getExtensions(CONTROL);
+      for (Element el : list) {
+        if (el.getAttributeValue("ref").equals(control))
+          return (Control)el;
+      }
+      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});
+  }
+  
+  /**
+   * Returns true if the collection contains the specified feature element(s)
+   */
+  public static boolean supportsFeature(
+    Collection collection, 
+    String... features) {
+      List<Element> list = collection.getExtensions(FEATURE);
+      return check(list,features) == features.length;
+  }
+  
+  /**
+   * Returns true if the collection contains the specified control element
+   */
+  public static boolean supportsControl(
+    Collection collection,
+    String control) { 
+      return supportsControl(collection, new String[] {control});
+  }
+  
+  /**
+   * Returns true if the collection contains the specified control element(s)
+   */
+  public static boolean supportsControl(
+    Collection collection, 
+    String... controls) {
+      List<Element> list = collection.getExtensions(CONTROL);
+      return check(list, controls) == controls.length;
+  }
+  
+  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 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,
+    boolean required,
+    String href,
+    String label) 
+      throws IRISyntaxException {
+    if (supportsFeature(collection, feature)) 
+      throw new IllegalArgumentException("Feature already supported");
+    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);
+    return el;
+  }
+  
+  /**
+   * Add the specified control to the collection
+   * @param collection The collection
+   * @param feature The IRI of the control to add 
+   * @param required True if the control is required
+   * @param href An IRI pointing to a human readable resource describing the control
+   * @param label A human readable label for the control
+   */
+  public static Control addControl(
+    Collection collection,
+    String control,
+    boolean required,
+    String href,
+    String label) 
+      throws IRISyntaxException {
+    if (supportsControl(collection, control)) 
+      throw new IllegalArgumentException("Control already supported");
+    Factory factory = collection.getFactory();
+    Control el = 
+      (Control)factory.newExtensionElement(
+        FeaturesHelper.CONTROL, collection);
+    collection.declareNS(FNS, "f");
+    el.setAttributeValue("ref", (new IRI(control)).toString());
+    if (required) el.setAttributeValue("required", "yes");
+    if (href != null) el.setAttributeValue("href", (new IRI(href)).toString());
+    if (label != null) el.setAttributeValue("label", label);
+    return el;
+  }
+}



Mime
View raw message