01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.forms.formmodel;
18:
19: import org.apache.avalon.framework.context.Context;
20: import org.apache.avalon.framework.context.ContextException;
21: import org.apache.avalon.framework.context.Contextualizable;
22: import org.apache.avalon.framework.thread.ThreadSafe;
23: import org.apache.cocoon.forms.util.DomHelper;
24: import org.w3c.dom.Element;
25:
26: /**
27: * Builds {@link CaptchaFieldDefinition}s.
28: *
29: * @see <a href="http://www.captcha.net">www.captcha.net</a>
30: * @version $Id: CaptchaDefinitionBuilder.java 449149 2006-09-23 03:58:05Z crossley $
31: */
32: public class CaptchaDefinitionBuilder extends
33: AbstractDatatypeWidgetDefinitionBuilder implements
34: Contextualizable, ThreadSafe {
35:
36: private Context avalonContext;
37:
38: public void contextualize(Context context) throws ContextException {
39: this .avalonContext = context;
40: }
41:
42: public WidgetDefinition buildWidgetDefinition(Element widgetElement)
43: throws Exception {
44: FieldDefinition definition = new CaptchaFieldDefinition(
45: avalonContext);
46: setupDefinition(widgetElement, definition);
47: definition.makeImmutable();
48: return definition;
49: }
50:
51: protected void setupDefinition(Element widgetElement,
52: FieldDefinition definition) throws Exception {
53: super .setupDefinition(widgetElement, definition);
54:
55: // parse "@required"
56: boolean required = DomHelper.getAttributeAsBoolean(
57: widgetElement, "required", false);
58: definition.setRequired(required);
59:
60: // parse "@length"
61: int length = DomHelper.getAttributeAsInteger(widgetElement,
62: "length", 7);
63: ((CaptchaFieldDefinition) definition).setLength(length);
64: }
65: }
|