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.commons.lang3.Validate;
25 import org.apache.maven.doxia.sink.SinkEventAttributes;
26 import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet;
27 import org.codehaus.plexus.util.StringUtils;
28
29 /**
30 * Abstract base class to execute <code>Macro</code>.
31 *
32 * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
33 * @since 1.0
34 */
35 public abstract class AbstractMacro
36 implements Macro
37 {
38 /**
39 * Check if the given parameter is required. Throws an
40 * IllegalArgumentException if paramValue is null or empty.
41 *
42 * @param paramName The name of the parameter to check.
43 * @param paramValue The parameter value.
44 * @since 1.1
45 * @deprecated Not used, use {@link Validate}
46 */
47 @Deprecated
48 protected void required( String paramName, String paramValue )
49 {
50 if ( StringUtils.isEmpty( paramValue ) )
51 {
52 throw new IllegalArgumentException( paramName + " is a required parameter!" );
53 }
54 }
55
56 /**
57 * Convert the Map of macro parameters to an AttributeSet.
58 * No check of validity is done, all parameters are added.
59 *
60 * @param parameters the macro parameters.
61 * @return a SinkEventAttributeSet containing the same parameters,
62 * or null if parameters is null.
63 * @since 1.1.1.
64 */
65 protected static SinkEventAttributes getAttributesFromMap( Map<?, ?> parameters )
66 {
67 if ( parameters == null )
68 {
69 return null;
70 }
71
72 final int count = parameters.size();
73
74 if ( count <= 0 )
75 {
76 return null;
77 }
78
79 final SinkEventAttributeSet atts = new SinkEventAttributeSet( count );
80
81 for ( Map.Entry<?, ?> entry : parameters.entrySet() )
82 {
83 atts.addAttribute( entry.getKey(), entry.getValue() );
84 }
85
86 return atts;
87 }
88 }