KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > 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.woody.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.woody.datatype.Datatype;
24 import org.apache.cocoon.woody.datatype.DatatypeBuilder;
25 import org.apache.cocoon.woody.datatype.ValidationError;
26 import org.apache.cocoon.woody.datatype.ValidationRule;
27 import org.apache.cocoon.woody.datatype.convertor.Convertor;
28 import org.outerj.expression.ExpressionContext;
29
30 /**
31  * Abstract base class for Datatype implementations. Most concreate datatypes
32  * will derive from this class.
33  * @version $Id: AbstractDatatype.java 30932 2004-07-29 17:35:38Z vgritsenko $
34  */

35 public abstract class AbstractDatatype implements Datatype {
36     private List JavaDoc validationRules = new ArrayList JavaDoc();
37     private boolean arrayType = false;
38     private DatatypeBuilder builder;
39     private Convertor convertor;
40
41     public ValidationError validate(Object JavaDoc value, ExpressionContext expressionContext) {
42         Iterator JavaDoc validationRulesIt = validationRules.iterator();
43         while (validationRulesIt.hasNext()) {
44             ValidationRule validationRule = (ValidationRule)validationRulesIt.next();
45             ValidationError result = validationRule.validate(value, expressionContext);
46             if (result != null)
47                 return result;
48         }
49         return null;
50     }
51
52     public void addValidationRule(ValidationRule validationRule) {
53         validationRules.add(validationRule);
54     }
55
56     public boolean isArrayType() {
57         return arrayType;
58     }
59
60     protected void setArrayType(boolean arrayType) {
61         this.arrayType = arrayType;
62     }
63
64     protected void setConvertor(Convertor convertor) {
65         this.convertor = convertor;
66     }
67
68     protected void setBuilder(DatatypeBuilder builder) {
69         this.builder = builder;
70     }
71
72     public Convertor getPlainConvertor() {
73         return builder.getPlainConvertor();
74     }
75
76     public DatatypeBuilder getBuilder() {
77         return builder;
78     }
79
80     public Convertor getConvertor() {
81         return convertor;
82     }
83
84     public Object JavaDoc convertFromString(String JavaDoc value, Locale JavaDoc locale) {
85         return getConvertor().convertFromString(value, locale, null);
86     }
87
88     public String JavaDoc convertToString(Object JavaDoc value, Locale JavaDoc locale) {
89         return getConvertor().convertToString(value, locale, null);
90     }
91 }
92
Popular Tags