This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository nuiton-decorator. See http://git.nuiton.org/nuiton-decorator.git commit 172793f2cb0bf4e35f434e9b09c317ac1bc2765e Author: Tony CHEMIT <chemit@codelutin.com> Date: Sun Nov 30 10:12:39 2014 +0100 fixes #3586: Be able to create a new decorator from another one --- .../java/org/nuiton/decorator/DecoratorUtil.java | 73 +++++++++++++++++++--- 1 file changed, 63 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/nuiton/decorator/DecoratorUtil.java b/src/main/java/org/nuiton/decorator/DecoratorUtil.java index 5a3b19c..f0a06a2 100644 --- a/src/main/java/org/nuiton/decorator/DecoratorUtil.java +++ b/src/main/java/org/nuiton/decorator/DecoratorUtil.java @@ -21,6 +21,7 @@ */ package org.nuiton.decorator; +import org.apache.commons.beanutils.MethodUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.nuiton.decorator.JXPathDecorator.Context; @@ -29,6 +30,7 @@ import org.nuiton.decorator.JXPathDecorator.JXPathComparator; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; +import java.util.Date; import java.util.List; import java.util.StringTokenizer; import java.util.regex.Matcher; @@ -131,14 +133,14 @@ public class DecoratorUtil { * When setting conext index to 1, then we will us this expression: * <pre>B - C - A</pre> * - * @param type type of bean - * @param expression initial expression - * @param separator expression context separator + * @param type type of bean + * @param expression initial expression + * @param separator expression context separator * @param separatorReplacement context separator replacement in decorator - * @param <O> type of bean + * @param <O> type of bean * @return the new decorator * @throws IllegalArgumentException ... - * @throws NullPointerException ... + * @throws NullPointerException ... * @since 3.0 */ public static <O> MultiJXPathDecorator<O> newMultiJXPathDecorator( @@ -170,14 +172,14 @@ public class DecoratorUtil { * When setting conext index to 1, then we will us this expression: * <pre>B - A - C</pre> * - * @param type type of bean - * @param expression initial expression - * @param separator expression context separator + * @param type type of bean + * @param expression initial expression + * @param separator expression context separator * @param separatorReplacement context separator replacement in decorator - * @param <O> type of bean + * @param <O> type of bean * @return the new decorator * @throws IllegalArgumentException ... - * @throws NullPointerException ... + * @throws NullPointerException ... * @since 3.0 */ public static <O> MultiJXPathDecorator<O> newMultiJXPathDecoratorKeepingOrder( @@ -200,6 +202,57 @@ public class DecoratorUtil { contexts); } + private static final Object[] EMPTY_CLASS_ARRAY = new Object[0]; + + /** + * Creates a new multijxpath decorator from a given jxpath decorator. + * + * If decorator is clonable, just clone it, otherwise creates a new using his definition. + * + * @param decorator the decorator to clone + * @param <O> generic type of decorator to clone + * @return cloned decorator + * @since 3.0 + */ + public static <O> MultiJXPathDecorator<O> cloneDecorator(JXPathDecorator<O> decorator) { + if (decorator == null) { + throw new NullPointerException( + "can not have a null decorator as parameter"); + } + String separator; + String separatorReplacement; + + if (decorator instanceof Cloneable) { + Cloneable cloneable = (Cloneable) decorator; + + try { + Object clone = MethodUtils.invokeExactMethod(cloneable, + "clone", + EMPTY_CLASS_ARRAY); + return (MultiJXPathDecorator<O>) clone; + } catch (Exception e) { + throw new IllegalStateException("Could not clone decorator " + decorator, e); + } + + } + if (decorator instanceof MultiJXPathDecorator<?>) { + + separator = ((MultiJXPathDecorator<?>) decorator).getSeparator(); + separatorReplacement = ((MultiJXPathDecorator<?>) decorator).getSeparatorReplacement(); + + } else { + + separator = "??" + new Date().getTime(); // trick to avoid collisions + separatorReplacement = " - "; + } + + return newMultiJXPathDecorator(decorator.getType(), + decorator.getInitialExpression(), + separator, + separatorReplacement); + + } + /** * Sort a list of data based on the first token property of a given context * in a given decorator. -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.