abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r609058 - in /incubator/abdera/java/trunk: core/src/main/java/org/apache/abdera/converter/ core/src/main/java/org/apache/abdera/util/ core/src/test/java/org/apache/abdera/test/core/ extensions/converters/src/main/java/org/apache/abdera/conv...
Date Fri, 04 Jan 2008 23:51:21 GMT
Author: jmsnell
Date: Fri Jan  4 15:51:16 2008
New Revision: 609058

URL: http://svn.apache.org/viewvc?rev=609058&view=rev
Log:
Move all the converter stuff out of the core module and into the converter extension module.
This piece is going to have to remain incomplete for a while longer

Added:
    incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/AbstractConversionContext.java
    incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/ConversionContext.java
    incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/ConversionException.java
    incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/Converter.java
    incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/ConverterProvider.java
    incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/DefaultConversionContext.java
    incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/ObjectContext.java
    incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/annotation/Converter.java
    incubator/abdera/java/trunk/extensions/converters/src/test/java/org/apache/abdera/test/converter/ConversionTest.java
    incubator/abdera/java/trunk/extensions/converters/src/test/java/org/apache/abdera/test/converter/FooConverterProvider.java
    incubator/abdera/java/trunk/extensions/converters/src/test/java/org/apache/abdera/test/converter/TestSuite.java
Removed:
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/converter/
    incubator/abdera/java/trunk/core/src/test/java/org/apache/abdera/test/core/ConversionTest.java
    incubator/abdera/java/trunk/core/src/test/java/org/apache/abdera/test/core/FooConverterProvider.java
Modified:
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbderaConfiguration.java
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/Configuration.java
    incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/ServiceUtil.java
    incubator/abdera/java/trunk/core/src/test/java/org/apache/abdera/test/core/TestSuite.java

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbderaConfiguration.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbderaConfiguration.java?rev=609058&r1=609057&r2=609058&view=diff
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbderaConfiguration.java
(original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/AbderaConfiguration.java
Fri Jan  4 15:51:16 2008
@@ -27,7 +27,6 @@
 import java.util.ResourceBundle;
 
 import org.apache.abdera.Abdera;
-import org.apache.abdera.converter.ConverterProvider;
 import org.apache.abdera.factory.ExtensionFactory;
 import org.apache.abdera.factory.Factory;
 import org.apache.abdera.factory.StreamBuilder;
@@ -83,7 +82,6 @@
   
   private final ResourceBundle bundle;
   private final List<ExtensionFactory> factories;
-  private final List<ConverterProvider> providers;
   private final Map<String,NamedWriter> writers;
   private final Map<String,Class<? extends StreamWriter>> streamwriters;
   private final Map<String,NamedParser> parsers;
@@ -98,7 +96,6 @@
         ServiceUtil.getClassLoader(), 
         Locale.getDefault());
     factories = ServiceUtil.loadExtensionFactories();
-    providers = ServiceUtil.loadConverterProviders();
     writers = initNamedWriters();
     parsers = initNamedParsers();
     streamwriters = initStreamWriters();
@@ -151,16 +148,6 @@
    */
   public List<ExtensionFactory> getExtensionFactories() {
     return factories;
-  }
-  
-  /**
-   * Returns the listing of registered ConverterProvider implementations
-   */
-  public ConverterProvider[] getConverterProviders() {
-    return providers != null ? 
-      providers.toArray(
-        new ConverterProvider[providers.size()]) : 
-          new ConverterProvider[0];
   }
   
   /**

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/Configuration.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/Configuration.java?rev=609058&r1=609057&r2=609058&view=diff
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/Configuration.java
(original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/Configuration.java
Fri Jan  4 15:51:16 2008
@@ -22,7 +22,6 @@
 import java.util.Map;
 
 import org.apache.abdera.Abdera;
-import org.apache.abdera.converter.ConverterProvider;
 import org.apache.abdera.factory.ExtensionFactory;
 import org.apache.abdera.factory.Factory;
 import org.apache.abdera.parser.NamedParser;
@@ -84,11 +83,6 @@
    * Get a new instance of the default StreamWriter impl
    */
   public StreamWriter newStreamWriterInstance(Abdera abdera);
