1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.apache.any23.validator; |
19 | |
|
20 | |
import org.w3c.dom.Element; |
21 | |
|
22 | |
import java.io.OutputStream; |
23 | |
import java.io.PrintStream; |
24 | |
import java.lang.annotation.Documented; |
25 | |
import java.lang.annotation.ElementType; |
26 | |
import java.lang.annotation.Retention; |
27 | |
import java.lang.annotation.RetentionPolicy; |
28 | |
import java.lang.annotation.Target; |
29 | |
import java.lang.reflect.Array; |
30 | |
import java.lang.reflect.Method; |
31 | |
import java.lang.reflect.Modifier; |
32 | |
import java.util.ArrayList; |
33 | |
import java.util.Collection; |
34 | |
import java.util.List; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 0 | public class XMLValidationReportSerializer implements ValidationReportSerializer { |
43 | |
|
44 | |
public void serialize(ValidationReport vr, OutputStream os) throws SerializationException { |
45 | 0 | final PrintStream ps = new PrintStream(os); |
46 | |
try { |
47 | 0 | serializeObject(vr, ps); |
48 | |
} finally { |
49 | 0 | ps.flush(); |
50 | 0 | } |
51 | 0 | } |
52 | |
|
53 | |
private void serializeObject(Object o, PrintStream ps) throws SerializationException { |
54 | 0 | if(o == null) { |
55 | 0 | return; |
56 | |
} |
57 | 0 | final Class oClass = o.getClass(); |
58 | 0 | final String oClassName = getClassName(oClass); |
59 | 0 | ps.printf("<%s>\n", oClassName); |
60 | 0 | List<Method> getters = filterGetters(o.getClass()); |
61 | 0 | if(getters.isEmpty()) { |
62 | 0 | ps.print( o.toString() ); |
63 | 0 | return; |
64 | |
} |
65 | 0 | for (Method getter : getters) { |
66 | 0 | serializeGetterValue(o, getter, ps); |
67 | |
} |
68 | 0 | ps.printf("</%s>\n", oClassName); |
69 | 0 | } |
70 | |
|
71 | |
private String getClassName(Class oClass) { |
72 | 0 | final NodeName nodeName = (NodeName) oClass.getAnnotation(NodeName.class); |
73 | 0 | if(nodeName != null) { |
74 | 0 | return nodeName.value(); |
75 | |
} |
76 | 0 | final String simpleName = oClass.getSimpleName(); |
77 | 0 | return Character.toLowerCase(simpleName.charAt(0)) + simpleName.substring(1); |
78 | |
} |
79 | |
|
80 | |
private List<Method> filterGetters(Class c) { |
81 | 0 | Method[] methods = c.getDeclaredMethods(); |
82 | 0 | List<Method> filtered = new ArrayList<Method>(); |
83 | 0 | for(Method method : methods) { |
84 | 0 | if(Modifier.isStatic(method.getModifiers())) { |
85 | 0 | continue; |
86 | |
} |
87 | 0 | final String methodName = method.getName(); |
88 | 0 | if( |
89 | |
method.getParameterTypes().length == 0 |
90 | |
&& |
91 | |
( |
92 | |
(methodName.length() > 3 && methodName.indexOf("get") == 0) |
93 | |
|| |
94 | |
(methodName.length() > 2 && methodName.indexOf("is") == 0) |
95 | |
) |
96 | |
) { |
97 | 0 | filtered.add(method); |
98 | |
} |
99 | |
} |
100 | 0 | return filtered; |
101 | |
} |
102 | |
|
103 | |
public void serializeGetterValue(Object o, Method m, PrintStream ps) throws SerializationException { |
104 | |
final Object value; |
105 | 0 | final String methodName = m.getName(); |
106 | |
try { |
107 | 0 | value = m.invoke(o); |
108 | 0 | } catch (Exception e) { |
109 | 0 | throw new SerializationException( String.format("Error while reading method '%s'", methodName), e ); |
110 | 0 | } |
111 | 0 | final String property = getPropertyFromMethodName(methodName); |
112 | 0 | if( isManaged(value) ) { |
113 | 0 | ps.printf("<%s>\n", property); |
114 | 0 | printObject(value, ps); |
115 | 0 | ps.printf("</%s>\n", property); |
116 | |
} else { |
117 | 0 | List<Method> getters = filterGetters(value.getClass()); |
118 | 0 | for (Method getter : getters) { |
119 | 0 | serializeGetterValue(value, getter, ps); |
120 | |
} |
121 | |
} |
122 | 0 | } |
123 | |
|
124 | |
private String getPropertyFromMethodName(String methodName) { |
125 | 0 | int i = methodName.indexOf("is"); |
126 | 0 | if(i == 0) { |
127 | 0 | return Character.toLowerCase( methodName.charAt(2) ) + methodName.substring(3); |
128 | |
} |
129 | 0 | return Character.toLowerCase( methodName.charAt(3) ) + methodName.substring(4); |
130 | |
} |
131 | |
|
132 | |
private void printObject(Object o, PrintStream ps) throws SerializationException { |
133 | 0 | if(o == null) { |
134 | 0 | return; |
135 | |
} |
136 | 0 | if(o instanceof Element) { |
137 | 0 | ps.print( o.toString() ); |
138 | 0 | return; |
139 | |
} |
140 | 0 | if(o instanceof Array) { |
141 | 0 | Object[] array = (Object[]) o; |
142 | 0 | if(array.length == 0) { |
143 | 0 | return; |
144 | |
} |
145 | 0 | for(Object a : array) { |
146 | 0 | serializeObject(a, ps); |
147 | |
} |
148 | 0 | return; |
149 | |
} |
150 | 0 | if(o instanceof Collection) { |
151 | 0 | Collection collection = (Collection) o; |
152 | 0 | if(collection.isEmpty()) { |
153 | 0 | return; |
154 | |
} |
155 | 0 | for(Object e : collection) { |
156 | 0 | serializeObject(e, ps); |
157 | |
} |
158 | 0 | return; |
159 | |
} |
160 | 0 | ps.print( o.toString() ); |
161 | 0 | } |
162 | |
|
163 | |
private boolean isManaged(Object o) { |
164 | 0 | return |
165 | |
o == null |
166 | |
|| |
167 | |
o instanceof String |
168 | |
|| |
169 | |
o.getClass().isPrimitive() |
170 | |
|| |
171 | |
(o instanceof Collection) |
172 | |
|| |
173 | |
o instanceof Element; |
174 | |
} |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | 0 | @Documented |
180 | |
@Retention(RetentionPolicy.RUNTIME) |
181 | |
@Target(ElementType.TYPE) |
182 | |
public @interface NodeName { |
183 | |
String value(); |
184 | |
} |
185 | |
|
186 | |
} |