View Javadoc

1   // ========================================================================
2   // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.mortbay.util.ajax;
15  
16  import java.util.Map;
17  
18  import org.mortbay.util.Loader;
19  import org.mortbay.util.ajax.JSON.Convertor;
20  import org.mortbay.util.ajax.JSON.Output;
21  
22  /**
23   * {@link JSONPojoConvertor} factory convertor.
24   * <p>
25   * This {@link Convertor} will create and register {@link JSONPojoConvertor} instances for unknown classes.
26   * 
27   */
28  public class JSONPojoConvertorFactory implements JSON.Convertor
29  {
30      private final JSON _json;
31      private final boolean _fromJSON;
32      
33      /**
34       * @param json The JSON to use for conversions and registrations
35       */
36      public JSONPojoConvertorFactory(JSON json)
37      {
38          _json=json;
39          _fromJSON=true;
40          if (json==null)
41          {
42              throw new IllegalArgumentException();
43          }
44      }
45      
46      /**
47       * @param json The JSON to use for conversions and registrations
48       * @param fromJSON Passed to {@link JSONPojoConvertor} constructor.
49       */
50      public JSONPojoConvertorFactory(JSON json,boolean fromJSON)
51      {
52          _json=json;
53          _fromJSON=fromJSON;
54          if (json==null)
55          {
56              throw new IllegalArgumentException();
57          }
58      }
59      
60      public void toJSON(Object obj, Output out)
61      {
62          String clsName=obj.getClass().getName();
63          Convertor convertor=_json.getConvertorFor(clsName);
64          if (convertor==null)
65          {
66              try
67              {
68                  Class cls=Loader.loadClass(JSON.class,clsName);
69                  convertor=new JSONPojoConvertor(cls,_fromJSON);
70                  _json.addConvertorFor(clsName, convertor);
71               }
72              catch (ClassNotFoundException e)
73              {
74                  e.printStackTrace();
75              }
76          }
77          if (convertor!=null&&obj.getClass()!=Object.class)
78          {
79              convertor.toJSON(obj, out);
80          }
81          else
82          {
83              out.add(obj.toString());
84          }
85      }
86  
87      public Object fromJSON(Map object)
88      {
89          Map map=object;
90          String clsName=(String)map.get("class");
91          if (clsName!=null)
92          {
93              Convertor convertor=_json.getConvertorFor(clsName);
94              if (convertor==null)
95              {
96                  try
97                  {
98                      Class cls=Loader.loadClass(JSON.class,clsName);
99                      convertor=new JSONPojoConvertor(cls);
100                     _json.addConvertorFor(clsName, convertor);
101                 }
102                 catch (ClassNotFoundException e)
103                 {
104                     e.printStackTrace();
105                 }
106             }
107             if (convertor!=null&&!clsName.equals(Object.class.getName()))
108             {
109                 return convertor.fromJSON(object);
110             }
111         }
112         return map;
113     }
114 }