View Javadoc

1   /*
2    * JCaptcha, the open source java framework for captcha definition and integration
3    * Copyright (c)  2007 jcaptcha.net. All Rights Reserved.
4    * See the LICENSE.txt file distributed with this package.
5    */
6   
7   package com.octo.captcha.component.word.wordgenerator;
8   
9   import com.octo.captcha.CaptchaException;
10  import com.octo.captcha.component.word.DefaultSizeSortedWordList;
11  import com.octo.captcha.component.word.DictionaryReader;
12  import com.octo.captcha.component.word.SizeSortedWordList;
13  
14  import java.util.HashMap;
15  import java.util.Locale;
16  
17  /***
18   * <p>WordGenerator based on a dictionary. Uses a Dictionary reader to retrieve words and an WordList to store the words
19   * retrieved. Be sure your dictionary contains words whose length covers the whole range specified in your factory, some
20   * rutime exception will occur!</p>
21   *
22   * @author <a href="mailto:mag@jcaptcha.net">Marc-Antoine Garrigue</a>
23   * @version 1.0
24   */
25  public class DictionaryWordGenerator implements WordGenerator {
26  
27      private Locale defaultLocale;
28  
29      private DictionaryReader factory;
30  
31      private HashMap localizedwords = new HashMap();
32  
33      public DictionaryWordGenerator(DictionaryReader reader) {
34          this.factory = reader;
35          //add the default wordlist to the localisedWordList
36          this.defaultLocale = factory.getWordList().getLocale();
37          this.localizedwords.put(defaultLocale, factory.getWordList());
38      }
39  
40      /***
41       * Return a word of length between min and max length
42       *
43       * @return a String of length between min and max length
44       */
45      public final String getWord(Integer length) {
46          return getWord(length, defaultLocale);
47      }
48  
49      /***
50       * Return a word of length between min and max length according to the given locale
51       *
52       * @return a String of length between min and max length according to the given locale
53       */
54      public String getWord(Integer length, Locale locale) {
55          SizeSortedWordList words;
56          words = getWordList(locale);
57  
58          String word = words.getNextWord(length);
59          //check if word with the specified length exist
60          if (word == null) {
61              //if not see if any
62              throw new CaptchaException("No word of length : " + length +
63                      " exists in dictionnary! please " +
64                      "update your dictionary or your range!");
65          }
66          return word;
67      }
68  
69      final SizeSortedWordList getWordList(Locale locale) {
70          SizeSortedWordList words;
71          if (localizedwords.containsKey(locale)) {
72              words = (DefaultSizeSortedWordList) localizedwords.get(locale);
73          } else {
74  
75              words = factory.getWordList(locale);
76              //add to cache
77              localizedwords.put(locale, words);
78          }
79          return words;
80      }
81  }