You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@abdera.apache.org by jm...@apache.org on 2007/09/13 20:49:42 UTC

svn commit: r575400 [3/3] - in /incubator/abdera/java/trunk/extensions/converters: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/abdera/ src/main/java/org/apache/abdera/converter/ src/main/java/o...

Added: incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/SourceConverter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/SourceConverter.java?rev=575400&view=auto
==============================================================================
--- incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/SourceConverter.java (added)
+++ incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/SourceConverter.java Thu Sep 13 11:49:38 2007
@@ -0,0 +1,184 @@
+/*
+* 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.converter.impl;
+
+import java.lang.reflect.AccessibleObject;
+
+import org.apache.abdera.converter.BaseConverter;
+import org.apache.abdera.converter.Conventions;
+import org.apache.abdera.converter.ConversionContext;
+import org.apache.abdera.converter.ObjectContext;
+import org.apache.abdera.converter.annotation.Author;
+import org.apache.abdera.converter.annotation.Category;
+import org.apache.abdera.converter.annotation.Contributor;
+import org.apache.abdera.converter.annotation.Generator;
+import org.apache.abdera.converter.annotation.ID;
+import org.apache.abdera.converter.annotation.Icon;
+import org.apache.abdera.converter.annotation.Link;
+import org.apache.abdera.converter.annotation.Logo;
+import org.apache.abdera.converter.annotation.Rights;
+import org.apache.abdera.converter.annotation.Subtitle;
+import org.apache.abdera.converter.annotation.Title;
+import org.apache.abdera.converter.annotation.Updated;
+import org.apache.abdera.model.DateTime;
+import org.apache.abdera.model.IRIElement;
+import org.apache.abdera.model.Person;
+import org.apache.abdera.model.Source;
+import org.apache.abdera.model.Text;
+import org.apache.abdera.util.Constants;
+
+public class SourceConverter 
+  extends BaseConverter<Source> {
+  
+  @Override 
+  protected Source create(
+    ConversionContext context) {
+      return context.getAbdera().getFactory().newSource();
+  }
+
+  @Override protected void process(
+    Object source, 
+    ObjectContext objectContext,
+    ConversionContext context,
+    Conventions conventions,
+    Source feed, 
+    AccessibleObject accessor) {
+      
+      if (accessor.isAnnotationPresent(ID.class) || 
+          ID.class.equals(conventions.matchConvention(accessor))) {
+        IRIConverter c = new IRIConverter(Constants.ID);
+        Object value = eval(accessor, source);
+        ObjectContext valueContext = new ObjectContext(value,source,accessor);
+        IRIElement iri = c.convert(value, valueContext, context);
+        feed.setIdElement(iri);
+      }
+      
+      else if (accessor.isAnnotationPresent(Title.class) || 
+          Title.class.equals(conventions.matchConvention(accessor))) {
+        TextConverter c = new TextConverter(Constants.TITLE);
+        Object value = eval(accessor, source);
+        ObjectContext valueContext = new ObjectContext(value,source,accessor);
+        Text text = c.convert(value, valueContext, context);
+        feed.setTitleElement(text);
+      }
+    
+      else if (accessor.isAnnotationPresent(Subtitle.class) || 
+          Subtitle.class.equals(conventions.matchConvention(accessor))) {
+        TextConverter c = new TextConverter(Constants.SUBTITLE);
+        Object value = eval(accessor, source);
+        ObjectContext valueContext = new ObjectContext(value,source,accessor);
+        Text text = c.convert(value, valueContext, context);
+        feed.setSubtitleElement(text);
+      }
+      
+      else if (accessor.isAnnotationPresent(Rights.class) || 
+          Rights.class.equals(conventions.matchConvention(accessor))) {
+        TextConverter c = new TextConverter(Constants.RIGHTS);
+        Object value = eval(accessor, source);
+        ObjectContext valueContext = new ObjectContext(value,source,accessor);
+        Text text = c.convert(value, valueContext, context);
+        feed.setRightsElement(text);
+      }
+      
+      else if (accessor.isAnnotationPresent(Updated.class) || 
+          Updated.class.equals(conventions.matchConvention(accessor))) {
+        DateTimeConverter c = new DateTimeConverter(Constants.UPDATED);
+        Object value = eval(accessor, source);
+        ObjectContext valueContext = new ObjectContext(value,source,accessor);
+        DateTime dt = c.convert(value, valueContext, context);
+        feed.setUpdatedElement(dt);        
+      }
+      
+      else if (accessor.isAnnotationPresent(Link.class) || 
+          Link.class.equals(conventions.matchConvention(accessor))) {
+        LinkConverter c = new LinkConverter();
+        Object val = eval(accessor, source);
+        Object[] values = toArray(val);
+        for (Object value : values) {
+          ObjectContext valueContext = new ObjectContext(value,source,accessor);
+          org.apache.abdera.model.Link link = c.convert(value, valueContext, context);
+          if (link != null) feed.addLink(link);
+        }
+      }
+      
+      else if (accessor.isAnnotationPresent(Category.class) || 
+          Category.class.equals(conventions.matchConvention(accessor))) {
+        CategoryConverter c = new CategoryConverter();
+        Object val = eval(accessor, source);
+        Object[] values = toArray(val);
+        for (Object value : values) {
+          ObjectContext valueContext = new ObjectContext(value,source,accessor);
+          org.apache.abdera.model.Category category = c.convert(value, valueContext, context);
+          if (category != null) feed.addCategory(category);
+        }
+      }
+      
+      else if (accessor.isAnnotationPresent(Author.class) || 
+          Author.class.equals(conventions.matchConvention(accessor))) {
+        PersonConverter c = new PersonConverter(Constants.AUTHOR);
+        Object val = eval(accessor, source);
+        Object[] values = toArray(val);
+        for (Object value : values) {
+          ObjectContext valueContext = new ObjectContext(value,source,accessor);
+          Person person = c.convert(value, valueContext, context);
+          if (person != null) feed.addAuthor(person);
+        }
+      }
+      
+      else if (accessor.isAnnotationPresent(Contributor.class) || 
+          Contributor.class.equals(conventions.matchConvention(accessor))) {
+        PersonConverter c = new PersonConverter(Constants.CONTRIBUTOR);
+        Object val = eval(accessor, source);
+        Object[] values = toArray(val);
+        for (Object value : values) {
+          ObjectContext valueContext = new ObjectContext(value,source,accessor);
+          Person person = c.convert(value, valueContext, context);
+          if (person != null) feed.addContributor(person);
+        }
+      }
+      
+      else if (accessor.isAnnotationPresent(Generator.class) || 
+          Generator.class.equals(conventions.matchConvention(accessor))) {
+        GeneratorConverter c = new GeneratorConverter();
+        Object value = eval(accessor, source);
+        ObjectContext valueContext = new ObjectContext(value,source,accessor);
+        org.apache.abdera.model.Generator generator = c.convert(value, valueContext, context);
+        feed.setGenerator(generator);
+      }
+      
+      else if (accessor.isAnnotationPresent(Icon.class) || 
+          Icon.class.equals(conventions.matchConvention(accessor))) {
+        IRIConverter c = new IRIConverter(Constants.ICON);
+        Object value = eval(accessor, source);
+        ObjectContext valueContext = new ObjectContext(value,source,accessor);
+        IRIElement iri = c.convert(value, valueContext, context);
+        feed.setIconElement(iri);
+      }
+      
+      else if (accessor.isAnnotationPresent(Logo.class) || 
+          Logo.class.equals(conventions.matchConvention(accessor))) {
+        IRIConverter c = new IRIConverter(Constants.LOGO);
+        Object value = eval(accessor, source);
+        ObjectContext valueContext = new ObjectContext(value,source,accessor);
+        IRIElement iri = c.convert(value, valueContext, context);
+        feed.setLogoElement(iri);
+      }
+      
+  }
+    
+}

Added: incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/StringConverter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/StringConverter.java?rev=575400&view=auto
==============================================================================
--- incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/StringConverter.java (added)
+++ incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/StringConverter.java Thu Sep 13 11:49:38 2007
@@ -0,0 +1,59 @@
+/*
+* 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.converter.impl;
+
+import java.lang.reflect.AccessibleObject;
+
+import org.apache.abdera.converter.BaseConverter;
+import org.apache.abdera.converter.Conventions;
+import org.apache.abdera.converter.ConversionContext;
+import org.apache.abdera.converter.ObjectContext;
+
+public class StringConverter extends BaseConverter<StringBuffer> {
+  
+  @Override 
+  protected StringBuffer create(
+    ConversionContext context) {
+      return new StringBuffer();
+  }
+
+  @Override 
+  protected void finish(
+    Object source, 
+    ObjectContext objectContext,
+    ConversionContext context,
+    StringBuffer dest) {
+      if (dest.length() == 0) {
+        dest.append(source.toString());
+      }
+  }
+
+  @Override 
+  protected void process(
+    Object source, 
+    ObjectContext objectContext,
+    ConversionContext context, 
+    Conventions conventions, 
+    StringBuffer dest,
+    AccessibleObject accessor) {
+      
+  }
+  
+  
+  
+}

Added: incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/TextConverter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/TextConverter.java?rev=575400&view=auto
==============================================================================
--- incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/TextConverter.java (added)
+++ incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/TextConverter.java Thu Sep 13 11:49:38 2007
@@ -0,0 +1,97 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.converter.impl;
+
+import java.lang.reflect.AccessibleObject;
+
+import javax.xml.namespace.QName;
+
+import org.apache.abdera.converter.Conventions;
+import org.apache.abdera.converter.ConversionContext;
+import org.apache.abdera.converter.ObjectContext;
+import org.apache.abdera.converter.annotation.TextType;
+import org.apache.abdera.converter.annotation.Value;
+import org.apache.abdera.model.Div;
+import org.apache.abdera.model.Text;
+
+public class TextConverter 
+  extends QNameConverter<Text> {
+  
+  protected TextConverter(QName qname) {
+    super(qname);
+  }
+
+  @Override 
+  protected Text create(
+    ConversionContext context) {
+      return context.getAbdera().getFactory()
+        .newText(getQName(), null, null);
+  }
+
+  @Override protected void finish(
+    Object source, 
+    ObjectContext objectContext,
+    ConversionContext context, 
+    Text dest) {
+    String text = dest.getText();
+    if (text == null || text.trim().length() == 0) {
+      dest.setValue(source.toString());
+      
+      TextType tt = objectContext.getAnnotation(TextType.class);
+      Text.Type type = tt != null ? tt.value() : Text.Type.TEXT;
+      
+      dest.setTextType(type);
+      dest.setValue(source.toString());
+      
+    }
+  }
+
+  @Override 
+  protected void process(
+    Object source, 
+    ObjectContext objectContext,
+    ConversionContext context, 
+    Conventions conventions, 
+    Text dest,
+    AccessibleObject accessor) {
+      
+    if (accessor.isAnnotationPresent(Value.class) || 
+        Value.class.equals(conventions.matchConvention(accessor))) {
+     
+      StringConverter c = new StringConverter();
+      Object value = eval(accessor, source);
+      ObjectContext valueContext = new ObjectContext(value);
+      if (value instanceof Div) {
+        dest.setValueElement((Div)value);
+        dest.setTextType(Text.Type.XHTML);
+      } else {
+        StringBuffer result = c.convert(value, valueContext, context);
+        if (result != null) dest.setValue(result.toString());      
+        if (accessor.isAnnotationPresent(TextType.class)) {
+          TextType tt = accessor.getAnnotation(TextType.class);
+          dest.setTextType(tt.value());
+        }
+      }
+    } else if (accessor.isAnnotationPresent(TextType.class) || 
+        TextType.class.equals(conventions.matchConvention(accessor))) {
+      
+    }
+    
+  }
+
+}

Added: incubator/abdera/java/trunk/extensions/converters/src/test/java/org/apache/abdera/test/converter/ConventionsTest.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/converters/src/test/java/org/apache/abdera/test/converter/ConventionsTest.java?rev=575400&view=auto
==============================================================================
--- incubator/abdera/java/trunk/extensions/converters/src/test/java/org/apache/abdera/test/converter/ConventionsTest.java (added)
+++ incubator/abdera/java/trunk/extensions/converters/src/test/java/org/apache/abdera/test/converter/ConventionsTest.java Thu Sep 13 11:49:38 2007
@@ -0,0 +1,85 @@
+/*
+* 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.test.converter;
+
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Annotation;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+import java.lang.reflect.AccessibleObject;
+
+import junit.framework.TestCase;
+
+import org.apache.abdera.Abdera;
+import org.apache.abdera.converter.ConventionConversionContext;
+import org.apache.abdera.converter.Conventions;
+import org.apache.abdera.converter.ObjectContext;
+import org.apache.abdera.converter.annotation.ID;
+import org.apache.abdera.converter.annotation.Title;
+
+public class ConventionsTest extends TestCase {
+
+  /**
+   * Tests the convention matching mechanism
+   */
+  public static void testConventions() throws Exception {
+    
+    Abdera abdera = new Abdera();
+    ConventionConversionContext context = 
+      new ConventionConversionContext(abdera);
+    Conventions conventions = context.getConventions();
+    Foo foo = new Foo();
+    ObjectContext fooContext = new ObjectContext(foo);
+    
+    AccessibleObject[] accessors = fooContext.getAccessors();
+    for (AccessibleObject accessor : accessors) {
+      Match match = accessor.getAnnotation(Match.class);
+      Class<? extends Annotation> matchingAnnotation = 
+        conventions.matchConvention(accessor);
+      if (match != null) {
+        assertEquals(match.value(),matchingAnnotation);
+      } else {
+        assertNull(matchingAnnotation);
+      }
+    }
+  }
+  
+  public static class Foo {
+    @Match(ID.class)
+    public String getId() {
+      return null;
+    }
+    public String getIdentifier() {
+      return null;
+    }
+    @Match(Title.class)
+    public String getTitle() {
+      return null;
+    }
+  }
+  
+  @Retention(RUNTIME)
+  @Target({METHOD,FIELD})
+  public @interface Match {
+    Class<? extends Annotation> value();
+  }
+  
+}

