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.woody.datatype.typeimpl;
18:
19: import java.util.ArrayList;
20: import java.util.Iterator;
21: import java.util.List;
22: import java.util.Locale;
23:
24: import org.apache.cocoon.woody.datatype.Datatype;
25: import org.apache.cocoon.woody.datatype.DatatypeBuilder;
26: import org.apache.cocoon.woody.datatype.ValidationError;
27: import org.apache.cocoon.woody.datatype.ValidationRule;
28: import org.apache.cocoon.woody.datatype.convertor.Convertor;
29: import org.outerj.expression.ExpressionContext;
30:
31: /**
32: * Abstract base class for Datatype implementations. Most concreate datatypes
33: * will derive from this class.
34: * @version $Id: AbstractDatatype.java 433543 2006-08-22 06:22:54Z crossley $
35: */
36: public abstract class AbstractDatatype implements Datatype {
37: private List validationRules = new ArrayList();
38: private boolean arrayType = false;
39: private DatatypeBuilder builder;
40: private Convertor convertor;
41:
42: public ValidationError validate(Object value,
43: ExpressionContext expressionContext) {
44: Iterator validationRulesIt = validationRules.iterator();
45: while (validationRulesIt.hasNext()) {
46: ValidationRule validationRule = (ValidationRule) validationRulesIt
47: .next();
48: ValidationError result = validationRule.validate(value,
49: expressionContext);
50: if (result != null)
51: return result;
52: }
53: return null;
54: }
55:
56: public void addValidationRule(ValidationRule validationRule) {
57: validationRules.add(validationRule);
58: }
59:
60: public boolean isArrayType() {
61: return arrayType;
62: }
63:
64: protected void setArrayType(boolean arrayType) {
65: this .arrayType = arrayType;
66: }
67:
68: protected void setConvertor(Convertor convertor) {
69: this .convertor = convertor;
70: }
71:
72: protected void setBuilder(DatatypeBuilder builder) {
73: this .builder = builder;
74: }
75:
76: public Convertor getPlainConvertor() {
77: return builder.getPlainConvertor();
78: }
79:
80: public DatatypeBuilder getBuilder() {
81: return builder;
82: }
83:
84: public Convertor getConvertor() {
85: return convertor;
86: }
87:
88: public Object convertFromString(String value, Locale locale) {
89: return getConvertor().convertFromString(value, locale, null);
90: }
91:
92: public String convertToString(Object value, Locale locale) {
93: return getConvertor().convertToString(value, locale, null);
94: }
95: }
|