public void addSubstitutions(Substitutions substituter, GadgetContext context, GadgetSpec spec) { UserPrefs values = context.getUserPrefs(); for (UserPref pref : spec.getUserPrefs().values()) { String name = pref.getName(); String value = values.getPref(name); if (value == null) { value = pref.getDefaultValue(); if (value == null) { value = ""; } } substituter.addSubstitution(Substitutions.Type.USER_PREF, name, StringEscapeUtils .escapeHtml(value)); } } }
public void addSubstitutions(Substitutions substituter, GadgetContext context, GadgetSpec spec) { UserPrefs values = context.getUserPrefs(); for (UserPref pref : spec.getUserPrefs().values()) { String name = pref.getName(); String value = values.getPref(name); if (value == null) { value = pref.getDefaultValue(); if (value == null) { value = ""; } } substituter.addSubstitution(Substitutions.Type.USER_PREF, name, StringEscapeUtils .escapeHtml4(value)); } } }
public void addSubstitutions(Substitutions substituter, GadgetContext context, GadgetSpec spec) { UserPrefs values = context.getUserPrefs(); for (UserPref pref : spec.getUserPrefs().values()) { String name = pref.getName(); String value = values.getPref(name); if (value == null) { value = pref.getDefaultValue(); if (value == null) { value = ""; } } substituter.addSubstitution(Substitutions.Type.USER_PREF, name, StringEscapeUtils .escapeHtml(value)); } } }
protected void addAllUserPrefs(UriBuilder uri, Gadget gadget, View view, boolean useTpl) { GadgetContext context = gadget.getContext(); UserPrefs prefs = context.getUserPrefs(); for (UserPref up : gadget.getSpec().getUserPrefs().values()) { String name = up.getName(); String data = prefs.getPref(name); if (data == null) { data = up.getDefaultValue(); } boolean upInFragment = !view.needsUserPrefSubstitution(); addParam(uri, UriCommon.USER_PREF_PREFIX + up.getName(), data, useTpl, upInFragment); } }
@Test public void testCorrectExtraction() throws Exception { JSONObject gadget = new JSONObject() .put("url", SPEC_URL) .put("moduleId", SPEC_ID) .put("prefs", prefs) .put("gadget-field", "gadget-value"); JSONObject context = new JSONObject() .put("language", Locale.US.getLanguage()) .put("country", Locale.US.getCountry().toUpperCase()) .put("context-field", "context-value"); GadgetContext jsonContext = new JsonRpcGadgetContext(context, gadget); assertEquals(SPEC_URL, jsonContext.getUrl().toString()); assertEquals(SPEC_ID, jsonContext.getModuleId()); assertEquals(Locale.US.getLanguage(), jsonContext.getLocale().getLanguage()); assertEquals(Locale.US.getCountry(), jsonContext.getLocale().getCountry()); for (String key : PREF_KEYS) { String value = jsonContext.getUserPrefs().getPref(key); assertEquals(prefs.get(key), value); } assertEquals("gadget-value", jsonContext.getParameter("gadget-field")); assertEquals("context-value", jsonContext.getParameter("context-field")); } }
@Test public void testCorrectExtraction() throws Exception { JSONObject gadget = new JSONObject() .put("url", SPEC_URL) .put("moduleId", SPEC_ID) .put("prefs", prefs) .put("gadget-field", "gadget-value"); JSONObject context = new JSONObject() .put("language", Locale.US.getLanguage()) .put("country", Locale.US.getCountry().toUpperCase()) .put("context-field", "context-value"); GadgetContext jsonContext = new JsonRpcGadgetContext(context, gadget); assertEquals(SPEC_URL, jsonContext.getUrl().toString()); assertEquals(SPEC_ID, jsonContext.getModuleId()); assertEquals(Locale.US.getLanguage(), jsonContext.getLocale().getLanguage()); assertEquals(Locale.US.getCountry(), jsonContext.getLocale().getCountry()); for (String key : PREF_KEYS) { String value = jsonContext.getUserPrefs().getPref(key); assertEquals(prefs.get(key), value); } assertEquals("gadget-value", jsonContext.getParameter("gadget-field")); assertEquals("context-value", jsonContext.getParameter("context-field")); } }
@Test public void testCorrectExtraction() throws Exception { JSONObject gadget = new JSONObject() .put("url", SPEC_URL) .put("moduleId", SPEC_ID) .put("prefs", prefs) .put("gadget-field", "gadget-value"); JSONObject context = new JSONObject() .put("language", Locale.US.getLanguage()) .put("country", Locale.US.getCountry().toUpperCase()) .put("context-field", "context-value"); GadgetContext jsonContext = new JsonRpcGadgetContext(context, gadget); assertEquals(SPEC_URL, jsonContext.getUrl().toString()); assertEquals(SPEC_ID, jsonContext.getModuleId()); assertEquals(Locale.US.getLanguage(), jsonContext.getLocale().getLanguage()); assertEquals(Locale.US.getCountry(), jsonContext.getLocale().getCountry()); for (String key : PREF_KEYS) { String value = jsonContext.getUserPrefs().getPref(key); assertEquals(prefs.get(key), value); } assertEquals("gadget-value", jsonContext.getParameter("gadget-field")); assertEquals("context-value", jsonContext.getParameter("context-field")); } }
for (UserPref up : gadget.getSpec().getUserPrefs().values()) { String name = up.getName(); String data = prefs.getPref(name); if (data == null) { data = up.getDefaultValue();
for (UserPref up : gadget.getSpec().getUserPrefs().values()) { String name = up.getName(); String data = prefs.getPref(name); if (data == null) { data = up.getDefaultValue();