01: /******************************************************************************
02: * JBoss, a division of Red Hat *
03: * Copyright 2006, Red Hat Middleware, LLC, and individual *
04: * contributors as indicated by the @authors tag. See the *
05: * copyright.txt in the distribution for a full listing of *
06: * individual contributors. *
07: * *
08: * This is free software; you can redistribute it and/or modify it *
09: * under the terms of the GNU Lesser General Public License as *
10: * published by the Free Software Foundation; either version 2.1 of *
11: * the License, or (at your option) any later version. *
12: * *
13: * This software is distributed in the hope that it will be useful, *
14: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16: * Lesser General Public License for more details. *
17: * *
18: * You should have received a copy of the GNU Lesser General Public *
19: * License along with this software; if not, write to the Free *
20: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
21: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
22: ******************************************************************************/package org.jboss.portal.theme.impl.render.dynamic;
23:
24: import org.jboss.portal.theme.impl.render.dynamic.json.JSONException;
25: import org.jboss.portal.theme.impl.render.dynamic.json.JSONWriter;
26: import org.jboss.portal.theme.impl.render.dynamic.response.UpdatePageLocationResponse;
27: import org.jboss.portal.theme.impl.render.dynamic.response.UpdatePageStateResponse;
28:
29: import java.io.IOException;
30: import java.io.Writer;
31: import java.util.Iterator;
32: import java.util.Map;
33:
34: /**
35: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
36: * @version $Revision: 8784 $
37: */
38: public class JSONMarshaller {
39:
40: public void write(DynaResponse response, Writer w)
41: throws IOException {
42: JSONWriter writer = new JSONWriter(w);
43:
44: try {
45: if (response instanceof UpdatePageStateResponse) {
46: UpdatePageStateResponse umr = (UpdatePageStateResponse) response;
47:
48: //
49: writer.object();
50:
51: //
52: writer.key("type").value("update_markup");
53:
54: //
55: if (umr.getViewState() != null) {
56: writer.key("view_state").value(umr.getViewState());
57: }
58:
59: //
60: writer.key("fragments").object();
61: for (Iterator i = umr.getFragments().entrySet()
62: .iterator(); i.hasNext();) {
63: Map.Entry entry = (Map.Entry) i.next();
64: String id = (String) entry.getKey();
65: String fragment = (String) entry.getValue();
66: writer.key(id).value(fragment);
67: }
68: writer.endObject();
69:
70: //
71: writer.endObject();
72: } else if (response instanceof UpdatePageLocationResponse) {
73: UpdatePageLocationResponse upr = (UpdatePageLocationResponse) response;
74: writer.object();
75: writer.key("type").value("update_page");
76: writer.key("location").value(upr.getLocation());
77: writer.endObject();
78: } else {
79: throw new IllegalArgumentException();
80: }
81: } catch (JSONException e) {
82: Throwable cause = e.getCause();
83: if (cause instanceof IOException) {
84: throw (IOException) cause;
85: } else {
86: IOException ex = new IOException();
87: ex.initCause(e);
88: throw ex;
89: }
90: }
91: }
92: }
|