001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.forms.datatype.typeimpl;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Locale;
023:
024: import org.apache.cocoon.forms.datatype.Datatype;
025: import org.apache.cocoon.forms.datatype.DatatypeBuilder;
026: import org.apache.cocoon.forms.datatype.ValidationRule;
027: import org.apache.cocoon.forms.datatype.convertor.Convertor;
028: import org.apache.cocoon.forms.datatype.convertor.ConversionResult;
029: import org.apache.cocoon.forms.validation.ValidationError;
030: import org.apache.cocoon.forms.FormsConstants;
031: import org.apache.cocoon.xml.AttributesImpl;
032: import org.outerj.expression.ExpressionContext;
033: import org.xml.sax.ContentHandler;
034: import org.xml.sax.SAXException;
035:
036: /**
037: * Abstract base class for Datatype implementations. Most concreate datatypes
038: * will derive from this class.
039: * @version $Id: AbstractDatatype.java 449149 2006-09-23 03:58:05Z crossley $
040: */
041: public abstract class AbstractDatatype implements Datatype {
042: private List validationRules = new ArrayList();
043: private boolean arrayType = false;
044: private DatatypeBuilder builder;
045: private Convertor convertor;
046:
047: public ValidationError validate(Object value,
048: ExpressionContext expressionContext) {
049: Iterator validationRulesIt = validationRules.iterator();
050: while (validationRulesIt.hasNext()) {
051: ValidationRule validationRule = (ValidationRule) validationRulesIt
052: .next();
053: ValidationError result = validationRule.validate(value,
054: expressionContext);
055: if (result != null)
056: return result;
057: }
058: return null;
059: }
060:
061: public void addValidationRule(ValidationRule validationRule) {
062: validationRules.add(validationRule);
063: }
064:
065: public boolean isArrayType() {
066: return arrayType;
067: }
068:
069: protected void setArrayType(boolean arrayType) {
070: this .arrayType = arrayType;
071: }
072:
073: public void setConvertor(Convertor convertor) {
074: this .convertor = convertor;
075: }
076:
077: protected void setBuilder(DatatypeBuilder builder) {
078: this .builder = builder;
079: }
080:
081: public Convertor getPlainConvertor() {
082: return builder.getPlainConvertor();
083: }
084:
085: public DatatypeBuilder getBuilder() {
086: return builder;
087: }
088:
089: public Convertor getConvertor() {
090: return convertor;
091: }
092:
093: public ConversionResult convertFromString(String value,
094: Locale locale) {
095: return getConvertor().convertFromString(value, locale, null);
096: }
097:
098: public String convertToString(Object value, Locale locale) {
099: return getConvertor().convertToString(value, locale, null);
100: }
101:
102: private static final String DATATYPE_EL = "datatype";
103:
104: public void generateSaxFragment(ContentHandler contentHandler,
105: Locale locale) throws SAXException {
106: AttributesImpl attrs = new AttributesImpl();
107: attrs.addCDATAAttribute("type", getDescriptiveName());
108: contentHandler.startElement(FormsConstants.INSTANCE_NS,
109: DATATYPE_EL, FormsConstants.INSTANCE_PREFIX_COLON
110: + DATATYPE_EL, attrs);
111: getConvertor().generateSaxFragment(contentHandler, locale);
112: contentHandler.endElement(FormsConstants.INSTANCE_NS,
113: DATATYPE_EL, FormsConstants.INSTANCE_PREFIX_COLON
114: + DATATYPE_EL);
115: }
116: }
|