[prev in list] [next in list] [prev in thread] [next in thread] 

List:       xml-cocoon-cvs
Subject:    svn commit: r959417 - in /cocoon/cocoon3/trunk/cocoon-optional/src:
From:       simonetripodi () apache ! org
Date:       2010-06-30 19:26:12
Message-ID: 20100630192612.30AE2238897A () eris ! apache ! org
[Download RAW message or body]

Author: simonetripodi
Date: Wed Jun 30 19:26:11 2010
New Revision: 959417

URL: http://svn.apache.org/viewvc?rev=959417&view=rev
Log:
JAXB generator made final
changed API to marshal objects

Added:
    cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/GenericType.java \
(with props) Removed:
    cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/GenericUtils.java
 Modified:
    cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/JAXBGenerator.java
  cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/Animal.java
  cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/JAXBGeneratorTestCase.java


Added: cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/GenericType.java
                
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-optional/src/main/java/o \
rg/apache/cocoon/optional/pipeline/components/sax/jaxb/GenericType.java?rev=959417&view=auto
 ==============================================================================
--- cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/GenericType.java \
                (added)
+++ cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/GenericType.java \
Wed Jun 30 19:26:11 2010 @@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  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.
+ */
+package org.apache.cocoon.optional.pipeline.components.sax.jaxb;
+
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+
+/**
+ * @version $Id$
+ */
+public abstract class GenericType<T> {
+
+    private final T object;
+
+    private final Class<?> rawType;
+
+    private final Class<?> type;
+
+    public GenericType(T object) {
+        this.object = object;
+        this.rawType = object.getClass();
+
+        Type superclass = this.getClass().getGenericSuperclass();
+        if (!(superclass instanceof ParameterizedType)) {
+            throw new RuntimeException("Missing type parameter.");
+        }
+        ParameterizedType parameterized = (ParameterizedType) superclass;
+        Type type = parameterized.getActualTypeArguments()[0];
+        if (type instanceof ParameterizedType) {
+            ParameterizedType parameterizedType = (ParameterizedType) type;
+            Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
+            this.type = (Class<?>) actualTypeArguments[0];
+        } else {
+            this.type = null;
+        }
+    }
+
+    public final T getObject() {
+        return this.object;
+    }
+
+    public final Class<?> getRawType() {
+        return this.rawType;
+    }
+
+    public final Class<?> getType() {
+        return this.type;
+    }
+
+    @Override
+    public String toString() {
+        return "{ object="
+                + this.object
+                + ", rawType="
+                + this.rawType
+                + ", type="
+                + this.type
+                + " }";
+    }
+
+    
+
+}

Propchange: cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/GenericType.java
                
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/GenericType.java
                
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/GenericType.java
                
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/JAXBGenerator.java
                
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-optional/src/main/java/o \
rg/apache/cocoon/optional/pipeline/components/sax/jaxb/JAXBGenerator.java?rev=959417&r1=959416&r2=959417&view=diff
 ==============================================================================
--- cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/JAXBGenerator.java \
                (original)
+++ cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/JAXBGenerator.java \
Wed Jun 30 19:26:11 2010 @@ -18,8 +18,6 @@
  */
 package org.apache.cocoon.optional.pipeline.components.sax.jaxb;
 
-import java.lang.reflect.Type;
-
 import javax.xml.bind.Marshaller;
 import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.bind.annotation.XmlType;
@@ -34,74 +32,68 @@ import org.xml.sax.helpers.AttributesImp
 /**
  * SAX generator that produces SAX events from Object using the JAXB marshaller.
  */
