1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.datadictionary.validator;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.kuali.rice.krad.datadictionary.uif.UifDictionaryBean;
21 import org.kuali.rice.krad.uif.component.Component;
22 import org.kuali.rice.krad.uif.component.DataBinding;
23 import org.springframework.core.io.ResourceLoader;
24 import org.w3c.dom.Document;
25 import org.w3c.dom.NodeList;
26
27 import javax.xml.parsers.DocumentBuilder;
28 import javax.xml.parsers.DocumentBuilderFactory;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.Iterator;
32 import java.util.Map;
33
34
35
36
37
38
39
40 public class ValidationTrace {
41 private static final Log LOG = LogFactory.getLog(ValidationTrace.class);
42
43
44 public static final String NO_BEAN_ID = "NOBEANID";
45
46
47 public static final int START_UP = 0;
48
49
50 public static final int BUILD = 1;
51
52 private ArrayList<String> beanIds;
53 private ArrayList<String> beanTypes;
54 private Map<String, Document> beanMap;
55 private int validationStage;
56
57
58
59
60 public ValidationTrace() {
61 beanIds = new ArrayList<String>();
62 beanTypes = new ArrayList<String>();
63 beanMap = new HashMap<String, Document>();
64 }
65
66
67
68
69 public ValidationTrace(String[] files, ResourceLoader loader) {
70 beanIds = new ArrayList<String>();
71 beanTypes = new ArrayList<String>();
72 beanMap = new HashMap<String, Document>();
73 loadFiles(files, loader);
74 }
75
76
77
78
79
80
81
82 public void addBean(String beanType, String beanId) {
83 beanIds.add(beanId);
84 beanTypes.add(beanType);
85 }
86
87
88
89
90
91
92 public void addBean(Component component) {
93 String beanId = NO_BEAN_ID;
94 String beanType = component.getClass().getSimpleName();
95 if (component.getId() != null) {
96 if (component.getId().compareTo("null") != 0) {
97 beanId = component.getId();
98 } else {
99 try {
100 beanId = ((DataBinding) component).getPropertyName();
101
102 } catch (Exception e) {
103 beanId = NO_BEAN_ID;
104 }
105 }
106 } else {
107 try {
108 beanId = ((DataBinding) component).getPropertyName();
109 } catch (Exception e) {
110 beanId = NO_BEAN_ID;
111 }
112 }
113 addBean(beanType, beanId);
114 }
115
116
117
118
119
120
121 public void addBean(UifDictionaryBean configurable) {
122 String beanId = "configurable";
123 String beanType = configurable.getClass().getSimpleName();
124 addBean(beanType, beanId);
125 }
126
127
128
129
130
131
132 public void removeBean(int index) {
133 beanIds.remove(index);
134 beanTypes.remove(index);
135 }
136
137
138
139
140
141
142
143
144 public void modifyBean(int index, String beanId, String beanType) {
145 beanIds.set(index, beanId);
146 beanTypes.set(index, beanType);
147 }
148
149
150
151
152
153
154 public ValidationTrace getCopy() {
155 ValidationTrace copy = new ValidationTrace();
156
157 for (int i = 0; i < getTraceSize(); i++) {
158 copy.addBean(getBeanType(i), getBeanId(i));
159 }
160 copy.setValidationStage(getValidationStage());
161 copy.setBeanMap(beanMap);
162 return copy;
163 }
164
165
166
167
168
169
170
171
172 private void loadFiles(String[] beanFiles, ResourceLoader loader) {
173 LOG.debug("Started Loading Parser Files");
174
175 for (int i = 0; i < beanFiles.length; i++) {
176 try {
177 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
178 DocumentBuilder builder = factory.newDocumentBuilder();
179 Document document;
180 String file = beanFiles[i];
181 LOG.debug("Loading file: " + file);
182 document = builder.parse(loader.getResource(file).getInputStream());
183 beanMap.put(file, document);
184 } catch (Exception e) {
185 LOG.error("Not Found: " + beanFiles[i]);
186 }
187 }
188 LOG.debug("Finished Loading Parser Files");
189 }
190
191
192
193
194
195
196
197
198
199
200
201 private Map<String, Document> findBeanById(String id, Map<String, Document> beans) {
202 Map<String, Document> result = new HashMap<String, Document>();
203 LOG.debug("Searching for bean of Id: " + id);
204
205 Iterator iter = beans.entrySet().iterator();
206
207 while (iter.hasNext()) {
208 Map.Entry entry = (Map.Entry) iter.next();
209 Document document = (Document) entry.getValue();
210 NodeList nodes = document.getElementsByTagName("bean");
211
212 for (int i = 0; i < nodes.getLength(); i++) {
213 if (nodes.item(i).hasAttributes()) {
214 for (int j = 0; j < nodes.item(i).getAttributes().getLength(); j++) {
215 if (nodes.item(i).getAttributes().item(j).getNodeValue().toLowerCase().compareTo(
216 id.toLowerCase()) == 0) {
217 LOG.debug("Found bean of Id = " + id);
218
219 result.put((String) entry.getKey(), (Document) entry.getValue());
220
221 break;
222 }
223 }
224 }
225 }
226 }
227
228 return result;
229 }
230
231
232
233
234
235
236
237
238 public ArrayList<String> findXmlFiles() {
239 Map<String, Document> result = new HashMap<String, Document>();
240 LOG.debug("Looking for Xml files");
241
242 for (int i = 0; i < getTraceSize(); i++) {
243 if (getBeanId(i) != null) {
244 if (getBeanId(i).compareTo(NO_BEAN_ID) != 0) {
245 result.putAll(findBeanById(getBeanId(i), beanMap));
246 }
247 }
248 }
249
250 ArrayList<String> files = new ArrayList<String>();
251 Iterator iter = result.entrySet().iterator();
252 while (iter.hasNext()) {
253 Map.Entry entry = (Map.Entry) iter.next();
254 files.add((String) entry.getKey());
255 }
256
257 return files;
258 }
259
260
261
262
263
264
265 public void setValidationStage(int stage) {
266 validationStage = stage;
267 }
268
269
270
271
272
273
274 private void setBeanMap(Map<String, Document> newMap) {
275 beanMap = newMap;
276 }
277
278
279
280
281
282
283
284 public void createError(String validation, String values[]) {
285 ErrorReport report = new ErrorReport(ErrorReport.ERROR, validation, this, values);
286 Validator.addErrorReport(report);
287
288 }
289
290
291
292
293
294
295
296 public void createWarning(String validation, String values[]) {
297 ErrorReport report = new ErrorReport(ErrorReport.WARNING, validation, this, values);
298 Validator.addErrorReport(report);
299
300 }
301
302
303
304
305
306
307
308 public String getBeanId(int index) {
309 return beanIds.get(index);
310 }
311
312
313
314
315
316
317
318
319 public String getBeanType(int index) {
320 return beanTypes.get(index);
321 }
322
323
324
325
326
327
328
329 public int getValidationStage() {
330 return validationStage;
331 }
332
333
334
335
336
337
338 public int getTraceSize() {
339 return beanIds.size();
340 }
341
342
343
344
345
346
347 public String getBeanLocation() {
348 String path = "";
349
350 for (int i = 0; i < beanTypes.size() - 1; i++) {
351 path = path + beanTypes.get(i) + "(" + beanIds.get(i) + ")" + ".";
352 }
353
354 if (getTraceSize() > 0) {
355 path = path + beanTypes.get(beanTypes.size() - 1) + "(" + beanIds.get(beanTypes.size() - 1) + ")";
356 }
357
358 return path;
359 }
360
361
362
363
364
365
366 public ArrayList<String> getRelatedXmls() {
367 return findXmlFiles();
368 }
369 }