abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r575785 - in /incubator/abdera/java/trunk/extensions/converters/src: main/java/org/apache/abdera/converter/impl/ test/java/org/apache/abdera/test/converter/
Date Fri, 14 Sep 2007 20:20:20 GMT
Author: jmsnell
Date: Fri Sep 14 13:20:19 2007
New Revision: 575785

URL: http://svn.apache.org/viewvc?rev=575785&view=rev
Log:
Adding basic extension support

Added:
    incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/ExtensionConverter.java
Modified:
    incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/CategoryConverter.java
    incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/EntryConverter.java
    incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/LinkConverter.java
    incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/PersonConverter.java
    incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/SourceConverter.java
    incubator/abdera/java/trunk/extensions/converters/src/test/java/org/apache/abdera/test/converter/ConverterTest.java

Modified: incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/CategoryConverter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/CategoryConverter.java?rev=575785&r1=575784&r2=575785&view=diff
==============================================================================
--- incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/CategoryConverter.java
(original)
+++ incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/CategoryConverter.java
Fri Sep 14 13:20:19 2007
@@ -19,7 +19,6 @@
 
 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;
@@ -29,7 +28,7 @@
 import org.apache.abdera.model.Category;
 
 public class CategoryConverter 
-  extends BaseConverter<Category> {
+  extends ExtensionConverter<Category> {
 
   @Override 
   protected Category create(
@@ -77,7 +76,7 @@
         if (v != null) dest.setTerm(v.toString());
       }
       
-      if (accessor.isAnnotationPresent(Scheme.class) || 
+      else if (accessor.isAnnotationPresent(Scheme.class) || 
           Scheme.class.equals(conventions.matchConvention(accessor))) {
         Object value = eval(accessor, source);
         ObjectContext valueContext = new ObjectContext(value,source,accessor);
@@ -86,13 +85,23 @@
         if (v != null) dest.setScheme(v.toString());
       }
       
-      if (accessor.isAnnotationPresent(Label.class) || 
+      else if (accessor.isAnnotationPresent(Label.class) || 
           Label.class.equals(conventions.matchConvention(accessor))) {
         Object value = eval(accessor, source);
         ObjectContext valueContext = new ObjectContext(value,source,accessor);
         StringConverter s = new StringConverter();
         StringBuffer v = s.convert(value, valueContext, context);
         if (v != null) dest.setLabel(v.toString());
+      }
+      
+      else {
+        super.process(
+          source, 
+          objectContext, 
+          context, 
+          conventions, 
+          dest, 
+          accessor);
       }
   }
 

Modified: incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/EntryConverter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/EntryConverter.java?rev=575785&r1=575784&r2=575785&view=diff
==============================================================================
--- incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/EntryConverter.java
(original)
+++ incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/EntryConverter.java
Fri Sep 14 13:20:19 2007
@@ -19,7 +19,6 @@
 
 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;
@@ -43,7 +42,7 @@
 import org.apache.abdera.util.Constants;
 
 public class EntryConverter 
-  extends BaseConverter<Entry> {
+  extends ExtensionConverter<Entry> {
   
   @Override 
   protected Entry create(
@@ -178,7 +177,16 @@
         org.apache.abdera.model.Content content = c.convert(value, valueContext, context);
         entry.setContentElement(content);
       }
-      
+
+      else {
+        super.process(
+          source, 
+          objectContext, 
+          context, 
+          conventions, 
+          entry, 
+          accessor);
+      }
   }
     
 }

Added: incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/ExtensionConverter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/ExtensionConverter.java?rev=575785&view=auto
==============================================================================
--- incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/ExtensionConverter.java
(added)
+++ incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/ExtensionConverter.java
Fri Sep 14 13:20:19 2007
@@ -0,0 +1,93 @@
+/*
+* 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.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.Extension;
+import org.apache.abdera.model.Element;
+import org.apache.abdera.model.ExtensibleElement;
+
+public abstract class ExtensionConverter<T extends ExtensibleElement> 
+  extends BaseConverter<T> {
+
+  @Override 
+  protected void process(
+  Object source, 
+  ObjectContext objectContext,
+  ConversionContext context, 
+  Conventions conventions, 
+  T dest,
+  AccessibleObject accessor) {
+    if (accessor.isAnnotationPresent(Extension.class) || 
+        Extension.class.equals(conventions.matchConvention(accessor))) {
+      Object val = eval(accessor, source);
+      Object[] values = toArray(val);
+      for (Object value : values) {
+        ObjectContext valueContext = new ObjectContext(value, source, accessor);
+        Extension ext = valueContext.getAnnotation(Extension.class);
+        QName qname = getQName(ext);
+        if (ext.simple() && qname != null) {
+          StringConverter c = new StringConverter();
+          StringBuffer buf = c.convert(value, valueContext, context);
+          dest.addSimpleExtension(qname, buf.toString());
+        } else {
+          Object converted = context.convert(value, valueContext);
+          if (converted != null && converted instanceof Element) {
+            dest.addExtension((Element)converted);
+          }
+        }
+      }
+    }
+  }
+  
+  protected static boolean isUndefined(String value) {
+    return value == null || "##default".equals(value);
+  }
+  
+  protected static QName getQName(
+    Extension extension) {
+    QName qname = null;
+    if (extension != null) {
+      if (isUndefined(extension.prefix()) && 
+          isUndefined(extension.ns()) && 
+          isUndefined(extension.name())) {
+        qname = new QName(
+          extension.ns(), 
+          extension.name(), 
+          extension.prefix());
+      } else if (
+          isUndefined(extension.prefix()) && 
+          !isUndefined(extension.ns()) && 
+          !isUndefined(extension.name())) {
+        qname = new QName(
+          extension.ns(), 
+          extension.name());
+      } else if (!isUndefined(extension.name())) {
+        qname = new QName(extension.name());
+      }
+    }
+    return qname;
+  }
+}

Modified: incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/LinkConverter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/LinkConverter.java?rev=575785&r1=575784&r2=575785&view=diff
==============================================================================
--- incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/LinkConverter.java
(original)
+++ incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/LinkConverter.java
Fri Sep 14 13:20:19 2007
@@ -21,7 +21,6 @@
 
 import javax.activation.MimeType;
 
-import org.apache.abdera.converter.BaseConverter;
 import org.apache.abdera.converter.Conventions;
 import org.apache.abdera.converter.ConversionContext;
 import org.apache.abdera.converter.ObjectContext;
@@ -35,7 +34,7 @@
 import org.apache.abdera.model.Link;
 
 public class LinkConverter 
-  extends BaseConverter<Link> {
+  extends ExtensionConverter<Link> {
 
   @Override 
   protected Link create(
@@ -89,7 +88,7 @@
         if (v != null) dest.setHref(v.toString());
       }
 
-      if (accessor.isAnnotationPresent(MediaType.class) || 
+      else if (accessor.isAnnotationPresent(MediaType.class) || 
           MediaType.class.equals(conventions.matchConvention(accessor))) {
         Object value = eval(accessor, source);
         if (value != null && value instanceof MimeType) {
@@ -102,7 +101,7 @@
         }
       }
       
-      if (accessor.isAnnotationPresent(Title.class) || 
+      else if (accessor.isAnnotationPresent(Title.class) || 
           Title.class.equals(conventions.matchConvention(accessor))) {
         Object value = eval(accessor, source);
         ObjectContext valueContext = new ObjectContext(value,source,accessor);
@@ -111,7 +110,7 @@
         if (v != null) dest.setTitle(v.toString());
       }
 
-      if (accessor.isAnnotationPresent(HrefLanguage.class) || 
+      else if (accessor.isAnnotationPresent(HrefLanguage.class) || 
           HrefLanguage.class.equals(conventions.matchConvention(accessor))) {
         Object value = eval(accessor, source);
         ObjectContext valueContext = new ObjectContext(value,source,accessor);
@@ -120,7 +119,7 @@
         if (v != null) dest.setHrefLang(v.toString());
       }
 
-      if (accessor.isAnnotationPresent(Rel.class) || 
+      else if (accessor.isAnnotationPresent(Rel.class) || 
           Rel.class.equals(conventions.matchConvention(accessor))) {
         Object value = eval(accessor, source);
         ObjectContext valueContext = new ObjectContext(value,source,accessor);
@@ -129,7 +128,7 @@
         if (v != null) dest.setRel(v.toString());
       }
 
-      if (accessor.isAnnotationPresent(Length.class) || 
+      else if (accessor.isAnnotationPresent(Length.class) || 
           Length.class.equals(conventions.matchConvention(accessor))) {
         Object value = eval(accessor, source);
         if (value instanceof Long) {
@@ -148,6 +147,15 @@
         }
       }
 
+      else {
+        super.process(
+          source, 
+          objectContext, 
+          context, 
+          conventions, 
+          dest, 
+          accessor);
+      }
   }
 
 }

Modified: incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/PersonConverter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/PersonConverter.java?rev=575785&r1=575784&r2=575785&view=diff
==============================================================================
--- incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/PersonConverter.java
(original)
+++ incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/PersonConverter.java
Fri Sep 14 13:20:19 2007
@@ -25,8 +25,10 @@
 import org.apache.abdera.converter.ConversionContext;
 import org.apache.abdera.converter.ObjectContext;
 import org.apache.abdera.converter.annotation.Email;
+import org.apache.abdera.converter.annotation.Extension;
 import org.apache.abdera.converter.annotation.Name;
 import org.apache.abdera.converter.annotation.URI;
+import org.apache.abdera.model.Element;
 import org.apache.abdera.model.IRIElement;
 import org.apache.abdera.model.Person;
 import org.apache.abdera.util.Constants;
@@ -76,7 +78,7 @@
         if (v != null) dest.setName(v.toString());
       }
 
-      if (accessor.isAnnotationPresent(Email.class) || 
+      else if (accessor.isAnnotationPresent(Email.class) || 
           Email.class.equals(conventions.matchConvention(accessor))) {
         Object value = eval(accessor, source);
         ObjectContext valueContext = new ObjectContext(value);
@@ -85,13 +87,34 @@
         if (v != null) dest.setEmail(v.toString());
       }
       
-      if (accessor.isAnnotationPresent(URI.class) || 
+      else if (accessor.isAnnotationPresent(URI.class) || 
           URI.class.equals(conventions.matchConvention(accessor))) {
         Object value = eval(accessor, source);
         ObjectContext valueContext = new ObjectContext(value);
         IRIConverter c = new IRIConverter(Constants.URI);
         IRIElement iri = c.convert(value, valueContext, context);
         if (iri != null) dest.setUriElement(iri);
+      }
+      
+      else if (accessor.isAnnotationPresent(Extension.class) || 
+          Extension.class.equals(conventions.matchConvention(accessor))) {
+        Extension ext = objectContext.getAnnotation(Extension.class);
+        Object val = eval(accessor, source);
+        Object[] values = toArray(val);
+        for (Object value : values) {
+          ObjectContext valueContext = new ObjectContext(value, source, accessor);
+          QName qname = ExtensionConverter.getQName(ext);
+          if (ext.simple() && qname != null) {
+            StringConverter c = new StringConverter();
+            StringBuffer buf = c.convert(value, valueContext, context);
+            dest.addSimpleExtension(qname, buf.toString());
+          } else {
+            Object converted = context.convert(value, valueContext);
+            if (converted != null && converted instanceof Element) {
+              dest.addExtension((Element)converted);
+            }
+          }
+        }
       }
   }
 

Modified: 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=575785&r1=575784&r2=575785&view=diff
==============================================================================
--- incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/SourceConverter.java
(original)
+++ incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/impl/SourceConverter.java
Fri Sep 14 13:20:19 2007
@@ -19,7 +19,6 @@
 
 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;
@@ -43,7 +42,7 @@
 import org.apache.abdera.util.Constants;
 
 public class SourceConverter 
-  extends BaseConverter<Source> {
+  extends ExtensionConverter<Source> {
   
   @Override 
   protected Source create(
@@ -177,6 +176,16 @@
         ObjectContext valueContext = new ObjectContext(value,source,accessor);
         IRIElement iri = c.convert(value, valueContext, context);
         feed.setLogoElement(iri);
+      }
+      
+      else {
+        super.process(
+          source, 
+          objectContext, 
+          context, 
+          conventions, 
+          feed, 
+          accessor);
       }
       
   }

Modified: 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=575785&r1=575784&r2=575785&view=diff
==============================================================================
--- incubator/abdera/java/trunk/extensions/converters/src/test/java/org/apache/abdera/test/converter/ConverterTest.java
(original)
+++ incubator/abdera/java/trunk/extensions/converters/src/test/java/org/apache/abdera/test/converter/ConverterTest.java
Fri Sep 14 13:20:19 2007
@@ -30,6 +30,7 @@
 import org.apache.abdera.converter.ConversionContext;
 import org.apache.abdera.converter.annotation.Category;
 import org.apache.abdera.converter.annotation.Entry;
+import org.apache.abdera.converter.annotation.Extension;
 import org.apache.abdera.converter.annotation.Feed;
 import org.apache.abdera.converter.annotation.Generator;
 import org.apache.abdera.converter.annotation.Link;
@@ -94,6 +95,8 @@
     //@MediaType("text/html") public IRI content = new IRI("http://example.com");
     
     public DataHandler content = new DataHandler(new URLDataSource(getUrl()));
+    
+    @Extension(simple=true,name="foo",ns="urn:foo") public String ext = "extension";
     
     private static URL getUrl() {
       try {



Mime
View raw message