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.cavallo;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.Locale;
21  
22  import org.codehaus.plexus.util.StringUtils;
23  import org.mcavallo.opencloud.Cloud;
24  import org.mcavallo.opencloud.Cloud.Case;
25  import org.mcavallo.opencloud.Tag;
26  import org.mcavallo.opencloud.filters.TagFilter;
27  
28  import de.smartics.tagcloud.TagCloud;
29  
30  /**
31   * An adaptor for the tag cloud implementation <a
32   * href="http://opencloud.mcavallo.org/">opencloud</a> by Marco Cavallo.
33   *
34   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
35   * @version $Revision:591 $
36   */
37  public class CavalloCloudAdapter implements TagCloud
38  {
39    // ********************************* Fields *********************************
40  
41    // --- constants ------------------------------------------------------------
42  
43    // --- members --------------------------------------------------------------
44  
45    /**
46     * The wrapped implementation.
47     */
48    private final Cloud adaptedCloud;
49  
50    // ****************************** Initializer *******************************
51  
52    // ****************************** Constructors ******************************
53  
54    /**
55     * Default constructor.
56     *
57     * @param locale the locale to pass to the implementation.
58     * @param maxTagsToDisplay the maximum number of tags to display.
59     * @param minWeight the minimum font weight of a tag displayed.
60     * @param maxWeight the maximum font weight of a tag displayed.
61     */
62    public CavalloCloudAdapter(final Locale locale, final int maxTagsToDisplay,
63        final double minWeight, final double maxWeight)
64    {
65      this.adaptedCloud = new Cloud(locale);
66      if (maxTagsToDisplay > 0)
67      {
68        this.adaptedCloud.setMaxTagsToDisplay(maxTagsToDisplay);
69      }
70      this.adaptedCloud.setMinWeight(minWeight);
71      this.adaptedCloud.setMaxWeight(maxWeight);
72      this.adaptedCloud.setTagCase(Case.PRESERVE_CASE);
73    }
74  
75    // ****************************** Inner Classes *****************************
76  
77    // ********************************* Methods ********************************
78  
79    // --- init -----------------------------------------------------------------
80  
81    // --- get&set --------------------------------------------------------------
82  
83    /**
84     * {@inheritDoc}
85     *
86     * @see de.smartics.tagcloud.TagCloud#addTag(java.lang.String)
87     */
88    @Override
89    public void addTag(final String tag)
90    {
91      adaptedCloud.addTag(tag);
92    }
93  
94    /**
95     * {@inheritDoc}
96     *
97     * @see de.smartics.tagcloud.TagCloud#toHtml()
98     */
99    @Override
100   public String toHtml()
101   {
102     final StringBuilder buffer = new StringBuilder(1024);
103     buffer.append("<div class='tag-cloud'>\n");
104     for (final Tag tag : adaptedCloud.tags())
105     {
106       final String link = tag.getLink();
107       final boolean linkProvided = StringUtils.isNotBlank(link);
108       if (linkProvided)
109       {
110         buffer.append("<a href='").append(link).append('\'');
111       }
112       else
113       {
114         buffer.append("<span");
115       }
116 
117       buffer.append(" style='font-size: ").append(Math.round(tag.getNormScore() * tag.getWeight()))
118           .append("px;'>").append(tag.getName());
119 
120       if (linkProvided)
121       {
122         buffer.append("</a>\n");
123       }
124       else
125       {
126         buffer.append("</span>\n");
127       }
128     }
129     buffer.append("</div>");
130 
131     return buffer.toString();
132   }
133 
134   /**
135    * Adds the given output filter.
136    *
137    * @param filter the filter to add.
138    */
139   public void addOutputFilter(final TagFilter filter)
140   {
141     adaptedCloud.addOutputFilter(filter);
142   }
143 
144   /**
145    * {@inheritDoc}
146    *
147    * @see de.smartics.tagcloud.TagCloud#clear()
148    */
149   @Override
150   public void clear()
151   {
152     adaptedCloud.clear();
153   }
154 
155   /**
156    * {@inheritDoc}
157    *
158    * @see de.smartics.tagcloud.TagCloud#getTags()
159    */
160   @Override
161   public List<de.smartics.tagcloud.Tag> getTags()
162   {
163     final List<Tag> adaptedTags = adaptedCloud.tags();
164     final List<de.smartics.tagcloud.Tag> tags =
165         new ArrayList<de.smartics.tagcloud.Tag>(adaptedTags.size());
166     for (final Tag cavalloTag : adaptedTags)
167     {
168       final de.smartics.tagcloud.Tag tag =
169           new de.smartics.tagcloud.Tag(cavalloTag.getName(),
170               cavalloTag.getScoreInt(), cavalloTag.getWeightInt());
171       tags.add(tag);
172     }
173     return tags;
174   }
175 
176   // --- business -------------------------------------------------------------
177 
178   // --- object basics --------------------------------------------------------
179 
180 }