Skip to content

Instantly share code, notes, and snippets.

@NLMartian
Created January 23, 2014 11:48
Show Gist options
  • Save NLMartian/8577291 to your computer and use it in GitHub Desktop.
Save NLMartian/8577291 to your computer and use it in GitHub Desktop.
麻痹的难道有逻辑错误?回去慢慢看。。
package ctrip.business.help;
import ctrip.business.ServiceHelper;
import ctrip.business.ServiceInfoModel;
import ctrip.business.handle.ClassModel;
import ctrip.business.handle.ClassModelCache;
import ctrip.business.handle.FieldAnnotationModel;
import ctrip.business.handle.FieldModel;
import ctrip.business.handle.annotation.SerializeField;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlSerializer;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
/**
* Created by jgzhu on 14-1-23.
*/
public class ServiceXmlGenerator {
private XmlPullParserFactory factory;
private StringWriter stringWriter;
private XmlSerializer xmlSerializer;
public ServiceXmlGenerator() {
try {
factory = XmlPullParserFactory.newInstance();
xmlSerializer = factory.newSerializer();
stringWriter = new StringWriter();
xmlSerializer.setOutput(stringWriter);
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public String getXmlStr() {
return stringWriter.toString();
}
public boolean buildDocument(String serviceCode) {
try {
stringWriter.flush();
ServiceInfoModel serviceInfo = ServiceHelper.getServiceInfo(serviceCode);
if (serviceInfo.getServiceName() == null) {
return false;
}
startRootElement(serviceInfo);
Class<?> requestType= null;
Class<?> responseType = null;
try {
requestType = Class.forName(serviceInfo.getRequestFullName());
responseType = Class.forName(serviceInfo.getResponseFullName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
addRequestElement(requestType);
addResponseElement(responseType);
endRootElement();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
private void startRootElement(ServiceInfoModel serviceInfo) {
try {
xmlSerializer.startDocument("gb2312", true);
xmlSerializer.startTag("", "Service");
xmlSerializer.attribute("", "name", serviceInfo.getServiceName());
xmlSerializer.attribute("", "code", serviceInfo.getServiceCode());
xmlSerializer.attribute("", "type", "Service");
xmlSerializer.attribute("", "description", "");
} catch (IOException e) {
e.printStackTrace();
}
}
private void endRootElement() {
try {
xmlSerializer.endTag("", "Service");
xmlSerializer.endDocument();
} catch (IOException e) {
e.printStackTrace();
}
}
private void addRequestElement(Class<?> type) {
FieldModel fieldModel = new FieldModel(type);
try {
xmlSerializer.startTag("", "Request");
xmlSerializer.attribute("", "name", type.getSimpleName());
xmlSerializer.attribute("", "type", "Request");
xmlSerializer.attribute("", "version", "0");
addRequestOrResponse(fieldModel);
xmlSerializer.endTag("", "Request");
} catch (IOException e) {
e.printStackTrace();
}
}
private void addResponseElement(Class<?> type) {
FieldModel fieldModel = new FieldModel(type);
try {
xmlSerializer.startTag("", "Response");
xmlSerializer.attribute("", "name", type.getSimpleName());
xmlSerializer.attribute("", "type", "Response");
xmlSerializer.attribute("", "version", "0");
addRequestOrResponse(fieldModel);
xmlSerializer.endTag("", "Response");
} catch (IOException e) {
e.printStackTrace();
}
}
private void addRequestOrResponse(FieldModel fieldModel) {
ClassModel classModel = ClassModelCache.getClassModel(fieldModel.getFieldClass());
List<FieldModel> fieldModelList = classModel.getFieldList();
for (FieldModel tmp : fieldModelList) {
addField(tmp);
}
}
private void addField(FieldModel fieldInfo) {
Class<?> type = fieldInfo.getFieldClass();
addElementInfo(fieldInfo);
ClassModel classModel = null;
if (type.equals(String.class)
|| type.equals(byte[].class)
|| type.equals(int.class)
|| type.equals(Integer.class)
|| type.equals(boolean.class)
|| type.equals(Boolean.class)
|| type.getSuperclass().equals(Enum.class)) {
return;
} else if (type.equals(ArrayList.class)){
classModel = ClassModelCache.getClassModel(fieldInfo.getFieldParmClass());
} else {
classModel = ClassModelCache.getClassModel(fieldInfo.getFieldClass());
}
List<FieldModel> fieldModelList = classModel.getFieldList();
for (FieldModel tmp : fieldModelList) {
addField(tmp);
}
try {
xmlSerializer.endTag("", "item");
} catch (IOException e) {
e.printStackTrace();
}
}
private void addElementInfo(FieldModel tmp) {
FieldAnnotationModel annotation = tmp.getFieldAnnotation();
Class<?> type = tmp.getFieldClass();
try {
xmlSerializer.startTag("", "item");
xmlSerializer.attribute("", "index", String.valueOf(annotation.index));
String name = Character.toUpperCase(tmp.getName().charAt(0)) + tmp.getName().substring(1);
if (type.equals(String.class)
|| type.equals(byte[].class)
|| type.equals(int.class)
|| type.equals(Integer.class)
|| type.equals(boolean.class)
|| type.equals(Boolean.class)
|| type.getSuperclass().equals(Enum.class)) {
xmlSerializer.attribute("", "name", name);
} else {
// 如果是JavaBean则去掉Model,列表则不去除
int suffixLength = name.contains("Model") ? 5 : 0;
name = name.substring(0, name.length() - suffixLength);
xmlSerializer.attribute("", "name", name);
}
if (annotation.length > 0) {
xmlSerializer.attribute("", "length", String.valueOf(annotation.length));
}
String require = String.valueOf(annotation.require);
require = Character.toUpperCase(require.charAt(0)) + require.substring(1);
xmlSerializer.attribute("", "require", require);
if (!"String".equals(annotation.serverType)) {
xmlSerializer.attribute("", "type", annotation.serverType);
}
xmlSerializer.attribute("", "metadata", String.valueOf(annotation.type));
xmlSerializer.attribute("", "description", "");
if (!"".equals(annotation.format)) {
xmlSerializer.attribute("", "format", annotation.format);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment