View Javadoc

1   /*
2    * Copyright 2011-2013 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.tagcloud.maven;
17  
18  import java.util.Properties;
19  
20  import org.apache.maven.plugin.MojoExecutionException;
21  import org.apache.maven.project.MavenProject;
22  
23  import de.smartics.tagcloud.TagCloud;
24  import de.smartics.tagcloud.TagCloudFactory;
25  
26  /**
27   * Provides a tag cloud snippet as a property to be included in a page of a
28   * Maven site. Since the fragment is XHTML it may be included in other HTML
29   * pages as well.
30   *
31   * @goal tagcloud-property
32   * @phase pre-site
33   * @requiresProject
34   * @description Provides a tag cloud snippet as a property to be included in a
35   *              page of a Maven site.
36   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
37   * @version $Revision:591 $
38   */
39  public class TagCloudPropertyMojo extends AbstractTagCloudMojo
40  {
41    // ********************************* Fields *********************************
42  
43    // --- constants ------------------------------------------------------------
44  
45    // --- members --------------------------------------------------------------
46  
47    /**
48     * The name of the property used to store the generated XHTML fragment.
49     *
50     * @parameter default-value="tagcloud"
51     * @since 1.0
52     */
53    private String propertyName;
54  
55    /**
56     * The minimum weight for the font.
57     *
58     * @parameter default-value="0.0"
59     */
60    private double minWeight;
61  
62    /**
63     * The maximum weight for the font.
64     *
65     * @parameter default-value="36.0"
66     */
67    private double maxWeight;
68  
69    /**
70     * The maximum number of tags to display.
71     *
72     * @parameter default-value="0"
73     */
74    private int maxTagsToDisplay;
75  
76    // ****************************** Initializer *******************************
77  
78    // ****************************** Constructors ******************************
79  
80    // ****************************** Inner Classes *****************************
81  
82    // ********************************* Methods ********************************
83  
84    // --- init -----------------------------------------------------------------
85  
86    // --- get&set --------------------------------------------------------------
87  
88    // --- business -------------------------------------------------------------
89  
90    /**
91     * Exports the cloud data as XML.
92     *
93     * @param tagCloud the cloud to be exported.
94     */
95    @Override
96    protected void runWith(final TagCloud tagCloud) throws MojoExecutionException
97    {
98      final String html = tagCloud.toHtml();
99      final Properties properties = getProjectProperties(project);
100     properties.setProperty(propertyName, html);
101   }
102 
103   /**
104    * Fetches the project properties and if <code>null</code> returns a new empty
105    * properties instance that is associated with the project.
106    *
107    * @param project the project whose properties are requested.
108    * @return the properties of the project.
109    */
110   private Properties getProjectProperties(final MavenProject project)
111   {
112     Properties projectProperties = project.getProperties();
113     if (projectProperties == null)
114     {
115       projectProperties = new Properties();
116       project.getModel().setProperties(projectProperties);
117     }
118 
119     return projectProperties;
120   }
121 
122   @Override
123   protected TagCloudFactory createFactoryInstance()
124     throws MojoExecutionException
125   {
126     final TagCloudFactory factory = super.createFactoryInstance();
127     factory.setMaxTagsToDisplay(maxTagsToDisplay);
128     factory.setMinWeight(minWeight);
129     factory.setMaxWeight(maxWeight);
130     return factory;
131   }
132 
133   // --- object basics --------------------------------------------------------
134 
135 }