KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > frontend > templateone > form > CmsCaptchaSettings


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/frontend/templateone/form/CmsCaptchaSettings.java,v $
3  * Date : $Date: 2006/03/27 14:52:20 $
4  * Version: $Revision: 1.6 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (C) 2002 - 2004 Alkacon Software (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.frontend.templateone.form;
33
34 import org.opencms.file.CmsFile;
35 import org.opencms.file.CmsObject;
36 import org.opencms.i18n.CmsEncoder;
37 import org.opencms.jsp.CmsJspActionElement;
38 import org.opencms.main.CmsLog;
39 import org.opencms.util.CmsStringUtil;
40 import org.opencms.xml.content.CmsXmlContent;
41 import org.opencms.xml.content.CmsXmlContentFactory;
42
43 import java.awt.Color JavaDoc;
44 import java.util.Locale JavaDoc;
45
46 import javax.servlet.http.HttpServletRequest JavaDoc;
47
48 import org.apache.commons.logging.Log;
49
50 /**
51  * Stores the settings to render captcha images.
52  * <p>
53  *
54  * @author Thomas Weckert
55  *
56  * @author Achim Westermann
57  *
58  * @version $Revision: 1.6 $
59  */

60 public final class CmsCaptchaSettings implements Cloneable JavaDoc {
61
62     /** Request parameter for the background color. */
63     public static final String JavaDoc C_PARAM_BACKGROUND_COLOR = "bgcol";
64
65     /** Request parameter for the min phrase length. */
66     public static final String JavaDoc C_PARAM_CHARACTERS = "crs";
67
68     /** Request parameter for the filter amplitude. */
69     public static final String JavaDoc C_PARAM_FILTER_AMPLITUDE = "famplit";
70
71     /** Request parameter for the filter amplitude. */
72     public static final String JavaDoc C_PARAM_FILTER_WAVE_LENGTH = "fwavlen";
73
74     /** Request parameter for the font color. */
75     public static final String JavaDoc C_PARAM_FONT_COLOR = "fcol";
76
77     /** Request parameter for the font color. */
78     public static final String JavaDoc C_PARAM_HOLES_PER_GLYPH = "holes";
79
80     /** Request parameter for the image height. */
81     public static final String JavaDoc C_PARAM_IMAGE_HEIGHT = "h";
82
83     /** Request parameter for the image width. */
84     public static final String JavaDoc C_PARAM_IMAGE_WIDTH = "w";
85
86     /** Request parameter for the max. font size. */
87     public static final String JavaDoc C_PARAM_MAX_FONT_SIZE = "maxfs";
88
89     /** Request parameter for the max phrase length. */
90     public static final String JavaDoc C_PARAM_MAX_PHRASE_LENGTH = "maxpl";
91
92     /** Request parameter for the min. font size. */
93     public static final String JavaDoc C_PARAM_MIN_FONT_SIZE = "minfs";
94
95     /** Request parameter for the min phrase length. */
96     public static final String JavaDoc C_PARAM_MIN_PHRASE_LENGTH = "minpl";
97
98     /** Request parameter for the min phrase length. */
99     public static final String JavaDoc C_PARAM_PRESET = "prst";
100
101     /** Request parameter for the min phrase length. */
102     public static final String JavaDoc C_PARAM_USE_BACKGROUND_IMAGE = "bgimg";
103
104     /** Configuration node name for the optional captcha background color. */
105     public static final String JavaDoc NODE_CAPTCHAPRESET_BACKGROUNDCOLOR = "BackgroundColor";
106
107     /** Configuration node name for the field value node. */
108     public static final String JavaDoc NODE_CAPTCHAPRESET_FILTER_AMPLITUDE = "FilterAmplitude";
109
110     /** Configuration node name for the optional captcha image holes per glyph. */
111     public static final String JavaDoc NODE_CAPTCHAPRESET_FILTER_WAVELENGTH = "FilterWaveLength";
112
113     /** Configuration node name for the optional captcha font color. */
114     public static final String JavaDoc NODE_CAPTCHAPRESET_FONTCOLOR = "FontColor";
115
116     /** Configuration node name for the optional captcha image holes per glyph. */
117     public static final String JavaDoc NODE_CAPTCHAPRESET_HOLESPERGLYPH = "HolesPerGlyph";
118
119     /** Configuration node name for the optional captcha image height. */
120     public static final String JavaDoc NODE_CAPTCHAPRESET_IMAGEHEIGHT = "ImageHeight";
121
122     /** Configuration node name for the optional captcha image width. */
123     public static final String JavaDoc NODE_CAPTCHAPRESET_IMAGEWIDTH = "ImageWidth";
124
125     /** Configuration node name for the optional captcha max. font size. */
126     public static final String JavaDoc NODE_CAPTCHAPRESET_MAX_FONT_SIZE = "MaxFontSize";
127
128     /** Configuration node name for the optional captcha max. phrase length. */
129     public static final String JavaDoc NODE_CAPTCHAPRESET_MAX_PHRASE_LENGTH = "MaxPhraseLength";
130
131     /** Configuration node name for the optional captcha min. font size. */
132     public static final String JavaDoc NODE_CAPTCHAPRESET_MIN_FONT_SIZE = "MinFontSize";
133
134     /** Configuration node name for the optional captcha min. phrase length. */
135     public static final String JavaDoc NODE_CAPTCHAPRESET_MIN_PHRASE_LENGTH = "MinPhraseLength";
136
137     /** The log object for this class. */
138     private static final Log LOG = CmsLog.getLog(CmsCaptchaSettings.class);
139
140     /** The the background color. */
141     private Color JavaDoc m_backgroundColor = Color.WHITE;
142
143     /** The string containing the characters to use for word generation. */
144     private String JavaDoc m_characterPool = "abcdefghiklmnoprstuvwxyz";
145
146     /** The filter amplitude for the water filter that bends the text. */
147     private int m_filterAmplitude = 2;
148
149     /** The filter wave length for the water filter that bends the text. */
150     private int m_filterWaveLength = 100;
151
152     /** The font color. */
153     private Color JavaDoc m_fontColor = Color.BLACK;
154
155     /** The amount of holes per glyph. */
156     private Integer JavaDoc m_holesPerGlyp = new Integer JavaDoc(0);
157
158     /** The image height in pixels. */
159     private int m_imageHeight = 50;
160
161     /** The image width in pixels. */
162     private int m_imageWidth = 150;
163
164     /** The maximum font size in pixels. */
165     private int m_maxFontSize = 40;
166
167     /** The maximum phrase length. */
168     private int m_maxPhraseLength = 5;
169
170     /** minimum font size in pixels. */
171     private int m_minFontSize = 30;
172
173     /** The minimum phrase length. */
174     private int m_minPhraseLength = 5;
175
176     /**
177      * The path to the preset configuration (captchapreset) that has been used to initialize these
178      * settings. This is read only, as the path is internally read from a nested CmsForm/FormCaptcha
179      * XML content.
180      */

181     private String JavaDoc m_presetPath = "factory defaults (classfile)";
182
183     /** The flag that decides wethter a background image or a background color is used. */
184     private boolean m_useBackgroundImage = true;
185
186     /**
187      * Private constructor for the clone method.
188      * <p>
189      *
190      * May only be called from {@link #clone()} as that method guarantees to install the default
191      * value from the master captcha settings.
192      * <p>
193      */

194     private CmsCaptchaSettings() {
195
196         // nop
197
}
198
199     /**
200      * Constructor that will use request parameters to init theses settings.
201      * <p>
202      */

203     private CmsCaptchaSettings(CmsJspActionElement jsp) {
204
205         init(jsp);
206     }
207
208     /**
209      * Returns a clone of the singleton instance of the
210      * <em>"master"</em> <code>CmsCaptchaSettings</code> and potential overridden values from
211      * the request context.
212      * <p>
213      *
214      * The <em>"master"</em> <code>CmsCaptchaSettings</code> are read from an XML content that
215      * contains the global defaults.
216      * <p>
217      *
218      * @param jsp used to potentially access the XML content with the default captcha settings and
219      * to read overriden values from the request parameters.
220      *
221      * @return a clone of the singleton instance of the
222      * <em>"master"</em> <code>CmsCaptchaSettings</code>.
223      */

224     public static CmsCaptchaSettings getInstance(CmsJspActionElement jsp) {
225
226         CmsCaptchaSettings result = new CmsCaptchaSettings(jsp);
227         return (CmsCaptchaSettings)result.clone();
228     }
229
230     /**
231      * Returns the background color.
232      * <p>
233      *
234      * @return the background color
235      */

236     public Color JavaDoc getBackgroundColor() {
237
238         return m_backgroundColor;
239     }
240
241     /**
242      * Returns the background color as a hex string.
243      * <p>
244      *
245      * @return the background color as a hex string
246      */

247     public String JavaDoc getBackgroundColorString() {
248
249         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
250
251         buf.append("#");
252         buf.append(toHexString(m_backgroundColor.getRed()));
253         buf.append(toHexString(m_backgroundColor.getGreen()));
254         buf.append(toHexString(m_backgroundColor.getBlue()));
255
256         return buf.toString();
257     }
258
259     /**
260      * Returns the filter amplitude for the water filter that bends the text.
261      * <p>
262      *
263      * @return the filter amplitude for the water filter that bends the text.
264      */

265     public int getFilterAmplitude() {
266
267         return m_filterAmplitude;
268     }
269
270     /**
271      * Returns the filter wave length for the water filter that bends the text.
272      * <p>
273      *
274      * @return the filter wave length for the water filter that bends the text.
275      */

276     public int getFilterWaveLength() {
277
278         return m_filterWaveLength;
279     }
280
281     /**
282      * Returns the font color.
283      * <p>
284      *
285      * @return the font color
286      */

287     public Color JavaDoc getFontColor() {
288
289         return m_fontColor;
290     }
291
292     /**
293      * Returns the font color as a hex string.
294      * <p>
295      *
296      * @return the font color as a hex string
297      */

298     public String JavaDoc getFontColorString() {
299
300         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
301
302         buf.append("#");
303         buf.append(toHexString(m_fontColor.getRed()));
304         buf.append(toHexString(m_fontColor.getGreen()));
305         buf.append(toHexString(m_fontColor.getBlue()));
306
307         return buf.toString();
308     }
309
310     /**
311      * Returns the holes per glyph for a captcha image text (distortion).
312      *
313      * @return the holes per glyph for a captcha image text
314      */

315     public Integer JavaDoc getHolesPerGlyph() {
316
317         return m_holesPerGlyp;
318     }
319
320     /**
321      * Returns the image height.
322      * <p>
323      *
324      * @return the image height
325      */

326     public int getImageHeight() {
327
328         return m_imageHeight;
329     }
330
331     /**
332      * Returns the image width.
333      * <p>
334      *
335      * @return the image width
336      */

337     public int getImageWidth() {
338
339         return m_imageWidth;
340     }
341
342     /**
343      * Returns the max. font size.
344      * <p>
345      *
346      * @return the max. font size
347      */

348     public int getMaxFontSize() {
349
350         return m_maxFontSize;
351     }
352
353     /**
354      * Returns the max. phrase length.
355      * <p>
356      *
357      * @return the max. phrase length
358      */

359     public int getMaxPhraseLength() {
360
361         return m_maxPhraseLength;
362     }
363
364     /**
365      * Returns the min. font size.
366      * <p>
367      *
368      * @return the min. font size
369      */

370     public int getMinFontSize() {
371
372         return m_minFontSize;
373     }
374
375     /**
376      * Returns the min. phrase length.
377      * <p>
378      *
379      * @return the min. phrase length
380      */

381     public int getMinPhraseLength() {
382
383         return m_minPhraseLength;
384     }
385
386     /**
387      * Configures the instance with values overridden from the the request parameters.
388      * <p>
389      *
390      * @param jsp a Cms JSP page
391      *
392      * @see #C_PARAM_BACKGROUND_COLOR
393      *
394      * @see #C_PARAM_FILTER_AMPLITUDE
395      *
396      */

397     public void init(CmsJspActionElement jsp) {
398
399         HttpServletRequest JavaDoc request = jsp.getRequest();
400
401         // image width
402
String JavaDoc stringValue = request.getParameter(C_PARAM_IMAGE_WIDTH);
403         if (CmsStringUtil.isNotEmpty(stringValue)) {
404             m_imageWidth = Integer.parseInt(stringValue);
405         }
406
407         // image height
408
stringValue = request.getParameter(C_PARAM_IMAGE_HEIGHT);
409         if (CmsStringUtil.isNotEmpty(stringValue)) {
410             m_imageHeight = Integer.parseInt(stringValue);
411         }
412
413         // min. phrase length
414
stringValue = request.getParameter(C_PARAM_MIN_PHRASE_LENGTH);
415         if (CmsStringUtil.isNotEmpty(stringValue)) {
416             m_minPhraseLength = Integer.parseInt(stringValue);
417         }
418
419         // max. phrase length
420
stringValue = request.getParameter(C_PARAM_MAX_PHRASE_LENGTH);
421         if (CmsStringUtil.isNotEmpty(stringValue)) {
422             m_maxPhraseLength = Integer.parseInt(stringValue);
423         }
424
425         // min. font size
426
stringValue = request.getParameter(C_PARAM_MIN_FONT_SIZE);
427         if (CmsStringUtil.isNotEmpty(stringValue)) {
428             m_minFontSize = Integer.parseInt(stringValue);
429         }
430
431         // max. font size
432
stringValue = request.getParameter(C_PARAM_MAX_FONT_SIZE);
433         if (CmsStringUtil.isNotEmpty(stringValue)) {
434             m_maxFontSize = Integer.parseInt(stringValue);
435         }
436
437         // font color
438
stringValue = request.getParameter(C_PARAM_FONT_COLOR);
439         if (CmsStringUtil.isNotEmpty(stringValue)) {
440             stringValue = CmsEncoder.unescape(stringValue, jsp.getRequestContext().getEncoding());
441             setFontColor(stringValue);
442         }
443
444         // background color
445
stringValue = request.getParameter(C_PARAM_BACKGROUND_COLOR);
446         if (CmsStringUtil.isNotEmpty(stringValue)) {
447             stringValue = CmsEncoder.unescape(stringValue, jsp.getRequestContext().getEncoding());
448         }
449         setBackgroundColor(stringValue);
450
451         // holes per glyph
452
stringValue = request.getParameter(C_PARAM_HOLES_PER_GLYPH);
453         if (CmsStringUtil.isNotEmpty(stringValue)) {
454             setHolesPerGlyph(Integer.parseInt(stringValue));
455         }
456
457         // filter amplitude
458
stringValue = request.getParameter(C_PARAM_FILTER_AMPLITUDE);
459         if (CmsStringUtil.isNotEmpty(stringValue)) {
460             setFilterAmplitude(Integer.parseInt(stringValue));
461         }
462
463         // filter wave length
464
stringValue = request.getParameter(C_PARAM_FILTER_WAVE_LENGTH);
465         if (CmsStringUtil.isNotEmpty(stringValue)) {
466             setFilterWaveLength(Integer.parseInt(stringValue));
467         }
468         // flag for generation of background image (vs. background color)
469
stringValue = request.getParameter(C_PARAM_USE_BACKGROUND_IMAGE);
470         if (CmsStringUtil.isNotEmpty(stringValue)) {
471             setUseBackgroundImage(Boolean.valueOf(stringValue).booleanValue());
472         }
473
474         // characters to use for word generation:
475
stringValue = request.getParameter(C_PARAM_CHARACTERS);
476         if (CmsStringUtil.isNotEmpty(stringValue)) {
477             setCharacterPool(stringValue);
478         }
479         // characters to use for word generation:
480
stringValue = request.getParameter(C_PARAM_CHARACTERS);
481         if (CmsStringUtil.isNotEmpty(stringValue)) {
482             setCharacterPool(stringValue);
483         }
484
485         // just for logging comfort (find misconfigured presets):
486
stringValue = request.getParameter(C_PARAM_PRESET);
487         if (CmsStringUtil.isNotEmpty(stringValue)) {
488             m_presetPath = stringValue;
489         }
490
491     }
492
493     /**
494      * Configures the instance with overridden values from the given XML content.
495      * <p>
496      *
497      * <h3>Xmlcontent configuration notes</h3>
498      * <ol>
499      * <li>
500      * <ul>
501      * <li> If the xmlcontent contains no node for BackgroundColor ({@link CmsCaptchaSettings#NODE_CAPTCHAPRESET_BACKGROUNDCOLOR}),
502      * a background image will be used. </li>
503      * <li> If the xmlcontent node contains an empty node (trimmable to the empty String), the
504      * default background colour {@link Color#WHITE}) will be used as background. </li>
505      * <li> Else the chosen background color will be used. </li>
506      * </ul>
507      * </li>
508      * </ol>
509      * <p>
510      *
511      *
512      * @param cms the current user's Cms object
513      *
514      * @param content the XML content of the form
515      *
516      * @param locale the current locale
517      *
518      */

519     public void init(CmsObject cms, CmsXmlContent content, Locale JavaDoc locale) {
520
521         try {
522             String JavaDoc captchaSettingsPath = content.getStringValue(
523                 cms,
524                 new StringBuffer JavaDoc(CmsForm.NODE_CAPTCHA).append("/").append(CmsForm.NODE_CAPTCHA_PRESET).toString(),
525                 locale);
526             if (CmsStringUtil.isNotEmpty(captchaSettingsPath)) {
527                 m_presetPath = captchaSettingsPath;
528                 CmsFile captchaSettingsFile = cms.readFile(captchaSettingsPath);
529                 CmsXmlContent preset = CmsXmlContentFactory.unmarshal(cms, captchaSettingsFile);
530
531                 Locale JavaDoc captchaSettingsLocale = Locale.ENGLISH;
532
533                 // image width
534
String JavaDoc stringValue = preset.getStringValue(
535                     cms,
536                     CmsCaptchaSettings.NODE_CAPTCHAPRESET_IMAGEWIDTH,
537                     captchaSettingsLocale);
538                 if (CmsStringUtil.isNotEmpty(stringValue)) {
539                     m_imageWidth = Integer.parseInt(stringValue);
540                 }
541
542                 // image height
543
stringValue = preset.getStringValue(
544                     cms,
545                     CmsCaptchaSettings.NODE_CAPTCHAPRESET_IMAGEHEIGHT,
546                     captchaSettingsLocale);
547                 if (CmsStringUtil.isNotEmpty(stringValue)) {
548                     m_imageHeight = Integer.parseInt(stringValue);
549                 }
550
551                 // min. phrase length
552
stringValue = preset.getStringValue(
553                     cms,
554                     CmsCaptchaSettings.NODE_CAPTCHAPRESET_MIN_PHRASE_LENGTH,
555                     captchaSettingsLocale);
556                 if (CmsStringUtil.isNotEmpty(stringValue)) {
557                     m_minPhraseLength = Integer.parseInt(stringValue);
558                 }
559
560                 // max. phrase length
561
stringValue = preset.getStringValue(
562                     cms,
563                     CmsCaptchaSettings.NODE_CAPTCHAPRESET_MAX_PHRASE_LENGTH,
564                     captchaSettingsLocale);
565                 if (CmsStringUtil.isNotEmpty(stringValue)) {
566                     m_maxPhraseLength = Integer.parseInt(stringValue);
567                 }
568
569                 // min. font size
570
stringValue = preset.getStringValue(
571                     cms,
572                     CmsCaptchaSettings.NODE_CAPTCHAPRESET_MIN_FONT_SIZE,
573                     captchaSettingsLocale);
574                 if (CmsStringUtil.isNotEmpty(stringValue)) {
575                     m_minFontSize = Integer.parseInt(stringValue);
576                 }
577
578                 // max. font size
579
stringValue = preset.getStringValue(
580                     cms,
581                     CmsCaptchaSettings.NODE_CAPTCHAPRESET_MAX_FONT_SIZE,
582                     captchaSettingsLocale);
583                 if (CmsStringUtil.isNotEmpty(stringValue)) {
584                     m_maxFontSize = Integer.parseInt(stringValue);
585                 }
586
587                 // font color
588
stringValue = preset.getStringValue(
589                     cms,
590                     CmsCaptchaSettings.NODE_CAPTCHAPRESET_FONTCOLOR,
591                     captchaSettingsLocale);
592                 if (CmsStringUtil.isNotEmpty(stringValue)) {
593                     setFontColor(stringValue);
594                 }
595
596                 // background color
597
// if the field is defined but left blank, the default background color will be used
598
// if the field is not defined a gimpy background image will be used
599
stringValue = preset.getStringValue(
600                     cms,
601                     CmsCaptchaSettings.NODE_CAPTCHAPRESET_BACKGROUNDCOLOR,
602                     captchaSettingsLocale);
603                 setBackgroundColor(stringValue);
604
605                 // holes per glyph
606
stringValue = preset.getStringValue(
607                     cms,
608                     CmsCaptchaSettings.NODE_CAPTCHAPRESET_HOLESPERGLYPH,
609                     captchaSettingsLocale);
610                 if (CmsStringUtil.isNotEmpty(stringValue)) {
611                     setHolesPerGlyph(Integer.parseInt(stringValue));
612                 }
613
614                 // filter amplitude
615
stringValue = preset.getStringValue(
616                     cms,
617                     CmsCaptchaSettings.NODE_CAPTCHAPRESET_FILTER_AMPLITUDE,
618                     captchaSettingsLocale);
619                 if (CmsStringUtil.isNotEmpty(stringValue)) {
620                     setFilterAmplitude(Integer.parseInt(stringValue));
621                 }
622
623                 // filter wave length
624
stringValue = preset.getStringValue(
625                     cms,
626                     CmsCaptchaSettings.NODE_CAPTCHAPRESET_FILTER_WAVELENGTH,
627                     captchaSettingsLocale);
628                 if (CmsStringUtil.isNotEmpty(stringValue)) {
629                     setFilterWaveLength(Integer.parseInt(stringValue));
630                 }
631
632                 stringValue = preset.getStringValue(cms, CmsForm.NODE_CAPTCHA_CHARACTERS, captchaSettingsLocale);
633                 if (CmsStringUtil.isNotEmpty(stringValue)) {
634                     setCharacterPool(stringValue);
635                 }
636
637                 if (CmsStringUtil.isNotEmpty(stringValue)) {
638                     setCharacterPool(stringValue);
639                 }
640
641             } else {
642                 // the optional preset selector is missing...
643
}
644
645         } catch (Exception JavaDoc ex) {
646             if (LOG.isErrorEnabled()) {
647                 LOG.error(ex.getLocalizedMessage());
648             }
649
650         }
651
652     }
653
654     /**
655      * Returns the flag that decides wethter a background image or a background color is used.
656      * <p>
657      *
658      * @return the flag that decides wethter a background image or a background color is used
659      */

660     public boolean isUseBackgroundImage() {
661
662         return m_useBackgroundImage;
663     }
664
665     /**
666      * Sets the background color.
667      * <p>
668      *
669      * @param backgroundColor the background color to set
670      */

671     public void setBackgroundColor(Color JavaDoc backgroundColor) {
672
673         m_backgroundColor = backgroundColor;
674     }
675
676     /**
677      * Sets the background color as a hex string.
678      * <p>
679      *
680      * @param backgroundColor the background color to set as a hex string
681      */

682     public void setBackgroundColor(String JavaDoc backgroundColor) {
683
684         if (CmsStringUtil.isNotEmpty(backgroundColor)) {
685             if (backgroundColor.startsWith("#")) {
686                 backgroundColor = backgroundColor.substring(1);
687             }
688
689             m_backgroundColor = new Color JavaDoc(Integer.valueOf(backgroundColor, 16).intValue());
690             m_useBackgroundImage = false;
691         } else if (backgroundColor != null) {
692             // not totally empty but consists of whitespaces only: use default value
693
// this happens e.g. if the XML content to configure did contain the node but left the
694
// value empty
695
// in this case the default background color will be used
696
m_useBackgroundImage = false;
697             m_backgroundColor = Color.WHITE;
698         } else {
699             // really empty and null - not even defined in XML content:
700
// don't use background color but a gimpy background image
701
m_useBackgroundImage = true;
702             // the color is not used but we have to avoid NPE in getBackgroundColorString()
703
m_backgroundColor = Color.WHITE;
704         }
705     }
706
707     /**
708      * Sets the filter amplitude for the water filter that will bend the text.
709      *
710      * @param i the filter amplitude for the water filter that will bend the text to set.
711      */

712     public void setFilterAmplitude(int i) {
713
714         m_filterAmplitude = i;
715
716     }
717
718     /**
719      * Sets the filter wave length for the water filter that bends the text.
720      * <p>
721      *
722      * @param filterWaveLength the filter wave length for the water filter that bends the text to
723      * set
724      */

725     public void setFilterWaveLength(int filterWaveLength) {
726
727         m_filterWaveLength = filterWaveLength;
728     }
729
730     /**
731      * Sets the font color.
732      * <p>
733      *
734      * @param fontColor the font color to set
735      */

736     public void setFontColor(Color JavaDoc fontColor) {
737
738         m_fontColor = fontColor;
739     }
740
741     /**
742      * Sets the font color as a hex string.
743      * <p>
744      *
745      * @param fontColor the font color to set as a hex string
746      */

747     public void setFontColor(String JavaDoc fontColor) {
748
749         if (CmsStringUtil.isNotEmpty(fontColor)) {
750             if (fontColor.startsWith("#")) {
751                 fontColor = fontColor.substring(1);
752             }
753
754             m_fontColor = new Color JavaDoc(Integer.valueOf(fontColor, 16).intValue());
755         } else {
756             m_fontColor = Color.BLACK;
757         }
758     }
759
760     /**
761      * Sets the holes per glyph for a captcha image text (distortion).
762      *
763      * @param holes the holes per glyph for a captcha image text to set.
764      */

765     public void setHolesPerGlyph(int holes) {
766
767         m_holesPerGlyp = new Integer JavaDoc(holes);
768     }
769
770     /**
771      * Sets the image height.
772      * <p>
773      *
774      * @param imageHeight the image height to set
775      */

776     public void setImageHeight(int imageHeight) {
777
778         m_imageHeight = imageHeight;
779     }
780
781     /**
782      * Sets the image width.
783      * <p>
784      *
785      * @param imageWidth the image width to set
786      */

787     public void setImageWidth(int imageWidth) {
788
789         m_imageWidth = imageWidth;
790     }
791
792     /**
793      * Sets the max. font size.
794      * <p>
795      *
796      * @param maxFontSize the max. font size to set
797      */

798     public void setMaxFontSize(int maxFontSize) {
799
800         m_maxFontSize = maxFontSize;
801     }
802
803     /**
804      * Sets the max. phrase length.
805      * <p>
806      *
807      * @param maxPhraseLength the max. phrase length to set
808      */

809     public void setMaxPhraseLength(int maxPhraseLength) {
810
811         m_maxPhraseLength = maxPhraseLength;
812     }
813
814     /**
815      * Sets the min. font size.
816      * <p>
817      *
818      * @param minFontSize the min. font size to set
819      */

820     public void setMinFontSize(int minFontSize) {
821
822         m_minFontSize = minFontSize;
823     }
824
825     /**
826      * Sets the min. phrase length.
827      * <p>
828      *
829      * @param minPhraseLength the min. phrase length to set
830      */

831     public void setMinPhraseLength(int minPhraseLength) {
832
833         m_minPhraseLength = minPhraseLength;
834     }
835
836     /**
837      * Returns the flag that decides wethter a background image or a background color is used.
838      * <p>
839      *
840      * @param useBackgroundImage the flag that decides wethter a background image or a background
841      * color is used.
842      */

843     public void setUseBackgroundImage(boolean useBackgroundImage) {
844
845         m_useBackgroundImage = useBackgroundImage;
846     }
847
848     /**
849      * Creates a request parameter string from including all captcha settings.
850      * <p>
851      *
852      * @param cms needed for the context / encoding
853      * @return a request parameter string from including all captcha settings
854      */

855     public String JavaDoc toRequestParams(CmsObject cms) {
856
857         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
858
859         buf.append(C_PARAM_IMAGE_WIDTH).append("=").append(m_imageWidth);
860         buf.append("&").append(C_PARAM_IMAGE_HEIGHT).append("=").append(m_imageHeight);
861         buf.append("&").append(C_PARAM_MIN_FONT_SIZE).append("=").append(m_minFontSize);
862         buf.append("&").append(C_PARAM_MAX_FONT_SIZE).append("=").append(m_maxFontSize);
863         buf.append("&").append(C_PARAM_MIN_PHRASE_LENGTH).append("=").append(m_minPhraseLength);
864         buf.append("&").append(C_PARAM_MAX_PHRASE_LENGTH).append("=").append(m_maxPhraseLength);
865         buf.append("&").append(C_PARAM_FONT_COLOR).append("=").append(
866             CmsEncoder.escape(getFontColorString(), cms.getRequestContext().getEncoding()));
867         buf.append("&").append(C_PARAM_BACKGROUND_COLOR).append("=").append(
868             CmsEncoder.escape(getBackgroundColorString(), cms.getRequestContext().getEncoding()));
869         buf.append("&").append(C_PARAM_HOLES_PER_GLYPH).append("=").append(m_holesPerGlyp);
870         buf.append("&").append(C_PARAM_FILTER_AMPLITUDE).append("=").append(m_filterAmplitude);
871         buf.append("&").append(C_PARAM_FILTER_WAVE_LENGTH).append("=").append(m_filterWaveLength);
872         buf.append("&").append(C_PARAM_CHARACTERS).append("=").append(m_characterPool);
873         buf.append("&").append(C_PARAM_PRESET).append("=").append(m_presetPath);
874         buf.append("&").append(C_PARAM_USE_BACKGROUND_IMAGE).append("=").append(Boolean.toString(m_useBackgroundImage));
875         return buf.toString();
876     }
877
878     /**
879      * @see java.lang.Object#clone()
880      */

881     protected Object JavaDoc clone() {
882
883         CmsCaptchaSettings result = new CmsCaptchaSettings();
884         // copy all members here:
885
result.m_backgroundColor = m_backgroundColor;
886         result.m_filterAmplitude = m_filterAmplitude;
887         result.m_filterWaveLength = m_filterWaveLength;
888         result.m_fontColor = m_fontColor;
889         result.m_holesPerGlyp = m_holesPerGlyp;
890         result.m_imageHeight = m_imageHeight;
891         result.m_imageWidth = m_imageWidth;
892         result.m_maxFontSize = m_maxFontSize;
893         result.m_maxPhraseLength = m_maxPhraseLength;
894         result.m_minFontSize = m_minFontSize;
895         result.m_useBackgroundImage = m_useBackgroundImage;
896         result.m_minPhraseLength = m_minPhraseLength;
897         result.m_characterPool = m_characterPool;
898         result.m_presetPath = m_presetPath;
899         return result;
900     }
901
902     /**
903      * Returns the characterPool.
904      * <p>
905      *
906      * @return the characterPool
907      */

908     String JavaDoc getCharacterPool() {
909
910         return m_characterPool;
911     }
912
913     /**
914      * Returns the preset path that was used to configure these settings.
915      * <p>
916      *
917      * This is read only, as the path is internally read from a nested CmsForm/FormCaptcha XML
918      * content.
919      * <p>
920      *
921      * @return the preset path that was used to configure these settings
922      */

923     String JavaDoc getPresetPath() {
924
925         return m_presetPath;
926     }
927
928     /**
929      * Sets the characterPool.
930      * <p>
931      *
932      * @param characterPool the characterPool to set
933      */

934     void setCharacterPool(String JavaDoc characterPool) {
935
936         m_characterPool = characterPool;
937     }
938
939     /**
940      * Converts a color range of a color into a hex string.
941      * <p>
942      *
943      * @param colorRange the color range of a color
944      * @return the hex string of the color range
945      */

946     private String JavaDoc toHexString(int colorRange) {
947
948         if (colorRange < 10) {
949             return "0" + Integer.toHexString(colorRange);
950         } else {
951             return Integer.toHexString(colorRange);
952         }
953     }
954 }
955
Popular Tags