You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shindig.apache.org by zh...@apache.org on 2010/06/17 22:04:48 UTC

svn commit: r955719 - in /shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite: AbsolutePathReferenceRewriter.java AbsolutePathReferenceVisitor.java

Author: zhoresh
Date: Thu Jun 17 20:04:48 2010
New Revision: 955719

URL: http://svn.apache.org/viewvc?rev=955719&view=rev
Log:
Refactoring AbsolutePathReferenceVisitor
http://codereview.appspot.com/1699041/show

Added:
    shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/AbsolutePathReferenceRewriter.java
Modified:
    shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/AbsolutePathReferenceVisitor.java

Added: shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/AbsolutePathReferenceRewriter.java
URL: http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/AbsolutePathReferenceRewriter.java?rev=955719&view=auto
==============================================================================
--- shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/AbsolutePathReferenceRewriter.java (added)
+++ shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/AbsolutePathReferenceRewriter.java Thu Jun 17 20:04:48 2010
@@ -0,0 +1,37 @@
+/*
+ * 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.shindig.gadgets.rewrite;
+
+import com.google.inject.Inject;
+
+import org.apache.shindig.common.uri.Uri;
+import org.apache.shindig.gadgets.Gadget;
+
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * Rewriter that converts all url's to absolute.
+ */
+public class AbsolutePathReferenceRewriter extends DomWalker.Rewriter {
+  @Inject
+  public AbsolutePathReferenceRewriter() {
+    super(new AbsolutePathReferenceVisitor());
+  }
+}

Modified: shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/AbsolutePathReferenceVisitor.java
URL: http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/AbsolutePathReferenceVisitor.java?rev=955719&r1=955718&r2=955719&view=diff
==============================================================================
--- shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/AbsolutePathReferenceVisitor.java (original)
+++ shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/AbsolutePathReferenceVisitor.java Thu Jun 17 20:04:48 2010
@@ -18,7 +18,7 @@
  */
 package org.apache.shindig.gadgets.rewrite;
 
-import java.util.Map;
+import com.google.common.collect.ImmutableMap;
 
 import org.apache.commons.lang.StringUtils;
 import org.apache.shindig.common.uri.Uri;
@@ -27,9 +27,8 @@ import org.apache.shindig.gadgets.rewrit
 import org.w3c.dom.Attr;
 import org.w3c.dom.Node;
 
-import com.google.common.collect.ImmutableMap;
-
 import java.util.List;
+import java.util.Map;
 
 public class AbsolutePathReferenceVisitor implements Visitor {
   public final static Map<String, String> RESOURCE_TAGS =
@@ -46,22 +45,18 @@ public class AbsolutePathReferenceVisito
         .put("object", "src").build();
 
   public VisitStatus visit(Gadget gadget, Node node) throws RewritingException {
-    String nodeName = node.getNodeName().toLowerCase();
-    if (node.getNodeType() == Node.ELEMENT_NODE &&
-        RESOURCE_TAGS.containsKey(nodeName)) {
-      Attr attr = (Attr)node.getAttributes().getNamedItem(RESOURCE_TAGS.get(nodeName));
-      String nodeUri = attr != null ? attr.getValue() : null;
-      if (!StringUtils.isEmpty(nodeUri)) {
-        try {
-          Uri prevUri = Uri.parse(nodeUri);
-          Uri resolved = gadget.getSpec().getUrl().resolve(prevUri);
-          if (!resolved.equals(prevUri)) {
-            attr.setValue(resolved.toString());
-            return VisitStatus.MODIFY;
-          }
-        } catch (Uri.UriException e) {
-          // UriException on illegal input. Ignore.
+    Attr nodeAttr = getUriAttributeFromNode(node, RESOURCE_TAGS);
+
+    if (nodeAttr != null) {
+      try {
+        Uri prevUri = Uri.parse(nodeAttr.getValue());
+        Uri resolved = gadget.getSpec().getUrl().resolve(prevUri);
+        if (!resolved.equals(prevUri)) {
+          nodeAttr.setValue(resolved.toString());
+          return VisitStatus.MODIFY;
         }
+      } catch (Uri.UriException e) {
+        // UriException on illegal input. Ignore.
       }
     }
     return VisitStatus.BYPASS;
@@ -72,4 +67,27 @@ public class AbsolutePathReferenceVisito
     return false;
   }
 
+  /**
+   * Returns the uri attribute for the given node by looking up the
+   * tag name -> uri attribute map.
+   * NOTE: This function returns the node attribute only if the attribute has a
+   * non empty value.
+   * @param node The node to get uri attribute for.
+   * @param resourceTags Map from tag name -> uri attribute name.
+   * @return Uri attribute for the node.
+   */
+  public static Attr getUriAttributeFromNode(Node node, Map<String, String> resourceTags) {
+    String nodeName = node.getNodeName().toLowerCase();
+    if (node.getNodeType() == Node.ELEMENT_NODE &&
+        resourceTags.containsKey(nodeName)) {
+      Attr attr = (Attr) node.getAttributes().getNamedItem(
+          resourceTags.get(nodeName));
+      String nodeUri = attr != null ? attr.getValue() : null;
+      if (!StringUtils.isEmpty(nodeUri)) {
+        return attr;
+      }
+    }
+
+    return null;
+  }
 }