3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.remote
.view
;
12 import java
.io
.PrintWriter
;
13 import java
.util
.Collection
;
16 import javax
.servlet
.http
.HttpServletRequest
;
17 import javax
.servlet
.http
.HttpServletResponse
;
19 import net
.sf
.json
.JSON
;
20 import net
.sf
.json
.JSONArray
;
21 import net
.sf
.json
.JSONObject
;
22 import net
.sf
.json
.JsonConfig
;
23 import net
.sf
.json
.xml
.XMLSerializer
;
25 import org
.apache
.log4j
.Logger
;
26 import org
.springframework
.web
.servlet
.View
;
29 public class JsonView
extends BaseView
implements View
{
31 public static final Logger logger
= Logger
.getLogger(JsonView
.class);
33 private JsonConfig jsonConfig
;
36 JSON("application/json"),
37 XML("application/xml");
39 private String contentType
;
41 Type(String contentType
){
42 this.contentType
= contentType
;
45 public String
getContentType(){
50 private Type type
= Type
.JSON
;
54 public void setXsl(String xsl
) {
58 public Type
getType() {
63 * Default is Type.JSON
66 public void setType(Type type
) {
70 public void setJsonConfig(JsonConfig jsonConfig
) {
71 this.jsonConfig
= jsonConfig
;
76 * @see org.springframework.web.servlet.View#getContentType()
78 public String
getContentType() {
79 return type
.getContentType();
82 public void render(Object entity
, PrintWriter writer
) throws Exception
{
86 boolean isCollectionType
= false;
89 jsonObj
= JSONObject
.fromObject("{}");
90 } else if(Collection
.class.isAssignableFrom(entity
.getClass())){
91 isCollectionType
= true;
92 jsonObj
= JSONArray
.fromObject(entity
, jsonConfig
);
93 }else if(entity
instanceof String
){
94 jsonObj
= JSONObject
.fromObject("{\"String\":\""+entity
.toString().replace("\"", "\\\"")+"\"}");
95 } else if(entity
instanceof Integer
){
96 jsonObj
= JSONObject
.fromObject("{\"Integer\":"+((Integer
)entity
).intValue()+"}");
97 } else if(entity
instanceof Boolean
){
98 jsonObj
= JSONObject
.fromObject("{\"Boolean\":"+((Boolean
)entity
).toString()+"}");
100 jsonObj
= JSONObject
.fromObject(entity
, jsonConfig
);
103 if(type
.equals(Type
.XML
)){
104 XMLSerializer xmlSerializer
= new XMLSerializer();
105 if(isCollectionType
){
106 xmlSerializer
.setArrayName(entity
.getClass().getSimpleName());
107 Class elementType
= Object
.class;
108 Collection c
= (Collection
)entity
;
110 elementType
= c
.iterator().next().getClass();
112 xmlSerializer
.setObjectName(elementType
.getSimpleName());
113 } else if(entity
!= null){
114 xmlSerializer
.setObjectName(entity
.getClass().getSimpleName());
116 String xml
= xmlSerializer
.write( jsonObj
);
118 String xslInclude
= "\r\n<?xml-stylesheet type=\"text/xsl\" href=\"human.xsl\"?>\r\n";
119 xml
= xml
.replaceFirst("\r\n", xslInclude
);
124 writer
.append(jsonObj
.toString());
126 //TODO resp.setContentType(type);
132 * @see org.springframework.web.servlet.View#render(java.util.Map, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
134 public void render(Map model
, HttpServletRequest request
, HttpServletResponse response
) throws Exception
{
136 // Retrieve data from model
137 Object entity
= getResponseData(model
);
140 response
.setContentType(type
.getContentType());
143 render(entity
, response
.getWriter());