cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jo...@apache.org
Subject svn commit: r654400 - in /cocoon/branches/BRANCH_2_1_X/src/blocks/html/java/org/apache/cocoon: components/ components/NekoHtmlSaxParser.java generation/NekoHTMLGenerator.java transformation/NekoHTMLTransformer.java
Date Thu, 08 May 2008 03:19:26 GMT
Author: joerg
Date: Wed May  7 20:19:26 2008
New Revision: 654400

URL: http://svn.apache.org/viewvc?rev=654400&view=rev
Log:
avoid code duplication: extract NekoHtmlSaxParser from NekoHTMLGenerator and NekoHTMLTransformer

Added:
    cocoon/branches/BRANCH_2_1_X/src/blocks/html/java/org/apache/cocoon/components/
    cocoon/branches/BRANCH_2_1_X/src/blocks/html/java/org/apache/cocoon/components/NekoHtmlSaxParser.java
Modified:
    cocoon/branches/BRANCH_2_1_X/src/blocks/html/java/org/apache/cocoon/generation/NekoHTMLGenerator.java
    cocoon/branches/BRANCH_2_1_X/src/blocks/html/java/org/apache/cocoon/transformation/NekoHTMLTransformer.java

Added: cocoon/branches/BRANCH_2_1_X/src/blocks/html/java/org/apache/cocoon/components/NekoHtmlSaxParser.java
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X/src/blocks/html/java/org/apache/cocoon/components/NekoHtmlSaxParser.java?rev=654400&view=auto
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/html/java/org/apache/cocoon/components/NekoHtmlSaxParser.java
(added)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/html/java/org/apache/cocoon/components/NekoHtmlSaxParser.java
Wed May  7 20:19:26 2008
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.components;
+
+import java.util.Iterator;
+import java.util.Properties;
+
+import org.apache.xerces.parsers.AbstractSAXParser;
+import org.cyberneko.html.HTMLConfiguration;
+
+/**
+ * @version $Id$
+ */
+public class NekoHtmlSaxParser extends AbstractSAXParser {
+
+    public NekoHtmlSaxParser(Properties properties) {
+        super(getConfig(properties));
+    }
+
+    private static HTMLConfiguration getConfig(Properties properties) {
+        HTMLConfiguration config = new HTMLConfiguration();
+        config.setProperty("http://cyberneko.org/html/properties/names/elems", "lower");
+        if (properties != null) {
+            for (Iterator i = properties.keySet().iterator(); i.hasNext();) {
+                String name = (String) i.next();
+                if (name.indexOf("/features/") > -1) {
+                    config.setFeature(name, Boolean.getBoolean(properties.getProperty(name)));
+                } else if (name.indexOf("/properties/") > -1) {
+                    config.setProperty(name, properties.getProperty(name));
+                }
+            }
+        }
+        return config;
+    }
+
+}
\ No newline at end of file

Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/html/java/org/apache/cocoon/generation/NekoHTMLGenerator.java
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X/src/blocks/html/java/org/apache/cocoon/generation/NekoHTMLGenerator.java?rev=654400&r1=654399&r2=654400&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/html/java/org/apache/cocoon/generation/NekoHTMLGenerator.java
(original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/html/java/org/apache/cocoon/generation/NekoHTMLGenerator.java
Wed May  7 20:19:26 2008
@@ -19,7 +19,6 @@
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.util.Iterator;
 import java.util.Map;
 import java.util.Properties;
 
@@ -35,6 +34,7 @@
 import org.apache.cocoon.ProcessingException;
 import org.apache.cocoon.ResourceNotFoundException;
 import org.apache.cocoon.caching.CacheableProcessingComponent;
+import org.apache.cocoon.components.NekoHtmlSaxParser;
 import org.apache.cocoon.components.source.SourceUtil;
 import org.apache.cocoon.environment.ObjectModelHelper;
 import org.apache.cocoon.environment.Request;
@@ -47,8 +47,6 @@
 import org.apache.excalibur.source.SourceException;
 import org.apache.excalibur.source.SourceValidity;
 import org.apache.excalibur.xml.xpath.XPathProcessor;
-import org.apache.xerces.parsers.AbstractSAXParser;
-import org.cyberneko.html.HTMLConfiguration;
 import org.w3c.dom.Document;
 import org.w3c.dom.NodeList;
 import org.xml.sax.InputSource;
@@ -56,14 +54,14 @@
 
 /**
  * @cocoon.sitemap.component.documentation
- * The neko html generator reads HTML from a source, converts it to XHTML
+ * The Neko HTML generator reads HTML from a source, converts it to XHTML
  * and generates SAX Events. It uses the NekoHTML library to do this.
  * 
  * @cocoon.sitemap.component.name   nekohtml
  * @cocoon.sitemap.component.label  content
  * @cocoon.sitemap.component.logger sitemap.generator.nekohtml
- * @cocoon.sitemap.component.documentation.caching
- *               Uses the last modification date of the xml document for validation
+ * @cocoon.sitemap.component.documentation.caching Yes.
+ * Uses the last modification date of the xml document for validation
  * 
  * @cocoon.sitemap.component.pooling.max  32
  *
@@ -75,7 +73,7 @@
  * @version CVS $Id$
  */
 public class NekoHTMLGenerator extends ServiceableGenerator
-implements Configurable, CacheableProcessingComponent, Disposable {
+                               implements Configurable, CacheableProcessingComponent, Disposable
{
 
     /** The parameter that specifies what request attribute to use, if any */
     public static final String FORM_NAME = "form-name";
@@ -87,25 +85,24 @@
     private InputStream requestStream;
 
     /** XPATH expression */
-    private String xpath = null;
+    private String xpath;
 
     /** XPath Processor */
-    private XPathProcessor processor = null;
+    private XPathProcessor processor;
 
     /** Neko properties */
     private Properties properties;
 
     public void service(ServiceManager manager)
     throws ServiceException {
-        super.service( manager );
-        this.processor = (XPathProcessor)this.manager.lookup(XPathProcessor.ROLE);
+        super.service(manager);
+        this.processor = (XPathProcessor) this.manager.lookup(XPathProcessor.ROLE);
     }
 
     public void configure(Configuration config) throws ConfigurationException {
 
         String configUrl = config.getChild("neko-config").getValue(null);
-
-        if(configUrl != null) {
+        if (configUrl != null) {
             org.apache.excalibur.source.SourceResolver resolver = null;
             Source configSource = null;
             try {
@@ -136,7 +133,7 @@
      */
     public void recycle() {
         if (this.inputSource != null) {
-            this.resolver.release( this.inputSource );
+            this.resolver.release(this.inputSource);
             this.inputSource = null;
             this.requestStream = null;
         }
@@ -260,12 +257,12 @@
     public void generate()
     throws IOException, SAXException, ProcessingException {
         try {
-            HtmlSaxParser parser = new HtmlSaxParser(this.properties);
+            NekoHtmlSaxParser parser = new NekoHtmlSaxParser(this.properties);
             
             if (inputSource != null)
                 requestStream = this.inputSource.getInputStream();
 
-            if(xpath != null) {
+            if (xpath != null) {
                 DOMBuilder builder = new DOMBuilder();
                 parser.setContentHandler(builder);
                 parser.parse(new InputSource(requestStream));
@@ -295,7 +292,6 @@
         }
     }
 
-
     public void dispose() {
         if (this.manager != null) {
             this.manager.release(this.processor);
@@ -305,26 +301,4 @@
         super.dispose();
     }
 
-    public static class HtmlSaxParser extends AbstractSAXParser {
-
-        public HtmlSaxParser(Properties properties) {
-            super(getConfig(properties));
-        }
-    
-        private static HTMLConfiguration getConfig(Properties properties) {
-            HTMLConfiguration config = new HTMLConfiguration();
-            config.setProperty("http://cyberneko.org/html/properties/names/elems", "lower");
-            if (properties != null) {
-                for (Iterator i = properties.keySet().iterator();i.hasNext();) {
-                    String name = (String) i.next();
-                    if (name.indexOf("/features/") > -1) {
-                        config.setFeature(name, Boolean.getBoolean(properties.getProperty(name)));
-                    } else if (name.indexOf("/properties/") > -1) {
-                        config.setProperty(name, properties.getProperty(name));
-                    }
-                }
-            }
-            return config;
-        }
-    }
 }

Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/html/java/org/apache/cocoon/transformation/NekoHTMLTransformer.java
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X/src/blocks/html/java/org/apache/cocoon/transformation/NekoHTMLTransformer.java?rev=654400&r1=654399&r2=654400&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/html/java/org/apache/cocoon/transformation/NekoHTMLTransformer.java
(original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/html/java/org/apache/cocoon/transformation/NekoHTMLTransformer.java
Wed May  7 20:19:26 2008
@@ -19,7 +19,6 @@
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.Map;
 import java.util.Properties;
 import java.util.StringTokenizer;
@@ -29,12 +28,11 @@
 import org.apache.avalon.framework.configuration.ConfigurationException;
 import org.apache.avalon.framework.parameters.Parameters;
 import org.apache.cocoon.ProcessingException;
+import org.apache.cocoon.components.NekoHtmlSaxParser;
 import org.apache.cocoon.environment.SourceResolver;
-import org.apache.cocoon.xml.dom.DOMBuilder;
 import org.apache.cocoon.xml.IncludeXMLConsumer;
+import org.apache.cocoon.xml.dom.DOMBuilder;
 import org.apache.excalibur.source.Source;
-import org.apache.xerces.parsers.AbstractSAXParser;
-import org.cyberneko.html.HTMLConfiguration;
 import org.w3c.dom.Document;
 import org.xml.sax.Attributes;
 import org.xml.sax.InputSource;
@@ -48,9 +46,8 @@
  *
  * @version $Id$
  */
-public class NekoHTMLTransformer
-    extends AbstractSAXTransformer
-    implements Configurable {
+public class NekoHTMLTransformer extends AbstractSAXTransformer
+                                 implements Configurable {
 
     /**
      * Properties for Neko format
@@ -69,7 +66,7 @@
      * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
      */
     public void endElement(String uri, String name, String raw)
-        throws SAXException {
+    throws SAXException {
         if (this.tags.containsKey(name)) {
             String toBeNormalized = this.endTextRecording();
             try {
@@ -87,14 +84,12 @@
      *
      * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String,
org.xml.sax.Attributes)
      */
-    public void startElement(
-        String uri,
-        String name,
-        String raw,
-        Attributes attr)
-        throws SAXException {
+    public void startElement(String uri,
+                             String name,
+                             String raw,
+                             Attributes attr) throws SAXException {
         super.startElement(uri, name, raw, attr);
-		if (this.tags.containsKey(name)) {
+        if (this.tags.containsKey(name)) {
             this.startTextRecording();
         }
     }
@@ -143,7 +138,7 @@
      */
     private void normalize(String text) throws ProcessingException {
         try {
-            HtmlSaxParser parser = new HtmlSaxParser(this.properties);
+            NekoHtmlSaxParser parser = new NekoHtmlSaxParser(this.properties);
 
             ByteArrayInputStream bais =
                 new ByteArrayInputStream(text.getBytes());
@@ -164,17 +159,15 @@
     /**
      * Setup this component, passing the tag names to be tidied.
      */
-
-    public void setup(
-        SourceResolver resolver,
-        Map objectModel,
-        String src,
-        Parameters par)
-        throws ProcessingException, SAXException, IOException {
+    public void setup(SourceResolver resolver,
+                      Map objectModel,
+                      String src,
+                      Parameters par)
+    throws ProcessingException, SAXException, IOException {
         super.setup(resolver, objectModel, src, par);
         String tagsParam = par.getParameter("tags", "");        
         if (getLogger().isDebugEnabled()) {
-        	getLogger().debug("tags: " + tagsParam);
+            getLogger().debug("tags: " + tagsParam);
         }        
         this.tags = new HashMap();
         StringTokenizer tokenizer = new StringTokenizer(tagsParam, ",");
@@ -183,27 +176,4 @@
             this.tags.put(tok, tok);
         }
     }
-
-    public static class HtmlSaxParser extends AbstractSAXParser {
-
-        public HtmlSaxParser(Properties properties) {
-            super(getConfig(properties));
-        }
-
-        private static HTMLConfiguration getConfig(Properties properties) {
-            HTMLConfiguration config = new HTMLConfiguration();
-            config.setProperty("http://cyberneko.org/html/properties/names/elems", "lower");
-            if (properties != null) {
-                for (Iterator i = properties.keySet().iterator();i.hasNext();) {
-                    String name = (String) i.next();
-                    if (name.indexOf("/features/") > -1) {
-                        config.setFeature(name, Boolean.getBoolean(properties.getProperty(name)));
-                    } else if (name.indexOf("/properties/") > -1) {
-                        config.setProperty(name, properties.getProperty(name));
-                    }
-                }
-            }
-            return config;
-        }
-    }
 }



Mime
View raw message