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.sound.speller;
8   
9   import com.octo.captcha.CaptchaException;
10  import com.octo.captcha.CaptchaQuestionHelper;
11  import com.octo.captcha.component.sound.wordtosound.WordToSound;
12  import com.octo.captcha.component.word.worddecorator.SpellerWordDecorator;
13  import com.octo.captcha.component.word.worddecorator.WordDecorator;
14  import com.octo.captcha.component.word.wordgenerator.WordGenerator;
15  import com.octo.captcha.sound.SoundCaptcha;
16  import com.octo.captcha.sound.SoundCaptchaFactory;
17  
18  import javax.sound.sampled.AudioInputStream;
19  import java.security.SecureRandom;
20  import java.util.Locale;
21  import java.util.Random;
22  
23  /***
24   * Factory for SpellerSound
25   *
26   * @author Benoit Doumas
27   * @version 1.0
28   */
29  public class SpellerSoundFactory extends SoundCaptchaFactory {
30  
31      private WordGenerator wordGenerator;
32  
33      private WordToSound word2Sound;
34  
35      private WordDecorator wordDecorator;
36  
37      private Random myRandom = new SecureRandom();
38  
39      /***
40       * The bundle question key for CaptchaQuestionHelper
41       */
42      public static final String BUNDLE_QUESTION_KEY = SpellerSound.class.getName();
43  
44      /***
45       * Construct a GimpySoundFactory from a word generator component and a wordtosound component
46       *
47       * @param thewordGenerator component
48       * @param theword2Sound    component
49       */
50      public SpellerSoundFactory(WordGenerator wordGenerator, WordToSound word2Sound,
51                                 SpellerWordDecorator wordDecorator) {
52          if (wordGenerator == null) {
53              throw new CaptchaException("Invalid configuration for a "
54                      + "SpellingSoundFactory : WordGenerator can't be null");
55          }
56          if (word2Sound == null) {
57              throw new CaptchaException("Invalid configuration for a "
58                      + "SpellingSoundFactory : Word2Sound can't be null");
59          }
60          if (wordDecorator == null) {
61              throw new CaptchaException("Invalid configuration for a "
62                      + "SpellingSoundFactory : wordDecorator can't be null");
63          }
64          this.wordGenerator = wordGenerator;
65          this.word2Sound = word2Sound;
66          this.wordDecorator = wordDecorator;
67      }
68  
69      public WordToSound getWordToSound() {
70          return this.word2Sound;
71      }
72  
73      public WordGenerator getWordGenerator() {
74          return this.wordGenerator;
75      }
76  
77      /***
78       * @return a Sound Captcha
79       */
80      public SoundCaptcha getSoundCaptcha() {
81          String word = this.wordGenerator.getWord(getRandomLength(), Locale.getDefault());
82          AudioInputStream sound = this.word2Sound.getSound(wordDecorator.decorateWord(word));
83          SoundCaptcha soundCaptcha = new SpellerSound(getQuestion(Locale.getDefault()), sound, word);
84          return soundCaptcha;
85      }
86  
87      /***
88       * @param locale the locale
89       * @return a localized sound captcha
90       */
91      public SoundCaptcha getSoundCaptcha(Locale locale) {
92          String word = this.wordGenerator.getWord(getRandomLength(), locale);
93          AudioInputStream sound = this.word2Sound.getSound(wordDecorator.decorateWord(word), locale);
94          SoundCaptcha soundCaptcha = new SpellerSound(getQuestion(locale), sound, word);
95          return soundCaptcha;
96      }
97  
98      protected String getQuestion(Locale locale) {
99          return CaptchaQuestionHelper.getQuestion(locale, BUNDLE_QUESTION_KEY);
100     }
101 
102     protected Integer getRandomLength() {
103         Integer wordLength;
104         int range = getWordToSound().getMaxAcceptedWordLength()
105                 - getWordToSound().getMinAcceptedWordLength();
106         int randomRange = range != 0 ? myRandom.nextInt(range + 1) : 0;
107         wordLength = new Integer(randomRange + getWordToSound().getMinAcceptedWordLength());
108         return wordLength;
109     }
110 
111 }