Author: johnh
Date: Wed May 27 00:08:58 2009
New Revision: 778932

URL: http://svn.apache.org/viewvc?rev=778932&view=rev
Log:
Add needsUserPrefSubstitution field to ModulePrefs. Useful for determining, 
downstream, whether up_foo values should be appended to rendering URLs in the 
query string or fragment.


Modified:
    
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/ModulePrefs.java
    
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/ModulePrefsTest.java

Modified: 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/ModulePrefs.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/ModulePrefs.java?rev=778932&r1=778931&r2=778932&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/ModulePrefs.java
 (original)
+++ 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/spec/ModulePrefs.java
 Wed May 27 00:08:58 2009
@@ -68,9 +68,11 @@
   private static final String ATTR_CATEGORY = "category";
   private static final String ATTR_CATEGORY2 = "category2";
   private static final Uri EMPTY_URI = Uri.parse("");
+  private static final String UP_SUBST_PREFIX = "__UP_";
 
   private final Map<String, String> attributes;
   private final Uri base;
+  private final boolean needsUserPrefSubstitution;
 
   public ModulePrefs(Element element, Uri base) throws SpecParserException {
     this.base = base;
@@ -114,6 +116,7 @@
     locales = Collections.unmodifiableMap(localeVisitor.localeMap);
     links = Collections.unmodifiableMap(linkVisitor.linkMap);
     oauth = oauthVisitor.oauthSpec;
+    needsUserPrefSubstitution = prefsNeedsUserPrefSubstitution(this);
   }
 
   /**
@@ -151,6 +154,7 @@
       attributes.put(attr.getKey(), substituted);
     }
     this.attributes = attributes.build();
+    this.needsUserPrefSubstitution = prefs.needsUserPrefSubstitution;
   }
 
   // Canonical spec items first.
@@ -454,6 +458,14 @@
   public OAuthSpec getOAuthSpec() {
     return oauth;
   }
+  
+  /**
+   * Not part of the spec. Indicates whether UserPref-substitutable
+   * fields in this prefs require __UP_ substitution.
+   */
+  public boolean needsUserPrefSubstitution() {
+    return needsUserPrefSubstitution;
+  }
 
   /**
    * Attempts to retrieve a valid LocaleSpec for the given Locale.
@@ -532,6 +544,21 @@
     buf.append("</ModulePrefs>");
     return buf.toString();
   }
+  
+  /**
+   * @param prefs ModulePrefs object
+   * @return true if any UserPref-substitutable fields in the given
+   * {...@code prefs} require such substitution.
+   */
+  static boolean prefsNeedsUserPrefSubstitution(ModulePrefs prefs) {
+    for (Preload preload : prefs.preloads) {
+      if (preload.getHref().toString().contains(UP_SUBST_PREFIX)) {
+        return true;
+      }
+    }
+    return prefs.getTitle().contains(UP_SUBST_PREFIX) ||
+           prefs.getTitleUrl().toString().contains(UP_SUBST_PREFIX);
+  }
 
   interface ElementVisitor {
     void visit(Element element) throws SpecParserException;

Modified: 
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/ModulePrefsTest.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/ModulePrefsTest.java?rev=778932&r1=778931&r2=778932&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/ModulePrefsTest.java
 (original)
+++ 
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/spec/ModulePrefsTest.java
 Wed May 27 00:08:58 2009
@@ -219,4 +219,26 @@
     ModulePrefs prefs = new ModulePrefs(XmlUtil.parse(FULL_XML), SPEC_URL);
     doAsserts(new ModulePrefs(XmlUtil.parse(prefs.toString()), SPEC_URL));
   }
+  
+  @Test
+  public void needsUserPrefSubstInTitle() throws Exception {
+    String xml = "<ModulePrefs title='Title __UP_foo__'/>";
+    ModulePrefs prefs = new ModulePrefs(XmlUtil.parse(xml), SPEC_URL);
+    assertTrue(prefs.needsUserPrefSubstitution());
+  }
+  
+  @Test
+  public void needsUserPrefSubstInTitleUrl() throws Exception {
+    String xml = "<ModulePrefs title='foo' title_url='http://__UP_url__'/>";
+    ModulePrefs prefs = new ModulePrefs(XmlUtil.parse(xml), SPEC_URL);
+    assertTrue(prefs.needsUserPrefSubstitution());
+  }
+  
+  @Test
+  public void needsUserPrefSubstInPreload() throws Exception {
+    String xml = "<ModulePrefs title='foo'>" +
+        "  <Preload href='__UP_foo__' authz='signed'/></ModulePrefs>";
+    ModulePrefs prefs = new ModulePrefs(XmlUtil.parse(xml), SPEC_URL);
+    assertTrue(prefs.needsUserPrefSubstitution());
+  }
 }


Reply via email to