|
1 /* |
|
2 * Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 * Trace activation information Importer class |
|
17 * |
|
18 */ |
|
19 package com.nokia.traceviewer.engine.activation; |
|
20 |
|
21 import java.io.File; |
|
22 import java.io.IOException; |
|
23 import java.util.ArrayList; |
|
24 import java.util.List; |
|
25 |
|
26 import javax.xml.parsers.DocumentBuilder; |
|
27 import javax.xml.parsers.DocumentBuilderFactory; |
|
28 import javax.xml.parsers.ParserConfigurationException; |
|
29 |
|
30 import org.w3c.dom.Attr; |
|
31 import org.w3c.dom.Document; |
|
32 import org.w3c.dom.Element; |
|
33 import org.w3c.dom.NamedNodeMap; |
|
34 import org.w3c.dom.Node; |
|
35 import org.w3c.dom.NodeList; |
|
36 import org.xml.sax.SAXException; |
|
37 |
|
38 import com.nokia.traceviewer.engine.TraceViewerGlobals; |
|
39 |
|
40 /** |
|
41 * Trace activation information Importer class |
|
42 * |
|
43 */ |
|
44 public class TraceActivationXMLImporter { |
|
45 |
|
46 /** |
|
47 * Category for trigger events |
|
48 */ |
|
49 private static final String TRIGGER_CATEGORY = Messages |
|
50 .getString("TraceActivationXMLImporter.TriggerCategory"); //$NON-NLS-1$ |
|
51 |
|
52 /** |
|
53 * File path where to import |
|
54 */ |
|
55 private final String filePath; |
|
56 |
|
57 /** |
|
58 * Constructor |
|
59 * |
|
60 * @param configurationFileName |
|
61 * file name where to export data |
|
62 */ |
|
63 public TraceActivationXMLImporter(String configurationFileName) { |
|
64 this.filePath = configurationFileName; |
|
65 } |
|
66 |
|
67 /** |
|
68 * Imports configuration names found from this XML file |
|
69 * |
|
70 * @return the configuration names |
|
71 */ |
|
72 public String[] importConfigurationNames() { |
|
73 ArrayList<String> confNameList = new ArrayList<String>(); |
|
74 String[] confNameArr = null; |
|
75 File file = new File(filePath); |
|
76 if (file.exists() && file.length() > 0) { |
|
77 Document doc = getDocument(); |
|
78 |
|
79 // Get activations node from the XML file |
|
80 NodeList list = doc |
|
81 .getElementsByTagName(TraceActivationXMLConstants.ACTIVATIONS_TAG); |
|
82 Node parent = list.item(0); |
|
83 |
|
84 // Go through activations |
|
85 if (parent != null) { |
|
86 NodeList activations = parent.getChildNodes(); |
|
87 if (activations != null) { |
|
88 for (int i = 0; i < activations.getLength(); i++) { |
|
89 if (activations.item(i) instanceof Element) { |
|
90 NamedNodeMap itemAttributes = activations.item(i) |
|
91 .getAttributes(); |
|
92 |
|
93 // Get name |
|
94 Attr nameAttr = (Attr) itemAttributes |
|
95 .getNamedItem(TraceActivationXMLConstants.NAME_TAG); |
|
96 String name = nameAttr.getValue(); |
|
97 confNameList.add(name); |
|
98 } |
|
99 } |
|
100 } else { |
|
101 // If comes here, activation information was not found |
|
102 } |
|
103 } else { |
|
104 // If comes here, this XML file isn't the correct format |
|
105 String xmlCorrupted = Messages |
|
106 .getString("TraceActivationXMLImporter.XMLCorrupted"); //$NON-NLS-1$ |
|
107 TraceViewerGlobals.getTraceViewer().getDialogs() |
|
108 .showErrorMessage(xmlCorrupted); |
|
109 } |
|
110 } |
|
111 |
|
112 // Get items from list to array |
|
113 confNameArr = new String[confNameList.size()]; |
|
114 for (int j = 0; j < confNameList.size(); j++) { |
|
115 confNameArr[j] = confNameList.get(j); |
|
116 } |
|
117 |
|
118 return confNameArr; |
|
119 } |
|
120 |
|
121 /** |
|
122 * Inserts components from configuration to the given array |
|
123 * |
|
124 * @param components |
|
125 * components |
|
126 * @param configurationName |
|
127 * configuration name |
|
128 */ |
|
129 public void createComponentListFromConfigurationName( |
|
130 List<TraceActivationComponentItem> components, |
|
131 String configurationName) { |
|
132 File file = new File(filePath); |
|
133 if (file.exists()) { |
|
134 Document doc = getDocument(); |
|
135 |
|
136 // Get activations node from the XML file |
|
137 NodeList list = doc |
|
138 .getElementsByTagName(TraceActivationXMLConstants.ACTIVATIONS_TAG); |
|
139 Node parent = list.item(0); |
|
140 |
|
141 // Go trough activations |
|
142 if (parent != null) { |
|
143 NodeList activations = parent.getChildNodes(); |
|
144 if (activations != null) { |
|
145 boolean found = false; |
|
146 for (int i = 0; i < activations.getLength(); i++) { |
|
147 if (activations.item(i) instanceof Element) { |
|
148 NamedNodeMap itemAttributes = activations.item(i) |
|
149 .getAttributes(); |
|
150 |
|
151 // Get name |
|
152 Attr nameAttr = (Attr) itemAttributes |
|
153 .getNamedItem(TraceActivationXMLConstants.NAME_TAG); |
|
154 String name = nameAttr.getValue(); |
|
155 if (name.equals(configurationName)) { |
|
156 NodeList componentList = activations.item(i) |
|
157 .getChildNodes(); |
|
158 for (int j = 0; j < componentList.getLength(); j++) { |
|
159 if (componentList.item(j) instanceof Element) { |
|
160 processXMLTriggerRule(componentList |
|
161 .item(j), components); |
|
162 } |
|
163 } |
|
164 found = true; |
|
165 break; |
|
166 } |
|
167 } |
|
168 } |
|
169 if (!found) { |
|
170 // Specific activation information was not found |
|
171 String confReference = Messages |
|
172 .getString("TraceActivationXMLImporter.ConfNotFound"); //$NON-NLS-1$ |
|
173 TraceViewerGlobals.postErrorEvent(confReference, |
|
174 TRIGGER_CATEGORY, configurationName); |
|
175 } |
|
176 } else { |
|
177 // Activation informations were not found |
|
178 String confNotFound = Messages |
|
179 .getString("TraceActivationXMLImporter.ConfNotFound"); //$NON-NLS-1$ |
|
180 TraceViewerGlobals.postErrorEvent(confNotFound, |
|
181 TRIGGER_CATEGORY, configurationName); |
|
182 } |
|
183 } else { |
|
184 // XML file is corrupted |
|
185 String xmlCorrupted = Messages |
|
186 .getString("TraceActivationXMLImporter.XMLCorrupted"); //$NON-NLS-1$ |
|
187 TraceViewerGlobals.postErrorEvent(xmlCorrupted, |
|
188 TRIGGER_CATEGORY, filePath); |
|
189 } |
|
190 } else { |
|
191 // File was not found |
|
192 String fileNotFound = Messages |
|
193 .getString("TraceActivationXMLImporter.FileNotFound"); //$NON-NLS-1$ |
|
194 TraceViewerGlobals.postErrorEvent(fileNotFound, TRIGGER_CATEGORY, |
|
195 filePath); |
|
196 } |
|
197 } |
|
198 |
|
199 private void processXMLTriggerRule(Node item, |
|
200 List<TraceActivationComponentItem> components) { |
|
201 NamedNodeMap itemAttributes = item.getAttributes(); |
|
202 |
|
203 // Get id |
|
204 Attr idAttr = (Attr) itemAttributes |
|
205 .getNamedItem(TraceActivationXMLConstants.ID_TAG); |
|
206 int id = Integer.parseInt(idAttr.getValue()); |
|
207 |
|
208 // Create new Activation item |
|
209 TraceActivationComponentItem newActivationItem = new TraceActivationComponentItem( |
|
210 id, ""); //$NON-NLS-1$ |
|
211 components.add(newActivationItem); |
|
212 |
|
213 // Get groups from Activation item |
|
214 List<TraceActivationGroupItem> groups = newActivationItem.getGroups(); |
|
215 |
|
216 // Get groups from XML item |
|
217 NodeList XMLgroups = item.getChildNodes(); |
|
218 |
|
219 // Process groups from the Node item |
|
220 for (int i = 0; i < XMLgroups.getLength(); i++) { |
|
221 if (XMLgroups.item(i) instanceof Element) { |
|
222 processGroups(XMLgroups.item(i), groups, newActivationItem); |
|
223 } |
|
224 } |
|
225 } |
|
226 |
|
227 private void processGroups(Node groupItem, |
|
228 List<TraceActivationGroupItem> groups, |
|
229 TraceActivationComponentItem parent) { |
|
230 NamedNodeMap itemAttributes = groupItem.getAttributes(); |
|
231 |
|
232 // Get id |
|
233 Attr idAttr = (Attr) itemAttributes |
|
234 .getNamedItem(TraceActivationXMLConstants.ID_TAG); |
|
235 int id = Integer.parseInt(idAttr.getValue()); |
|
236 |
|
237 // Create new group |
|
238 TraceActivationGroupItem newGroup = new TraceActivationGroupItem( |
|
239 parent, id, ""); //$NON-NLS-1$ |
|
240 groups.add(newGroup); |
|
241 |
|
242 // Check activated status |
|
243 String activated = groupItem.getTextContent(); |
|
244 if (activated.equals(TraceActivationXMLConstants.YES_TAG)) { |
|
245 newGroup.setActivated(true); |
|
246 } |
|
247 } |
|
248 |
|
249 /** |
|
250 * Imports activation data |
|
251 * |
|
252 * @param components |
|
253 * components |
|
254 * @param configurationName |
|
255 * name of the configuration |
|
256 * @param changedComponents |
|
257 * list of changed components to use when activating |
|
258 * @return true if everything went fine |
|
259 */ |
|
260 public boolean importData(List<TraceActivationComponentItem> components, |
|
261 String configurationName, |
|
262 List<TraceActivationComponentItem> changedComponents) { |
|
263 boolean success = true; |
|
264 File file = new File(filePath); |
|
265 if (file.exists()) { |
|
266 Document doc = getDocument(); |
|
267 |
|
268 // Get activations node from the XML file |
|
269 NodeList list = doc |
|
270 .getElementsByTagName(TraceActivationXMLConstants.ACTIVATIONS_TAG); |
|
271 Node parent = list.item(0); |
|
272 |
|
273 // Go trough activations |
|
274 if (parent != null) { |
|
275 NodeList activations = parent.getChildNodes(); |
|
276 if (activations != null) { |
|
277 boolean found = false; |
|
278 for (int i = 0; i < activations.getLength(); i++) { |
|
279 if (activations.item(i) instanceof Element) { |
|
280 NamedNodeMap itemAttributes = activations.item(i) |
|
281 .getAttributes(); |
|
282 |
|
283 // Get name |
|
284 Attr nameAttr = (Attr) itemAttributes |
|
285 .getNamedItem(TraceActivationXMLConstants.NAME_TAG); |
|
286 String name = nameAttr.getValue(); |
|
287 if (name.equals(configurationName)) { |
|
288 NodeList componentList = activations.item(i) |
|
289 .getChildNodes(); |
|
290 for (int j = 0; j < componentList.getLength(); j++) { |
|
291 if (componentList.item(j) instanceof Element) { |
|
292 processXMLComponent(componentList |
|
293 .item(j), components, |
|
294 changedComponents); |
|
295 } |
|
296 } |
|
297 found = true; |
|
298 break; |
|
299 } |
|
300 } |
|
301 } |
|
302 if (!found) { |
|
303 // Specific activation information was not found |
|
304 String confReference = Messages |
|
305 .getString("TraceActivationXMLImporter.ConfNotFound"); //$NON-NLS-1$ |
|
306 TraceViewerGlobals.getTraceViewer().getDialogs() |
|
307 .showErrorMessage(confReference); |
|
308 success = false; |
|
309 } |
|
310 } else { |
|
311 // Activation information was not found |
|
312 String confNotFound = Messages |
|
313 .getString("TraceActivationXMLImporter.ConfNotFound"); //$NON-NLS-1$ |
|
314 TraceViewerGlobals.getTraceViewer().getDialogs() |
|
315 .showErrorMessage(confNotFound); |
|
316 success = false; |
|
317 } |
|
318 } else { |
|
319 // XML file is corrupted |
|
320 String xmlCorrupted = Messages |
|
321 .getString("TraceActivationXMLImporter.XMLCorrupted"); //$NON-NLS-1$ |
|
322 TraceViewerGlobals.getTraceViewer().getDialogs() |
|
323 .showErrorMessage(xmlCorrupted); |
|
324 success = false; |
|
325 } |
|
326 } else { |
|
327 // File was not found |
|
328 String fileNotFound = Messages |
|
329 .getString("TraceActivationXMLImporter.FileNotFound"); //$NON-NLS-1$ |
|
330 TraceViewerGlobals.getTraceViewer().getDialogs().showErrorMessage( |
|
331 fileNotFound); |
|
332 success = false; |
|
333 } |
|
334 return success; |
|
335 } |
|
336 |
|
337 /** |
|
338 * Process XML component |
|
339 * |
|
340 * @param component |
|
341 * XML component to process |
|
342 * @param components |
|
343 * all components loaded to model |
|
344 * @param changedComponents |
|
345 * changed components list |
|
346 */ |
|
347 private void processXMLComponent(Node component, |
|
348 List<TraceActivationComponentItem> components, |
|
349 List<TraceActivationComponentItem> changedComponents) { |
|
350 NamedNodeMap itemAttributes = component.getAttributes(); |
|
351 |
|
352 // Get id |
|
353 Attr idAttr = (Attr) itemAttributes |
|
354 .getNamedItem(TraceActivationXMLConstants.ID_TAG); |
|
355 int id = Integer.parseInt(idAttr.getValue()); |
|
356 |
|
357 // Find component with correct id |
|
358 for (int i = 0; i < components.size(); i++) { |
|
359 if (components.get(i).getId() == id) { |
|
360 processGroups(components.get(i), component, changedComponents); |
|
361 break; |
|
362 } |
|
363 } |
|
364 } |
|
365 |
|
366 /** |
|
367 * Process groups from this component |
|
368 * |
|
369 * @param item |
|
370 * real component item |
|
371 * @param component |
|
372 * XML component node |
|
373 * @param changedComponents |
|
374 * changed components list |
|
375 */ |
|
376 private void processGroups(TraceActivationComponentItem item, |
|
377 Node component, List<TraceActivationComponentItem> changedComponents) { |
|
378 // Get groups from real component |
|
379 List<TraceActivationGroupItem> groups = item.getGroups(); |
|
380 |
|
381 // Get groups from xml component |
|
382 NodeList XMLgroups = component.getChildNodes(); |
|
383 |
|
384 // Go trough all groups from xml component |
|
385 for (int i = 0; i < XMLgroups.getLength(); i++) { |
|
386 if (XMLgroups.item(i) instanceof Element) { |
|
387 NamedNodeMap itemAttributes = XMLgroups.item(i).getAttributes(); |
|
388 |
|
389 // Get id |
|
390 Attr idAttr = (Attr) itemAttributes |
|
391 .getNamedItem(TraceActivationXMLConstants.ID_TAG); |
|
392 int id = Integer.parseInt(idAttr.getValue()); |
|
393 |
|
394 // Find this id from real groups |
|
395 for (int j = 0; j < groups.size(); j++) { |
|
396 TraceActivationGroupItem group = groups.get(j); |
|
397 if (group.getId() == id) { |
|
398 // Read the activation status |
|
399 String activated = XMLgroups.item(i).getTextContent(); |
|
400 |
|
401 // Activate a group if it wasn't already activated |
|
402 if (activated |
|
403 .equals(TraceActivationXMLConstants.YES_TAG)) { |
|
404 if (!group.isActivated()) { |
|
405 group.setActivated(true); |
|
406 |
|
407 // Add this components to the changed components |
|
408 // list |
|
409 if (!changedComponents.contains(group |
|
410 .getParent())) { |
|
411 changedComponents.add(group.getParent()); |
|
412 } |
|
413 } |
|
414 } else { |
|
415 |
|
416 // Deactivate a group if it was activated |
|
417 if (group.isActivated()) { |
|
418 group.setActivated(false); |
|
419 |
|
420 // Add this to the changed components |
|
421 // list |
|
422 if (!changedComponents.contains(group |
|
423 .getParent())) { |
|
424 changedComponents.add(group.getParent()); |
|
425 } |
|
426 } |
|
427 } |
|
428 } |
|
429 } |
|
430 } |
|
431 } |
|
432 } |
|
433 |
|
434 /** |
|
435 * Gets DOM document for the file path |
|
436 * |
|
437 * @return document |
|
438 */ |
|
439 protected Document getDocument() { |
|
440 DocumentBuilderFactory docFactory = DocumentBuilderFactory |
|
441 .newInstance(); |
|
442 DocumentBuilder docBuilder = null; |
|
443 try { |
|
444 docBuilder = docFactory.newDocumentBuilder(); |
|
445 } catch (ParserConfigurationException e) { |
|
446 e.printStackTrace(); |
|
447 } |
|
448 |
|
449 // Get the document |
|
450 Document doc = null; |
|
451 try { |
|
452 if (docBuilder != null) { |
|
453 File file = new File(filePath); |
|
454 if (file.exists()) { |
|
455 doc = docBuilder.parse(filePath); |
|
456 } |
|
457 } |
|
458 } catch (SAXException e1) { |
|
459 e1.printStackTrace(); |
|
460 |
|
461 // Show file incorrect message |
|
462 String fileIncorrect = Messages |
|
463 .getString("TraceActivationXMLImporter.FileIncorrect"); //$NON-NLS-1$ |
|
464 TraceViewerGlobals.getTraceViewer().getDialogs().showErrorMessage( |
|
465 fileIncorrect); |
|
466 |
|
467 } catch (IOException e1) { |
|
468 e1.printStackTrace(); |
|
469 } |
|
470 return doc; |
|
471 } |
|
472 } |