cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject svn commit: r1096155 - in /cocoon/cocoon3/trunk/cocoon-optional/src: main/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/ test/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/
Date Sat, 23 Apr 2011 14:49:10 GMT
Author: simonetripodi
Date: Sat Apr 23 14:49:10 2011
New Revision: 1096155

URL: http://svn.apache.org/viewvc?rev=1096155&view=rev
Log:
fix for COCOON3-58: The org.apache.cocoon.optional.pipeline.components.sax.jaxb.JAXBGenerator
is incomplete
test cases included

Added:
    cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/PluralStemmer.java
  (with props)
    cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/Alias.java
  (with props)
    cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/Equipment.java
  (with props)
    cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/Person.java
  (with props)
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/JAXBGeneratorTestCase.java

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/org/apache/cocoon/optional/pipeline/components/sax/jaxb/JAXBGenerator.java?rev=1096155&r1=1096154&r2=1096155&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
Sat Apr 23 14:49:10 2011
@@ -42,6 +42,8 @@ public class JAXBGenerator extends Abstr
 
     private InMemoryLRUMarshallerCache marshallerCache = InMemoryLRUMarshallerCache.getInstance();
 
+    private PluralStemmer pluralStemmer = PluralStemmer.getInstance();
+
     private GenericType<?> toBeMarshalled;
 
     private String charset = UTF_8;
@@ -124,8 +126,7 @@ public class JAXBGenerator extends Abstr
                 xmlRootElementNameSpace = EMPTY;
             }
 
