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