Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added long to int conversion for soy models

  • Loading branch information...
commit 5f818b04b749519a9780855b6e6884ce953823eb 1 parent f83a346
@michelegonella authored
View
41 zen-jsweb/src/main/java/com/nominanuda/springsoy/SoyViewResolver.java
@@ -15,8 +15,11 @@
*/
package com.nominanuda.springsoy;
+import java.util.LinkedList;
+import java.util.List;
import java.util.Locale;
import java.util.Map;
+import java.util.Map.Entry;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -25,6 +28,7 @@
import org.springframework.web.servlet.ViewResolver;
import com.google.template.soy.tofu.SoyTofu;
+import com.nominanuda.springsoy.SoyViewResolver.SoyView.LongToInt;
import com.nominanuda.web.http.HttpProtocol;
public class SoyViewResolver implements ViewResolver {
@@ -42,6 +46,7 @@ public void setSoySource(SoySource soySource) {
public static class SoyView implements View {
private final SoyTofu tofu;
private final String name;
+ private static final LongToInt longToInt = new LongToInt();
public SoyView(String name, SoyTofu tofu) {
this.name = name;
@@ -55,13 +60,45 @@ public String getContentType() {
public void render(Map<String, ?> model, HttpServletRequest request,
HttpServletResponse response) throws Exception {
byte[] b = tofu.newRenderer(name)
- .setData(model)
+ .setData(longToInt.longToInt((Map<String, Object>)model))
.render()
.getBytes(HttpProtocol.CS_UTF_8);
response.setContentType(getContentType());
response.setContentLength(b.length);
response.getOutputStream().write(b);
}
-
+ static class LongToInt {
+ public Map<String, ?> longToInt(Map<String, Object> m) {
+ for(Entry<String, Object> e : m.entrySet()) {
+ Object o = e.getValue();
+ e.setValue(o);
+ if(o != null) {
+ if(o instanceof Long) {
+ e.setValue(((Long) o).intValue());
+ } else if(o instanceof Map) {
+ longToInt((Map<String, Object>)o);
+ } else if(o instanceof List) {
+ longToInt((List<Object>)o);
+ }
+ }
+ }
+ return m;
+ }
+ public void longToInt(List<Object> l) {
+ int len = l.size();
+ for(int i = 0; i < len; i++) {
+ Object o = l.get(i);
+ if(o != null) {
+ if(o instanceof Long) {
+ l.set(i, ((Long)o).intValue());
+ } else if(o instanceof Map) {
+ longToInt((Map<String, Object>)o);
+ } else if(o instanceof List) {
+ longToInt((List<Object>)o);
+ }
+ }
+ }
+ }
+ }
}
}
View
48 zen-jsweb/src/test/java/com/nominanuda/springsoy/LongToIntTest.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2008-2011 the original author or authors.
+ *
+ * Licensed 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 com.nominanuda.springsoy;
+
+import static org.junit.Assert.*;
+
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+import org.junit.Test;
+
+import com.nominanuda.springsoy.SoyViewResolver.SoyView.LongToInt;
+
+public class LongToIntTest {
+
+ @Test
+ public void test() {
+ LongToInt l2i = new LongToInt();
+ HashMap<String, Object> m1 = new HashMap<String, Object>();
+ m1.put("a1", 1L);
+ HashMap<String, Object> m2 = new HashMap<String, Object>();
+ m2.put("a2", 1L);
+ LinkedList<Object> l = new LinkedList<Object>();
+ l.add(m2);
+ l.add(1L);
+ m1.put("l", l);
+ l2i.longToInt(m1);
+ assertEquals(new Integer(1), m1.get("a1"));
+ assertEquals(new Integer(1), ((List)m1.get("l")).get(1));
+ assertEquals(new Integer(1), ((Map)((List)m1.get("l")).get(0)).get("a2"));
+ }
+
+}
View
15 zen-jsweb/src/test/java/com/nominanuda/springsoy/SoySourceTest.java
@@ -1,3 +1,18 @@
+/*
+ * Copyright 2008-2011 the original author or authors.
+ *
+ * Licensed 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 com.nominanuda.springsoy;
import java.util.Collections;
Please sign in to comment.
Something went wrong with that request. Please try again.