KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > datatype > typeimpl > AbstractDatatype


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.forms.datatype.typeimpl;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Locale JavaDoc;
22
23 import org.apache.cocoon.forms.datatype.Datatype;
24 import org.apache.cocoon.forms.datatype.DatatypeBuilder;
25 import org.apache.cocoon.forms.datatype.ValidationRule;
26 import org.apache.cocoon.forms.datatype.convertor.Convertor;
27 import org.apache.cocoon.forms.datatype.convertor.ConversionResult;
28 import org.apache.cocoon.forms.validation.ValidationError;
29 import org.apache.cocoon.forms.FormsConstants;
30 import org.apache.cocoon.xml.AttributesImpl;
31 import org.outerj.expression.ExpressionContext;
32 import org.xml.sax.ContentHandler JavaDoc;
33 import org.xml.sax.SAXException JavaDoc;
34
35 /**
36  * Abstract base class for Datatype implementations. Most concreate datatypes
37  * will derive from this class.
38  * @version $Id: AbstractDatatype.java 326838 2005-10-20 06:26:53Z sylvain $
39  */

40 public abstract class AbstractDatatype implements Datatype {
41     private List JavaDoc validationRules = new ArrayList JavaDoc();
42     private boolean arrayType = false;
43     private DatatypeBuilder builder;
44     private Convertor convertor;
45
46     public ValidationError validate(Object JavaDoc value, ExpressionContext expressionContext) {
47         Iterator JavaDoc validationRulesIt = validationRules.iterator();
48         while (validationRulesIt.hasNext()) {
49             ValidationRule validationRule = (ValidationRule)validationRulesIt.next();
50             ValidationError result = validationRule.validate(value, expressionContext);
51             if (result != null)
52                 return result;
53         }
54         return null;
55     }
56
57     public void addValidationRule(ValidationRule validationRule) {
58         validationRules.add(validationRule);
59     }
60
61     public boolean isArrayType() {
62         return arrayType;
63     }
64
65     protected void setArrayType(boolean arrayType) {
66         this.arrayType = arrayType;
67     }
68
69     protected void setConvertor(Convertor convertor) {
70         this.convertor = convertor;
71     }
72
73     protected void setBuilder(DatatypeBuilder builder) {
74         this.builder = builder;
75     }
76
77     public Convertor getPlainConvertor() {
78         return builder.getPlainConvertor();
79     }
80
81     public DatatypeBuilder getBuilder() {
82         return builder;
83     }
84
85     public Convertor getConvertor() {
86         return convertor;
87     }
88
89     public ConversionResult convertFromString(String JavaDoc value, Locale JavaDoc locale) {
90         return getConvertor().convertFromString(value, locale, null);
91     }
92
93     public String JavaDoc convertToString(Object JavaDoc value, Locale JavaDoc locale) {
94         return getConvertor().convertToString(value, locale, null);
95     }
96
97     private static final String JavaDoc DATATYPE_EL = "datatype";
98
99     public void generateSaxFragment(ContentHandler JavaDoc contentHandler, Locale JavaDoc locale) throws SAXException JavaDoc {
100         AttributesImpl attrs = new AttributesImpl();
101         attrs.addCDATAAttribute("type", getDescriptiveName());
102         contentHandler.startElement(FormsConstants.INSTANCE_NS, DATATYPE_EL, FormsConstants.INSTANCE_PREFIX_COLON + DATATYPE_EL, attrs);
103         getConvertor().generateSaxFragment(contentHandler, locale);
104         contentHandler.endElement(FormsConstants.INSTANCE_NS, DATATYPE_EL, FormsConstants.INSTANCE_PREFIX_COLON + DATATYPE_EL);
105     }
106 }
107
Popular Tags