KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > editor > HtmlCleaningConvertor


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
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.outerj.daisy.frontend.editor;
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 JavaDoc;
23 import org.xml.sax.SAXException JavaDoc;
24 import org.outerj.daisy.htmlcleaner.HtmlCleanerTemplate;
25 import org.outerj.daisy.htmlcleaner.HtmlCleaner;
26
27 import java.util.Locale JavaDoc;
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 JavaDoc value, Locale JavaDoc locale, Convertor.FormatCache formatCache) {
41         HtmlCleaner cleaner = template.newHtmlCleaner();
42         try {
43             String JavaDoc result = cleaner.cleanToString(value);
44             return new ConversionResult(result);
45         } catch (Exception JavaDoc e) {
46             Throwable JavaDoc t = ExceptionUtils.getRootCause(e);
47             if (t == null)
48                 t = e;
49             String JavaDoc message = t.getMessage();
50             if (message == null)
51                 message = t.toString();
52             ValidationError validationError = new ValidationError(message, false);
53             return new ConversionResult(validationError);
54         }
55     }
56
57     public String JavaDoc convertToString(Object JavaDoc object, Locale JavaDoc locale, Convertor.FormatCache formatCache) {
58         return (String JavaDoc)object;
59     }
60
61     public Class JavaDoc getTypeClass() {
62         return java.lang.String JavaDoc.class;
63     }
64
65     public void generateSaxFragment(ContentHandler JavaDoc contentHandler, Locale JavaDoc locale) throws SAXException JavaDoc {
66         // nothing to say about me
67
}
68 }
69
Popular Tags