01: /*
02: * Copyright 2004 Outerthought bvba and Schaubroeck nv
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.convertor;
17:
18: import org.apache.cocoon.forms.datatype.convertor.Convertor;
19: import org.apache.cocoon.forms.datatype.convertor.ConversionResult;
20: import org.apache.cocoon.forms.validation.ValidationError;
21: import org.apache.commons.lang.exception.ExceptionUtils;
22: import org.xml.sax.ContentHandler;
23: import org.xml.sax.SAXException;
24: import org.outerj.daisy.htmlcleaner.HtmlCleanerTemplate;
25: import org.outerj.daisy.htmlcleaner.HtmlCleaner;
26:
27: import java.util.Locale;
28:
29: /**
30: * A CForms convertor doing string-to-string conversion by utilizing
31: * the Daisy HtmlCleaner component.
32: */
33: public class HtmlCleaningConvertor implements Convertor {
34: HtmlCleanerTemplate template;
35:
36: public HtmlCleaningConvertor(HtmlCleanerTemplate template) {
37: this .template = template;
38: }
39:
40: public ConversionResult convertFromString(String value,
41: Locale locale, Convertor.FormatCache formatCache) {
42: HtmlCleaner cleaner = template.newHtmlCleaner();
43: try {
44: String result = cleaner.cleanToString(value);
45: return new ConversionResult(result);
46: } catch (Exception e) {
47: Throwable t = ExceptionUtils.getRootCause(e);
48: if (t == null)
49: t = e;
50: String message = t.getMessage();
51: if (message == null)
52: message = t.toString();
53: ValidationError validationError = new ValidationError(
54: message, false);
55: return new ConversionResult(validationError);
56: }
57: }
58:
59: public String convertToString(Object object, Locale locale,
60: Convertor.FormatCache formatCache) {
61: return (String) object;
62: }
63:
64: public Class getTypeClass() {
65: return java.lang.String.class;
66: }
67:
68: public void generateSaxFragment(ContentHandler contentHandler,
69: Locale locale) throws SAXException {
70: // nothing to say about me
71: }
72: }
|