abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r1199390 [2/2] - in /abdera/abdera2: activities/src/main/java/org/apache/abdera2/activities/extra/ common/src/main/java/org/apache/abdera2/common/date/ common/src/main/java/org/apache/abdera2/common/lang/ common/src/main/java/org/apache/abd...
Date Tue, 08 Nov 2011 19:12:18 GMT
Modified: abdera/abdera2/core/src/main/java/org/apache/abdera2/factory/StreamBuilder.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/core/src/main/java/org/apache/abdera2/factory/StreamBuilder.java?rev=1199390&r1=1199389&r2=1199390&view=diff
==============================================================================
--- abdera/abdera2/core/src/main/java/org/apache/abdera2/factory/StreamBuilder.java (original)
+++ abdera/abdera2/core/src/main/java/org/apache/abdera2/factory/StreamBuilder.java Tue Nov
 8 19:12:16 2011
@@ -38,6 +38,8 @@ import org.apache.abdera2.model.Element;
 import org.apache.abdera2.model.Content.Type;
 import org.apache.abdera2.writer.AbstractStreamWriter;
 
+import static com.google.common.base.Preconditions.*;
+
 /**
  * StreamBuilder is a special implementation of the StreamWriter interface that can be used
to create Feed Object Model
  * instances using the StreamWriter interface. StreamBuilder provides an additional method
(getBase) for returning the
@@ -74,8 +76,7 @@ public class StreamBuilder extends Abstr
     }
 
     public StreamBuilder startDocument(String xmlversion, String charset) {
-        if (root != null)
-            throw new IllegalStateException("Document already started");
+        checkState(root == null, "Document already started");
         root = abdera.getFactory().newDocument();
         ((Document<?>)root).setCharset(charset);
         current = root;
@@ -87,19 +88,19 @@ public class StreamBuilder extends Abstr
     }
 
     private static QName getQName(String name, String namespace, String prefix) {
-        if (prefix != null)
-            return new QName(namespace, name, prefix);
-        else if (namespace != null)
-            return new QName(namespace, name);
-        else
-            return new QName(name);
+      if (prefix != null)
+        return new QName(namespace, name, prefix);
+      else if (namespace != null)
+        return new QName(namespace, name);
+      else
+        return new QName(name);
     }
 
     public StreamBuilder startElement(String name, String namespace, String prefix) {
-        current = abdera.getFactory().newElement(getQName(name, namespace, prefix), current);
-        if (root == null)
-            root = current;
-        return this;
+      current = abdera.getFactory().newElement(getQName(name, namespace, prefix), current);
+      if (root == null)
+        root = current;
+      return this;
     }
 
     public StreamBuilder endElement() {
@@ -108,10 +109,9 @@ public class StreamBuilder extends Abstr
     }
 
     public StreamBuilder writeAttribute(String name, String namespace, String prefix, String
value) {
-        if (!(current instanceof Element))
-            throw new IllegalStateException("Not currently an element");
-        ((Element)current).setAttributeValue(getQName(name, namespace, prefix), value);
-        return this;
+      checkIsElement(current);
+      ((Element)current).setAttributeValue(getQName(name, namespace, prefix), value);
+      return this;
     }
 
     public StreamBuilder writeComment(String value) {
@@ -119,28 +119,30 @@ public class StreamBuilder extends Abstr
         return this;
     }
 
+    private void checkIsElement(Base current) {
+      checkState(current instanceof Element, "Not currently an element");
+    }
+    
     public StreamBuilder writeElementText(String value) {
-        if (!(current instanceof Element))
-            throw new IllegalStateException("Not currently an element");
-        Element element = (Element)current;
-        String text = element.getText();
-        element.setText(text + value);
-        return this;
+      checkIsElement(current);
+      Element element = (Element)current;
+      String text = element.getText();
+      element.setText(text + value);
+      return this;
     }
 
     public StreamBuilder writeId() {
-        return writeId(abdera.getFactory().newUuidUri());
+      return writeId(abdera.getFactory().newUuidUri());
     }
 
     public StreamBuilder writePI(String value) {
-        return writePI(value, null);
+      return writePI(value, null);
     }
 
     public StreamBuilder writePI(String value, String target) {
-        if (!(current instanceof Document))
-            throw new IllegalStateException("Not currently a document");
-        ((Document<?>)current).addProcessingInstruction(target != null ? target : "",
value);
-        return this;
+      checkState(current instanceof Document, "Not currently a document");
+      ((Document<?>)current).addProcessingInstruction(target != null ? target : "",
value);
+      return this;
     }
 
     public void close() throws IOException {
@@ -805,8 +807,7 @@ public class StreamBuilder extends Abstr
     }
 
     public StreamBuilder setPrefix(String prefix, String uri) {
-        if (!(current instanceof Element))
-            throw new IllegalStateException("Not currently an element");
+        checkIsElement(current);
         ((Element)current).declareNS(uri, prefix);
         return this;
     }

Modified: abdera/abdera2/core/src/main/java/org/apache/abdera2/model/ElementList.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/core/src/main/java/org/apache/abdera2/model/ElementList.java?rev=1199390&r1=1199389&r2=1199390&view=diff
==============================================================================
--- abdera/abdera2/core/src/main/java/org/apache/abdera2/model/ElementList.java (original)
+++ abdera/abdera2/core/src/main/java/org/apache/abdera2/model/ElementList.java Tue Nov  8
19:12:16 2011
@@ -25,6 +25,8 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.ListIterator;
 
+import com.google.common.collect.ImmutableList;
+
 
 /**
  * Implements the List interface around an internal buffered iterator. Here's the rationale:
Axiom parses incrementally.
@@ -49,33 +51,33 @@ public class ElementList<T extends Eleme
     }
     
     public List<T> getAsList() {
-        buffer(-1);
-        return java.util.Collections.unmodifiableList(buffer);
+      buffer(-1);
+      return ImmutableList.<T>copyOf(buffer);
     }
 
     private boolean finished() {
-        return !i.hasNext();
+      return !i.hasNext();
     }
 
     private int buffered() {
-        return buffer.size() - 1;
+      return buffer.size() - 1;
     }
 
     private int buffer(int n) {
-        if (i.hasNext()) {
-            int read = 0;
-            while (i.hasNext() && (read++ < n || n == -1)) {
-                buffer.add(i.next());
-            }
+      if (i.hasNext()) {
+        int read = 0;
+        while (i.hasNext() && (read++ < n || n == -1)) {
+          buffer.add(i.next());
         }
-        return buffered();
+      }
+      return buffered();
     }
 
     public T get(int index) {
-        int n = buffered();
-        if (index > n && (index > buffer(index - n)))
-            throw new ArrayIndexOutOfBoundsException(index);
-        return (T)buffer.get(index);
+      int n = buffered();
+      if (index > n && (index > buffer(index - n)))
+          throw new ArrayIndexOutOfBoundsException(index);
+      return (T)buffer.get(index);
     }
 
     public int size() {
@@ -169,8 +171,8 @@ public class ElementList<T extends Eleme
     }
 
     public List<T> subList(int fromIndex, int toIndex) {
-        buffer(-1);
-        return Collections.unmodifiableList(buffer.subList(fromIndex, toIndex));
+      buffer(-1);
+      return ImmutableList.copyOf(buffer.subList(fromIndex, toIndex));
     }
 
     public Object[] toArray() {

Modified: abdera/abdera2/core/src/main/java/org/apache/abdera2/model/selector/CategorySchemeSelector.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/core/src/main/java/org/apache/abdera2/model/selector/CategorySchemeSelector.java?rev=1199390&r1=1199389&r2=1199390&view=diff
==============================================================================
--- abdera/abdera2/core/src/main/java/org/apache/abdera2/model/selector/CategorySchemeSelector.java
(original)
+++ abdera/abdera2/core/src/main/java/org/apache/abdera2/model/selector/CategorySchemeSelector.java
Tue Nov  8 19:12:16 2011
@@ -17,14 +17,16 @@
  */
 package org.apache.abdera2.model.selector;
 
