/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.rendering.macro;

import org.xwiki.component.annotation.Requirement;
import org.xwiki.component.logging.AbstractLogEnabled;
import org.xwiki.component.phase.Initializable;
import org.xwiki.component.phase.InitializationException;
import org.xwiki.properties.BeanManager;
import org.xwiki.rendering.macro.descriptor.ContentDescriptor;
import org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor;
import org.xwiki.rendering.macro.descriptor.DefaultMacroDescriptor;
import org.xwiki.rendering.macro.descriptor.MacroDescriptor;

/**
 * @param <P> the type of the macro parameters bean
 * @version $Id$
 * @since 1.5M2
 */
public abstract class AbstractMacro<P> extends AbstractLogEnabled implements Macro<P>, Initializable
{
    /**
     * The {@link BeanManager} component.
     */
    @Requirement
    protected BeanManager beanManager;

    /**
     * Macro description used to generate the macro descriptor.
     */
    private String description;

    /**
     * Content descriptor used to generate the macro descriptor.
     */
    private ContentDescriptor contentDescriptor;

    /**
     * Parameter bean class used to generate the macro descriptor.
     */
    private Class< ? > parametersBeanClass;

    /**
     * The descriptor of the macro.
     */
    private MacroDescriptor macroDescriptor;

    /**
     * @see Macro#getPriority()
     */
    private int priority = 1000;

    /**
     * Creates a new {@link Macro} instance.
     * 
     * @param description a string describing this macro.
     */
    public AbstractMacro(String description)
    {
        this.description = description;
        this.contentDescriptor = new DefaultContentDescriptor();
        this.parametersBeanClass = Object.class;
    }
    
    /**
     * Creates a new {@link Macro} instance.
     * 
     * @param description a string describing this macro.
     * @param contentDescriptor {@link ContentDescriptor} for this macro.
     */
    public AbstractMacro(String description, ContentDescriptor contentDescriptor)
    {
        this(description);
        this.contentDescriptor = contentDescriptor;
    }
    
    /**
     * Creates a new {@link Macro} instance.
     * 
     * @param description a string describing this macro.
     * @param parametersBeanClass class of the parameters bean of this macro.
     */
    public AbstractMacro(String description,  Class< ? > parametersBeanClass)
    {
        this(description);
        this.parametersBeanClass = parametersBeanClass;
    }

    /**
     * Creates a new {@link Macro} instance.
     * 
     * @param description string describing this macro.
     * @param contentDescriptor the {@link ContentDescriptor} describing the content of this macro.
     * @param parametersBeanClass class of the parameters bean.
     */
    public AbstractMacro(String description, ContentDescriptor contentDescriptor, Class< ? > parametersBeanClass)
    {
        this(description, contentDescriptor);
        this.parametersBeanClass = parametersBeanClass;
    }

    /**
     * {@inheritDoc}
     */
    public void initialize() throws InitializationException
    {
        setDescriptor(new DefaultMacroDescriptor(description, contentDescriptor, beanManager
            .getBeanDescriptor(parametersBeanClass)));
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.xwiki.rendering.macro.Macro#getPriority()
     */
    public int getPriority()
    {
        return this.priority;
    }

    /**
     * @param priority the macro priority to use (lower means execute before others)
     */
    public void setPriority(int priority)
    {
        this.priority = priority;
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.xwiki.rendering.macro.Macro#getDescriptor()
     */
    public MacroDescriptor getDescriptor()
    {
        return this.macroDescriptor;
    }

    /**
     * {@inheritDoc}
     * 
     * @see java.lang.Comparable#compareTo(java.lang.Object)
     */
    public int compareTo(Macro< ? > macro)
    {
        if (getPriority() != macro.getPriority()) {
            return getPriority() - macro.getPriority();
        }
        return this.getClass().getSimpleName().compareTo(macro.getClass().getSimpleName());
    }

    /**
     * Allows macro classes extending other macro classes to override the macro descriptor with their own.
     * 
     * @param descriptor the overriding descriptor to set
     */
    protected void setDescriptor(MacroDescriptor descriptor)
    {
        this.macroDescriptor = descriptor;
    }
}

@@ -1243,78 +1243,8 @@
 or;%0A
-import org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor;%0A
 impo
@@ -2422,17 +2422,17 @@
     this
-.
+(
 descript
@@ -2438,123 +2438,15 @@
 tion
- = description;%0A        this.contentDescriptor = new DefaultContentDescriptor();%0A        this.parametersBeanClass =
+, null,
  Obj
@@ -2454,24 +2454,25 @@
 ct.class
+)
 ;%0A    %7D%0A
     %0A   
@@ -2455,36 +2455,32 @@
 t.class);%0A    %7D%0A
-    
 %0A    /**%0A     * 
@@ -2777,32 +2777,18 @@
 cription
-);%0A        this.
+, 
 contentD
@@ -2800,36 +2800,31 @@
 ptor
- = contentDescriptor
+, Object.class)
 ;%0A    %7D%0A
     
@@ -2819,20 +2819,16 @@
 ;%0A    %7D%0A
-    
 %0A    /**
@@ -3069,17 +3069,16 @@
 ription,
- 
  Class%3C 
@@ -3136,45 +3136,15 @@
 tion
-);%0A        this.parametersBeanClass =
+, null,
  par
@@ -3151,32 +3151,33 @@
 ametersBeanClass
+)
 ;%0A    %7D%0A%0A    /**
@@ -3582,33 +3582,33 @@
   %7B%0A        this
-(
+.
 description, con
@@ -3606,28 +3606,71 @@
 tion
-, contentDescriptor)
+ = description;%0A        this.parametersBeanClass = Object.class
 ;%0A  
@@ -3913,28 +3913,16 @@
 criptor,
- beanManager
 %0A       
@@ -3926,16 +3926,27 @@
         
+beanManager
 .getBean

Support creating wiki content based rendering macros * fix AbstractMacro initialization