cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From reinh...@apache.org
Subject svn commit: r701959 - in /cocoon/cocoon3/trunk/cocoon-optional: ./ src/main/java/org/apache/cocoon/optional/pipeline/components/sax/betwixt/ src/test/java/org/apache/cocoon/optional/pipeline/components/ src/test/java/org/apache/cocoon/optional/pipeline...
Date Mon, 06 Oct 2008 06:46:44 GMT
Author: reinhard
Date: Sun Oct  5 23:46:43 2008
New Revision: 701959

URL: http://svn.apache.org/viewvc?rev=701959&view=rev
Log:
COCOON3-4 add a BetwixtBeanGenerator (+ a unit test)

Added:
    cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/betwixt/
    cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/betwixt/BetwixtBeanGenerator.java
  (with props)
    cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/
    cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/
    cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/betwixt/
  (props changed)
      - copied from r701954, cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/sax/
    cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/betwixt/Animal.java
  (with props)
    cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/betwixt/BetwixtBeanGeneratorTest.java
  (with props)
Removed:
    cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/sax/
Modified:
    cocoon/cocoon3/trunk/cocoon-optional/pom.xml

Modified: cocoon/cocoon3/trunk/cocoon-optional/pom.xml
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-optional/pom.xml?rev=701959&r1=701958&r2=701959&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-optional/pom.xml (original)
+++ cocoon/cocoon3/trunk/cocoon-optional/pom.xml Sun Oct  5 23:46:43 2008
@@ -44,6 +44,26 @@
       <groupId>org.apache.cocoon.pipeline</groupId>
       <artifactId>cocoon-pipeline</artifactId>
     </dependency>
+    <!-- Other libraries -->
+     <dependency>
+      <groupId>commons-betwixt</groupId>
+      <artifactId>commons-betwixt</artifactId>
+      <optional>true</optional>      
+      <exclusions>
+        <exclusion>
+          <groupId>commons-logging</groupId>
+          <artifactId>commons-logging</artifactId>
+        </exclusion>
+        <exclusion>
+          <groupId>commons-digester</groupId>
+          <artifactId>commons-digester</artifactId>
+        </exclusion>
+        <exclusion>
+          <groupId>commons-beanutils</groupId>
+          <artifactId>commons-beanutils</artifactId>
+        </exclusion>
+      </exclusions>
+    </dependency>    
     <!-- Test libraries -->
     <dependency>
       <groupId>junit</groupId>

