forked from chenjianjx/wsdl2html
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FreemarkerWebServiceDisplayer.java
131 lines (104 loc) · 3.75 KB
/
FreemarkerWebServiceDisplayer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package org.jaxws.stub2html.view.freemarker;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.jaxws.stub2html.model.Stub;
import org.jaxws.stub2html.model.WebServiceStubSet;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import freemarker.template.utility.DeepUnwrap;
/**
*
* @author chenjianjx
*
*/
public class FreemarkerWebServiceDisplayer {
private final Template template;
private final WebServiceStubSet serviceStubSet;
FreemarkerWebServiceDisplayer(Template template, WebServiceStubSet serviceStubSet) {
this.template = template;
this.serviceStubSet = serviceStubSet;
}
public String displayWebSerivce() {
try {
Map<String, Object> rootMap = new HashMap<String, Object>();
rootMap.put("service", serviceStubSet);
rootMap.put("stubName", new DisplayStubNameMethodModel());
rootMap.put("stubOgnl", new DisplayStubOgnlPathMethodModel());
rootMap.put("stubType", new DisplayStubTypeMethodModel());
rootMap.put("className", new DisplayClassNameMethodModel());
rootMap.put("description", new DisplayStubDescriptionMethodModel());
StringWriter out = new StringWriter();
template.process(rootMap, out);
return out.toString();
} catch (IOException e) {
throw new IllegalStateException(e);
} catch (TemplateException e) {
throw new IllegalStateException(e);
}
}
private final class DisplayStubOgnlPathMethodModel implements TemplateMethodModelEx {
@SuppressWarnings("rawtypes")
@Override
public Object exec(List arguments) throws TemplateModelException {
Stub stub = (Stub) DeepUnwrap.unwrap((TemplateModel) arguments.get(0));
String parentPath = (String) DeepUnwrap.unwrap((TemplateModel) arguments.get(1));
if (parentPath == null || StringUtils.isEmpty(parentPath)) {
return getStubNameConsideringMultiple(stub);
}
return parentPath + "." + getStubNameConsideringMultiple(stub);
}
}
private final class DisplayStubNameMethodModel implements TemplateMethodModelEx {
@SuppressWarnings("rawtypes")
@Override
public Object exec(List arguments) throws TemplateModelException {
Stub stub = (Stub) DeepUnwrap.unwrap((TemplateModel) arguments.get(0));
return getStubNameConsideringMultiple(stub);
}
}
private final class DisplayStubDescriptionMethodModel implements TemplateMethodModelEx {
@SuppressWarnings("rawtypes")
@Override
public Object exec(List arguments) throws TemplateModelException {
Stub stub = (Stub) DeepUnwrap.unwrap((TemplateModel) arguments.get(0));
return stub.getDescription();
}
}
private final class DisplayClassNameMethodModel implements TemplateMethodModelEx {
@SuppressWarnings("rawtypes")
@Override
public Object exec(List arguments) throws TemplateModelException {
Class<?> clazz = (Class<?>) DeepUnwrap.unwrap((TemplateModel) arguments.get(0));
return clazz.getSimpleName();
}
}
private final class DisplayStubTypeMethodModel implements TemplateMethodModelEx {
@SuppressWarnings("rawtypes")
@Override
public Object exec(List arguments) throws TemplateModelException {
Stub stub = (Stub) DeepUnwrap.unwrap((TemplateModel) arguments.get(0));
String typeText = stub.getType().getSimpleName();
if (stub.isMultiOccurs()) {
typeText += "[]";
}
return typeText;
}
}
private static String getStubNameConsideringMultiple(Stub stub) {
if (StringUtils.isEmpty(stub.getStubName())) {
return "";
}
if (stub.isMultiOccurs()) {
return stub.getStubName() + "[]";
} else {
return stub.getStubName();
}
}
}