-  
-  /**
-   * Get a listing of ConverterProviders
-   */
-  public ConverterProvider[] getConverterProviders();
   
   /**
    * Get the collection of NamedParsers;

Modified: incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/ServiceUtil.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/ServiceUtil.java?rev=609058&r1=609057&r2=609058&view=diff
==============================================================================
--- incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/ServiceUtil.java
(original)
+++ incubator/abdera/java/trunk/core/src/main/java/org/apache/abdera/util/ServiceUtil.java
Fri Jan  4 15:51:16 2008
@@ -29,7 +29,6 @@
 import java.util.List;
 
 import org.apache.abdera.Abdera;
-import org.apache.abdera.converter.ConverterProvider;
 import org.apache.abdera.factory.ExtensionFactory;
 import org.apache.abdera.factory.Factory;
 import org.apache.abdera.parser.Parser;
@@ -318,11 +317,12 @@
       return factories;
   }
   
-  protected static synchronized List<ConverterProvider> loadConverterProviders() {
-    List<ConverterProvider> providers =
-      _loadimpls(
-        "META-INF/services/org.apache.abdera.converter.ConverterProvider");
-    return providers;    
+  public static synchronized <T>List<T> loadimpls(String sid) {
+    return loadimpls(sid,false);
+  }
+  
+  public static synchronized <T>List<T> loadimpls(String sid, boolean classesonly)
{
+    return _loadimpls(sid,classesonly);
   }
   
   @SuppressWarnings("unchecked")

Modified: incubator/abdera/java/trunk/core/src/test/java/org/apache/abdera/test/core/TestSuite.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/core/src/test/java/org/apache/abdera/test/core/TestSuite.java?rev=609058&r1=609057&r2=609058&view=diff
==============================================================================
--- incubator/abdera/java/trunk/core/src/test/java/org/apache/abdera/test/core/TestSuite.java
(original)
+++ incubator/abdera/java/trunk/core/src/test/java/org/apache/abdera/test/core/TestSuite.java
Fri Jan  4 15:51:16 2008
@@ -25,6 +25,6 @@
 
   public TestSuite() {
     addTestSuite(CoreTest.class);
-    addTestSuite(ConversionTest.class);
+    addTestSuite(EntityTagTest.class);
   }
 }

Added: incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/AbstractConversionContext.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/AbstractConversionContext.java?rev=609058&view=auto
==============================================================================
--- incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/AbstractConversionContext.java
(added)
+++ incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/AbstractConversionContext.java
Fri Jan  4 15:51:16 2008
@@ -0,0 +1,169 @@
+/*
+* 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;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.AccessibleObject;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.abdera.Abdera;
+
+@SuppressWarnings("unchecked")
+public abstract class AbstractConversionContext
+  implements ConversionContext {
+
+  private transient final Abdera abdera;
+  private final Map<Class,Converter> converters = 
+    new HashMap<Class,Converter>();
+  
+  protected AbstractConversionContext() {
+    this(new Abdera());
+  }
+  
+  protected AbstractConversionContext(Abdera abdera) {
+    this.abdera = abdera;
+  }
+  
+  public Abdera getAbdera() {
+    return abdera;
+  }
+  
+  public <T> Converter<T> getConverter(ObjectContext objectContext) {
+    try {
+      Class type = objectContext.getObjectType();
+      Converter<T> converter = converters.get(type);
+      if (converter == null) converter = objectContext.getConverter();
+      if (converter == null) {
+        for (Annotation annotation : objectContext.getAnnotations()) {
+          converter = converters.get(annotation.annotationType());
+          if (converter != null) return converter;
+        }
+      }
+      if (converter == null && !type.isAnnotation()) {
+        for (Class knownType : converters.keySet()) {
+          if (!knownType.isAnnotation() && 
+              knownType.isAssignableFrom(type)) {
+            return converters.get(knownType);
+          }
+        }
+      }
+      return converter;
+    } catch (Throwable t) {
+      throw new ConversionException(t);
+    }
+  }
+
+  public boolean hasConverter(ObjectContext objectContext) {
+    return getConverter(objectContext) != null;
+  }
+  
+  public boolean hasConverter(Object object) {
+    return hasConverter(new ObjectContext(object));
+  }
+  
+  public boolean hasConverter(
+    Object object, 
+    Object parent, 
+    AccessibleObject accessor) {
+      return hasConverter(new ObjectContext(object,parent,accessor));
+  }
+  
+  public boolean hasConverter(Class<?> type) {
+    if (converters.containsKey(type)) return true;
+    if (!type.isAnnotation()) {
+      for (Class<?> t : converters.keySet()) {
+        if (!t.isAnnotation() && 
+            t.isAssignableFrom(type)) return true;
+      }
+    }
+    return false;
+  }
+  
+  public boolean hasConverter(AccessibleObject accessor) {
+    Class<? extends Object> returnType = getReturnType(accessor);
+    org.apache.abdera.converter.annotation.Converter converter = 
+      accessor.getAnnotation(org.apache.abdera.converter.annotation.Converter.class);
+    if (converter != null && hasConverter(converter.value())) return true;
+    if (returnType != null && hasConverter(returnType)) return true;
+    return false;
+  }
+
+  public void setConverter(Class<?> type, Converter<?> converter) {
+    converters.put(type, converter);
+  }
+
+  public <T> T convert(Object object) {
+    return (T)convert(object,new ObjectContext(object));
+  }
+
+  public <T> T convert(Object object, ObjectContext objectContext) {
+    if (objectContext == null) objectContext = new ObjectContext(object);
+    Converter<T> converter = getConverter(objectContext);
+    if (converter != null)
+      return convert(object,objectContext,converter);
+    else 
+      throw new ConversionException(
+        "No converter available for " + 
+        objectContext.getObjectType());
+  }
+
+  public <T> T convert(Object object, Converter<T> converter) {
+    return (T)convert(object,new ObjectContext(object),converter);
+  }
+
+  public <T> T convert(
+    Object object, 
+    ObjectContext objectContext, 
+    Converter<T> converter) {
+      if (objectContext == null) objectContext = new ObjectContext(object);
+      Converter<T> overrideConverter = getConverter(objectContext);
+      if (overrideConverter != null) converter = overrideConverter;
+      if (converter != null) {
+        return converter.convert(object, objectContext, this);
+      } else {
+        throw new ConversionException(
+          "No converter available for " + 
+          objectContext.getObjectType());
+      }
+  }
+  
+  public Object clone() {
+    try {
+      return super.clone();
+    } catch (CloneNotSupportedException e) {
+      return copy();
+    }
+  }
+  
+  protected Object copy() {
+    return new RuntimeException(new CloneNotSupportedException());
+  }
+  
+  public static Class<? extends Object> getReturnType(
+    AccessibleObject accessor) {
+      if (accessor instanceof Field)
+        return ((Field)accessor).getType();
+      else if (accessor instanceof Method) 
+        return ((Method)accessor).getReturnType();
+      else return null;
+  }
+
+}

Added: incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/ConversionContext.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/ConversionContext.java?rev=609058&view=auto
==============================================================================
--- incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/ConversionContext.java
(added)
+++ incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/ConversionContext.java
Fri Jan  4 15:51:16 2008
@@ -0,0 +1,87 @@
+/*
+* 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;
+
+import java.io.Serializable;
+import java.lang.reflect.AccessibleObject;
+
+import org.apache.abdera.Abdera;
+
+public interface ConversionContext
+  extends Cloneable, 
+          Serializable {
+
+  Abdera getAbdera();
+  
+  /**
+   * Convert using the appropriate detected converter
+   */
+  <T>T convert(Object object);
+  
+  /**
+   * Convert using the appropriate detected converter
+   */
+  <T>T convert(Object object, ObjectContext objectContext);
+  
+  /**
+   * Convert using the specified converter
+   */
+  <T>T convert(Object object, Converter<T> converter);
+  
+  /**
+   * Convert using the specified converter
+   */
+  <T>T convert(Object object, ObjectContext objectContext, Converter<T> converter);
+  
+  /**
+   * Specify the appropriate converter for the given object or annotation type
+   */
+  void setConverter(Class<?> type, Converter<?> converter);
+  
+  /**
+   * Get the appropriate converter for the given object or annotation type
+   */
+  <T>Converter<T> getConverter(ObjectContext objectContext);
+  
+  /**
+   * True if a converter for the specified object is available
+   */
+  boolean hasConverter(ObjectContext objectContext);
+
+  /**
+   * True if a converter for the specified object is available
+   */
+  boolean hasConverter(Object object);
+  
+  /**
+   * True if a converter for the specified object is available
+   */
+  boolean hasConverter(Object object, Object parent, AccessibleObject accessor);
+
+  /**
+   * True if a converter for the specified object is available
+   */
+  boolean hasConverter(Class<?> type);
+  
+  /**
+   * True if a converter for the specified accessor is available
+   */
+  boolean hasConverter(AccessibleObject accessor);
+  
+  Object clone();
+}