-import java.util.HashSet;
 import java.util.Set;
 
 import org.apache.abdera2.common.iri.IRI;
+import org.apache.abdera2.common.misc.MoreFunctions;
 import org.apache.abdera2.common.selector.AbstractSelector;
 import org.apache.abdera2.common.selector.Selector;
 import org.apache.abdera2.model.Category;
 
+import com.google.common.collect.ImmutableSet;
+
 /**
  * Selector implementation that selects Category elements using the 
  * specified scheme(s).
@@ -34,17 +36,23 @@ public class CategorySchemeSelector 
   extends AbstractSelector<Category>
   implements Selector<Category> {
 
+  public static Selector<Category> of(String... schemes) {
+    return new CategorySchemeSelector(schemes);
+  }
+  
+  public static Selector<Category> of(IRI... schemes) {
+    return new CategorySchemeSelector(schemes);
+  }
+  
   private static final long serialVersionUID = 7008363856043465676L;
-  private final Set<IRI> schemes = new HashSet<IRI>();
+  private final Set<IRI> schemes;
   
-  public CategorySchemeSelector(String... schemes) {
-    for (String scheme : schemes)
-      this.schemes.add(new IRI(scheme));
+  CategorySchemeSelector(String... schemes) {
+    this.schemes = MoreFunctions.immutableSetOf(schemes, IRI.parser, IRI.class);
   }
   
-  public CategorySchemeSelector(IRI... schemes) {
-    for (IRI scheme : schemes) 
-      this.schemes.add(scheme);
+  CategorySchemeSelector(IRI... schemes) {
+    this.schemes = ImmutableSet.copyOf(schemes);
   }
   
   public boolean select(Object item) {

Modified: abdera/abdera2/core/src/main/java/org/apache/abdera2/model/selector/CollectionAcceptSelector.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/core/src/main/java/org/apache/abdera2/model/selector/CollectionAcceptSelector.java?rev=1199390&r1=1199389&r2=1199390&view=diff
==============================================================================
--- abdera/abdera2/core/src/main/java/org/apache/abdera2/model/selector/CollectionAcceptSelector.java
(original)
+++ abdera/abdera2/core/src/main/java/org/apache/abdera2/model/selector/CollectionAcceptSelector.java
Tue Nov  8 19:12:16 2011
@@ -17,16 +17,18 @@
  */
 package org.apache.abdera2.model.selector;
 
-import java.util.LinkedHashSet;
 import java.util.Set;
 
 import javax.activation.MimeType;
 
 import org.apache.abdera2.common.mediatype.MimeTypeHelper;
+import org.apache.abdera2.common.misc.MoreFunctions;
 import org.apache.abdera2.common.selector.AbstractSelector;
 import org.apache.abdera2.common.selector.Selector;
 import org.apache.abdera2.model.Collection;
 
