View Javadoc

1   /*
2    * Copyright 2009-2012 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.maven.apidoc;
17  
18  import java.util.Map;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.apache.maven.project.MavenProject;
23  import org.codehaus.plexus.util.xml.Xpp3Dom;
24  
25  /**
26   * Helper to add additional parameters provided by the maven-javadoc-plugin to
27   * be passed to the Javadoc tool.
28   *
29   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
30   * @version $Revision:591 $
31   */
32  public class JavadocPluginConfigurationExtractor
33  {
34    /**
35     * The prefix of the id attribute of an <code>additionalParam</code> element
36     * to be relevant for the extractor.
37     * <p>
38     * The value of this constant is {@value}.
39     * </p>
40     */
41    protected static final String ID_PREFIX_OF_RELEVANT_ELEMENT = "apidoc";
42  
43    // ********************************* Fields *********************************
44  
45    // --- constants ------------------------------------------------------------
46  
47    /**
48     * Reference to the logger for this class.
49     */
50    private static final Log LOG = LogFactory
51        .getLog(JavadocPluginConfigurationExtractor.class);
52  
53    // --- members --------------------------------------------------------------
54  
55    // ****************************** Initializer *******************************
56  
57    // ****************************** Constructors ******************************
58  
59    /**
60     * Default constructor.
61     */
62    public JavadocPluginConfigurationExtractor()
63    {
64    }
65  
66    // ****************************** Inner Classes *****************************
67  
68    // ********************************* Methods ********************************
69  
70    // --- init -----------------------------------------------------------------
71  
72    // --- get&set --------------------------------------------------------------
73  
74    // --- business -------------------------------------------------------------
75  
76    /**
77     * Adds additional parameters found in the configuration of the
78     * maven-javadoc-plugin to the given <code>javadocArguments</code>.
79     *
80     * @param project the reference to the maven project to look for the
81     *          configuration of the maven-javadoc-plugin.
82     * @param reportSetId the identifier of the report set to watch for
83     *          configurations.
84     * @param javadocArguments the arguments to add to.
85     */
86    public void addJavadocPluginArguments(final MavenProject project,
87        final String reportSetId, final Map<String, String> javadocArguments)
88    {
89      final Xpp3Dom dom =
90          project.getReportConfiguration("org.apache.maven.plugins",
91              "maven-javadoc-plugin", reportSetId);
92  
93      if (dom != null)
94      {
95        final Xpp3Dom[] additionalParams = dom.getChildren("additionalparam");
96        final StringBuilder buffer = new StringBuilder();
97        for (Xpp3Dom additionalParam : additionalParams)
98        {
99          final String id = additionalParam.getAttribute("id");
100         if (isIdRelevant(id))
101         {
102           final String paramValue = additionalParam.getValue().trim();
103           if (LOG.isDebugEnabled())
104           {
105             LOG.debug("Adding additional parameters specified by"
106                       + " the maven-javadoc-plugin with report set ID '"
107                       + reportSetId + "' and element ID '" + id + "': "
108                       + paramValue);
109           }
110 
111           buffer.append(' ').append(paramValue);
112         }
113       }
114 
115       if (buffer.length() > 0)
116       {
117         final String oldParams =
118             javadocArguments.get(Constants.ADDITIONAL_PARAMS);
119         final String newValue = appendNewValue(oldParams, buffer.toString());
120         javadocArguments.put(Constants.ADDITIONAL_PARAMS, newValue);
121       }
122     }
123   }
124 
125   /**
126    * Checks if the given <code>id</code> is not <code>null</code> and starts
127    * with the character sequence
128    * <code>{@value #ID_PREFIX_OF_RELEVANT_ELEMENT}</code>.
129    *
130    * @param id the identifier of an XML element to check.
131    * @return <code>true</code> if the identifier is not <code>null</code> and
132    *         starts with the character sequence
133    *         <code>{@value #ID_PREFIX_OF_RELEVANT_ELEMENT}</code>,
134    *         <code>false</code> otherwise.
135    */
136   private static boolean isIdRelevant(final String id)
137   {
138     return id != null && id.startsWith(ID_PREFIX_OF_RELEVANT_ELEMENT);
139   }
140 
141   /**
142    * Appends the <code>newValue</code> to the <code>oldValue</code>.
143    *
144    * @param oldValue the old value (may be <code>null</code>) to append to.
145    * @param newValue the new value to append.
146    * @return the concatenated value, separated by a single space character.
147    */
148   private static String appendNewValue(final String oldValue,
149       final String newValue)
150   {
151     return oldValue != null ? oldValue + ' ' + newValue : newValue;
152   }
153 
154   // --- object basics --------------------------------------------------------
155 
156 }