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.SinkEventAttributeSet; 027import org.apache.maven.doxia.sink.SinkEventAttributes; 028 029import org.codehaus.plexus.util.StringUtils; 030 031/** 032 * Abstract base class to execute <code>Macro</code>. 033 * 034 * @author <a href="mailto:jason@maven.org">Jason van Zyl</a> 035 * @version $Id$ 036 * @since 1.0 037 */ 038public abstract class AbstractMacro 039 implements Macro 040{ 041 /** Log instance. */ 042 private Log logger; 043 044 /** {@inheritDoc} */ 045 public void enableLogging( Log log ) 046 { 047 this.logger = log; 048 } 049 050 /** 051 * Returns a logger for this macro. 052 * If no logger has been configured, a new SystemStreamLog is returned. 053 * 054 * @return Log 055 * @since 1.1 056 */ 057 protected Log getLog() 058 { 059 if ( logger == null ) 060 { 061 logger = new SystemStreamLog(); 062 } 063 064 return logger; 065 } 066 067 /** 068 * Check if the given parameter is required. Throws an 069 * IllegalArgumentException if paramValue is null or empty. 070 * 071 * @param paramName The name of the parameter to check. 072 * @param paramValue The parameter value. 073 * @since 1.1 074 */ 075 protected void required( String paramName, String paramValue ) 076 { 077 if ( StringUtils.isEmpty( paramValue ) ) 078 { 079 throw new IllegalArgumentException( paramName + " is a required parameter!" ); 080 } 081 } 082 083 /** 084 * Convert the Map of macro parameters to an AttributeSet. 085 * No check of validity is done, all parameters are added. 086 * 087 * @param parameters the macro parameters. 088 * @return a SinkEventAttributeSet containing the same parameters, 089 * or null if parameters is null. 090 * 091 * @since 1.1.1. 092 */ 093 protected static SinkEventAttributes getAttributesFromMap( Map<?, ?> parameters ) 094 { 095 if ( parameters == null ) 096 { 097 return null; 098 } 099 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}