-            // TODO has to be improved, see PluralStemmers reference impl
-            xmlRootElementName += 's';
+            xmlRootElementName = pluralStemmer.toPlural(xmlRootElementName);
         }
 
         try {

Added: cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/PluralStemmer.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/PluralStemmer.java?rev=1096155&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/PluralStemmer.java
(added)
+++ cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/PluralStemmer.java
Sat Apr 23 14:49:10 2011
@@ -0,0 +1,194 @@
+/*
+ * 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 static java.util.regex.Pattern.CASE_INSENSITIVE;
+import static java.util.regex.Pattern.compile;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Default implementation of the plural name stemmer which
+ * tests for some common English plural/singular patterns and
+ * then uses a simple starts-with algorithm.
+ */
+final class PluralStemmer {
+
+    private static final PluralStemmer INSTANCE = new PluralStemmer();
+
+    public static PluralStemmer getInstance() {
+        return INSTANCE;
+    }
+
+    private final Map<Pattern, String> rules = new LinkedHashMap<Pattern, String>();
+
+    /**
+     * This class can't be instantiated
+     */
+    private PluralStemmer() {
+        // uncountable
+        addIdentityRule("advice");
+        addIdentityRule("air");
+        addIdentityRule("alcohol");
+        addIdentityRule("art");
+        addIdentityRule("beef");
+        addIdentityRule("blood");
+        addIdentityRule("butter");
+        addIdentityRule("cheese");
+        addIdentityRule("chocolate");
+        addIdentityRule("coffee");
+        addIdentityRule("confusion");
+        addIdentityRule("cotton");
+        addIdentityRule("education");
+        addIdentityRule("electricity");
+        addIdentityRule("entertainment");
+        addIdentityRule("equipment");
+        addIdentityRule("experience");
+        addIdentityRule("fiction");
+        addIdentityRule("flour");
+        addIdentityRule("food");
+        addIdentityRule("forgiveness");
+        addIdentityRule("furniture");
+        addIdentityRule("gold");
+        addIdentityRule("grass");
+        addIdentityRule("ground");
+        addIdentityRule("happiness");
+        addIdentityRule("history");
+        addIdentityRule("homework");
+        addIdentityRule("honey");
+        addIdentityRule("hope");
+        addIdentityRule("ice");
+        addIdentityRule("information");
+        addIdentityRule("jam");
+        addIdentityRule("juice");
+        addIdentityRule("knowledge");
+        addIdentityRule("lamb");
+        addIdentityRule("lightning");
+        addIdentityRule("literature");
+        addIdentityRule("love");
+        addIdentityRule("luck");
+        addIdentityRule("luggage");
+        addIdentityRule("meat");
+        addIdentityRule("milk");
+        addIdentityRule("mist");
+        addIdentityRule("money");
+        addIdentityRule("music");
+        addIdentityRule("news");
+        addIdentityRule("noise");
+        addIdentityRule("oil");
+        addIdentityRule("oxygen");
+        addIdentityRule("paper");
+        addIdentityRule("patience");
+        addIdentityRule("pay");
+        addIdentityRule("peace");
+        addIdentityRule("pepper");
+        addIdentityRule("petrol");
+        addIdentityRule("plastic");
+        addIdentityRule("pork");
+        addIdentityRule("power");
+        addIdentityRule("pressure");
+        addIdentityRule("rain");
+        addIdentityRule("rice");
+        addIdentityRule("sadness");
+        addIdentityRule("salt");
+        addIdentityRule("sand");
+        addIdentityRule("shopping");
+        addIdentityRule("silver");
+        addIdentityRule("snow");
+        addIdentityRule("space");
+        addIdentityRule("speed");
+        addIdentityRule("steam");
+        addIdentityRule("sugar");
+        addIdentityRule("sunshine");
+        addIdentityRule("tea");
+        addIdentityRule("tennis");
+        addIdentityRule("time");
+        addIdentityRule("toothpaste");
+        addIdentityRule("traffic");
+        addIdentityRule("trousers");
+        addIdentityRule("vinegar");
+        addIdentityRule("water");
+        addIdentityRule("weather");
+        addIdentityRule("wine");
+        addIdentityRule("wood");
+        addIdentityRule("wool");
+        addIdentityRule("work");
+
+        // irregular
+        addRule("person", "people");
+        addRule("man", "men");
+        addRule("child", "children");
+        addRule("sex", "sexes");
+        addRule("move", "moves");
+
+        // rules for plural
+        addRule("(ax|test)is$", "$1es");
+        addRule("(octop|vir)us$", "$1i");
+        addRule("(alias|status)$", "$1es");
+        addRule("(bu)s$", "$1ses");
+        addRule("(buffal|tomat)o$", "$1oes");
+        addRule("([ti])um$", "$1a");
+        addRule("sis$", "ses");
+        addRule("(?:([^f])fe|([lr])f)$", "$1$2ves");
+        addRule("(hive)$", "$1s");
+        addRule("([^aeiouy]|qu)y$", "$1ies");
+        addRule("(x|ch|ss|sh)$", "$1es"); 
+        addRule("(matr|vert|ind)(?:ix|ex)$", "$1ices");
+        addRule("([m|l])ouse$", "$1ice");
+        addRule("^(ox)$", "$1en");
+        addRule("(quiz)$", "$1zes");
+
+        // normal pattern
+        addRule("s$", "s");
+        addRule("$", "s");
+    }
+
+    private void addIdentityRule(String pattern) {
+        addRule(pattern, "$1");
+    }
+
+    private void addRule(String pattern, String rule) {
+        rules.put(compile(pattern, CASE_INSENSITIVE), rule);
+    }
+
+    /**
+     * Algorithm that supports common English plural patterns to 'pluralize' names.
+     *
+     * If no matches are found then - if one exists - a property starting with the
+     * singular name will be returned.
+     *
+     * @param xmlRootElementName
+     * @return
+     */
+    public String toPlural(final String xmlRootElementName) {
+        for (Entry<Pattern, String> replacer : rules.entrySet()) {
+            Matcher matcher = replacer.getKey().matcher(xmlRootElementName);
+            if (matcher.find()) {
+                return matcher.replaceFirst(replacer.getValue());
+            }
+        }
+
+        return xmlRootElementName;
+    }
+
+}

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

Propchange: cocoon/cocoon3/trunk/cocoon-optional/src/main/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/PluralStemmer.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/PluralStemmer.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/Alias.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/Alias.java?rev=1096155&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/Alias.java
(added)
+++ cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/Alias.java
Sat Apr 23 14:49:10 2011
@@ -0,0 +1,50 @@
+/*
+ * 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 javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+@XmlRootElement(name = "alias")
+@XmlAccessorType(XmlAccessType.FIELD)
+public class Alias {
+
+    @XmlElement
+    private String name;
+
+    @XmlElement
+    private String aka;
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public String getAka() {
+        return aka;
+    }
+
+    public void setAka(String aka) {
+        this.aka = aka;
+    }
+
+}

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

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

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

Added: cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/Equipment.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/Equipment.java?rev=1096155&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/Equipment.java
(added)
+++ cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/Equipment.java
Sat Apr 23 14:49:10 2011
@@ -0,0 +1,27 @@
+/*
+ * 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 javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlRootElement;
+
+@XmlRootElement(name = "equipment")
+@XmlAccessorType(XmlAccessType.FIELD)
+public class Equipment {
+
+}

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

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

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

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/org/apache/cocoon/optional/pipeline/components/sax/jaxb/JAXBGeneratorTestCase.java?rev=1096155&r1=1096154&r2=1096155&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
Sat Apr 23 14:49:10 2011
@@ -50,6 +50,34 @@ public final class JAXBGeneratorTestCase
     }
 
     @Test
+    public void testPipelineWithIrregularNamedBean() throws Exception {
+        Alias alias = new Alias();
+        alias.setName("Simone");
+        alias.setAka("Simo");
+
+        this.internalAssert(new GenericType<Alias[]>(new Alias[]{ alias }) {},
+                "<?xml version=\"1.0\" encoding=\"UTF-8\"?><aliases><alias><name>Simone</name><aka>Simo</aka></alias></aliases>");
+    }
+
+    @Test
+    public void testPipelineWithExceptionNamedBean() throws Exception {
+        Person person = new Person();
+        person.setName("Simone");
+        person.setSurname("Tripodi");
+
+        this.internalAssert(new GenericType<Person[]>(new Person[]{ person }) {},
+                "<?xml version=\"1.0\" encoding=\"UTF-8\"?><people><person><name>Simone</name><surname>Tripodi</surname></person></people>");
+    }
+
+    @Test
+    public void testPipelineWithUncountableNamedBean() throws Exception {
+        Equipment equipment = new Equipment();
+
+        this.internalAssert(new GenericType<Equipment[]>(new Equipment[]{ equipment
}) {},
+                "<?xml version=\"1.0\" encoding=\"UTF-8\"?><equipment><equipment
/></equipment>");
+    }
+
+    @Test
     public void testPipelineWithBeanArray() throws Exception {
         Animal animal = new Animal();
         animal.setAge(5);

Added: cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/Person.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/Person.java?rev=1096155&view=auto
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/Person.java
(added)
+++ cocoon/cocoon3/trunk/cocoon-optional/src/test/java/org/apache/cocoon/optional/pipeline/components/sax/jaxb/Person.java
Sat Apr 23 14:49:10 2011
@@ -0,0 +1,50 @@
+/*
+ * 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 javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+
+@XmlRootElement(name = "person")
+@XmlAccessorType(XmlAccessType.FIELD)
+public class Person {
+
+    @XmlElement
+    private String name;
+
+    @XmlElement
+    private String surname;
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public String getSurname() {
+        return surname;
+    }
+
+    public void setSurname(String surname) {
+        this.surname = surname;
+    }
+
+}

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

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

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



Mime
View raw message