View Javadoc
1   package com.foxinmy.weixin4j.xml;
2   
3   import java.io.IOException;
4   import java.io.StringWriter;
5   import java.lang.reflect.Field;
6   import java.util.HashMap;
7   import java.util.List;
8   import java.util.Map;
9   
10  import javax.xml.stream.XMLOutputFactory;
11  import javax.xml.stream.XMLStreamException;
12  import javax.xml.stream.XMLStreamWriter;
13  
14  import com.alibaba.fastjson.JSON;
15  import com.alibaba.fastjson.JSONObject;
16  import com.alibaba.fastjson.TypeReference;
17  import com.alibaba.fastjson.annotation.JSONField;
18  import com.alibaba.fastjson.serializer.NameFilter;
19  import com.foxinmy.weixin4j.util.Consts;
20  import com.foxinmy.weixin4j.util.StringUtil;
21  
22  /**
23   * 对 后缀为_$n 的 xml节点序列化
24   * 
25   * @className ListsuffixResultSerializer
26   * @author jinyu(foxinmy@gmail.com)
27   * @date 2015年3月24日
28   * @since JDK 1.6
29   * @see
30   */
31  public class ListsuffixResultSerializer {
32  
33  	/**
34  	 * 序列化为json
35  	 * 
36  	 * @param object
37  	 * @return json
38  	 */
39  	public static JSONObject serializeToJSON(Object object) {
40  		JSONObject result = (JSONObject) JSON.toJSON(object);
41  		Map<Field, String[]> listsuffixFields = ListsuffixResultDeserializer
42  				.getListsuffixFields(object.getClass());
43  		if (!listsuffixFields.isEmpty()) {
44  			JSONField jsonField = null;
45  			Object value = null;
46  			for (Field field : listsuffixFields.keySet()) {
47  				jsonField = field.getAnnotation(JSONField.class);
48  				if (jsonField != null
49  						&& StringUtil.isNotBlank(jsonField.name())) {
50  					result.remove(jsonField.name());
51  				} else {
52  					result.remove(field.getName());
53  				}
54  				try {
55  					field.setAccessible(true);
56  					value = field.get(object);
57  				} catch (Exception e) {
58  					;//
59  				}
60  				if (value != null && value instanceof List) {
61  					result.putAll(listsuffixConvertMap((List<?>) value));
62  				}
63  			}
64  		}
65  		return result;
66  	}
67  
68  	/**
69  	 * list对象转换为map的$n形式
70  	 * 
71  	 * @param listsuffix
72  	 * @return
73  	 */
74  	public static Map<String, String> listsuffixConvertMap(List<?> listsuffix) {
75  		Map<String, String> listMap = new HashMap<String, String>();
76  		if (listsuffix != null && !listsuffix.isEmpty()) {
77  			for (int i = 0; i < listsuffix.size(); i++) {
78  				listMap.putAll(JSON.parseObject(JSON.toJSONString(
79  						listsuffix.get(i), new ListsuffixEndNameFilter(i)),
80  						new TypeReference<Map<String, String>>() {
81  						}));
82  			}
83  		}
84  		return listMap;
85  	}
86  
87  	private static class ListsuffixEndNameFilter implements NameFilter {
88  		private final int index;
89  
90  		public ListsuffixEndNameFilter(int index) {
91  			this.index = index;
92  		}
93  
94  		@Override
95  		public String process(Object object, String name, Object value) {
96  			return String.format("%s_%d", name, index);
97  		}
98  	}
99  
100 	/**
101 	 * 序列化为xml
102 	 * 
103 	 * @param object
104 	 * @return xml
105 	 */
106 	public static String serializeToXML(Object object) {
107 		JSONObject obj = serializeToJSON(object);
108 		StringWriter sw = new StringWriter();
109 		XMLStreamWriter xw = null;
110 		try {
111 			xw = XMLOutputFactory.newInstance().createXMLStreamWriter(sw);
112 			xw.writeStartDocument(Consts.UTF_8.name(), "1.0");
113 			xw.writeStartElement("xml");
114 			for (String key : obj.keySet()) {
115 				if (StringUtil.isBlank(obj.getString(key))) {
116 					continue;
117 				}
118 				xw.writeStartElement(key);
119 				xw.writeCData(obj.getString(key));
120 				xw.writeEndElement();
121 			}
122 			xw.writeEndElement();
123 			xw.writeEndDocument();
124 		} catch (XMLStreamException e) {
125 			e.printStackTrace();
126 		} finally {
127 			if (xw != null) {
128 				try {
129 					xw.close();
130 				} catch (XMLStreamException e) {
131 					;
132 				}
133 			}
134 			try {
135 				sw.close();
136 			} catch (IOException e) {
137 				;
138 			}
139 		}
140 		return sw.getBuffer().toString();
141 	}
142 }