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 package com.mindtree.techworks.insight.preferences.xmlpersistence;
26
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 import java.net.URL;
32 import java.nio.channels.FileChannel;
33 import java.util.ArrayList;
34 import java.util.Collection;
35 import java.util.Iterator;
36 import java.util.List;
37
38 import javax.xml.parsers.ParserConfigurationException;
39 import javax.xml.parsers.SAXParser;
40 import javax.xml.parsers.SAXParserFactory;
41
42 import org.xml.sax.InputSource;
43 import org.xml.sax.SAXException;
44 import org.xml.sax.SAXParseException;
45 import org.xml.sax.XMLReader;
46 import org.xml.sax.helpers.DefaultHandler;
47
48 import com.mindtree.techworks.insight.InsightConstants;
49 import com.mindtree.techworks.insight.preferences.PreferenceDataHandler;
50 import com.mindtree.techworks.insight.preferences.PreferenceHandlerInstantiationException;
51 import com.mindtree.techworks.insight.preferences.PreferenceHandlerStoreException;
52 import com.mindtree.techworks.insight.preferences.model.Preference;
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 public class XMLPreferenceDataHandler implements PreferenceDataHandler {
72
73
74
75
76 private static final String SCHEMA_LOCATION = "com/mindtree/techworks/insight/preferences/xmlpersistence/res/InsightPreferences.xsd";
77
78
79
80
81 private static final String TARGET_NAMESPACE = "http://mindtree.com/techworks/insight/Preferences";
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 private static final String FEATURE_VALIDATION = "http://xml.org/sax/features/validation";
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139 private static final String FEATURE_VALIDATION_SCHEMA = "http://apache.org/xml/features/validation/schema";
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174 private static final String PROPERTY_SCHEMA_EXTLOC = "http://apache.org/xml/properties/schema/external-schemaLocation";
175
176
177
178
179
180 protected static final String LEXICAL_HANDLER_PROPERTY_ID = "http://xml.org/sax/properties/lexical-handler";
181
182
183
184
185 public static final String XML_PROP_FILE_URI = "/config/insight-preferences.xml";
186
187
188
189
190 private String preferenceFileLocation;
191
192
193
194
195
196
197
198
199
200 protected String schemaFilePath;
201
202
203
204
205
206
207
208 public XMLPreferenceDataHandler ()
209 throws PreferenceHandlerInstantiationException {
210
211 preferenceFileLocation = System
212 .getProperty(InsightConstants.INSIGHT_HOME)
213 + XML_PROP_FILE_URI;
214
215 File prefFile = new File(preferenceFileLocation);
216 if (!prefFile.isFile() || !prefFile.canRead() || !prefFile.canWrite()) {
217 throw new PreferenceHandlerInstantiationException(
218 "Preference File Cannot be read, or is not writable");
219 }
220
221 XMLPreferenceDataCache dataCache = XMLPreferenceDataCache.getInstance();
222
223
224 if (!dataCache.isXMLValidated()) {
225 boolean fileValidity = validateXMLFile();
226 if (false == fileValidity) {
227 throw new PreferenceHandlerInstantiationException(
228 "Cannot get valid xml");
229 }
230 dataCache.setXMLValidated(true);
231 }
232 }
233
234
235
236
237
238
239
240
241
242
243
244
245
246 public Collection getPreferenceNameList () {
247
248 XMLPreferenceDataCache dataCache = XMLPreferenceDataCache.getInstance();
249 if (!dataCache.isXMLLoaded()) {
250 loadPreferencesIntoCache();
251 }
252
253 ArrayList preferenceInfo = new ArrayList();
254
255 for (Iterator preferencesIterator = dataCache.iteratePreferences(); preferencesIterator
256 .hasNext();) {
257 Preference preference = (Preference) preferencesIterator.next();
258 preferenceInfo.add(preference.getPreferenceInfo());
259 }
260
261 return preferenceInfo;
262 }
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277 public Preference getPreference (String preferenceId) {
278
279 XMLPreferenceDataCache dataCache = XMLPreferenceDataCache.getInstance();
280 if (!dataCache.isXMLLoaded()) {
281 loadPreferencesIntoCache();
282 }
283
284 return dataCache.getPreference(preferenceId);
285 }
286
287
288
289
290
291
292
293 public void savePreferences (List preferences)
294 throws PreferenceHandlerStoreException {
295
296 String tempFilePath;
297 try {
298 File tempFile = File.createTempFile("insight-preference", ".xml");
299 tempFile.deleteOnExit();
300 tempFilePath = tempFile.getAbsolutePath();
301 } catch (IOException e) {
302 throw new PreferenceHandlerStoreException(e);
303 }
304
305 XMLDOMPreferenceDataWriter writer = new XMLDOMPreferenceDataWriter(
306 preferenceFileLocation);
307 writer.writePreferences(preferences, tempFilePath);
308
309
310 File origFile = new File(preferenceFileLocation);
311 origFile.delete();
312 origFile = new File(preferenceFileLocation);
313 File sourceFile = new File(tempFilePath);
314 try {
315 copyFile(sourceFile, origFile);
316 } catch (IOException e) {
317 throw new PreferenceHandlerStoreException(e);
318 }
319 sourceFile.delete();
320 }
321
322
323
324
325
326
327 public Collection getAllPreferences () {
328
329 XMLPreferenceDataCache dataCache = XMLPreferenceDataCache.getInstance();
330 if (!dataCache.isXMLLoaded()) {
331 loadPreferencesIntoCache();
332 }
333
334 return dataCache.getAllPreferences();
335 }
336
337
338
339
340
341
342
343 private boolean validateXMLFile () {
344
345
346 URL schemaLocation = Thread.currentThread().getContextClassLoader().getResource(
347 SCHEMA_LOCATION);
348 schemaFilePath = schemaLocation.getFile();
349 String schemaExtLoc = TARGET_NAMESPACE + " " + schemaFilePath;
350
351
352 FileInputStream fis = null;
353
354
355 SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
356 saxParserFactory.setNamespaceAware(true);
357
358 SAXParser parser = null;
359
360 try {
361 parser = saxParserFactory.newSAXParser();
362 XMLReader xmlReader = parser.getXMLReader();
363
364
365 xmlReader.setFeature(FEATURE_VALIDATION, true);
366 xmlReader.setFeature(FEATURE_VALIDATION_SCHEMA, true);
367 xmlReader.setProperty(PROPERTY_SCHEMA_EXTLOC, schemaExtLoc);
368
369
370
371
372
373 DefaultHandler validationHandler = (new DefaultHandler() {
374
375
376
377
378
379
380 public void warning (SAXParseException e) throws SAXException {
381
382 throw e;
383 }
384
385
386
387
388
389
390 public void error (SAXParseException e) throws SAXException {
391
392 throw e;
393 }
394
395
396
397
398
399
400 public void fatalError (SAXParseException e)
401 throws SAXException {
402
403 throw e;
404 }
405
406
407
408
409
410
411
412 public InputSource resolveEntity (String publicId,
413 String systemId) throws SAXException {
414
415 if (systemId.endsWith(schemaFilePath)) {
416 InputSource schemaInputSource = new InputSource(this
417 .getClass().getClassLoader()
418 .getResourceAsStream(SCHEMA_LOCATION));
419 return schemaInputSource;
420 }
421 try {
422 return super.resolveEntity(publicId, systemId);
423 } catch (Exception e) {
424 throw new SAXException("Exception occured resolving entity", e);
425 }
426 }
427
428
429 });
430
431 fis = new FileInputStream(preferenceFileLocation);
432 parser.parse(new InputSource(fis), validationHandler);
433
434 } catch (ParserConfigurationException e) {
435 e.printStackTrace();
436 return false;
437 } catch (SAXException e) {
438 e.printStackTrace();
439 return false;
440 } catch (IOException e) {
441 e.printStackTrace();
442 return false;
443 } finally {
444 try {
445 fis.close();
446 } catch (IOException e) {
447 e.printStackTrace();
448 }
449 }
450 return true;
451 }
452
453
454
455
456 private void loadPreferencesIntoCache () {
457
458
459 FileInputStream fis = null;
460
461
462 SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
463 saxParserFactory.setNamespaceAware(true);
464
465 SAXParser parser = null;
466
467 try {
468 parser = saxParserFactory.newSAXParser();
469
470 DefaultHandler validationHandler = new XMLPreferenceDataReader();
471
472 fis = new FileInputStream(preferenceFileLocation);
473 parser.parse(new InputSource(fis), validationHandler);
474
475 } catch (ParserConfigurationException e) {
476 e.printStackTrace();
477 } catch (SAXException e) {
478 e.printStackTrace();
479 } catch (IOException e) {
480 e.printStackTrace();
481 } finally {
482 try {
483 fis.close();
484 } catch (IOException e) {
485 e.printStackTrace();
486 }
487 }
488
489 XMLPreferenceDataCache cache = XMLPreferenceDataCache.getInstance();
490 cache.setXMLLoaded(true);
491 }
492
493
494
495
496
497
498
499
500
501 private void copyFile (File in, File out) throws IOException {
502
503 FileChannel sourceChannel = new FileInputStream(in).getChannel();
504 FileChannel destinationChannel = new FileOutputStream(out).getChannel();
505 sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel);
506 sourceChannel.close();
507 destinationChannel.close();
508 }
509 }