Added: cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/betwixt/BetwixtBeanGenerator.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/betwixt/BetwixtBeanGenerator.java?rev=701959&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/betwixt/BetwixtBeanGenerator.java
(added)
+++ cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/betwixt/BetwixtBeanGenerator.java
Sun Oct  5 23:46:43 2008
@@ -0,0 +1,84 @@
+/*
+ * 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.betwixt;
+
+import java.beans.IntrospectionException;
+import java.io.IOException;
+
+import org.apache.cocoon.pipeline.component.sax.AbstractGenerator;
+import org.apache.cocoon.pipeline.component.sax.XMLConsumer;
+import org.apache.cocoon.pipeline.util.StringRepresentation;
+import org.apache.commons.betwixt.BindingConfiguration;
+import org.apache.commons.betwixt.XMLIntrospector;
+import org.apache.commons.betwixt.io.SAXBeanWriter;
+import org.xml.sax.SAXException;
+
+public final class BetwixtBeanGenerator extends AbstractGenerator {
+
+    private Object bean;
+
+    private SAXBeanWriter saxBeanWriter;
+
+    private BindingConfiguration bindingConfiguration;
+
+    private XMLIntrospector xmlIntrospector;
+
+    public BetwixtBeanGenerator(Object bean) {
+        this(bean, null, null);
+    }
+
+    public BetwixtBeanGenerator(Object bean, BindingConfiguration bindingConfiguration, XMLIntrospector
xmlIntrospector) {
+        super();
+        if (bean == null) {
+            throw new IllegalArgumentException("A Bean has to be passed.");
+        }
+
+        this.bean = bean;
+        this.bindingConfiguration = bindingConfiguration;
+        this.xmlIntrospector = xmlIntrospector;
+    }
+
+    @Override
+    protected void setXMLConsumer(XMLConsumer xmlConsumer) {
+        this.saxBeanWriter = new SAXBeanWriter(xmlConsumer);
+
+        if (this.bindingConfiguration != null) {
+            this.saxBeanWriter.setBindingConfiguration(this.bindingConfiguration);
+        }
+        if (this.xmlIntrospector != null) {
+            this.saxBeanWriter.setXMLIntrospector(this.xmlIntrospector);
+        }
+    }
+
+    public void execute() {
+        try {
+            this.saxBeanWriter.write(this.bean);
+        } catch (IOException e) {
+            throw new RuntimeException("Fatal protocol violation", e);
+        } catch (SAXException e) {
+            throw new RuntimeException("Fatal XML error", e);
+        } catch (IntrospectionException e) {
+            throw new RuntimeException("Impossible to analyze input bean", e);
+        }
+    }
+
+    @Override
+    public String toString() {
+        return StringRepresentation.buildString(this, "bean=" + this.bean, "bindingConfiguration="
+                + this.bindingConfiguration, "xmlIntrospector=" + this.xmlIntrospector);
+    }
+}

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

Propchange: cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/betwixt/BetwixtBeanGenerator.java
------------------------------------------------------------------------------
    svn:keywords = Id

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

Propchange: cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/betwixt/
------------------------------------------------------------------------------
    svn:mergeinfo = 

Added: cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/betwixt/Animal.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/betwixt/Animal.java?rev=701959&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/betwixt/Animal.java
(added)
+++ cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/betwixt/Animal.java
Sun Oct  5 23:46:43 2008
@@ -0,0 +1,58 @@
+/*
+ * 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.betwixt;
+
+final class Animal {
+
+    private String call;
+
+    private String colour;
+
+    private String latinName;
+
+    private String name;
+
+    int age;
+
+    public Animal(int age, String call, String colour, String latinName, String name) {
+        this.age = age;
+        this.call = call;
+        this.colour = colour;
+        this.latinName = latinName;
+        this.name = name;
+    }
+
+    public int getAge() {
+        return this.age;
+    }
+
+    public String getCall() {
+        return this.call;
+    }
+
+    public String getColour() {
+        return this.colour;
+    }
+
+    public String getLatinName() {
+        return this.latinName;
+    }
+
+    public String getName() {
+        return this.name;
+    }
+}
\ No newline at end of file

Propchange: cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/betwixt/Animal.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/betwixt/Animal.java
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/betwixt/Animal.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/betwixt/BetwixtBeanGeneratorTest.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/betwixt/BetwixtBeanGeneratorTest.java?rev=701959&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/betwixt/BetwixtBeanGeneratorTest.java
(added)
+++ cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/betwixt/BetwixtBeanGeneratorTest.java
Sun Oct  5 23:46:43 2008
@@ -0,0 +1,77 @@
+/*
+ * 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.betwixt;
+
+import static junit.framework.Assert.*;
+
+import java.io.ByteArrayOutputStream;
+
+import org.apache.cocoon.optional.pipeline.components.sax.betwixt.BetwixtBeanGenerator;
+import org.apache.cocoon.pipeline.NonCachingPipeline;
+import org.apache.cocoon.pipeline.Pipeline;
+import org.apache.cocoon.pipeline.component.sax.XMLSerializer;
+import org.apache.commons.betwixt.BindingConfiguration;
+import org.apache.commons.betwixt.IntrospectionConfiguration;
+import org.apache.commons.betwixt.XMLIntrospector;
+import org.custommonkey.xmlunit.Diff;
+import org.junit.Test;
+
+public class BetwixtBeanGeneratorTest {
+
+    @Test
+    public void testPipelineWithBeanGenerator() throws Exception {
+        Animal animal = new Animal(5, "Dook", "albino", "Mustela putoris furo", "Lector");
+
+        Pipeline pipeline = new NonCachingPipeline();
+        pipeline.addComponent(new BetwixtBeanGenerator(animal));
+        pipeline.addComponent(new XMLSerializer());
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        pipeline.setup(baos);
+        pipeline.execute();
+
+        Diff diff = new Diff(
+                "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Animal id=\"1\"><age>5</age><call>Dook</call><colour>albino</colour><latinName>Mustela
putoris furo</latinName><name>Lector</name></Animal>",
+                new String(baos.toByteArray()));
+        assertTrue("Bean generation didn't work as expected " + diff, diff.identical());
+    }
+
+    @Test
+    public void testPipelineWithConfiguredBeanGenerator() throws Exception {
+        Animal animal = new Animal(5, "Dook", "albino", "Mustela putoris furo", "Lector");
+
+        BindingConfiguration bindingConfiguration = new BindingConfiguration();
+        bindingConfiguration.setMapIDs(false);
+
+        XMLIntrospector xmlIntrospector = new XMLIntrospector();
+        IntrospectionConfiguration configuration = xmlIntrospector.getConfiguration();
+        configuration.setAttributesForPrimitives(true);
+
+        Pipeline pipeline = new NonCachingPipeline();
+        pipeline.addComponent(new BetwixtBeanGenerator(animal, bindingConfiguration, xmlIntrospector));
+        pipeline.addComponent(new XMLSerializer());
+
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        pipeline.setup(baos);
+        pipeline.execute();
+
+        Diff diff = new Diff(
+                "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Animal age=\"5\" call=\"Dook\"
colour=\"albino\" latinName=\"Mustela putoris furo\" name=\"Lector\"/>",
+                new String(baos.toByteArray()));
+        assertTrue("Bean generation didn't work as expected " + diff, diff.identical());
+    }
+}

Propchange: cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/betwixt/BetwixtBeanGeneratorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/betwixt/BetwixtBeanGeneratorTest.java
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/betwixt/BetwixtBeanGeneratorTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message