Author: johnh
Date: Thu Dec  9 17:41:14 2010
New Revision: 1044048

URL: http://svn.apache.org/viewvc?rev=1044048&view=rev
Log:
Augment feature.xml to support <api> tags with <exports> and <uses> tags. 
Support two subtypes of such tags: gadgets.rpc service IDs and JS symbol names.

Next up: expose this data as metadata, fill in appropriate section(s) in each 
feature.


Modified:
    
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/features/FeatureParser.java
    
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/features/FeatureParserTest.java

Modified: 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/features/FeatureParser.java
URL: 
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/features/FeatureParser.java?rev=1044048&r1=1044047&r2=1044048&view=diff
==============================================================================
--- 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/features/FeatureParser.java
 (original)
+++ 
shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/features/FeatureParser.java
 Thu Dec  9 17:41:14 2010
@@ -82,7 +82,25 @@ class FeatureParser {
                 src != null && src.length() != 0 ? null : content,
                 getAttribs(resourceChild)));
           }
-          bundles.add(new ParsedFeature.Bundle(type, getAttribs(element), 
resources));
+          List<ParsedFeature.ApiDirective> apiDirectives = 
Lists.newArrayList();
+          NodeList apiKids = element.getElementsByTagName("api");
+          for (int x = 0, y = apiKids.getLength(); x < y; ++x) {
+            Element apiChild = (Element)apiKids.item(x);
+            NodeList apiElems = apiChild.getChildNodes();
+            for (int a = 0, b = apiElems.getLength(); a < b; ++a) {
+              Node apiElemNode = apiElems.item(a);
+              if (apiElemNode.getNodeType() == Node.ELEMENT_NODE) {
+                Element apiElem = (Element)apiElemNode;
+                boolean isImport = "uses".equals(apiElem.getNodeName());
+                boolean isExport = "exports".equals(apiElem.getNodeName());
+                if (isImport || isExport) {
+                  apiDirectives.add(new ParsedFeature.ApiDirective(
+                      apiElem.getAttribute("type"), apiElem.getTextContent(), 
isImport));
+                }
+              }
+            }
+          }
+          bundles.add(new ParsedFeature.Bundle(type, getAttribs(element), 
resources, apiDirectives));
         }
       }
     }
@@ -125,15 +143,66 @@ class FeatureParser {
       return bundles;
     }
     