+import com.google.common.collect.ImmutableSet;
+
 /**
  * Selector implementation that selects Collections elements that contain
  * specific mimetypes, or compatible equivalent mimetimes within child
@@ -36,18 +38,23 @@ public class CollectionAcceptSelector 
 extends AbstractSelector<Collection>
   implements Selector<Collection> {
 
+  public static Selector<Collection> of(String... types) {
+    return new CollectionAcceptSelector(types);
+  }
+  
+  public static Selector<Collection> of(MimeType... types) {
+    return new CollectionAcceptSelector(types);
+  }
+  
   private static final long serialVersionUID = 1821941024155067263L;
-  private final Set<MimeType> types = 
-    new LinkedHashSet<MimeType>();
+  private final Set<MimeType> types;
   
-  public CollectionAcceptSelector(String... types) {
-    for (String type:types)
-      this.types.add(MimeTypeHelper.unmodifiableMimeType(type));
+  CollectionAcceptSelector(String... types) {
+    this.types = MoreFunctions.immutableSetOf(types, MimeTypeHelper.parser, MimeType.class);
   }
   
-  public CollectionAcceptSelector(MimeType... types) {
-    for (MimeType type : types)
-      this.types.add(MimeTypeHelper.unmodifiableMimeType(type));
+  CollectionAcceptSelector(MimeType... types) {
+    this.types = ImmutableSet.copyOf(types);
   }
   
   public boolean select(Object item) {

Modified: abdera/abdera2/core/src/main/java/org/apache/abdera2/model/selector/LinkHrefLangSelector.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/core/src/main/java/org/apache/abdera2/model/selector/LinkHrefLangSelector.java?rev=1199390&r1=1199389&r2=1199390&view=diff
==============================================================================
--- abdera/abdera2/core/src/main/java/org/apache/abdera2/model/selector/LinkHrefLangSelector.java
(original)
+++ abdera/abdera2/core/src/main/java/org/apache/abdera2/model/selector/LinkHrefLangSelector.java
Tue Nov  8 19:12:16 2011
@@ -17,15 +17,17 @@
  */
 package org.apache.abdera2.model.selector;
 
-import java.util.HashSet;
 import java.util.Set;
 
 import org.apache.abdera2.common.lang.Lang;
 import org.apache.abdera2.common.lang.Range;
+import org.apache.abdera2.common.misc.MoreFunctions;
 import org.apache.abdera2.common.selector.AbstractSelector;
 import org.apache.abdera2.common.selector.Selector;
 import org.apache.abdera2.model.Link;
 
+import com.google.common.collect.ImmutableSet;
+
 /**
  * Selector implementation that selects Link elements specifying
  * a matching hreflang attribute.
@@ -35,24 +37,35 @@ public class LinkHrefLangSelector 
 extends AbstractSelector<Link>
 implements Selector<Link> {
 
+  public static Selector<Link> of(Range range) {
+    return new LinkHrefLangSelector(range);
+  }
+  
+  public static Selector<Link> of(String... langs) {
+    return new LinkHrefLangSelector(langs);
+  }
+  
+  public static Selector<Link> of(Lang... langs) {
+    return new LinkHrefLangSelector(langs);
+  }
+  
   private static final long serialVersionUID = 7008363856043465676L;
-  private final Set<Lang> langs = new HashSet<Lang>();
+  private final Set<Lang> langs;
   private final Range range;
   
-  public LinkHrefLangSelector(Range range) {
+  LinkHrefLangSelector(Range range) {
     this.range = range;
+    this.langs = ImmutableSet.<Lang>of();
   }
   
-  public LinkHrefLangSelector(String... langs) {
+  LinkHrefLangSelector(String... langs) {
     this.range = null;
-    for (String lang : langs)
-      this.langs.add(new Lang(lang));
+    this.langs = MoreFunctions.immutableSetOf(langs, Lang.parser, Lang.class);
   }
   
-  public LinkHrefLangSelector(Lang... langs) {
+  LinkHrefLangSelector(Lang... langs) {
     this.range = null;
-    for (Lang lang : langs)
-      this.langs.add(lang);
+    this.langs = ImmutableSet.copyOf(langs);
   }
 
   public boolean select(Object item) {

Modified: abdera/abdera2/core/src/main/java/org/apache/abdera2/model/selector/LinkRelSelector.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/core/src/main/java/org/apache/abdera2/model/selector/LinkRelSelector.java?rev=1199390&r1=1199389&r2=1199390&view=diff
==============================================================================
--- abdera/abdera2/core/src/main/java/org/apache/abdera2/model/selector/LinkRelSelector.java
(original)
+++ abdera/abdera2/core/src/main/java/org/apache/abdera2/model/selector/LinkRelSelector.java
Tue Nov  8 19:12:16 2011
@@ -17,13 +17,14 @@
  */
 package org.apache.abdera2.model.selector;
 
-import java.util.HashSet;
 import java.util.Set;
 
 import org.apache.abdera2.common.selector.AbstractSelector;
 import org.apache.abdera2.common.selector.Selector;
 import org.apache.abdera2.model.Link;
 
