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