cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject svn commit: r934977 - in /cocoon/cocoon3/trunk/cocoon-sax/src: main/java/org/apache/cocoon/sax/component/ test/java/org/apache/cocoon/sax/component/
Date Fri, 16 Apr 2010 16:13:07 GMT
Author: simonetripodi
Date: Fri Apr 16 16:13:07 2010
New Revision: 934977

URL: http://svn.apache.org/viewvc?rev=934977&view=rev
Log:
no needs to have an external ToRewrite data structure

Removed:
    cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/ToRewrite.java
Modified:
    cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/LinkRewriterTransformer.java
    cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/component/LinkRewriterTransformerTest.java

Modified: cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/LinkRewriterTransformer.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/LinkRewriterTransformer.java?rev=934977&r1=934976&r2=934977&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/LinkRewriterTransformer.java
(original)
+++ cocoon/cocoon3/trunk/cocoon-sax/src/main/java/org/apache/cocoon/sax/component/LinkRewriterTransformer.java
Fri Apr 16 16:13:07 2010
@@ -18,6 +18,11 @@
  */
 package org.apache.cocoon.sax.component;
 
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
 import org.apache.cocoon.pipeline.caching.CacheKey;
 import org.apache.cocoon.pipeline.caching.SimpleCacheKey;
 import org.apache.cocoon.pipeline.component.CachingPipelineComponent;
@@ -28,17 +33,20 @@ import org.xml.sax.Attributes;
 import org.xml.sax.SAXException;
 import org.xml.sax.helpers.AttributesImpl;
 
-public class LinkRewriterTransformer extends AbstractSAXTransformer implements CachingPipelineComponent
{
+public final class LinkRewriterTransformer extends AbstractSAXTransformer implements CachingPipelineComponent
{
+
+    public static final String ALL_NAMESPACES = "*";
+
+    private static final String EMPY_NAMESPACE = "";
 
     private final Log log = LogFactory.getLog(this.getClass());
 
-    private final LinkRewriter linkRewriter;
+    private final Map<Element, Set<Element>> elements = new HashMap<Element,
Set<Element>>();
 
-    private final ToRewrite toRewrite;
+    private final LinkRewriter linkRewriter;
 
-    public LinkRewriterTransformer(LinkRewriter linkRewriter, ToRewrite toRewrite) {
+    public LinkRewriterTransformer(LinkRewriter linkRewriter) {
         this.linkRewriter = linkRewriter;
-        this.toRewrite = toRewrite;
     }
 
     @Override
@@ -67,7 +75,7 @@ public class LinkRewriterTransformer ext
                         + ")");
             }
 
-            if (this.toRewrite.contains(uri, localName, attributeNamespace, attributeName))
{
+            if (this.contains(uri, localName, attributeNamespace, attributeName)) {
                 if (this.log.isDebugEnabled()) {
                     this.log.debug("Element has attributes to rewrite");
                 }
@@ -100,6 +108,111 @@ public class LinkRewriterTransformer ext
         super.startElement(uri, localName, name, atts);
     }
 
+    public void addElement(String elementName, String attributeName) {
+        this.addElement(ALL_NAMESPACES, elementName, ALL_NAMESPACES, attributeName);
+    }
+
+    public void addElement(String elementNamespace,
+            String elementName,
+            String attributeNamespace,
+            String attributeName) {
+        if (elementName == null) {
+            throw new IllegalArgumentException("Parameter 'elementName' must not be null");
+        }
+        if (attributeName == null) {
+            throw new IllegalArgumentException("Parameter 'attributeName' must not be null");
+        }
+
+        if (elementNamespace == null) {
+            elementNamespace = EMPY_NAMESPACE;
+        }
+        Element key = new Element(elementNamespace, elementName);
+
+        Set<Element> attributes;
+        if (this.elements.containsKey(key)) {
+            attributes = this.elements.get(key);
+        } else {
+            attributes = new HashSet<Element>();
+            this.elements.put(key, attributes);
+        }
+
+        if (attributeNamespace == null) {
+            attributeNamespace = EMPY_NAMESPACE;
+        }
+        attributes.add(new Element(attributeNamespace, attributeName));
+    }
+
+    private boolean contains(String elementNamespace,
+            String elementName,
+            String atributeNamespace,
+            String attributeName) {
+        Element element = new Element(elementNamespace, elementName);
+        return this.elements.containsKey(element) &&
+                this.elements.get(element).contains(new Element(atributeNamespace, attributeName));
+    }
+
+    private final class Element {
+
+        private final String namespace;
+
+        private final String name;
+
+        public Element(String namespace, String name) {
+            this.namespace = namespace;
+            this.name = name;
+        }
+
+        public String getNamespace() {
+            return this.namespace;
+        }
+
+        public String getName() {
+            return this.name;
+        }
+
+        @Override
+        public int hashCode() {
+            final int initialNonZeroOddNumber = 97;
+            final int multiplierNonZeroOddNumber = 7;
+            return initialNonZeroOddNumber * multiplierNonZeroOddNumber + this.name.hashCode();
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (this == obj) {
+                return true;
+            }
+
+            if (obj == null || this.getClass() != obj.getClass()) {
+                return false;
+            }
+
+            Element other = (Element) obj;
+
+            if (!(ALL_NAMESPACES.equals(this.namespace)
+                    || ALL_NAMESPACES.equals(other.getNamespace()))
+                    && !this.namespace.equals(other.getNamespace())) {
+                return false;
+            }
+
+            if (!this.name.equals(other.getName())) {
+                return false;
+            }
+
+            return true;
+        }
+
+        @Override
+        public String toString() {
+            return "{ namespace='"
+                    + this.namespace
+                    + "', name='"
+                    + this.name
+                    + "' }";
+        }
+
+    }
+
     public CacheKey constructCacheKey() {
         return new SimpleCacheKey();
     }

Modified: cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/component/LinkRewriterTransformerTest.java
URL: http://svn.apache.org/viewvc/cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/component/LinkRewriterTransformerTest.java?rev=934977&r1=934976&r2=934977&view=diff
==============================================================================
--- cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/component/LinkRewriterTransformerTest.java
(original)
+++ cocoon/cocoon3/trunk/cocoon-sax/src/test/java/org/apache/cocoon/sax/component/LinkRewriterTransformerTest.java
Fri Apr 16 16:13:07 2010
@@ -75,17 +75,17 @@ public final class LinkRewriterTransform
     public void testXhtmlLinkRewriting() throws Exception {
         URL source = this.getClass().getResource("/apache_home.html");
 
-        ToRewrite toRewrite = new ToRewrite();
-        toRewrite.addElement("a", "href");
-        toRewrite.addElement("http://www.w3.org/1999/xhtml",
+        Pipeline<SAXPipelineComponent> pipeline = new NonCachingPipeline<SAXPipelineComponent>();
+        pipeline.addComponent(new XMLGenerator(source));
+
+        LinkRewriterTransformer lrt = new LinkRewriterTransformer(new AbsoluteRewriter());
+        lrt.addElement("a", "href");
+        lrt.addElement("http://www.w3.org/1999/xhtml",
                 "link",
                 null,
                 "href");
+        pipeline.addComponent(lrt);
 
-        Pipeline<SAXPipelineComponent> pipeline = new NonCachingPipeline<SAXPipelineComponent>();
-        pipeline.addComponent(new XMLGenerator(source));
-        pipeline.addComponent(new LinkRewriterTransformer(new AbsoluteRewriter(),
-                toRewrite));
         pipeline.addComponent(new XMLSerializer());
 
         ByteArrayOutputStream baos = new ByteArrayOutputStream();



Mime
View raw message