Added: incubator/abdera/java/trunk/extensions/converters/src/test/java/org/apache/abdera/test/converter/ConverterTest.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/converters/src/test/java/org/apache/abdera/test/converter/ConverterTest.java?rev=575400&view=auto
==============================================================================
--- incubator/abdera/java/trunk/extensions/converters/src/test/java/org/apache/abdera/test/converter/ConverterTest.java (added)
+++ incubator/abdera/java/trunk/extensions/converters/src/test/java/org/apache/abdera/test/converter/ConverterTest.java Thu Sep 13 11:49:38 2007
@@ -0,0 +1,94 @@
+/*
+* 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.test.converter;
+
+import java.net.URL;
+import java.util.Date;
+
+import javax.activation.DataHandler;
+import javax.activation.URLDataSource;
+
+import junit.framework.TestCase;
+
+import org.apache.abdera.Abdera;
+import org.apache.abdera.converter.ConventionConversionContext;
+import org.apache.abdera.converter.ConversionContext;
+import org.apache.abdera.converter.annotation.Category;
+import org.apache.abdera.converter.annotation.Feed;
+import org.apache.abdera.converter.annotation.Generator;
+import org.apache.abdera.converter.annotation.Link;
+import org.apache.abdera.converter.annotation.Rel;
+import org.apache.abdera.converter.annotation.Scheme;
+import org.apache.abdera.converter.annotation.TextType;
+import org.apache.abdera.model.Text;
+
+public class ConverterTest extends TestCase {
+
+  public static void testConverter() throws Exception {
+    
+    Abdera abdera = new Abdera();
+    ConversionContext context = new ConventionConversionContext(abdera);
+    
+    FooFeed fooFeed = new FooFeed();
+    
+    Object object = context.convert(fooFeed);
+    
+    System.out.println(object);
+    
+  }
+  
+  @Feed
+  public static class FooFeed {
+    public String id = "http://example.org/1";
+    @TextType(Text.Type.XHTML) public String title = "The title";
+    public Date updated = new Date();
+    @TextType(Text.Type.HTML) public String subtitle = "The subtitle";
+    public String author = "james";
+    @Rel("enclosure") public String link = "foo";
+    @Link @Rel("related") public String related = "bar";
+    public String[] categories = new String[] {"baz","joe"};
+    @Category @Scheme("http://example.org/type") public String type = "Foo";
+    @Generator(version="1.0",uri="http://example.org") public String generator = "Bob";
+    
+    public FooEntry[] entries = new FooEntry[] { new FooEntry() };
+  }
+  
+  public static class FooEntry {
+    public String id = "http://example.org/1/1";
+    @TextType(Text.Type.XHTML) public String title = "The title";
+    public Date updated = new Date();
+    @TextType(Text.Type.HTML) public String summary = "The summary";    
+    public String author = "james";    
+    @Rel("enclosure") public String link = "foo";
+    @Link @Rel("related") public String related = "bar";    
+    public String category = "baz";    
+    @Category @Scheme("http://example.org/type") public String type = "Foo";
+    
+    //@MediaType("text/html") public IRI content = new IRI("http://example.com");
+    
+    public DataHandler content = new DataHandler(new URLDataSource(getUrl()));
+    
+    private static URL getUrl() {
+      try {
+        return new URL("http://www.snellspace.com/wp");
+      } catch (Exception e) {
+        return null;
+      }
+    }
+  }
+}