You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by re...@apache.org on 2004/11/26 10:57:02 UTC

svn commit: r106652 - cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor

Author: reinhard
Date: Fri Nov 26 01:57:02 2004
New Revision: 106652

URL: http://svn.apache.org/viewcvs?view=rev&rev=106652
Log:
uuups, forgot to check them in (HTMLCleaningConvertor)
Added:
   cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/HtmlCleanerConfiguration.xml
   cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/HtmlCleaningConvertor.java
   cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/HtmlCleaningConvertorBuilder.java

Added: cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/HtmlCleanerConfiguration.xml
Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/HtmlCleanerConfiguration.xml?view=auto&rev=106652
==============================================================================
--- (empty file)
+++ cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/HtmlCleanerConfiguration.xml	Fri Nov 26 01:57:02 2004
@@ -0,0 +1,143 @@
+<!--
+  Copyright 1999-2004 The Apache Software Foundation
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+<htmlcleaner>
+  <cleanup>
+    <!-- Span elements will only be retained if they have one of the classes listed belowed. -->
+    <allowed-span-classes>
+    </allowed-span-classes>
+
+    <!-- Div elements will only be retained if they have one of the classes listed belowed. -->
+    <allowed-div-classes>
+    </allowed-div-classes>
+
+    <!-- The class attribute of p elements will only be retained if they have one of the classes listed belowed. -->
+    <allowed-para-classes>
+      <class>note</class>
+      <class>warn</class>
+      <class>fixme</class>
+    </allowed-para-classes>
+
+    <!-- The class attribute of pre elements will only be retained if they have one of the classes listed belowed. -->
+    <allowed-pre-classes>
+      <class>query</class>
+      <class>include</class>
+      <class>query-and-include</class>      
+    </allowed-pre-classes>
+
+    <img-alternate-src-attr name="title"/>
+
+    <!-- Lists the allowed elements and their allowed attributes.
+
+         For the correct worker of the cleaner, some elements cannot be removed,
+         usch as html, body, p, some others...
+     -->
+    <allowed-elements>
+      <element name="html"/>
+      <element name="body"/>
+
+      <element name="div">
+        <attribute name="class"/>
+      </element>
+      <element name="span">
+        <attribute name="class"/>
+      </element>
+
+      <element name="p">
+        <attribute name="align"/>
+        <attribute name="class"/>
+      </element>
+      <element name="br"/>
+      <element name="pre">
+        <attribute name="class"/>
+      </element>
+
+      <!-- titles -->
+      <element name="h1"/>
+      <element name="h2"/>
+      <element name="h3"/>
+      <element name="h4"/>
+      <element name="h5"/>
+
+      <!-- linking -->
+      <element name="a">
+        <attribute name="href"/>
+      </element>
+
+      <!-- inline styles -->
+      <element name="strong"/>
+      <element name="em"/>
+      <element name="sup"/>
+      <element name="sub"/>
+
+      <!-- lists -->
+      <element name="ul"/>
+      <element name="ol"/>
+      <element name="li"/>
+
+      <element name="img">
+        <attribute name="src"/>
+        <attribute name="align"/>
+      </element>
+
+      <!-- tables -->
+      <element name="table">
+        <attribute name="class"/>
+      </element>
+      <element name="tbody"/>
+      <element name="tr"/>
+      <element name="td">
+        <attribute name="colspan"/>
+        <attribute name="rowspan"/>
+        <attribute name="valign"/>
+      </element>
+      <element name="th">
+        <attribute name="colspan"/>
+        <attribute name="rowspan"/>
+        <attribute name="valign"/>
+      </element>
+      <element name="hr"/>
+    </allowed-elements>
+  </cleanup>
+
+  <serialization>
+    <linewidth value="80"/>
+    <elements>
+      <!-- Note: to avoid unexpected results, mention all block-type tags here,
+           and NO inline tags. -->
+      <element name="html" afterOpen="1" beforeClose="1"/>
+      <element name="body" afterOpen="1" beforeClose="1"/>
+      <element name="p" beforeOpen="1" afterClose="1"/>
+      <element name="h1" beforeOpen="1" afterClose="1"/>
+      <element name="h2" beforeOpen="1" afterClose="1"/>
+      <element name="h3" beforeOpen="1" afterClose="1"/>
+      <element name="h4" beforeOpen="1" afterClose="1"/>
+      <element name="h5" beforeOpen="1" afterClose="1"/>
+      <element name="pre" beforeOpen="1" afterClose="1"/>
+      <element name="div" beforeOpen="1" afterClose="1"/>
+      <element name="ul" beforeOpen="1" beforeClose="1" afterClose="1"/>
+      <element name="ol" beforeOpen="1" beforeClose="1" afterClose="1"/>
+      <element name="li" beforeOpen="1"/>
+      <element name="table" beforeOpen="1" beforeClose="1" afterClose="1"/>
+      <element name="tbody" beforeOpen="1" beforeClose="1"/>
+      <element name="tr" beforeOpen="1" beforeClose="1"/>
+      <element name="td" beforeOpen="1"/>
+      <element name="th" beforeOpen="1"/>
+      <element name="br" afterClose="1"/>
+
+    </elements>
+  </serialization>
+
+</htmlcleaner>
\ No newline at end of file