+import com.google.common.collect.ImmutableSet;
+
 /**
  * Selector implementation that selects Link elements 
  * that match a given set of rel attribute values
@@ -33,12 +34,15 @@ public class LinkRelSelector 
 extends AbstractSelector<Link>
 implements Selector<Link> {
 
+  public static Selector<Link> of(String... rels) {
+    return new LinkRelSelector(rels);
+  }
+  
   private static final long serialVersionUID = 7008363856043465676L;
-  private final Set<String> rels = new HashSet<String>();
+  private final Set<String> rels;
   
-  public LinkRelSelector(String... rels) {
-    for (String rel : rels)
-      this.rels.add(rel);
+  LinkRelSelector(String... rels) {
+    this.rels = ImmutableSet.copyOf(rels);
   }
 
   public boolean select(Object item) {

Modified: abdera/abdera2/core/src/main/java/org/apache/abdera2/model/selector/Selectors.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/core/src/main/java/org/apache/abdera2/model/selector/Selectors.java?rev=1199390&r1=1199389&r2=1199390&view=diff
==============================================================================
--- abdera/abdera2/core/src/main/java/org/apache/abdera2/model/selector/Selectors.java (original)
+++ abdera/abdera2/core/src/main/java/org/apache/abdera2/model/selector/Selectors.java Tue
Nov  8 19:12:16 2011
@@ -19,8 +19,10 @@ import org.joda.time.DateTime;
 
 import com.google.common.base.Predicate;
 
-public class Selectors {
+public final class Selectors {
 
+  private Selectors() {}
+  
   public static Selector<Entry> updated(Predicate<DateTime> predicate) {
     return 
       PropertySelector.<Entry>create(
@@ -46,53 +48,54 @@ public class Selectors {
   }
   
   public static Selector<Category> withCategoryScheme(String... schemes) {
-    return new CategorySchemeSelector(schemes);
+    return CategorySchemeSelector.of(schemes);
   }
   
   public static Selector<Category> withCategoryScheme(IRI... schemes) {
-    return new CategorySchemeSelector(schemes);
+    return CategorySchemeSelector.of(schemes);
   }
   
   public static Selector<Collection> accepts(String... types) {
-    return new CollectionAcceptSelector(types);
+    return CollectionAcceptSelector.of(types);
   }
   
   public static Selector<Collection> accepts(MimeType... types) {
-    return new CollectionAcceptSelector(types);
+    return CollectionAcceptSelector.of(types);
   }
   
   public static Selector<Link> withHrefLang(Range range) {
-    return new LinkHrefLangSelector(range);
+    return LinkHrefLangSelector.of(range);
   }
   
   public static Selector<Link> withHrefLang(String... langs) {
-    return new LinkHrefLangSelector(langs);
+    return LinkHrefLangSelector.of(langs);
   }
   
   public static Selector<Link> withHrefLang(Lang... langs) {
-    return new LinkHrefLangSelector(langs);
+    return LinkHrefLangSelector.of(langs);
   }
   
   public static Selector<Link> withHrefLang(Locale locale) {
-    return new LinkHrefLangSelector(Lang.fromLocale(locale));
+    return LinkHrefLangSelector.of(Lang.fromLocale(locale));
   }
   
   public static Selector<Link> withRel(String... rels) {
-    return new LinkRelSelector(rels);
+    return LinkRelSelector.of(rels);
+  }
+  
+  public static Selector<Object> xpath(String path) {
+    return XPathSelector.make().path(path).get();
   }
   
-  @SuppressWarnings("rawtypes")
-  public static Selector xpath(String path) {
-    return new XPathSelector(path);
+  public static Selector<Object> xpath(String path, XPath xpath) {
+    return XPathSelector.make(xpath).path(path).get();
   }
   
-  @SuppressWarnings("rawtypes")
-  public static Selector xpath(String path, XPath xpath) {
-    return new XPathSelector(path,xpath);
+  public static Selector<Object> xpath(String path, XPath xpath, Map<String, String>
namespaces) {
+    return XPathSelector.make(xpath).path(path).with(namespaces).get();
   }
   
-  @SuppressWarnings("rawtypes")
-  public static Selector xpath(String path, XPath xpath, Map<String, String> namespaces)
{
-    return new XPathSelector(path,xpath,namespaces);
+  public static XPathSelector.Builder xpath() {
+    return XPathSelector.make();
   }
 }

Modified: abdera/abdera2/core/src/main/java/org/apache/abdera2/model/selector/XPathSelector.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/core/src/main/java/org/apache/abdera2/model/selector/XPathSelector.java?rev=1199390&r1=1199389&r2=1199390&view=diff
==============================================================================
--- abdera/abdera2/core/src/main/java/org/apache/abdera2/model/selector/XPathSelector.java
(original)
+++ abdera/abdera2/core/src/main/java/org/apache/abdera2/model/selector/XPathSelector.java
Tue Nov  8 19:12:16 2011
@@ -19,46 +19,73 @@ package org.apache.abdera2.model.selecto
 
 import java.util.Map;
 
-import org.apache.abdera2.Abdera;
 import org.apache.abdera2.common.selector.AbstractSelector;
 import org.apache.abdera2.common.selector.Selector;
 import org.apache.abdera2.model.Element;
 import org.apache.abdera2.xpath.XPath;
 
+import com.google.common.base.Supplier;
+import com.google.common.collect.ImmutableMap;
+
 /**
  * Selects a collection based on a boolean XPath expression
  * @see org.apache.abdera2.common.selector.Selector
  */
-@SuppressWarnings("rawtypes")
 public class XPathSelector 
