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.Locale;
19  
20  import org.apache.commons.lang.NullArgumentException;
21  import org.mcavallo.opencloud.filters.DictionaryFilter;
22  
23  import de.smartics.tagcloud.AbstractTagCloudFactory;
24  import de.smartics.tagcloud.TagCloud;
25  
26  /**
27   * A factory implementation to adapt the tag cloud implementation <a
28   * href="http://opencloud.mcavallo.org/">opencloud</a> by Marco Cavallo.
29   *
30   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
31   * @version $Revision:591 $
32   */
33  public class CavalloTagCloudFactory extends AbstractTagCloudFactory
34  {
35    // ********************************* Fields *********************************
36  
37    // --- constants ------------------------------------------------------------
38  
39    // --- members --------------------------------------------------------------
40  
41    /**
42     * The locale to pass to the cloud implementation.
43     */
44    private Locale locale;
45  
46    // ****************************** Initializer *******************************
47  
48    // ****************************** Constructors ******************************
49  
50    /**
51     * Default constructor using the system's default locale.
52     */
53    public CavalloTagCloudFactory()
54    {
55      this(Locale.getDefault());
56    }
57  
58    /**
59     * Convenience constructor to specify the desired locale on construction.
60     *
61     * @param locale the locale to pass to the cloud implementation.
62     */
63    public CavalloTagCloudFactory(final Locale locale)
64    {
65      this.locale = locale;
66    }
67  
68    // ****************************** Inner Classes *****************************
69  
70    // ********************************* Methods ********************************
71  
72    // --- init -----------------------------------------------------------------
73  
74    // --- get&set --------------------------------------------------------------
75  
76    /**
77     * Returns the locale to pass to the cloud implementation.
78     *
79     * @return the locale to pass to the cloud implementation.
80     */
81    public Locale getLocale()
82    {
83      return locale;
84    }
85  
86    /**
87     * Sets the locale to pass to the cloud implementation.
88     *
89     * @param locale the locale to pass to the cloud implementation.
90     * @throws NullArgumentException if <code>locale</code> is <code>null</code>.
91     */
92    public void setLocale(final Locale locale) throws NullArgumentException
93    {
94      if (locale == null)
95      {
96        throw new NullArgumentException("locale");
97      }
98      this.locale = locale;
99    }
100 
101   // --- business -------------------------------------------------------------
102 
103   /**
104    * {@inheritDoc}
105    *
106    * @see de.smartics.tagcloud.TagCloudFactory#createTextCloud()
107    */
108   @Override
109   public TagCloud createTextCloud()
110   {
111     return createAdapter();
112   }
113 
114   private CavalloCloudAdapter createAdapter()
115   {
116     return new CavalloCloudAdapter(locale, this.maxTagsToDisplay,
117         this.maxWeight, this.maxWeight);
118   }
119 
120   /**
121    * {@inheritDoc}
122    *
123    * @see de.smartics.tagcloud.TagCloudFactory#createJavaCloud()
124    */
125   @Override
126   public TagCloud createJavaCloud()
127   {
128     final CavalloCloudAdapter adapter = createAdapter();
129     if (useJavaReservedWordsFilter)
130     {
131       adapter.addOutputFilter(JavaReservedWordsFilter.FILTER);
132     }
133     if (useUsualWordsFilter)
134     {
135       adapter.addOutputFilter(UsualWordsFilter.FILTER);
136     }
137     if (wordsToFilter != null && wordsToFilter.length > 0)
138     {
139       adapter.addOutputFilter(new DictionaryFilter(wordsToFilter));
140     }
141     return adapter;
142   }
143 
144   // --- object basics --------------------------------------------------------
145 
146 }