001package org.apache.maven.doxia.macro;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.Map;
023
024import org.apache.maven.doxia.logging.Log;
025import org.apache.maven.doxia.logging.SystemStreamLog;
026import org.apache.maven.doxia.sink.SinkEventAttributes;
027import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet;
028import org.codehaus.plexus.util.StringUtils;
029
030/**
031 * Abstract base class to execute <code>Macro</code>.
032 *
033 * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
034 * @version $Id$
035 * @since 1.0
036 */
037public abstract class AbstractMacro
038    implements Macro
039{
040    /** Log instance. */
041    private Log logger;
042
043    /** {@inheritDoc} */
044    public void enableLogging( Log log )
045    {
046        this.logger = log;
047    }
048
049    /**
050     * Returns a logger for this macro.
051     * If no logger has been configured, a new SystemStreamLog is returned.
052     *
053     * @return Log
054     * @since 1.1
055     */
056    protected Log getLog()
057    {
058        if ( logger == null )
059        {
060            logger = new SystemStreamLog();
061        }
062
063        return logger;
064    }
065
066    /**
067     * Check if the given parameter is required. Throws an
068     * IllegalArgumentException if paramValue is null or empty.
069     *
070     * @param paramName The name of the parameter to check.
071     * @param paramValue The parameter value.
072     * @since 1.1
073     */
074    protected void required( String paramName, String paramValue )
075    {
076        if ( StringUtils.isEmpty( paramValue ) )
077        {
078            throw new IllegalArgumentException( paramName + " is a required parameter!" );
079        }
080    }
081
082    /**
083     * Convert the Map of macro parameters to an AttributeSet.
084     * No check of validity is done, all parameters are added.
085     *
086     * @param parameters the macro parameters.
087     * @return a SinkEventAttributeSet containing the same parameters,
088     *  or null if parameters is null.
089     *
090     * @since 1.1.1.
091     */
092    protected static SinkEventAttributes getAttributesFromMap( Map<?, ?> parameters )
093    {
094        if ( parameters == null )
095        {
096            return null;
097        }
098
099        final int count = parameters.size();
100
101        if ( count <= 0 )
102        {
103            return null;
104        }
105
106        final SinkEventAttributeSet atts = new SinkEventAttributeSet( count );
107
108        for ( Map.Entry<?, ?> entry : parameters.entrySet() )
109        {
110            atts.addAttribute( entry.getKey(), entry.getValue() );
111        }
112
113        return atts;
114    }
115}