-extends AbstractSelector
-implements Selector {
+extends AbstractSelector<Object>
+implements Selector<Object> {
 
+    public static Builder make() {
+      return new Builder();
+    }
+    
+    public static Builder make(XPath xpath) {
+      return new Builder().using(xpath);
+    }
+  
+    public static final class Builder 
+      implements Supplier<Selector<Object>> {
+      private XPath xpath;
+      private String path;
+      private ImmutableMap.Builder<String, String> namespaces =
+        ImmutableMap.<String,String>builder();
+      public Builder using(XPath xpath) {
+        this.xpath = xpath;
+        return this;
+      }
+      public Builder path(String path) {
+        this.path = path;
+        return this;
+      }
+      public Builder with(String prefix, String namespace) {
+        this.namespaces.put(prefix,namespace);
+        return this;
+      }
+      public Builder with(Map<String,String> namespaces) {
+        this.namespaces.putAll(namespaces);
+        return this;
+      }
+      public Selector<Object> get() {
+        return new XPathSelector(this);
+      }
+    }
+  
     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, (Abdera.getInstance()).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;
+    
+    XPathSelector(Builder builder) {
+      this.path = builder.path;
+      this.xpath = builder.xpath;
+      this.namespaces = builder.namespaces.build();
     }
 
     public boolean select(Object element) {
       if (!(element instanceof Element)) return false;
-        if (xpath.booleanValueOf(path, (Element)element, namespaces)) {
+        if (xpath.booleanValueOf(path, (Element)element, namespaces))
             return true;
-        }
         return false;
     }
 

Modified: abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMElement.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMElement.java?rev=1199390&r1=1199389&r2=1199390&view=diff
==============================================================================
--- abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMElement.java (original)
+++ abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMElement.java Tue
Nov  8 19:12:16 2011
@@ -51,8 +51,6 @@ import org.apache.abdera2.model.ElementW
 import org.apache.abdera2.model.ElementList;
 import org.apache.abdera2.model.Link;
 import org.apache.abdera2.model.Text;
-import org.apache.abdera2.model.selector.CategorySchemeSelector;
-import org.apache.abdera2.model.selector.LinkRelSelector;
 import org.apache.abdera2.parser.ParseException;
 import org.apache.abdera2.parser.Parser;
 import org.apache.abdera2.parser.ParserOptions;
@@ -75,6 +73,8 @@ import org.apache.axiom.om.OMText;
 import org.apache.axiom.om.OMXMLParserWrapper;
 import org.apache.axiom.om.impl.llom.OMElementImpl;
 
+import static org.apache.abdera2.model.selector.Selectors.*;
+
 @SuppressWarnings({"unchecked","rawtypes"})
 public class FOMElement extends OMElementImpl implements Element, OMElement, Constants {
 
@@ -812,21 +812,15 @@ public class FOMElement extends OMElemen
     }
 
     public static List<Link> getLinks(Element element, String... rels) {
-      LinkRelSelector sel =
-        new LinkRelSelector(rels);
-      return element.getElements(sel);
+      return element.getElements(withRel(rels));
     }
 
     public static List<Link> getLinks(Element element, String rel) {
-        LinkRelSelector sel =
-          new LinkRelSelector(rel);
-        return element.getElements(sel);
+        return element.getElements(withRel(rel));
     }
 
     public static List<Category> getCategories(Element element, String scheme) {
-      CategorySchemeSelector sel =
-        new CategorySchemeSelector(scheme);
-      return element.getElements(sel);
+      return element.getElements(withCategoryScheme(scheme));
     }
     
     /**

Modified: abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMWorkspace.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMWorkspace.java?rev=1199390&r1=1199389&r2=1199390&view=diff
==============================================================================
--- abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMWorkspace.java (original)
+++ abdera/abdera2/core/src/main/java/org/apache/abdera2/parser/axiom/FOMWorkspace.java Tue
Nov  8 19:12:16 2011
@@ -35,6 +35,8 @@ import org.apache.axiom.om.OMFactory;
 import org.apache.axiom.om.OMNamespace;
 import org.apache.axiom.om.OMXMLParserWrapper;
 
+import static org.apache.abdera2.model.selector.Selectors.*;
+
 @SuppressWarnings({"deprecation","rawtypes"})
 public class FOMWorkspace extends FOMExtensibleElement implements Workspace {
 
@@ -145,29 +147,21 @@ public class FOMWorkspace extends FOMExt
     }
 
     public Collection getCollectionThatAccepts(MimeType... types) {
-      CollectionAcceptSelector sel =
-        new CollectionAcceptSelector(types);
-      List<Collection> list = getCollections(sel);
+      List<Collection> list = getCollections(accepts(types));
       return list.size() > 0 ? list.get(0) : null;
     }
 
     public Collection getCollectionThatAccepts(String... types) {
-      CollectionAcceptSelector sel =
-        new CollectionAcceptSelector(types);
-      List<Collection> list = getCollections(sel);
+      List<Collection> list = getCollections(accepts(types));
       return list.size() > 0 ? list.get(0) : null;
     }
 
     public List<Collection> getCollectionsThatAccept(MimeType... types) {
-      CollectionAcceptSelector sel =
-        new CollectionAcceptSelector(types);
-      return getCollections(sel);
+      return getCollections(accepts(types));
     }
 
     public List<Collection> getCollectionsThatAccept(String... types) {
-      CollectionAcceptSelector sel =
-        new CollectionAcceptSelector(types);
-      return getCollections(sel);
+      return getCollections(accepts(types));
     }
 
     public List<Collection> getCollections(Selector selector) {

Modified: abdera/abdera2/core/src/main/java/org/apache/abdera2/writer/AbstractStreamWriter.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/core/src/main/java/org/apache/abdera2/writer/AbstractStreamWriter.java?rev=1199390&r1=1199389&r2=1199390&view=diff
==============================================================================
--- abdera/abdera2/core/src/main/java/org/apache/abdera2/writer/AbstractStreamWriter.java
(original)
+++ abdera/abdera2/core/src/main/java/org/apache/abdera2/writer/AbstractStreamWriter.java
Tue Nov  8 19:12:16 2011
@@ -146,9 +146,13 @@ public abstract class AbstractStreamWrit
     }
 
     public StreamWriter startLink(String iri, String rel, String type, String title, String
hreflang, long length) {
-        return startElement(Constants.LINK).writeAttribute("href", iri).writeAttribute("rel",
rel)
-            .writeAttribute("type", type).writeAttribute("title", title).writeAttribute("hreflang",
hreflang)
-            .writeAttribute("length", length > -1 ? String.valueOf(length) : null);
+        return startElement(Constants.LINK)
+          .writeAttribute("href", iri)
+          .writeAttribute("rel", rel)
+          .writeAttribute("type", type)
+          .writeAttribute("title", title)
+          .writeAttribute("hreflang", hreflang)
+          .writeAttribute("length", length > -1 ? String.valueOf(length) : null);
     }
 
     public StreamWriter startPerson(QName qname) {
@@ -160,7 +164,8 @@ public abstract class AbstractStreamWrit
     }
 
     public StreamWriter startText(QName qname, Text.Type type) {
-        return startElement(qname).writeAttribute("type", type != null ? type.name().toLowerCase()
: "text");
+        return startElement(qname)
+          .writeAttribute("type", type != null ? type.name().toLowerCase() : "text");
     }
 
     public StreamWriter writeDate(QName qname, String date) {
@@ -194,7 +199,8 @@ public abstract class AbstractStreamWrit
     }
 
     public StreamWriter startContent(String type, String src) {
-        return startElement(Constants.CONTENT).writeAttribute("type", type).writeAttribute("src",
src);
+        return startElement(Constants.CONTENT)
+          .writeAttribute("type", type).writeAttribute("src", src);
     }
 
     public StreamWriter startContent(Content.Type type) {
@@ -226,8 +232,10 @@ public abstract class AbstractStreamWrit
     }
 
     public StreamWriter writeCategory(String term, String scheme, String label) {
-        return startElement(Constants.CATEGORY).writeAttribute("term", term).writeAttribute("scheme",
scheme)
-            .writeAttribute("label", label).endElement();
+        return startElement(Constants.CATEGORY)
+          .writeAttribute("term", term)
+          .writeAttribute("scheme", scheme)
+          .writeAttribute("label", label).endElement();
     }
 
     public StreamWriter writeContent(Content.Type type, String value) {
@@ -280,7 +288,11 @@ public abstract class AbstractStreamWrit
     }
 
     public StreamWriter writePerson(QName qname, String name, String email, String uri) {
-        return startPerson(qname).writePersonName(name).writePersonEmail(email).writePersonUri(uri).endPerson();
+        return startPerson(qname)
+          .writePersonName(name)
+          .writePersonEmail(email)
+          .writePersonUri(uri)
+          .endPerson();
     }
 
     public StreamWriter writePublished(DateTime date) {

Modified: abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/uritemplates/URITemplates.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/uritemplates/URITemplates.java?rev=1199390&r1=1199389&r2=1199390&view=diff
==============================================================================
--- abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/uritemplates/URITemplates.java
(original)
+++ abdera/abdera2/examples/src/main/java/org/apache/abdera2/examples/uritemplates/URITemplates.java
Tue Nov  8 19:12:16 2011
@@ -17,121 +17,109 @@
  */
 package org.apache.abdera2.examples.uritemplates;
 
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
-import java.util.Map;
 
 import org.apache.abdera2.common.templates.CachingContext;
 import org.apache.abdera2.common.templates.MapContext;
 import org.apache.abdera2.common.templates.Template;
 
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Iterators;
+import com.google.common.collect.LinkedHashMultimap;
+import com.google.common.collect.Multimap;
+
+
 @SuppressWarnings("unchecked")
 public final class URITemplates {
 
     private static final Template template =
-        new Template(
-                     "http://example.org/~{user}{/categories}{?foo,bar}");
+        new Template("http://example.org/~{user}{/categories}{?foo,bar}");
 
     public static void main(String... args) throws Exception {
 
-        // two examples of resolving the template
         exampleWithObject();
         exampleIRIWithObject();
         exampleWithMap();
         exampleWithHashMapContext();
         exampleWithCustomContext();
 
-        // explain the template
-        System.out.println(template);
     }
 
     // Using a Java object
     private static void exampleWithObject() {
-        MyObject myObject = new MyObject();
-        System.out.println(template.expand(myObject));
+      System.out.println(template.expand(new MyObject()));
     }
 
     // Using a Java object
     private static void exampleIRIWithObject() {
-        MyObject myObject = new MyObject();
-        System.out.println(template.expand(myObject, true));
+      System.out.println(template.expand(new MyObject(), true));
     }
 
     // Using a Map
     private static void exampleWithMap() {
-        Map<String, Object> map = new HashMap<String,Object>();
-        map.put("user", "james");
-        map.put("categories", new String[] {"a", "b", "c"});
-        map.put("foo", "abc");
-        map.put("bar", "xyz");
-        System.out.println(template.expand(map));
+      Multimap<String, Object> map = 
+        LinkedHashMultimap.create();
+      map.put("user", "james");
+      map.put("categories", "a");
+      map.put("categories", "b");
+      map.put("categories", "c");
+      map.put("foo", "abc");
+      map.put("bar", "xyz");
+      System.out.println(template.expand(map));
     }
 
     // Using a HashMap based context
     private static void exampleWithHashMapContext() {
-        MapContext context = new MapContext();
-        context.put("user", "james");
-        context.put("categories", new String[] {"a", "b", "c"});
-        context.put("foo", "abc");
-        context.put("bar", "xyz");
-        System.out.println(template.expand(context));
+      MapContext context = new MapContext();
+      context.put("user", "james");
+      context.put("categories", new String[] {"a", "b", "c"});
+      context.put("foo", "abc");
+      context.put("bar", "xyz");
+      System.out.println(template.expand(context));
     }
 
     // Using a custom context implementation
     private static void exampleWithCustomContext() {
-        CachingContext context = new CachingContext() {
-            private static final long serialVersionUID = 4896250661828139020L;
-
-            protected <T> T resolveActual(String var) {
-                if (var.equals("user"))
-                    return (T)"james";
-                else if (var.equals("categories"))
-                    return (T)new String[] {"a", "b", "c"};
-                else if (var.equals("foo"))
-                    return (T)"abc";
-                else if (var.equals("bar"))
-                    return (T)"xyz";
-                else
-                    return null;
-            }
-
-            public Iterator<String> iterator() {
-                return Arrays.asList(new String[] {"user", "categories", "foo", "bar"}).iterator();
-            }
-
-            public boolean contains(String var) {
-              return resolveActual(var) != null;
-            }
-        };
-        System.out.println(template.expand(context));
-    }
-
-    public static class MyObject {
-        public String user = "james";
-
-        public List<String> getCategories() {
-            List<String> list = new ArrayList<String>();
-            list.add("a");
-            list.add("b");
-            list.add("c");
-            return list;
+      CachingContext context = new CachingContext(false) {
+        private static final long serialVersionUID = 4896250661828139020L;
+        protected <T> T resolveActual(String var) {
+          if (var.equals("user"))
+            return (T)"james";
+          else if (var.equals("categories"))
+            return (T)new String[] {"a", "b", "c"};
+          else if (var.equals("foo"))
+            return (T)"abc";
+          else if (var.equals("bar"))
+            return (T)"xyz";
+          else
+            return null;
         }
-
-        public Foo[] getFoo() {
-            return new Foo[] {new Foo(), new Foo()};
+        public Iterator<String> iterator() {
+          return Iterators.forArray("user","categories","foo","bar");
         }
-
-        public String getBar() {
-            return "xyz";
+        public boolean contains(String var) {
+          return resolveActual(var) != null;
         }
+      };
+      System.out.println(template.expand(context));
     }
 
+    public static class MyObject {
+      public String user = "james";
+      public List<String> getCategories() {
+        return ImmutableList.of("a","b","c");
+      }
+      public Foo[] getFoo() {
+        return new Foo[] {new Foo(), new Foo()};
+      }
+      public String getBar() {
+        return "xyz";
+      }
+    }
     private static class Foo {
-        public String toString() {
-            return "abcæ";
-        }
+      public String toString() {
+        return "abcæ";
+      }
     }
 }

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=1199390&r1=1199389&r2=1199390&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
Tue Nov  8 19:12:16 2011
@@ -27,7 +27,7 @@ import org.apache.abdera2.model.Link;
 import org.apache.abdera2.model.Person;
 import org.apache.abdera2.model.Generator;
 import org.apache.abdera2.model.Text;
-import org.apache.abdera2.model.selector.LinkRelSelector;
+import static org.apache.abdera2.model.selector.Selectors.withRel;
 
 /**
  * A default Atom Feed to Activity Stream Conversion implementation. 
@@ -155,9 +155,8 @@ public class FeedToActivityConverter {
       obj.setSummary(text.getValue());
     }
     obj.setObjectType(objectType(ext));
-    LinkRelSelector sel = 
-      new LinkRelSelector("alternate","preview");
-    List<Link> links = ext.getExtensions(Constants.LINK, sel);
+    List<Link> links = ext.getExtensions(
+      Constants.LINK, withRel("preview"));
     for (Link link : links) {
       String rel = link.getCanonicalRel();
       if (Link.REL_ALTERNATE.equalsIgnoreCase(rel)) {

Modified: abdera/abdera2/ext/src/main/java/org/apache/abdera2/ext/features/FeaturesHelper.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/ext/src/main/java/org/apache/abdera2/ext/features/FeaturesHelper.java?rev=1199390&r1=1199389&r2=1199390&view=diff
==============================================================================
--- abdera/abdera2/ext/src/main/java/org/apache/abdera2/ext/features/FeaturesHelper.java (original)
+++ abdera/abdera2/ext/src/main/java/org/apache/abdera2/ext/features/FeaturesHelper.java Tue
Nov  8 19:12:16 2011
@@ -23,8 +23,8 @@ import java.util.Set;
 import javax.xml.namespace.QName;
 
 import org.apache.abdera2.Abdera;
-import org.apache.abdera2.common.selector.MultiSelector;
 import org.apache.abdera2.common.selector.Selector;
+import org.apache.abdera2.common.selector.Selectors;
 import org.apache.abdera2.factory.Factory;
 import org.apache.abdera2.model.Collection;
 import org.apache.abdera2.model.Document;
@@ -336,8 +336,7 @@ public final class FeaturesHelper {
      * Select a Collection from the Workspace
      */
     public static Iterable<Collection> select(Workspace workspace, Selector... selectors)
{
-        Selector<Collection> selector = MultiSelector.and(selectors);
-        return workspace.getCollections(selector);
+        return workspace.getCollections(Selectors.and(selectors));
     }
 
 }

Modified: abdera/abdera2/test/src/main/java/org/apache/abdera2/test/ext/features/FeatureTest.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/test/src/main/java/org/apache/abdera2/test/ext/features/FeatureTest.java?rev=1199390&r1=1199389&r2=1199390&view=diff
==============================================================================
--- abdera/abdera2/test/src/main/java/org/apache/abdera2/test/ext/features/FeatureTest.java
(original)
+++ abdera/abdera2/test/src/main/java/org/apache/abdera2/test/ext/features/FeatureTest.java
Tue Nov  8 19:12:16 2011
@@ -27,8 +27,6 @@ import org.apache.abdera2.ext.features.F
 import org.apache.abdera2.ext.features.FeatureSelector;
 import org.apache.abdera2.ext.features.Features;
 import org.apache.abdera2.ext.features.FeaturesHelper;
-import org.apache.abdera2.model.selector.CollectionAcceptSelector;
-import org.apache.abdera2.model.selector.XPathSelector;
 import org.apache.abdera2.ext.features.FeaturesHelper.Status;
 import org.apache.abdera2.model.Collection;
 import org.apache.abdera2.model.Document;
@@ -36,6 +34,8 @@ import org.apache.abdera2.model.Service;
 import org.apache.abdera2.model.Workspace;
 import org.junit.Test;
 
+import static org.apache.abdera2.model.selector.Selectors.*;
+
 public class FeatureTest {
 
     @Test
@@ -83,16 +83,19 @@ public class FeatureTest {
 
         assertEquals(collections.iterator().next(), collection1);
 
-        Selector s2 = new CollectionAcceptSelector("image/png");
-
-        collections = FeaturesHelper.select(service, s2);
+        collections = FeaturesHelper.select(service, accepts("image/png"));
 
         assertEquals(collections.iterator().next(), collection2);
-
-        XPathSelector s3 = new XPathSelector("f:features/f:feature[@ref='" + FeaturesHelper.FEATURE_SUPPORTS_DRAFTS
+ "']");
-        s3.addNamespace("f", FeaturesHelper.FNS);
         
-        collections = FeaturesHelper.select(service, s3);
+        collections = 
+          FeaturesHelper
+            .select(
+              service, 
+              xpath()
+                .path(String.format("f:features/f:feature[@ref='%s']",
+                  FeaturesHelper.FEATURE_SUPPORTS_DRAFTS))
+                .with("f",FeaturesHelper.FNS)
+                .get());
 
         assertEquals(collections.iterator().next(), collection1);
     }

Modified: abdera/abdera2/test/src/main/java/org/apache/abdera2/test/selector/SelectorTest.java
URL: http://svn.apache.org/viewvc/abdera/abdera2/test/src/main/java/org/apache/abdera2/test/selector/SelectorTest.java?rev=1199390&r1=1199389&r2=1199390&view=diff
==============================================================================
--- abdera/abdera2/test/src/main/java/org/apache/abdera2/test/selector/SelectorTest.java (original)
+++ abdera/abdera2/test/src/main/java/org/apache/abdera2/test/selector/SelectorTest.java Tue
Nov  8 19:12:16 2011
@@ -7,10 +7,10 @@ import java.util.List;
 import org.apache.abdera2.Abdera;
 import org.apache.abdera2.model.Entry;
 import org.apache.abdera2.model.Link;
-import org.apache.abdera2.model.selector.LinkHrefLangSelector;
-import org.apache.abdera2.model.selector.LinkRelSelector;
 import org.junit.Test;
 
+import static org.apache.abdera2.model.selector.Selectors.*;
+
 public class SelectorTest {
 
   @Test
@@ -22,15 +22,12 @@ public class SelectorTest {
     entry.addLink("foo2", "bar").setHrefLang("fr");
     entry.addLink("foo3", "baz").setHrefLang("fr");
     
-    LinkRelSelector lrs = new LinkRelSelector("bar");
-    
-    List<Link> links = entry.getLinks(lrs);
+    List<Link> links = entry.getLinks(withRel("bar"));
     assertEquals(2,links.size());
     assertEquals("foo1",links.get(0).getHref().toString());
     assertEquals("foo2",links.get(1).getHref().toString());
     
-    LinkHrefLangSelector lhls = new LinkHrefLangSelector("fr");
-    links = entry.getLinks(lhls);
+    links = entry.getLinks(withHrefLang("fr"));
     assertEquals(2,links.size());
     assertEquals("foo2",links.get(0).getHref().toString());
     assertEquals("foo3",links.get(1).getHref().toString());



Mime
View raw message