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.engine.image;
8   
9   import java.util.Arrays;
10  
11  import com.octo.captcha.CaptchaException;
12  
13  /***
14   * <p>This engine is based on a java.util.List of factories. It has a default constructor. Sub class must implements the
15   * buildInitialFactories() method that should build an initial set of factories.</p>
16   *
17   * @author <a href="mailto:mag@jcaptcha.net">Marc-Antoine Garrigue</a>
18   * @version 1.0
19   */
20  public abstract class ListImageCaptchaEngine
21          extends com.octo.captcha.engine.image.ImageCaptchaEngine {    
22  
23      public ListImageCaptchaEngine() {
24          buildInitialFactories();
25          checkFactoriesSize();
26      }
27  
28      /***
29       * this method should be implemented as folow : <ul> <li>First construct all the factories you want to initialize
30       * the gimpy with</li> <li>then call the this.addFactoriy method for each factory</li> </ul>
31       */
32      protected abstract void buildInitialFactories();
33  
34      /***
35       * Add a factory to the gimpy list
36       *
37       * @return true if added false otherwise
38       */
39      public boolean addFactory(
40              com.octo.captcha.image.ImageCaptchaFactory factory) {
41          return factory != null && this.factories.add(factory);
42      }
43  
44      /***
45       * Add an array of factories to the gimpy list
46       */
47      public void addFactories(
48              com.octo.captcha.image.ImageCaptchaFactory[] factories) {
49          checkNotNullOrEmpty(factories);
50          this.factories.addAll(Arrays.asList(factories));
51      }
52  
53      private void checkFactoriesSize() {
54          if (factories.size() == 0)
55              throw new CaptchaException(
56                      "This gimpy has no factories. Please initialize it "
57                              + "properly with the buildInitialFactory() called by "
58                              + "the constructor or the addFactory() mehtod later!");
59      }
60  
61  }