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 java.util.Locale;
10  
11  /***
12   * <p>Description: dummy word generator contructed with a String returning the same string, with right length</p>
13   *
14   * @author <a href="mailto:mag@jcaptcha.net">Marc-Antoine Garrigue</a>
15   * @version 1.0
16   */
17  public class DummyWordGenerator implements WordGenerator {
18  
19      private String word = "JCAPTCHA";
20  
21      public DummyWordGenerator(String word) {
22          this.word = word == null || "".equals(word) ? this.word : word;
23      }
24  
25      /***
26       * Return a word of length between min and max length
27       *
28       * @return a String of length between min and max length
29       */
30      public String getWord(Integer length) {
31          int mod = length.intValue() % word.length();
32          String cut = "";
33          int mul = (length.intValue() - mod) / word.length();
34          if (mod > 0) {
35              cut = word.substring(0, mod);
36          }
37          StringBuffer returned = new StringBuffer();
38          for (int i = 0; i < mul; i++) {
39              returned.append(word);
40          }
41          returned.append(cut);
42          return returned.toString();
43      }
44  
45      /***
46       * Return a word of length between min and max length according to the given locale
47       *
48       * @param length the word length
49       *
50       * @return a String of length between min and max length according to the given locale
51       */
52      public String getWord(Integer length, Locale locale) {
53          return getWord(length);
54      }
55  }