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;
8   
9   import java.util.Locale;
10  import java.util.ResourceBundle;
11  import java.util.StringTokenizer;
12  
13  /***
14   * <p>Implementation of the DictionaryReader interface, uses a .properties file to retrieve words and return a
15   * WordList.Constructed with the name of the properties file. It uses standard java mecanism for I18N</p>
16   *
17   * @author <a href="mailto:mga@octo.com">Mathieu Gandin</a>
18   * @version 1.1
19   */
20  public class FileDictionary implements DictionaryReader {
21  
22      private String myBundle;
23  
24      public FileDictionary(String bundle) {
25          myBundle = bundle;
26      }
27  
28      public SizeSortedWordList getWordList() {
29          ResourceBundle bundle = ResourceBundle.getBundle(myBundle);
30          SizeSortedWordList list = generateWordList(Locale.getDefault(), bundle);
31          return list;
32      }
33  
34      public SizeSortedWordList getWordList(Locale locale) {
35          ResourceBundle bundle = ResourceBundle.getBundle(myBundle, locale);
36          SizeSortedWordList list = generateWordList(locale, bundle);
37          return list;
38      }
39  
40      protected SizeSortedWordList generateWordList(Locale locale, ResourceBundle bundle) {
41          DefaultSizeSortedWordList list = new DefaultSizeSortedWordList(locale);
42          StringTokenizer tokenizer = new StringTokenizer(bundle.getString("words"), ";");
43          int count = tokenizer.countTokens();
44          for (int i = 0; i < count; i++) {
45              list.addWord(tokenizer.nextToken());
46          }
47          return list;
48      }
49  
50  }