Added: incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/ConversionException.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/ConversionException.java?rev=609058&view=auto
==============================================================================
--- incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/ConversionException.java
(added)
+++ incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/ConversionException.java
Fri Jan  4 15:51:16 2008
@@ -0,0 +1,45 @@
+/*
+* 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;
+
+public class ConversionException
+    extends RuntimeException {
+
+  private static final long serialVersionUID = 3399703987771955406L;
+
+  public ConversionException() {
+    super();
+  }
+
+  public ConversionException(
+    String message, 
+    Throwable cause) {
+      super(message, cause);
+  }
+
+  public ConversionException(
+    String message) {
+      super(message);
+  }
+
+  public ConversionException(
+    Throwable cause) {
+      super(cause);
+  }
+  
+}

Added: incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/Converter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/Converter.java?rev=609058&view=auto
==============================================================================
--- incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/Converter.java
(added)
+++ incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/Converter.java
Fri Jan  4 15:51:16 2008
@@ -0,0 +1,36 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.converter;
+
+public abstract class Converter<T> {
+
+  public T convert(
+    Object source, 
+    ConversionContext context) {
+      return convert(
+        source, 
+        new ObjectContext(source), 
+        context);
+  }
+  
+  public abstract T convert(
+    Object source,
+    ObjectContext objectContext,
+    ConversionContext context);
+  
+}

Added: incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/ConverterProvider.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/ConverterProvider.java?rev=609058&view=auto
==============================================================================
--- incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/ConverterProvider.java
(added)
+++ incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/ConverterProvider.java
Fri Jan  4 15:51:16 2008
@@ -0,0 +1,41 @@
+/*
+* 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;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Map.Entry;
+
+public abstract class ConverterProvider 
+  implements Iterable<Map.Entry<Class<?>,Converter<?>>>{
+
+  protected Map<Class<?>,Converter<?>> converters = 
+    new HashMap<Class<?>,Converter<?>>();
+  
+  protected void setConverter(
+    Class<?> type, 
+    Converter<?> converter) {
+      converters.put(type, converter);
+  }
+
+  public Iterator<Entry<Class<?>, Converter<?>>> iterator() {
+    return converters.entrySet().iterator();
+  }
+  
+}

Added: incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/DefaultConversionContext.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/DefaultConversionContext.java?rev=609058&view=auto
==============================================================================
--- incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/DefaultConversionContext.java
(added)
+++ incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/DefaultConversionContext.java
Fri Jan  4 15:51:16 2008
@@ -0,0 +1,71 @@
+/*
+* 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;
+
+import java.util.List;
+import java.util.Map;
+
+import org.apache.abdera.Abdera;
+import org.apache.abdera.util.ServiceUtil;
+
+public class DefaultConversionContext 
+  extends AbstractConversionContext {
+
+  private static final long serialVersionUID = 740460842415905883L;
+
+  private final List<ConverterProvider> providers;
+  
+  public DefaultConversionContext() {
+    super();   
+    initConverters();
+    providers = loadConverterProviders();
+  }
+  
+  public DefaultConversionContext(Abdera abdera) {
+    super(abdera);
+    initConverters();
+    providers = loadConverterProviders();
+  }
+  
+  private void initConverters() {
+    ConverterProvider[] providers = getConverterProviders();
+    for (ConverterProvider provider : providers) {
+      for (Map.Entry<Class<?>,Converter<?>> entry : provider) {
+        setConverter(entry.getKey(), entry.getValue());
+      }
+    }
+  }
+  
+  /**
+   * Returns the listing of registered ConverterProvider implementations
+   */
+  public ConverterProvider[] getConverterProviders() {
+    return providers != null ? 
+      providers.toArray(
+        new ConverterProvider[providers.size()]) : 
+          new ConverterProvider[0];
+  }
+  
+  protected static synchronized List<ConverterProvider> loadConverterProviders() {
+    List<ConverterProvider> providers =
+      ServiceUtil.loadimpls(
+        "META-INF/services/org.apache.abdera.converter.ConverterProvider");
+    return providers;    
+  }
+  
+}

