|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| package net.sf.sojo.interchange.xmlrpc; |
|
17 |
| |
|
18 |
| import java.math.BigDecimal; |
|
19 |
| import java.math.BigInteger; |
|
20 |
| import java.text.SimpleDateFormat; |
|
21 |
| import java.util.Calendar; |
|
22 |
| import java.util.Date; |
|
23 |
| import java.util.Stack; |
|
24 |
| |
|
25 |
| import net.sf.sojo.common.WalkerInterceptor; |
|
26 |
| import net.sf.sojo.core.Constants; |
|
27 |
| |
|
28 |
| public class XmlRpcWalkerInterceptor implements WalkerInterceptor { |
|
29 |
| |
|
30 |
| private StringBuffer xmlRpcString = new StringBuffer(); |
|
31 |
| private Object rootObjectArray = null; |
|
32 |
| private Stack stack = new Stack(); |
|
33 |
| |
|
34 |
57
| public String getXmlRpcString() {
|
|
35 |
57
| return xmlRpcString.toString();
|
|
36 |
| } |
|
37 |
| |
|
38 |
54
| public void startWalk(Object pvStartObject) {
|
|
39 |
54
| xmlRpcString = new StringBuffer("<params>");
|
|
40 |
54
| if (pvStartObject != null && pvStartObject.getClass().isArray()) {
|
|
41 |
9
| rootObjectArray = pvStartObject;
|
|
42 |
| } |
|
43 |
| } |
|
44 |
| |
|
45 |
53
| public void endWalk() {
|
|
46 |
53
| xmlRpcString.append("</params>");
|
|
47 |
53
| stack.clear();
|
|
48 |
| } |
|
49 |
| |
|
50 |
1234
| private void createParamTag (String pvPath, String pvParam) {
|
|
51 |
1234
| if ((pvPath.length() == 0
|
|
52 |
| || (pvPath.startsWith("[") && pvPath.indexOf(".") < 0) |
|
53 |
| || (pvPath.startsWith("[") && pvPath.indexOf(".") == (pvPath.length() - 1)) |
|
54 |
| ) |
|
55 |
| ) { |
|
56 |
| |
|
57 |
123
| xmlRpcString.append(pvParam);
|
|
58 |
| } |
|
59 |
| } |
|
60 |
| |
|
61 |
494
| private void createMemberTag (Object pvKey) {
|
|
62 |
494
| if (pvKey.getClass().equals(String.class)) {
|
|
63 |
493
| xmlRpcString.append("<member><name>").append(pvKey).append("</name>");
|
|
64 |
| } else { |
|
65 |
1
| throw new XmlRpcException("Key must be from type String and not: " + pvKey.getClass());
|
|
66 |
| } |
|
67 |
| } |
|
68 |
| |
|
69 |
627
| public boolean visitElement(Object pvKey, int pvIndex, Object pvValue, int pvType, String pvPath, int pvNumberOfRecursion) {
|
|
70 |
627
| if (pvType == Constants.TYPE_NULL) {
|
|
71 |
1
| createParamTag(pvPath, "<param>");
|
|
72 |
1
| mapping2XmlRpcDataType(pvValue);
|
|
73 |
1
| createParamTag(pvPath, "</param>");
|
|
74 |
| } |
|
75 |
626
| else if (pvType == Constants.TYPE_SIMPLE) {
|
|
76 |
510
| createParamTag(pvPath, "<param>");
|
|
77 |
474
| if (pvKey != null) { createMemberTag(pvKey); }
|
|
78 |
509
| mapping2XmlRpcDataType(pvValue);
|
|
79 |
473
| if (pvKey != null) { xmlRpcString.append("</member>"); }
|
|
80 |
509
| createParamTag(pvPath, "</param>");
|
|
81 |
| } |
|
82 |
116
| else if (pvKey != null) {
|
|
83 |
20
| createMemberTag(pvKey);
|
|
84 |
20
| stack.push(new Integer(pvType));
|
|
85 |
| } |
|
86 |
626
| return false;
|
|
87 |
| } |
|
88 |
| |
|
89 |
234
| public void visitIterateableElement(Object pvValue, int pvType, String pvPath, int pvTypeBeginOrEnd) {
|
|
90 |
234
| if (pvTypeBeginOrEnd == Constants.ITERATOR_BEGIN) {
|
|
91 |
116
| if (pvType == Constants.TYPE_ITERATEABLE && pvValue.equals(rootObjectArray) == false) {
|
|
92 |
15
| createParamTag(pvPath, "<param>");
|
|
93 |
15
| xmlRpcString.append("<value><array><data>");
|
|
94 |
101
| } else if (pvType == Constants.TYPE_MAP) {
|
|
95 |
92
| createParamTag(pvPath, "<param>");
|
|
96 |
92
| xmlRpcString.append("<value><struct>");
|
|
97 |
| } |
|
98 |
| } |
|
99 |
| |
|
100 |
| |
|
101 |
118
| else if (pvTypeBeginOrEnd == Constants.ITERATOR_END) {
|
|
102 |
116
| if (pvType == Constants.TYPE_ITERATEABLE) {
|
|
103 |
24
| if (pvValue.equals(rootObjectArray) == false) {
|
|
104 |
15
| xmlRpcString.append("</data></array></value>");
|
|
105 |
| } |
|
106 |
24
| if (stack.size() > 0) {
|
|
107 |
12
| Integer i = (Integer) stack.peek();
|
|
108 |
12
| if (i.intValue() == Constants.TYPE_ITERATEABLE) {
|
|
109 |
12
| stack.pop();
|
|
110 |
12
| xmlRpcString.append("</member>");
|
|
111 |
| } |
|
112 |
| } |
|
113 |
24
| if (pvValue.equals(rootObjectArray) == false) {
|
|
114 |
15
| createParamTag(pvPath, "</param>");
|
|
115 |
| } |
|
116 |
| |
|
117 |
| } |
|
118 |
92
| else if (pvType == Constants.TYPE_MAP) {
|
|
119 |
91
| xmlRpcString.append("</struct></value>");
|
|
120 |
91
| if (stack.size() > 0) {
|
|
121 |
61
| Integer i = (Integer) stack.peek();
|
|
122 |
61
| if (i.intValue() == Constants.TYPE_MAP) {
|
|
123 |
8
| stack.pop();
|
|
124 |
8
| xmlRpcString.append("</member>");
|
|
125 |
| } |
|
126 |
| } |
|
127 |
91
| createParamTag(pvPath, "</param>");
|
|
128 |
| } |
|
129 |
| } |
|
130 |
| } |
|
131 |
| |
|
132 |
510
| public void mapping2XmlRpcDataType(Object pvValue) {
|
|
133 |
510
| xmlRpcString.append("<value>");
|
|
134 |
510
| if (pvValue == null) {
|
|
135 |
1
| xmlRpcString.append("<ex:nil>null</ex:nil>");
|
|
136 |
| } |
|
137 |
509
| else if (pvValue.getClass().equals(String.class)) {
|
|
138 |
368
| String s = pvValue.toString();
|
|
139 |
368
| s = s.replaceAll("<", "<");
|
|
140 |
368
| s = s.replaceAll(">", ">");
|
|
141 |
368
| xmlRpcString.append("<string>").append(s).append("</string>");
|
|
142 |
| |
|
143 |
| } |
|
144 |
141
| else if (pvValue.getClass().equals(Integer.class)) {
|
|
145 |
54
| xmlRpcString.append("<i4>").append(pvValue).append("</i4>");
|
|
146 |
| } |
|
147 |
87
| else if (pvValue.getClass().equals(Boolean.class)) {
|
|
148 |
60
| Boolean lvBoolean = Boolean.valueOf(pvValue.toString());
|
|
149 |
60
| int b = (lvBoolean.equals(Boolean.TRUE) ? 1 : 0);
|
|
150 |
60
| xmlRpcString.append("<boolean>").append(b).append("</boolean>");
|
|
151 |
| } |
|
152 |
27
| else if (pvValue.getClass().equals(Double.class)) {
|
|
153 |
10
| xmlRpcString.append("<double>").append(pvValue).append("</double>");
|
|
154 |
| } |
|
155 |
17
| else if (pvValue instanceof Date) {
|
|
156 |
8
| Date d = (Date) pvValue;
|
|
157 |
8
| SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMDD'T'HH:mm:ss");
|
|
158 |
8
| String s = sdf.format(d);
|
|
159 |
8
| xmlRpcString.append("<dateTime.iso8601>").append(s).append("</dateTime.iso8601>");
|
|
160 |
| } |
|
161 |
| |
|
162 |
| |
|
163 |
9
| else if (pvValue.getClass().equals(Byte.class)) {
|
|
164 |
1
| xmlRpcString.append("<ex:i1>").append(pvValue).append("</ex:i1>");
|
|
165 |
| } |
|
166 |
8
| else if (pvValue.getClass().equals(Short.class)) {
|
|
167 |
1
| xmlRpcString.append("<ex:i2>").append(pvValue).append("</ex:i2>");
|
|
168 |
| } |
|
169 |
7
| else if (pvValue.getClass().equals(Long.class)) {
|
|
170 |
1
| xmlRpcString.append("<ex:i8>").append(pvValue).append("</ex:i8>");
|
|
171 |
| } |
|
172 |
6
| else if (pvValue.getClass().equals(Float.class)) {
|
|
173 |
2
| xmlRpcString.append("<ex:float>").append(pvValue).append("</ex:float>");
|
|
174 |
| } |
|
175 |
4
| else if (pvValue.getClass().equals(BigDecimal.class)) {
|
|
176 |
1
| xmlRpcString.append("<ex:bigdecimal>").append(pvValue).append("</ex:bigdecimal>");
|
|
177 |
| } |
|
178 |
3
| else if (pvValue.getClass().equals(BigInteger.class)) {
|
|
179 |
1
| xmlRpcString.append("<ex:biginteger>").append(pvValue).append("</ex:biginteger>");
|
|
180 |
| } |
|
181 |
2
| else if (Calendar.class.isAssignableFrom(pvValue.getClass())) {
|
|
182 |
1
| Calendar lvCalendar = (Calendar) pvValue;
|
|
183 |
1
| SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMDD'T'HH:mm:ss.SSS");
|
|
184 |
1
| String s = sdf.format(lvCalendar.getTime());
|
|
185 |
1
| xmlRpcString.append("<ex:dateTime>").append(s).append("</ex:dateTime>");
|
|
186 |
| } |
|
187 |
| |
|
188 |
510
| xmlRpcString.append("</value>");
|
|
189 |
| } |
|
190 |
| } |