Project

General

Profile

« Previous | Next » 

Revision 22b33a05

Added by Andreas Müller almost 2 years ago

cleanup

View differences:

cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/view/JsonView.java
126 126
        // option to skip json processing for debugging purposes, see #4925
127 127
        if(System.getProperty("SkipJSON") == null) {
128 128

  
129
        // create JSON Object
130

  
131
//        long start = System.currentTimeMillis();
132
        boolean isCollectionType = false;
133
        JSON jsonObj;
134
        if (entity == null){
135
            jsonObj = JSONObject.fromObject("{}");
136
        } else if(Collection.class.isAssignableFrom(entity.getClass())){
137
            isCollectionType = true;
138
            jsonObj = JSONArray.fromObject(entity, jsonConfig);
139
        }else if(entity instanceof String){
140
            jsonObj = JSONObject.fromObject("{\"String\":\""+entity.toString().replace("\"", "\\\"")+"\"}");
141
        } else if(entity instanceof Integer){
142
            jsonObj = JSONObject.fromObject("{\"Integer\":"+((Integer)entity).intValue()+"}");
143
        } else if(entity instanceof Boolean){
144
            jsonObj = JSONObject.fromObject("{\"Boolean\":"+((Boolean)entity).toString()+"}");
145
        } else {
146
            jsonObj = JSONObject.fromObject(entity, jsonConfig);
147
        }
148
//        System.err.println("create JSON Object " + (System.currentTimeMillis() - start));
149

  
150
        if(type.equals(Type.XML)){
151
            XMLSerializer xmlSerializer = new XMLSerializer();
152
            if(isCollectionType){
153
                xmlSerializer.setArrayName(entity.getClass().getSimpleName());
154
                Class<?> elementType = Object.class;
155
                Collection<?> c = (Collection<?>)entity;
156
                if(c.size() > 0){
157
                    elementType = c.iterator().next().getClass();
158
                }
159
                xmlSerializer.setObjectName(elementType.getSimpleName());
160
            } else if(entity != null){
161
                xmlSerializer.setObjectName(entity.getClass().getSimpleName());
129
            // create JSON Object
130

  
131
    //        long start = System.currentTimeMillis();
132
            boolean isCollectionType = false;
133
            JSON jsonObj;
134
            if (entity == null){
135
                jsonObj = JSONObject.fromObject("{}");
136
            } else if(Collection.class.isAssignableFrom(entity.getClass())){
137
                isCollectionType = true;
138
                jsonObj = JSONArray.fromObject(entity, jsonConfig);
139
            }else if(entity instanceof String){
140
                jsonObj = JSONObject.fromObject("{\"String\":\""+entity.toString().replace("\"", "\\\"")+"\"}");
141
            } else if(entity instanceof Integer){
142
                jsonObj = JSONObject.fromObject("{\"Integer\":"+((Integer)entity).intValue()+"}");
143
            } else if(entity instanceof Boolean){
144
                jsonObj = JSONObject.fromObject("{\"Boolean\":"+((Boolean)entity).toString()+"}");
145
            } else {
146
                jsonObj = JSONObject.fromObject(entity, jsonConfig);
162 147
            }
163
            String xml = xmlSerializer.write( jsonObj );
164
            if(type.equals(Type.XML) && xsl != null){
165

  
166
                if(contextPath == null){
167
                    contextPath = "";
148
    //        System.err.println("create JSON Object " + (System.currentTimeMillis() - start));
149

  
150
            if(type.equals(Type.XML)){
151
                XMLSerializer xmlSerializer = new XMLSerializer();
152
                if(isCollectionType){
153
                    xmlSerializer.setArrayName(entity.getClass().getSimpleName());
154
                    Class<?> elementType = Object.class;
155
                    Collection<?> c = (Collection<?>)entity;
156
                    if(c.size() > 0){
157
                        elementType = c.iterator().next().getClass();
158
                    }
159
                    xmlSerializer.setObjectName(elementType.getSimpleName());
160
                } else if(entity != null){
161
                    xmlSerializer.setObjectName(entity.getClass().getSimpleName());
168 162
                }
169
                String basepath = dataSourceProperties.getXslBasePath(contextPath + "/xsl");
170
                String replace = "\r\n<?xml-stylesheet type=\"text/xsl\" href=\"" + basepath + "/" + xsl + "\"?>\r\n";
171
                xml = xml.replaceFirst("\r\n", replace);
172
            }
173
            writer.append(xml);
174
        } else {
175
            // assuming json
176
            if(jsonpCallback != null){
177
               // writer.append(jsonpCallback).append("(").append(jsonObj.toString()).append(");");
163
                String xml = xmlSerializer.write( jsonObj );
164
                if(type.equals(Type.XML) && xsl != null){
165

  
166
                    if(contextPath == null){
167
                        contextPath = "";
168
                    }
169
                    String basepath = dataSourceProperties.getXslBasePath(contextPath + "/xsl");
170
                    String replace = "\r\n<?xml-stylesheet type=\"text/xsl\" href=\"" + basepath + "/" + xsl + "\"?>\r\n";
171
                    xml = xml.replaceFirst("\r\n", replace);
172
                }
173
                writer.append(xml);
178 174
            } else {
179
                writer.append(jsonObj.toString());
175
                // assuming json
176
                if(jsonpCallback != null){
177
                   // writer.append(jsonpCallback).append("(").append(jsonObj.toString()).append(");");
178
                } else {
179
                    writer.append(jsonObj.toString());
180
                }
180 181
            }
181
        }
182
        //TODO resp.setContentType(type);
182
            //TODO resp.setContentType(type);
183 183

  
184 184
        } else {
185 185
            writer.append("SkipJSON mode detected, this is for debugging only! Please contact the adminitrator.");

Also available in: Unified diff