View Javadoc

1   package org.apache.maven.doxia.macro;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Map;
23  
24  import org.apache.maven.doxia.logging.Log;
25  import org.apache.maven.doxia.logging.SystemStreamLog;
26  import org.apache.maven.doxia.sink.SinkEventAttributeSet;
27  import org.apache.maven.doxia.sink.SinkEventAttributes;
28  
29  import org.codehaus.plexus.util.StringUtils;
30  
31  /**
32   * Abstract base class to execute <code>Macro</code>.
33   *
34   * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
35   * @version $Id: AbstractMacro.java 1090706 2011-04-09 23:15:28Z hboutemy $
36   * @since 1.0
37   */
38  public abstract class AbstractMacro
39      implements Macro
40  {
41      /** Log instance. */
42      private Log logger;
43  
44      /** {@inheritDoc} */
45      public void enableLogging( Log log )
46      {
47          this.logger = log;
48      }
49  
50      /**
51       * Returns a logger for this macro.
52       * If no logger has been configured, a new SystemStreamLog is returned.
53       *
54       * @return Log
55       * @since 1.1
56       */
57      protected Log getLog()
58      {
59          if ( logger == null )
60          {
61              logger = new SystemStreamLog();
62          }
63  
64          return logger;
65      }
66  
67      /**
68       * Check if the given parameter is required. Throws an
69       * IllegalArgumentException if paramValue is null or empty.
70       *
71       * @param paramName The name of the parameter to check.
72       * @param paramValue The parameter value.
73       * @since 1.1
74       */
75      protected void required( String paramName, String paramValue )
76      {
77          if ( StringUtils.isEmpty( paramValue ) )
78          {
79              throw new IllegalArgumentException( paramName + " is a required parameter!" );
80          }
81      }
82  
83      /**
84       * Convert the Map of macro parameters to an AttributeSet.
85       * No check of validity is done, all parameters are added.
86       *
87       * @param parameters the macro parameters.
88       * @return a SinkEventAttributeSet containing the same parameters,
89       *  or null if parameters is null.
90       *
91       * @since 1.1.1.
92       */
93      protected static SinkEventAttributes getAttributesFromMap( Map<?, ?> parameters )
94      {
95          if ( parameters == null )
96          {
97              return null;
98          }
99  
100         final int count = parameters.size();
101 
102         if ( count <= 0 )
103         {
104             return null;
105         }
106 
107         final SinkEventAttributeSet atts = new SinkEventAttributeSet( count );
108 
109         for ( Map.Entry<?, ?> entry : parameters.entrySet() )
110         {
111             atts.addAttribute( entry.getKey(), entry.getValue() );
112         }
113 
114         return atts;
115     }
116 }