Added: cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/HtmlCleaningConvertor.java
Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/HtmlCleaningConvertor.java?view=auto&rev=106652
==============================================================================
--- (empty file)
+++ cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/HtmlCleaningConvertor.java	Fri Nov 26 01:57:02 2004
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2004 Outerthought bvba and Schaubroeck nv
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.forms.datatype.convertor;
+
+import org.apache.cocoon.forms.datatype.convertor.Convertor;
+import org.apache.cocoon.forms.datatype.convertor.ConversionResult;
+import org.apache.cocoon.forms.validation.ValidationError;
+import org.apache.commons.lang.exception.ExceptionUtils;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+import org.outerj.daisy.htmlcleaner.HtmlCleanerTemplate;
+import org.outerj.daisy.htmlcleaner.HtmlCleaner;
+
+import java.util.Locale;
+
+/**
+ * A CForms convertor doing string-to-string conversion by utilizing
+ * the Daisy HtmlCleaner component.
+ */
+public class HtmlCleaningConvertor implements Convertor {
+    HtmlCleanerTemplate template;
+
+    public HtmlCleaningConvertor(HtmlCleanerTemplate template) {
+        this.template = template;
+    }
+
+    public ConversionResult convertFromString(String value, Locale locale, Convertor.FormatCache formatCache) {
+        HtmlCleaner cleaner = template.newHtmlCleaner();
+        try {
+            String result = cleaner.cleanToString(value);
+            return new ConversionResult(result);
+        } catch (Exception e) {
+            Throwable t = ExceptionUtils.getRootCause(e);
+            if (t == null)
+                t = e;
+            String message = t.getMessage();
+            if (message == null)
+                message = t.toString();
+            ValidationError validationError = new ValidationError(message, false);
+            return new ConversionResult(validationError);
+        }
+    }
+
+    public String convertToString(Object object, Locale locale, Convertor.FormatCache formatCache) {
+        return (String)object;
+    }
+
+    public Class getTypeClass() {
+        return java.lang.String.class;
+    }
+
+    public void generateSaxFragment(ContentHandler contentHandler, Locale locale) throws SAXException {
+        // nothing to say about me
+    }
+}

Added: cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/HtmlCleaningConvertorBuilder.java
Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/HtmlCleaningConvertorBuilder.java?view=auto&rev=106652
==============================================================================
--- (empty file)
+++ cocoon/trunk/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/HtmlCleaningConvertorBuilder.java	Fri Nov 26 01:57:02 2004
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2004 Outerthought bvba and Schaubroeck nv
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.cocoon.forms.datatype.convertor;
+
+import org.apache.cocoon.forms.datatype.convertor.ConvertorBuilder;
+import org.apache.cocoon.forms.datatype.convertor.Convertor;
+import org.apache.cocoon.forms.util.DomHelper;
+import org.apache.cocoon.forms.CacheManager;
+import org.apache.cocoon.components.source.SourceUtil;
+import org.apache.avalon.framework.service.Serviceable;
+import org.apache.avalon.framework.service.ServiceManager;
+import org.apache.avalon.framework.service.ServiceException;
+import org.apache.excalibur.source.SourceResolver;
+import org.apache.excalibur.source.Source;
+import org.w3c.dom.Element;
+import org.outerj.daisy.htmlcleaner.HtmlCleanerTemplate;
+import org.outerj.daisy.htmlcleaner.HtmlCleanerFactory;
+import org.xml.sax.InputSource;
+
+/**
+ * Builds {@link HtmlCleaningConvertor}s.
+ */
+public class HtmlCleaningConvertorBuilder implements ConvertorBuilder, Serviceable {
+    private ServiceManager serviceManager;
+
+    public void service(ServiceManager serviceManager) throws ServiceException {
+        this.serviceManager = serviceManager;
+    }
+
+    public Convertor build(Element element) throws Exception {
+        String config = DomHelper.getAttribute(element, "config");
+
+        Source source = null;
+        SourceResolver sourceResolver = null;
+        CacheManager cacheManager = null;
+        try {
+            cacheManager = (CacheManager)serviceManager.lookup(CacheManager.ROLE);
+            sourceResolver = (SourceResolver)serviceManager.lookup(SourceResolver.ROLE);
+            source = sourceResolver.resolveURI(config);
+
+            String prefix = HtmlCleanerTemplate.class.getName();
+            HtmlCleanerTemplate template = (HtmlCleanerTemplate)cacheManager.get(source, prefix);
+            if (template == null) {
+                HtmlCleanerFactory factory = new HtmlCleanerFactory();
+                InputSource is = SourceUtil.getInputSource(source);
+                template = factory.buildTemplate(is);
+                cacheManager.set(template, source, prefix);
+            }
+
+            return new HtmlCleaningConvertor(template);
+        } finally {
+            if (source != null)
+                sourceResolver.release(source);
+            if (sourceResolver != null)
+                serviceManager.release(sourceResolver);
+            if (cacheManager != null)
+                serviceManager.release(cacheManager);
+        }
+    }
+}