You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@shindig.apache.org by et...@apache.org on 2008/09/26 05:46:25 UTC
svn commit: r699169 - in /incubator/shindig/trunk/java:
common/src/main/java/org/apache/shindig/common/util/
gadgets/src/main/java/org/apache/shindig/gadgets/
gadgets/src/main/java/org/apache/shindig/gadgets/render/
gadgets/src/test/java/org/apache/shi...
Author: etnu
Date: Thu Sep 25 20:46:24 2008
New Revision: 699169
URL: http://svn.apache.org/viewvc?rev=699169&view=rev
Log:
Added back variable subtstitution by way of a small helper class.
Added:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/VariableSubstituter.java
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/VariableSubstituterTest.java
Removed:
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/util/InputStreamConsumer.java
Modified:
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/render/Renderer.java
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/render/RendererTest.java
Added: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/VariableSubstituter.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/VariableSubstituter.java?rev=699169&view=auto
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/VariableSubstituter.java (added)
+++ incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/VariableSubstituter.java Thu Sep 25 20:46:24 2008
@@ -0,0 +1,56 @@
+/*
+ * 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;
+
+import org.apache.shindig.gadgets.spec.GadgetSpec;
+import org.apache.shindig.gadgets.spec.MessageBundle;
+
+import com.google.inject.Inject;
+
+/**
+ * Performs variable substitution on a gadget spec.
+ */
+public class VariableSubstituter {
+ private final MessageBundleFactory messageBundleFactory;
+
+ @Inject
+ public VariableSubstituter(MessageBundleFactory messageBundleFactory) {
+ this.messageBundleFactory = messageBundleFactory;
+ }
+
+ /**
+ * Substitutes all hangman variables into the gadget spec.
+ *
+ * @return A new GadgetSpec, with all fields substituted as needed.
+ */
+ public GadgetSpec substitute(GadgetContext context, GadgetSpec spec) throws GadgetException {
+ MessageBundle bundle =
+ messageBundleFactory.getBundle(spec, context.getLocale(), context.getIgnoreCache());
+ String dir = bundle.getLanguageDirection();
+
+ Substitutions substituter = new Substitutions();
+ substituter.addSubstitutions(Substitutions.Type.MESSAGE, bundle.getMessages());
+ BidiSubstituter.addSubstitutions(substituter, dir);
+ substituter.addSubstitution(Substitutions.Type.MODULE, "ID",
+ Integer.toString(context.getModuleId()));
+ UserPrefSubstituter.addSubstitutions(substituter, spec, context.getUserPrefs());
+
+ return spec.substitute(substituter);
+ }
+}
Modified: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/render/Renderer.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/render/Renderer.java?rev=699169&r1=699168&r2=699169&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/render/Renderer.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/render/Renderer.java Thu Sep 25 20:46:24 2008
@@ -24,6 +24,7 @@
import org.apache.shindig.gadgets.GadgetContext;
import org.apache.shindig.gadgets.GadgetException;
import org.apache.shindig.gadgets.GadgetSpecFactory;
+import org.apache.shindig.gadgets.VariableSubstituter;
import org.apache.shindig.gadgets.spec.GadgetSpec;
import org.apache.shindig.gadgets.spec.View;
@@ -45,14 +46,17 @@
private final HtmlRenderer renderer;
private final GadgetSpecFactory gadgetSpecFactory;
private final ContainerConfig containerConfig;
+ private final VariableSubstituter substituter;
@Inject
public Renderer(HtmlRenderer renderer,
GadgetSpecFactory gadgetSpecFactory,
- ContainerConfig containerConfig) {
+ ContainerConfig containerConfig,
+ VariableSubstituter substituter) {
this.renderer = renderer;
this.gadgetSpecFactory = gadgetSpecFactory;
this.containerConfig = containerConfig;
+ this.substituter = substituter;
}
/**
@@ -81,7 +85,11 @@
try {
GadgetSpec spec = gadgetSpecFactory.getGadgetSpec(context);
- // TODO: Variable substitution.
+
+ // We have to perform all possible substitutions here, because subsequent steps may require
+ // access to any arbitrary post-substituted field.
+ spec = substituter.substitute(context, spec);
+
View view = getView(context, spec);
if (view == null) {
@@ -129,12 +137,11 @@
JSONArray parents = containerConfig.getJsonArray(container, "gadgets.parent");
if (parents == null) {
return true;
- } else {
- // We need to check each possible parent parameter against this regex.
- for (int i = 0, j = parents.length(); i < j; ++i) {
- if (Pattern.matches(parents.getString(i), parent)) {
- return true;
- }
+ }
+ // We need to check each possible parent parameter against this regex.
+ for (int i = 0, j = parents.length(); i < j; ++i) {
+ if (Pattern.matches(parents.getString(i), parent)) {
+ return true;
}
}
} catch (JSONException e) {
@@ -158,16 +165,14 @@
JSONArray aliases = containerConfig.getJsonArray(context.getContainer(),
"gadgets.features/views/" + viewName + "/aliases");
if (aliases != null) {
- try {
- for (int i = 0, j = aliases.length(); i < j; ++i) {
- viewName = aliases.getString(i);
+ for (int i = 0, j = aliases.length(); i < j; ++i) {
+ viewName = aliases.optString(i);
+ if (viewName != null) {
view = spec.getView(viewName);
if (view != null) {
break;
}
}
- } catch (JSONException e) {
- view = null;
}
}
Added: incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/VariableSubstituterTest.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/VariableSubstituterTest.java?rev=699169&view=auto
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/VariableSubstituterTest.java (added)
+++ incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/VariableSubstituterTest.java Thu Sep 25 20:46:24 2008
@@ -0,0 +1,105 @@
+/*
+ * 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;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.shindig.gadgets.spec.GadgetSpec;
+import org.apache.shindig.gadgets.spec.LocaleSpec;
+import org.apache.shindig.gadgets.spec.MessageBundle;
+
+import com.google.common.collect.Maps;
+
+import org.junit.Test;
+
+import java.net.URI;
+import java.util.Locale;
+
+public class VariableSubstituterTest {
+ private final FakeMessageBundleFactory messageBundleFactory = new FakeMessageBundleFactory();
+ private final VariableSubstituter substituter = new VariableSubstituter(messageBundleFactory);
+
+ private GadgetSpec substitute(String xml) throws Exception {
+ return substituter.substitute(new GadgetContext(), new GadgetSpec(URI.create("#"), xml));
+ }
+
+ @Test
+ public void messageBundlesSubstituted() throws Exception {
+ String xml =
+ "<Module><ModulePrefs title=''>" +
+ " <Locale>" +
+ " <msg name='foo'>bar</msg>" +
+ " <msg name='bar'>baz</msg>" +
+ " </Locale>" +
+ "</ModulePrefs>" +
+ "<Content>__MSG_foo__ - __MSG_bar__</Content>" +
+ "</Module>";
+ GadgetSpec spec = substitute(xml);
+
+ assertEquals("bar - baz", spec.getView("default").getContent());
+ }
+
+ @Test
+ public void bidiSubstituted() throws Exception {
+ String xml = "<Module><ModulePrefs title='__BIDI_END_EDGE__ way'/><Content/></Module>";
+ GadgetSpec spec = substitute(xml);
+
+ assertEquals("right way", spec.getModulePrefs().getTitle());
+ }
+
+ @Test
+ public void moduleIdSubstituted() throws Exception {
+ String xml = "<Module><ModulePrefs title='Module is: __MODULE_ID__'/><Content/></Module>";
+ GadgetSpec spec = substitute(xml);
+
+ assertEquals("Module is: 0", spec.getModulePrefs().getTitle());
+ }
+
+ @Test
+ public void userPrefsSubstituted() throws Exception {
+ String xml = "<Module>" +
+ "<ModulePrefs title='I heart __UP_foo__'/>" +
+ "<UserPref name='foo'/>" +
+ "<Content/>" +
+ "</Module>";
+ GadgetSpec spec = new GadgetSpec(URI.create("#"), xml);
+ GadgetContext context = new GadgetContext() {
+ @Override
+ public UserPrefs getUserPrefs() {
+ return new UserPrefs(Maps.immutableMap("foo", "shindig"));
+ }
+ };
+
+ spec = substituter.substitute(context, spec);
+
+ assertEquals("I heart shindig", spec.getModulePrefs().getTitle());
+ }
+
+ private static class FakeMessageBundleFactory implements MessageBundleFactory {
+
+ public MessageBundle getBundle(GadgetSpec spec, Locale locale, boolean ignoreCache)
+ throws GadgetException {
+ LocaleSpec localeSpec = spec.getModulePrefs().getLocale(locale);
+ if (localeSpec == null) {
+ return MessageBundle.EMPTY;
+ }
+ return localeSpec.getMessageBundle();
+ }
+ }
+}
Modified: incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/render/RendererTest.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/render/RendererTest.java?rev=699169&r1=699168&r2=699169&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/render/RendererTest.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/render/RendererTest.java Thu Sep 25 20:46:24 2008
@@ -20,6 +20,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
import org.apache.shindig.common.ContainerConfig;
import org.apache.shindig.common.ContainerConfigException;
@@ -28,6 +29,7 @@
import org.apache.shindig.gadgets.GadgetContext;
import org.apache.shindig.gadgets.GadgetException;
import org.apache.shindig.gadgets.GadgetSpecFactory;
+import org.apache.shindig.gadgets.VariableSubstituter;
import org.apache.shindig.gadgets.spec.GadgetSpec;
import org.json.JSONArray;
@@ -55,13 +57,14 @@
private final FakeHtmlRenderer htmlRenderer = new FakeHtmlRenderer();
private final FakeGadgetSpecFactory gadgetSpecFactory = new FakeGadgetSpecFactory();
+ private final FakeVariableSubstituter substituter = new FakeVariableSubstituter();
private FakeContainerConfig containerConfig;
private Renderer renderer;
@Before
public void setUp() throws Exception {
containerConfig = new FakeContainerConfig();
- renderer = new Renderer(htmlRenderer, gadgetSpecFactory, containerConfig);
+ renderer = new Renderer(htmlRenderer, gadgetSpecFactory, containerConfig, substituter);
}
private GadgetContext makeContext(final String view, final Uri specUrl) {
@@ -187,6 +190,18 @@
assertNotNull("No error message provided for bad parent.", results.getErrorMessage());
}
+ @Test
+ public void substitutionsPerformedTypeHtml() throws Exception {
+ renderer.render(makeContext("html", SPEC_URL));
+ assertTrue("Substitutions not performed", substituter.wasSubstituted);
+ }
+
+ @Test
+ public void substitutionsPerformedTypeUrl() throws Exception {
+ renderer.render(makeContext("url", SPEC_URL));
+ assertTrue("Substitutions not performed", substituter.wasSubstituted);
+ }
+
private static class FakeGadgetSpecFactory implements GadgetSpecFactory {
private GadgetException exception;
public GadgetSpec getGadgetSpec(GadgetContext context) throws GadgetException {
@@ -229,4 +244,18 @@
return gadget.getCurrentView().getContent();
}
}
+
+ private static class FakeVariableSubstituter extends VariableSubstituter {
+ private boolean wasSubstituted;
+
+ public FakeVariableSubstituter() {
+ super(null);
+ }
+
+ @Override
+ public GadgetSpec substitute(GadgetContext context, GadgetSpec spec) {
+ wasSubstituted = true;
+ return spec;
+ }
+ }
}