当前位置: 代码迷 >> JavaScript >> 自定义SenchaTouch数据JSON格式处置工具
  详细解决方案

自定义SenchaTouch数据JSON格式处置工具

热度:327   发布时间:2012-11-09 10:18:47.0
自定义SenchaTouch数据JSON格式处理工具

?

package org.fanzone.util;

?

import java.beans.IntrospectionException;

import java.beans.Introspector;

import java.beans.PropertyDescriptor;

import java.io.File;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import java.util.Set;

?

/**?

?* Title: Touch<br>?

?* Description: Base Handler Utilities<br>?

?* Copyright: Copyright (c) 2011 <br>?

?* Create DateTime: Jun 2, 2011 2:40:20 PM <br>?

?* @author wangmeng

?*/

public class SenchaUtil {

?

/**

* Appending object via string.

* @param object

* @return java.lang.String

*/

public static String objectToJson(Object object) {

StringBuilder json = new StringBuilder();

if (object == null) {

json.append("\"\"");

}else if (object instanceof String ) {

json.append("\"").append((String)object).append("\"");

}else if (object instanceof Long){

json.append("\"").append((Long)object).append("\"");

}else if (object instanceof Integer){

json.append("\"").append((Integer)object).append("\"");

}else if (object instanceof Double){

json.append("\"").append((Double)object).append("\"");

}else if (object instanceof Float){

json.append("\"").append((Float)object).append("\"");

}else if (object instanceof Short){

json.append("\"").append((Short)object).append("\"");

}else if (object instanceof Character){

json.append("\"").append((Character)object).append("\"");

}else if (object instanceof Character){

json.append("\"").append((Character)object).append("\"");

}else {

json.append(beanToJson(object));

}

return json.toString();

}

?

/**

* Formating attributes.

* @param bean

* @return String

*/

public static String beanToJson(Object bean) {

StringBuilder json = new StringBuilder();

json.append("{");

PropertyDescriptor[] props = null;

try {

props = Introspector.getBeanInfo(bean.getClass(), Object.class).getPropertyDescriptors();

} catch (IntrospectionException e) {

}

if (props != null) {

for (int i = 0; i < props.length; i++) {

try {

String name = objectToJson(props[i].getName());

String value = objectToJson(props[i].getReadMethod().invoke(bean));

json.append(name);

json.append(":");

json.append(value);

json.append(",");

} catch (Exception e) {

}

}

json.setCharAt(json.length() - 1, '}');

} else {

json.append("}");

}

return json.toString();

}

/**

* Make List to JSON formated.

* @param list, class

* @return java.lang.String

*/

public static String listToJson(List<?> list, Class<?> c) {

StringBuilder json = new StringBuilder();

json.append("{"+'"'+c.getSimpleName()+'"'+":[");

if((list != null) && (list.size() > 0)) {

for(Object obj : list){

json.append(objectToJson(obj));

json.append(",");

}

json.setCharAt(json.length()-1, ']');

}else {

json.append("]");

}

json.append("}");

return json.toString();

}

/**

* Make Set to JSON formated.

* @param list

* @return java.lang.String

*/

public static String setToJson(Set<?> set, Class<?> c){

StringBuilder json = new StringBuilder();

Iterator<?> itr = set.iterator();

if(itr.hasNext()){

json.append("{"+'"'+c.getSimpleName()+'"'+":[");

}

if(set != null && set.size() > 0){

for(Object obj : set){

json.append(objectToJson(obj));

json.append(",");

}

json.setCharAt(json.length()-1, ']');

}else{

json.append("]");

}

json.append("}");

return json.toString();

}

/**

* To build model in dynamic.

* @param className

* @return model string

*/

public static StringBuilder buildModel(String className){

StringBuilder builder = new StringBuilder();

Class<?> c;

PropertyDescriptor[] props = null;

try {

c = Class.forName(className);

builder.append("Ext.regModel('"+c.getSimpleName()+"', {");

builder.append("fields: [");

props = Introspector.getBeanInfo(c, Object.class).getPropertyDescriptors();

if (props != null) {

for (int i = 0; i < props.length; i++) {

String name = props[i].getName();

String type = props[i].getReadMethod().toString();

type = type.substring(type.indexOf(" ")+1, type.lastIndexOf(" "));

type = type.substring(type.lastIndexOf(".")+1,type.length());

builder.append("{name: '"+name+"', type: '"+type.toLowerCase()+"'},");

}

builder.replace(builder.length()-1, builder.length(), "");

}

builder.append("]");

builder.append("});");

} catch (ClassNotFoundException e1) {

e1.printStackTrace();

} catch (IntrospectionException e) {

e.printStackTrace();

} catch (IllegalArgumentException e) {

e.printStackTrace();

}

return builder;

}

?

/**

* To fetch all the entities under the specified package.

* @param pack

* @return entity list

*/

public static List<String> entities(String pack){

List<String> entities = new ArrayList<String>();

String classes = System.getProperty("java.class.path");

String dir = classes.substring(0,classes.indexOf(";"));

String path = dir + '\\' + pack.replace('.', '\\');

File file = new File(path);

if(file.exists()){

if(file.isDirectory()){

File[] files = file.listFiles();

for(File f : files){

entities.add(pack+'.'+f.getName().substring(0,f.getName().indexOf(".")));

}

}

}

return entities;

}

/**

* Build models with the specified package.

* @param pack

* @return all models string.

*/

public static StringBuilder buildModels(String pack){

StringBuilder builder = new StringBuilder();

for(String entity : entities(pack)){

builder.append(buildModel(entity));

builder.append("\r\n");

}

return builder;

}

/**

* Test Main

* @param args

*/

public static void main(String[] args) {

System.out.println(buildModels("org.mars.entity"));

}

}


  相关解决方案