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 * @since 1.0
035 */
036public abstract class AbstractMacro
037    implements Macro
038{
039    /** Log instance. */
040    private Log logger;
041
042    /** {@inheritDoc} */
043    public void enableLogging( Log log )
044    {
045        this.logger = log;
046    }
047
048    /**
049     * Returns a logger for this macro.
050     * If no logger has been configured, a new SystemStreamLog is returned.
051     *
052     * @return Log
053     * @since 1.1
054     */
055    protected Log getLog()
056    {
057        if ( logger == null )
058        {
059            logger = new SystemStreamLog();
060        }
061
062        return logger;
063    }
064
065    /**
066     * Check if the given parameter is required. Throws an
067     * IllegalArgumentException if paramValue is null or empty.
068     *
069     * @param paramName The name of the parameter to check.
070     * @param paramValue The parameter value.
071     * @since 1.1
072     */
073    protected void required( String paramName, String paramValue )
074    {
075        if ( StringUtils.isEmpty( paramValue ) )
076        {
077            throw new IllegalArgumentException( paramName + " is a required parameter!" );
078        }
079    }
080
081    /**
082     * Convert the Map of macro parameters to an AttributeSet.
083     * No check of validity is done, all parameters are added.
084     *
085     * @param parameters the macro parameters.
086     * @return a SinkEventAttributeSet containing the same parameters,
087     *  or null if parameters is null.
088     * @since 1.1.1.
089     */
090    protected static SinkEventAttributes getAttributesFromMap( Map<?, ?> parameters )
091    {
092        if ( parameters == null )
093        {
094            return null;
095        }
096
097        final int count = parameters.size();
098
099        if ( count <= 0 )
100        {
101            return null;
102        }
103
104        final SinkEventAttributeSet atts = new SinkEventAttributeSet( count );
105
106        for ( Map.Entry<?, ?> entry : parameters.entrySet() )
107        {
108            atts.addAttribute( entry.getKey(), entry.getValue() );
109        }
110
111        return atts;
112    }
113}