Added: incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/ObjectContext.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/ObjectContext.java?rev=609058&view=auto
==============================================================================
--- incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/ObjectContext.java
(added)
+++ incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/ObjectContext.java
Fri Jan  4 15:51:16 2008
@@ -0,0 +1,169 @@
+/*
+* 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;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.AccessibleObject;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+@SuppressWarnings("unchecked")
+public class ObjectContext {
+
+  private final Class objectType;
+  private final Object parent;
+  private final AccessibleObject accessor;
+  private final Annotation[] annotations;
+  private final Converter<?> converter;
+  private final Field[] fields;
+  private final Method[] methods;
+  
+  public ObjectContext(Object object) {
+    this(object,null,null);
+  }
+  
+  public ObjectContext(
+    Object object, 
+    Object parent, 
+    AccessibleObject accessor) {
+      this.objectType = 
+        object != null ? 
+          object.getClass() : 
+        accessor != null ? 
+          AbstractConversionContext.getReturnType(accessor) : 
+          null;
+      this.parent = parent;
+      this.accessor = accessor;
+      this.annotations = initAnnotations();
+      this.converter = initConverter();
+      this.fields = initFields();
+      this.methods = initMethods();
+  }
+  
+  private Field[] initFields() {
+    Field[] fields = objectType.getFields();
+    List<Field> list = new ArrayList<Field>();
+    for (Field field : fields) {
+      int mods = field.getModifiers();
+      // ignore static fields
+      if (!Modifier.isStatic(mods)) {
+        list.add(field);
+      }
+    }
+    return list.toArray(new Field[list.size()]);
+  }
+  
+  private Method[] initMethods() {
+    Method[] methods = objectType.getMethods();
+    List<Method> list = new ArrayList<Method>();
+    for (Method method : methods) {
+      // only methods that have no parameters, return a value, are not
+      // abstract and are not static
+      int mods = method.getModifiers();
+      if (!Modifier.isStatic(mods) &&
+          !Modifier.isAbstract(mods) && 
+          method.getParameterTypes().length == 0 && 
+          method.getReturnType() != Void.class) {
+        list.add(method);
+      }
+    }
+    return list.toArray(new Method[list.size()]);
+  }
+  
+  private Annotation[] initAnnotations() {
+    Map<Class<? extends Annotation>, Annotation> annotations = new 
+    HashMap<Class<? extends Annotation>, Annotation>();
+    if (objectType != null) {
+      for (Annotation annotation : objectType.getAnnotations()) {
+        annotations.put(annotation.annotationType(),annotation);
+      }
+    }
+    if (accessor != null) {
+      for (Annotation annotation : accessor.getAnnotations()) {
+        annotations.put(annotation.annotationType(), annotation);
+      }
+    }
+    return annotations.values().toArray(new Annotation[annotations.size()]);
+  }
+  
+  private Converter initConverter() {
+    try {
+      org.apache.abdera.converter.annotation.Converter conv = 
+        getAnnotation(
+          org.apache.abdera.converter.annotation.Converter.class);
+      if (conv != null) {
+        Class<? extends Converter> convclass = conv.value();
+        return convclass.newInstance();
+      }
+      return null;
+    } catch (Throwable t) {
+      throw new ConversionException(t);
+    }
+  }
+  
+  public AccessibleObject getAccessor() {
+    return accessor;
+  }
+  
+  public Object getParent() {
+    return parent;
+  }
+  
+  public Class getObjectType() {
+    return objectType;
+  }
+  
+  @SuppressWarnings("unchecked")
+  public <X extends Annotation>X getAnnotation(
+    Class<X> annotationType) {
+      for (Annotation annotation : annotations) {
+        if (annotation.annotationType() == annotationType)
+          return (X)annotation;
+      }
+      return null;
+  }
+  
+  public Annotation[] getAnnotations() {
+    return annotations;
+  }
+  
+  @SuppressWarnings("unchecked")
+  public <X>Converter<X> getConverter() {
+    return (Converter<X>) converter;
+  }
+  
+  public Field[] getFields() {
+    return fields;
+  }
+  
+  public Method[] getMethods() {
+    return methods;
+  }
+  
+  public AccessibleObject[] getAccessors() {
+    List<AccessibleObject> list = new ArrayList<AccessibleObject>();
+    for (Method method : methods) list.add(method);
+    for (Field field : fields) list.add(field);
+    return list.toArray(new AccessibleObject[list.size()]);
+  }
+}

Added: incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/annotation/Converter.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/annotation/Converter.java?rev=609058&view=auto
==============================================================================
--- incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/annotation/Converter.java
(added)
+++ incubator/abdera/java/trunk/extensions/converters/src/main/java/org/apache/abdera/converter/annotation/Converter.java
Fri Jan  4 15:51:16 2008
@@ -0,0 +1,33 @@
+/*
+* 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.annotation;
+
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+@Retention(RUNTIME)
+@Target({TYPE,METHOD,FIELD,ANNOTATION_TYPE})
+public @interface Converter {
+  Class<? extends org.apache.abdera.converter.Converter<?>> value();
+}

Added: incubator/abdera/java/trunk/extensions/converters/src/test/java/org/apache/abdera/test/converter/ConversionTest.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/converters/src/test/java/org/apache/abdera/test/converter/ConversionTest.java?rev=609058&view=auto
==============================================================================
--- incubator/abdera/java/trunk/extensions/converters/src/test/java/org/apache/abdera/test/converter/ConversionTest.java
(added)
+++ incubator/abdera/java/trunk/extensions/converters/src/test/java/org/apache/abdera/test/converter/ConversionTest.java
Fri Jan  4 15:51:16 2008
@@ -0,0 +1,122 @@
+/*
+* 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 junit.framework.TestCase;
+
+import org.apache.abdera.converter.ConversionContext;
+import org.apache.abdera.converter.Converter;
+import org.apache.abdera.converter.DefaultConversionContext;
+import org.apache.abdera.converter.ObjectContext;
+import org.apache.abdera.model.Element;
+import org.apache.abdera.model.ElementWrapper;
+
+public class ConversionTest extends TestCase {
+
+  public static void testConverter() 
+    throws Exception {
+    
+    ConversionContext context = new DefaultConversionContext();
+    context.setConverter(Foo.class, new FooConverter());
+    Foo foo = new Foo();
+    
+    ObjectContext fooContext = new ObjectContext(foo);
+    Converter<Element> converter = context.getConverter(fooContext);
+    
+    assertNotNull(converter);
+    assertTrue(converter instanceof FooConverter);
+    
+    Element fooEl = context.convert(foo,fooContext);
+    
+    assertNotNull(fooEl);
+    
+  }
+  
+  public static void testConverterProvider()
+    throws Exception {
+    
+    ConversionContext context = new DefaultConversionContext();
+    Bar bar = new Bar();
+    
+    ObjectContext barContext = new ObjectContext(bar);
+    Converter<Element> converter = context.getConverter(barContext);
+    
+    assertNotNull(converter);
+    assertTrue(converter instanceof FooConverter);
+    
+    Element barEl = context.convert(bar);
+    
+    assertNotNull(barEl);
+    
+  }
+  
+  public static void testConverterAnnotation() 
+    throws Exception {
+    
+    ConversionContext context = new DefaultConversionContext();
+    Baz baz = new Baz();
+    
+    ObjectContext bazContext = new ObjectContext(baz);
+    Converter<Element> converter = context.getConverter(bazContext);
+    
+    assertNotNull(converter);
+    assertTrue(converter instanceof FooConverter);
+    
+    Element bazEl = context.convert(baz);
+    
+    assertNotNull(bazEl);
+    
+  }
+  
+  public static void testNoConverter() 
+    throws Exception {
+
+    ConversionContext context = new DefaultConversionContext();
+    Foo foo = new Foo();
+    
+    ObjectContext fooContext = new ObjectContext(foo);
+    Converter<Element> converter = context.getConverter(fooContext);
+    
+    assertNull(converter);
+    
+    try {
+      context.convert(foo);
+      fail("Expected error not thrown");
+    } catch (Exception e) {}
+   
+  }
+  
+  
+  public static class Foo {}
+  
+  public static class Bar {}
+  
+  @org.apache.abdera.converter.annotation.Converter(FooConverter.class) 
+  public static class Baz {}
+  
+  public static class FooConverter 
+    extends Converter<Element> {
+      public Element convert(
+        Object source,
+        ObjectContext objectContext, 
+        ConversionContext context) {
+          Element el = new ElementWrapper(null) {};
+          return el;
+    }
+  }
+}

Added: incubator/abdera/java/trunk/extensions/converters/src/test/java/org/apache/abdera/test/converter/FooConverterProvider.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/converters/src/test/java/org/apache/abdera/test/converter/FooConverterProvider.java?rev=609058&view=auto
==============================================================================
--- incubator/abdera/java/trunk/extensions/converters/src/test/java/org/apache/abdera/test/converter/FooConverterProvider.java
(added)
+++ incubator/abdera/java/trunk/extensions/converters/src/test/java/org/apache/abdera/test/converter/FooConverterProvider.java
Fri Jan  4 15:51:16 2008
@@ -0,0 +1,31 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* under the Apache License, Version 2.0 (the "License"); you may not
+* use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.test.converter;
+
+import org.apache.abdera.converter.ConverterProvider;
+import org.apache.abdera.test.converter.ConversionTest.Bar;
+import org.apache.abdera.test.converter.ConversionTest.FooConverter;
+
+public class FooConverterProvider 
+  extends ConverterProvider {
+  
+  public FooConverterProvider() {
+    setConverter(Bar.class,new FooConverter());
+  }
+      
+}
\ No newline at end of file

Added: incubator/abdera/java/trunk/extensions/converters/src/test/java/org/apache/abdera/test/converter/TestSuite.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/extensions/converters/src/test/java/org/apache/abdera/test/converter/TestSuite.java?rev=609058&view=auto
==============================================================================
--- incubator/abdera/java/trunk/extensions/converters/src/test/java/org/apache/abdera/test/converter/TestSuite.java
(added)
+++ incubator/abdera/java/trunk/extensions/converters/src/test/java/org/apache/abdera/test/converter/TestSuite.java
Fri Jan  4 15:51:16 2008
@@ -0,0 +1,30 @@
+/*
+* 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;
+
+
+public class TestSuite extends junit.framework.TestSuite {
+  public static void main(String[] args) {
+    junit.textui.TestRunner.run(new TestSuite());
+  }
+
+  public TestSuite() {
+    addTestSuite(ConversionTest.class);
+    addTestSuite(ConventionsTest.class);
+  }
+}



Mime
View raw message