-public abstract class JAXBGenerator<T> extends AbstractSAXGenerator implements \
                CachingPipelineComponent {
-
-    private static final String DEFAULT = "##default";
+public class JAXBGenerator extends AbstractSAXGenerator implements \
CachingPipelineComponent {  
-    private static final String UTF_8 = "UTF-8";
+    private static String DEFAULT = "##default";
 
-    private static final String EMPTY = "";
+    private static String UTF_8 = "UTF-8";
 
-    private final InMemoryLRUMarshallerCache marshallerCache = \
InMemoryLRUMarshallerCache.getInstance(); +    private static String EMPTY = "";
 
-    private final T toBeMarshalled;
+    private InMemoryLRUMarshallerCache marshallerCache = \
InMemoryLRUMarshallerCache.getInstance();  
-    private final Class<?> type;
+    private GenericType<?> toBeMarshalled;
 
     private String charset = UTF_8;
 
     private boolean formattedOutput = false;
 
-    private boolean pluralize = false;
-
-    public JAXBGenerator(T toBeMarshalled) {
+    public JAXBGenerator(GenericType<?> toBeMarshalled) {
         if (toBeMarshalled == null) {
             throw new IllegalArgumentException("Argument 'toBeMarshalled' must not \
be null");  }
         this.toBeMarshalled = toBeMarshalled;
-        Class<?> rawType = toBeMarshalled.getClass();
-        // analyze generics for collections
-        if (Iterable.class.isAssignableFrom(rawType)) {
-            Type type = GenericUtils.getSuperclassTypeParameter(this.getClass());
-            if (type == null) {
-                throw new IllegalArgumentException("Generic type is not defined");
-            }
-            this.type = GenericUtils.getGenericParamType(type);
-            this.pluralize = true;
-        } else if (rawType.isArray()) { // else get array type
-            this.pluralize = true;
-            this.type = rawType.getComponentType();
-        } else { // otherwise is the passed argument type
-            this.type = rawType;
-        }
     }
 
-    public final String getCharset() {
+    public String getCharset() {
         return this.charset;
     }
 
-    public final void setCharset(String charset) {
+    public void setCharset(String charset) {
         this.charset = charset;
     }
 
-    public final boolean isFormattedOutput() {
+    public boolean isFormattedOutput() {
         return this.formattedOutput;
     }
 
-    public final void setFormattedOutput(boolean formattedOutput) {
+    public void setFormattedOutput(boolean formattedOutput) {
         this.formattedOutput = formattedOutput;
     }
 
-    public final void execute() {
+    public void execute() {
         // retrieve the right type has to be marshaled
         String xmlRootElementName = null;
         String xmlRootElementNameSpace = null;
+        boolean pluralize = false;
+        Class<?> type;
+
+        // analyze generics for collections
+        if (Iterable.class.isAssignableFrom(toBeMarshalled.getRawType())) {
+            type = this.toBeMarshalled.getType();
+            pluralize = true;
+        } else if (this.toBeMarshalled.getRawType().isArray()) { // else get array \
type +            pluralize = true;
+            type = this.toBeMarshalled.getRawType().getComponentType();
+        } else { // otherwise is the passed argument type
+            type = this.toBeMarshalled.getRawType();
+        }
 
         // validate the type can be marshaled using JAXB
-        if (!this.type.isAnnotationPresent(XmlRootElement.class)
-                && !this.type.isAnnotationPresent(XmlType.class)) {
+        if (!type.isAnnotationPresent(XmlRootElement.class)
+                && !type.isAnnotationPresent(XmlType.class)) {
             throw new IllegalArgumentException("Object of type "
-                    + this.type.getName()
+                    + type.getName()
                     + " can't be marshalled since neither of "
                     + XmlRootElement.class.getName()
                     + " or "
@@ -110,14 +102,14 @@ public abstract class JAXBGenerator<T> e
         }
 
         // get the root element name if needed
-        if (this.pluralize) {
+        if (pluralize) {
             String name = null;
-            if (this.type.isAnnotationPresent(XmlRootElement.class)) {
-                XmlRootElement xmlRootElement = \
this.type.getAnnotation(XmlRootElement.class); +            if \
(type.isAnnotationPresent(XmlRootElement.class)) { +                XmlRootElement \
xmlRootElement = type.getAnnotation(XmlRootElement.class);  name = \
xmlRootElement.name();  xmlRootElementNameSpace = xmlRootElement.namespace();
-            } else if (this.type.isAnnotationPresent(XmlType.class)) {
-                XmlType xmlType = this.type.getAnnotation(XmlType.class);
+            } else if (type.isAnnotationPresent(XmlType.class)) {
+                XmlType xmlType = type.getAnnotation(XmlType.class);
                 name = xmlType.name();
                 xmlRootElementNameSpace = xmlType.namespace();
             }
@@ -125,7 +117,7 @@ public abstract class JAXBGenerator<T> e
             if (name != null && !DEFAULT.equals(name)) {
                 xmlRootElementName = name;
             } else {
-                xmlRootElementName = this.type.getSimpleName();
+                xmlRootElementName = type.getSimpleName();
             }
 
             if (DEFAULT.equals(xmlRootElementNameSpace)) {
@@ -144,17 +136,17 @@ public abstract class JAXBGenerator<T> e
             xmlMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, \
this.formattedOutput);  
             // setup the Marshaler if a collection has to be marshaled
-            if (this.pluralize) {
+            if (pluralize) {
                 xmlMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
                 this.getSAXConsumer().startDocument();
                 this.getSAXConsumer().startElement(xmlRootElementNameSpace, \
xmlRootElementName, xmlRootElementName, new AttributesImpl());  
-                if (Iterable.class.isAssignableFrom(this.toBeMarshalled.getClass())) \
                {
-                    for (Object object : (Iterable<Object>) this.toBeMarshalled) {
+                if (Iterable.class.isAssignableFrom(this.toBeMarshalled.getRawType())) \
{ +                    for (Object object : (Iterable<Object>) \
                this.toBeMarshalled.getObject()) {
                         xmlMarshaller.marshal(object, this.getSAXConsumer());
                     }
-                } else if (this.toBeMarshalled.getClass().isArray()) {
-                    for (Object object : (Object[]) this.toBeMarshalled) {
+                } else if (this.toBeMarshalled.getRawType().isArray()) {
+                    for (Object object : (Object[]) this.toBeMarshalled.getObject()) \
                {
                         xmlMarshaller.marshal(object, this.getSAXConsumer());
                     }
                 }
@@ -162,16 +154,16 @@ public abstract class JAXBGenerator<T> e
                 this.getSAXConsumer().endElement(xmlRootElementNameSpace, \
xmlRootElementName, xmlRootElementName);  this.getSAXConsumer().endDocument();
             } else {
-                xmlMarshaller.marshal(this.toBeMarshalled, this.getSAXConsumer());
+                xmlMarshaller.marshal(this.toBeMarshalled.getObject(), \
this.getSAXConsumer());  }
         } catch (Exception e) {
             throw new ProcessingException("Impossible to marshal object of type "
-                    + this.type
+                    + type
                     + " to XML", e);
         }
     }
 
-    public final CacheKey constructCacheKey() {
+    public CacheKey constructCacheKey() {
         return new ObjectCacheKey(this.toBeMarshalled);
     }
 

Modified: cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/Animal.java
                
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-optional/src/test/java/o \
rg/apache/cocoon/optional/pipeline/components/sax/jaxb/Animal.java?rev=959417&r1=959416&r2=959417&view=diff
 ==============================================================================
--- cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/Animal.java \
                (original)
+++ cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/Animal.java \
Wed Jun 30 19:26:11 2010 @@ -18,7 +18,6 @@ package org.apache.cocoon.optional.pipel
 
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
-import javax.xml.bind.annotation.XmlAttribute;
 import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.XmlRootElement;
 
@@ -81,4 +80,19 @@ final class Animal {
         this.age = age;
     }
 
+    @Override
+    public String toString() {
+        return "Animal [call="
+                + call
+                + ", colour="
+                + colour
+                + ", latinName="
+                + latinName
+                + ", name="
+                + name
+                + ", age="
+                + age
+                + "]";
+    }
+
 }

Modified: cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/JAXBGeneratorTestCase.java
                
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-optional/src/test/java/o \
rg/apache/cocoon/optional/pipeline/components/sax/jaxb/JAXBGeneratorTestCase.java?rev=959417&r1=959416&r2=959417&view=diff
 ==============================================================================
--- cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/JAXBGeneratorTestCase.java \
                (original)
+++ cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/JAXBGeneratorTestCase.java \
Wed Jun 30 19:26:11 2010 @@ -45,7 +45,7 @@ public final class JAXBGeneratorTestCase
         animal.setLatinName("Mustela putoris furo");
         animal.setName("Lector");
 
-        this.internalAssert(new JAXBGenerator<Animal>(animal) {},
+        this.internalAssert(new GenericType<Animal>(animal) {},
                 "<?xml version=\"1.0\" \
encoding=\"UTF-8\"?><animal><call>Dook</call><colour>albino</colour><latinName>Mustela \
putoris furo</latinName><name>Lector</name><age>5</age></animal>");  }
 
@@ -58,7 +58,7 @@ public final class JAXBGeneratorTestCase
         animal.setLatinName("Mustela putoris furo");
         animal.setName("Lector");
 
-        this.internalAssert(new JAXBGenerator<Animal[]>(new Animal[]{ animal, animal \
}) {}, +        this.internalAssert(new GenericType<Animal[]>(new Animal[]{ animal, \
animal }) {},  "<?xml version=\"1.0\" \
encoding=\"UTF-8\"?><animals><animal><call>Dook</call><colour>albino</colour><latinName>Mustela \
putoris furo</latinName><name>Lector</name><age>5</age></animal><animal><call>Dook</call><colour>albino</colour><latinName>Mustela \
putoris furo</latinName><name>Lector</name><age>5</age></animal></animals>");  }
 
@@ -75,13 +75,13 @@ public final class JAXBGeneratorTestCase
         animals.add(animal);
         animals.add(animal);
 
-        this.internalAssert(new JAXBGenerator<List<Animal>>(animals) {},
+        this.internalAssert(new GenericType<List<Animal>>(animals) {},
                 "<?xml version=\"1.0\" \
encoding=\"UTF-8\"?><animals><animal><call>Dook</call><colour>albino</colour><latinName>Mustela \
putoris furo</latinName><name>Lector</name><age>5</age></animal><animal><call>Dook</call><colour>albino</colour><latinName>Mustela \
putoris furo</latinName><name>Lector</name><age>5</age></animal></animals>");  }
 
-    private void internalAssert(JAXBGenerator<?> generator, String expected) throws \
Exception { +    private void internalAssert(GenericType<?> genericType, String \
                expected) throws Exception {
         Pipeline<SAXPipelineComponent> pipeline = new \
                NonCachingPipeline<SAXPipelineComponent>();
-        pipeline.addComponent(generator);
+        pipeline.addComponent(new JAXBGenerator(genericType));
         pipeline.addComponent(new XMLSerializer());
 
         ByteArrayOutputStream baos = new ByteArrayOutputStream();


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic