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.SinkEventAttributes;
27 import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet;
28 import org.codehaus.plexus.util.StringUtils;
29
30 /**
31 * Abstract base class to execute <code>Macro</code>.
32 *
33 * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
34 * @version $Id$
35 * @since 1.0
36 */
37 public abstract class AbstractMacro
38 implements Macro
39 {
40 /** Log instance. */
41 private Log logger;
42
43 /** {@inheritDoc} */
44 public void enableLogging( Log log )
45 {
46 this.logger = log;
47 }
48
49 /**
50 * Returns a logger for this macro.
51 * If no logger has been configured, a new SystemStreamLog is returned.
52 *
53 * @return Log
54 * @since 1.1
55 */
56 protected Log getLog()
57 {
58 if ( logger == null )
59 {
60 logger = new SystemStreamLog();
61 }
62
63 return logger;
64 }
65
66 /**
67 * Check if the given parameter is required. Throws an
68 * IllegalArgumentException if paramValue is null or empty.
69 *
70 * @param paramName The name of the parameter to check.
71 * @param paramValue The parameter value.
72 * @since 1.1
73 */
74 protected void required( String paramName, String paramValue )
75 {
76 if ( StringUtils.isEmpty( paramValue ) )
77 {
78 throw new IllegalArgumentException( paramName + " is a required parameter!" );
79 }
80 }
81
82 /**
83 * Convert the Map of macro parameters to an AttributeSet.
84 * No check of validity is done, all parameters are added.
85 *
86 * @param parameters the macro parameters.
87 * @return a SinkEventAttributeSet containing the same parameters,
88 * or null if parameters is null.
89 *
90 * @since 1.1.1.
91 */
92 protected static SinkEventAttributes getAttributesFromMap( Map<?, ?> parameters )
93 {
94 if ( parameters == null )
95 {
96 return null;
97 }
98
99 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 }