+    public final static class ApiDirective {
+      public enum Type {
+        JS("js"),
+        RPC("rpc");
+        
+        private final String code;
+        
+        private Type(String code) {
+          this.code = code;
+        }
+        
+        public static Type fromCode(String code) {
+          for (Type value : Type.values()) {
+            if (value.code.equals(code)) {
+              return value;
+            }
+          }
+          return null;
+        }
+      }
+      
+      private final Type type;
+      private final String value;
+      private final boolean isUses;
+      
+      public ApiDirective(String type, String value, boolean isUses) {
+        this.type = Type.fromCode(type);
+        this.value = value;
+        this.isUses = isUses;
+      }
+      
+      public Type getType() {
+        return type;
+      }
+      
+      public String getValue() {
+        return value;
+      }
+      
+      public boolean isUses() {
+        return isUses;
+      }
+      
+      public boolean isExports() {
+        return !isUses;
+      }
+    }
+    
     public final static class Bundle {
       private final String type;
       private final Map<String, String> attribs;
       private final List<Resource> resources;
+      private final List<ApiDirective> apiDirectives;
       
-      private Bundle(String type, Map<String, String> attribs, List<Resource> 
resources) {
+      private Bundle(String type, Map<String, String> attribs,
+          List<Resource> resources, List<ApiDirective> apiDirectives) {
         this.type = type;
         this.attribs = attribs;
         this.resources = resources;
+        this.apiDirectives = apiDirectives;
       }
       
       public String getType() {
@@ -147,6 +216,10 @@ class FeatureParser {
       public List<Resource> getResources() {
         return resources;
       }
+      
+      public List<ApiDirective> getApis() {
+        return apiDirectives;
+      }
     }
     
     static final class Resource {

Modified: 
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/features/FeatureParserTest.java
URL: 
http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/features/FeatureParserTest.java?rev=1044048&r1=1044047&r2=1044048&view=diff
==============================================================================
--- 
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/features/FeatureParserTest.java
 (original)
+++ 
shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/features/FeatureParserTest.java
 Thu Dec  9 17:41:14 2010
@@ -18,7 +18,9 @@
 package org.apache.shindig.gadgets.features;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 
 import org.apache.shindig.common.uri.Uri;
 import org.apache.shindig.gadgets.GadgetException;
@@ -36,7 +38,19 @@ public class FeatureParserTest {
       "  <dependency>mySecondDep</dependency>" +
       "  <gadget>" +
       "    <ignored>This tag is ignored</ignored>" +
+      "    <api>" +
+      "      <!-- This is a comment about the API -->" +
+      "      <uses type=\"js\">foo.symbol</uses>" +
+      "      <exports type=\"rpc\">rpc_service</exports>" +
+      "      More extraneous crap." +
+      "      <exports type=\"js\">bar.symbol</exports>" +
+      "    </api>" +
       "    <script src=\"http://www.apache.org/file.js\"/>" +
+      "    <api>" +
+      "      <!-- A way of adding APIs together per-bundle but not tied to a 
resource -->" +
+      "      <uses type=\"rpc\">uses_service</uses>" +
+      "      <uses type=\"js\">last.symbol</uses>" +
+      "    </api>" +
       "    <script src=\"relative/resource.js\" 
gadget_attrib=\"gadget_value\"/>" +
       "  </gadget>" +
       "  <gadget container=\"container1\">" +
@@ -47,6 +61,7 @@ public class FeatureParserTest {
       "    <script>Inlined content</script>" +
       "  </container>" +
       "  <other_type>" +
+      "    <api> <!-- No actual API decls here --> </api>" +
       "    <script src=\"http://www.apache.org/two.js\"/>" +
       "    <script src=\"//extern/unchanged.dat\" inline=\"false\"/>" +
       "  </other_type>" +
@@ -74,6 +89,22 @@ public class FeatureParserTest {
         bundle1.getResources().get(1).getSource());
     assertEquals(1, bundle1.getResources().get(1).getAttribs().size());
     assertEquals("gadget_value", 
bundle1.getResources().get(1).getAttribs().get("gadget_attrib"));
+    assertEquals(5, bundle1.getApis().size());
+    assertEquals(FeatureParser.ParsedFeature.ApiDirective.Type.JS, 
bundle1.getApis().get(0).getType());
+    assertTrue(bundle1.getApis().get(0).isUses());
+    assertEquals("foo.symbol", bundle1.getApis().get(0).getValue());
+    assertEquals(FeatureParser.ParsedFeature.ApiDirective.Type.RPC, 
bundle1.getApis().get(1).getType());
+    assertFalse(bundle1.getApis().get(1).isUses());
+    assertEquals("rpc_service", bundle1.getApis().get(1).getValue());
+    assertEquals(FeatureParser.ParsedFeature.ApiDirective.Type.JS, 
bundle1.getApis().get(2).getType());
+    assertFalse(bundle1.getApis().get(2).isUses());
+    assertEquals("bar.symbol", bundle1.getApis().get(2).getValue());
+    assertEquals(FeatureParser.ParsedFeature.ApiDirective.Type.RPC, 
bundle1.getApis().get(3).getType());
+    assertTrue(bundle1.getApis().get(3).isUses());
+    assertEquals("uses_service", bundle1.getApis().get(3).getValue());
+    assertEquals(FeatureParser.ParsedFeature.ApiDirective.Type.JS, 
bundle1.getApis().get(4).getType());
+    assertTrue(bundle1.getApis().get(4).isUses());
+    assertEquals("last.symbol", bundle1.getApis().get(4).getValue());
     
     // Second gadget bundle.
     FeatureParser.ParsedFeature.Bundle bundle2 = parsed.getBundles().get(1);
@@ -81,6 +112,7 @@ public class FeatureParserTest {
     assertEquals(1, bundle2.getAttribs().size());
     assertEquals("container1", bundle2.getAttribs().get("container"));
     assertEquals(0, bundle2.getResources().size());
+    assertEquals(0, bundle2.getApis().size());
     
     // Container bundle.
     FeatureParser.ParsedFeature.Bundle bundle3 = parsed.getBundles().get(2);
@@ -98,6 +130,7 @@ public class FeatureParserTest {
     assertNull(bundle3.getResources().get(1).getSource());
     assertEquals("Inlined content", 
bundle3.getResources().get(1).getContent());
     assertEquals(0, bundle3.getResources().get(1).getAttribs().size());
+    assertEquals(0, bundle3.getApis().size());
     
     // Other_type bundle.
     FeatureParser.ParsedFeature.Bundle bundle4 = parsed.getBundles().get(3);
@@ -111,6 +144,7 @@ public class FeatureParserTest {
     assertEquals(Uri.parse("//extern/unchanged.dat"),
         bundle4.getResources().get(1).getSource());
     assertEquals(0, bundle4.getResources().get(0).getAttribs().size());
+    assertEquals(0, bundle4.getApis().size());
   }
   
   @Test(expected=GadgetException.class)


Reply via email to