|
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 * Imports Trigger configuration from XML file |
|
17 * |
|
18 */ |
|
19 package com.nokia.traceviewer.engine.preferences; |
|
20 |
|
21 import java.io.File; |
|
22 |
|
23 import org.w3c.dom.Attr; |
|
24 import org.w3c.dom.Document; |
|
25 import org.w3c.dom.Element; |
|
26 import org.w3c.dom.NamedNodeMap; |
|
27 import org.w3c.dom.Node; |
|
28 import org.w3c.dom.NodeList; |
|
29 |
|
30 import com.nokia.traceviewer.dialog.treeitem.TreeItem; |
|
31 import com.nokia.traceviewer.dialog.treeitem.TreeItemListener; |
|
32 import com.nokia.traceviewer.dialog.treeitem.TriggerTreeBaseItem; |
|
33 import com.nokia.traceviewer.dialog.treeitem.TriggerTreeItem; |
|
34 import com.nokia.traceviewer.dialog.treeitem.TriggerTreeTextItem; |
|
35 import com.nokia.traceviewer.dialog.treeitem.TriggerTreeItem.Type; |
|
36 import com.nokia.traceviewer.engine.TraceViewerGlobals; |
|
37 |
|
38 /** |
|
39 * Imports Trigger configuration from XML file |
|
40 * |
|
41 */ |
|
42 public class XMLTriggerConfigurationImporter extends |
|
43 XMLBaseConfigurationImporter { |
|
44 |
|
45 /** |
|
46 * Constructor |
|
47 * |
|
48 * @param root |
|
49 * root of the Trigger elements |
|
50 * @param configurationFileName |
|
51 * file name where to export data |
|
52 * @param pathRelative |
|
53 * tells is the path relative |
|
54 */ |
|
55 public XMLTriggerConfigurationImporter(TreeItem root, |
|
56 String configurationFileName, boolean pathRelative) { |
|
57 super(root, configurationFileName, pathRelative); |
|
58 } |
|
59 |
|
60 /** |
|
61 * Imports data |
|
62 */ |
|
63 public void importData() { |
|
64 File file = new File(filePath); |
|
65 if (file.exists()) { |
|
66 Document doc = getDocument(); |
|
67 |
|
68 if (doc != null) { |
|
69 |
|
70 // Delete old rules |
|
71 Object[] oldRules = root.getChildren(); |
|
72 for (int i = 0; i < oldRules.length; i++) { |
|
73 root.removeChild((TriggerTreeItem) oldRules[i]); |
|
74 } |
|
75 |
|
76 // Get trigger node from the XML file |
|
77 NodeList list = doc.getElementsByTagName(TRIGGER_TAG); |
|
78 Node parent = list.item(0); |
|
79 |
|
80 // Import children |
|
81 if (parent != null) { |
|
82 NodeList children = parent.getChildNodes(); |
|
83 for (int i = 0; i < children.getLength(); i++) { |
|
84 if (children.item(i) instanceof Element) { |
|
85 processChild(children.item(i), root); |
|
86 } |
|
87 } |
|
88 } |
|
89 } |
|
90 } |
|
91 } |
|
92 |
|
93 /** |
|
94 * Import this trigger rule to root |
|
95 * |
|
96 * @param node |
|
97 * Trigger node to import |
|
98 * @param root |
|
99 * Root node of trigger dialog |
|
100 */ |
|
101 private void processChild(Node node, TreeItem root) { |
|
102 String name = null; |
|
103 String text = null; |
|
104 boolean matchCase = false; |
|
105 Type triggerType = null; |
|
106 |
|
107 NamedNodeMap itemAttributes = node.getAttributes(); |
|
108 |
|
109 // Get name |
|
110 Attr nameAttr = (Attr) itemAttributes.getNamedItem(NAME_TAG); |
|
111 name = nameAttr.getValue(); |
|
112 |
|
113 // Group item |
|
114 TreeItem newItem = null; |
|
115 if (node.getNodeName().equals(GROUP_TAG)) { |
|
116 newItem = new TriggerTreeBaseItem(TraceViewerGlobals |
|
117 .getTraceViewer().getDataProcessorAccess() |
|
118 .getTriggerProcessor().getTreeItemListener(), root, name, |
|
119 TriggerTreeItem.Rule.GROUP, |
|
120 TriggerTreeItem.Type.STARTTRIGGER); |
|
121 |
|
122 // This is for Text rules |
|
123 } else if (node.getNodeName().equals(CONFIGURATION_TAG)) { |
|
124 |
|
125 // Get rule |
|
126 Attr ruleAttr = (Attr) itemAttributes.getNamedItem(RULE_TAG); |
|
127 String rule = ruleAttr.getValue(); |
|
128 |
|
129 if (rule.equals(TEXTRULE_TAG)) { |
|
130 newItem = processTextRule(node, root, name, text, matchCase, |
|
131 triggerType); |
|
132 |
|
133 } else { |
|
134 // Unknown rule, shouldn't come here |
|
135 } |
|
136 |
|
137 // Check if the rules should be kept over boot |
|
138 Attr enabledAttr = (Attr) itemAttributes.getNamedItem(ENABLED_TAG); |
|
139 if (enabledAttr != null && newItem != null) { |
|
140 String enabled = enabledAttr.getValue(); |
|
141 if (enabled.equals(YES_TAG)) { |
|
142 TraceViewerGlobals.getTraceViewer() |
|
143 .getDataProcessorAccess().getTriggerProcessor() |
|
144 .enableRule((TriggerTreeItem) newItem); |
|
145 } |
|
146 } |
|
147 |
|
148 } else { |
|
149 // Unknown rule, shouldn't come here |
|
150 } |
|
151 |
|
152 // Add the child to the parent node |
|
153 if (newItem != null) { |
|
154 root.addChild(newItem); |
|
155 } |
|
156 |
|
157 // If node was group, process it's children |
|
158 if (node.getNodeName().equals(GROUP_TAG)) { |
|
159 // Get children |
|
160 NodeList children = node.getChildNodes(); |
|
161 for (int i = 0; i < children.getLength(); i++) { |
|
162 if (children.item(i) instanceof Element) { |
|
163 processChild(children.item(i), newItem); |
|
164 } |
|
165 } |
|
166 } |
|
167 |
|
168 } |
|
169 |
|
170 /** |
|
171 * Processes text rule |
|
172 * |
|
173 * @param node |
|
174 * xml node |
|
175 * @param root |
|
176 * root of trigger rules |
|
177 * @param name |
|
178 * name of the rule |
|
179 * @param text |
|
180 * text of the rule |
|
181 * @param matchCase |
|
182 * match case value |
|
183 * @param triggerType |
|
184 * trigger type |
|
185 * @return newly created trigger item |
|
186 */ |
|
187 private TreeItem processTextRule(Node node, TreeItem root, String name, |
|
188 String text, boolean matchCase, Type triggerType) { |
|
189 String configurationFilePath = null; |
|
190 String configurationName = null; |
|
191 |
|
192 TreeItem newItem; |
|
193 // Get the text properties |
|
194 NodeList textProperties = node.getChildNodes(); |
|
195 for (int i = 0; i < textProperties.getLength(); i++) { |
|
196 if (textProperties.item(i) instanceof Element) { |
|
197 // Text |
|
198 if (textProperties.item(i).getNodeName().equals(TEXT_TAG)) { |
|
199 text = textProperties.item(i).getTextContent(); |
|
200 // Match case |
|
201 } else if (textProperties.item(i).getNodeName().equals( |
|
202 MATCHCASE_TAG)) { |
|
203 if (textProperties.item(i).getTextContent().equals(NO_TAG)) { |
|
204 matchCase = false; |
|
205 } else { |
|
206 matchCase = true; |
|
207 } |
|
208 // Trigger type |
|
209 } else if (textProperties.item(i).getNodeName().equals( |
|
210 TRIGGERTYPE_TAG)) { |
|
211 if (textProperties.item(i).getTextContent().equals( |
|
212 STARTTRIGGER_TAG)) { |
|
213 triggerType = TriggerTreeItem.Type.STARTTRIGGER; |
|
214 } else if (textProperties.item(i).getTextContent().equals( |
|
215 STOPTRIGGER_TAG)) { |
|
216 triggerType = TriggerTreeItem.Type.STOPTRIGGER; |
|
217 } else if (textProperties.item(i).getTextContent().equals( |
|
218 ACTIVATIONTRIGGER_TAG)) { |
|
219 triggerType = TriggerTreeItem.Type.ACTIVATIONTRIGGER; |
|
220 } else { |
|
221 // Unknown type, shouldn't come here |
|
222 } |
|
223 // Configuration file |
|
224 } else if (textProperties.item(i).getNodeName().equals( |
|
225 CONFIGURATION_FILE_TAG)) { |
|
226 configurationFilePath = textProperties.item(i) |
|
227 .getTextContent(); |
|
228 |
|
229 // Configuration name |
|
230 } else if (textProperties.item(i).getNodeName().equals( |
|
231 CONFIGURATION_NAME_TAG)) { |
|
232 configurationName = textProperties.item(i).getTextContent(); |
|
233 } |
|
234 } |
|
235 } |
|
236 |
|
237 // Create new item |
|
238 TreeItemListener listener = TraceViewerGlobals.getTraceViewer() |
|
239 .getDataProcessorAccess().getTriggerProcessor() |
|
240 .getTreeItemListener(); |
|
241 newItem = new TriggerTreeTextItem(listener, root, name, |
|
242 TriggerTreeItem.Rule.TEXT_RULE, text, matchCase, triggerType, |
|
243 configurationFilePath, configurationName); |
|
244 return newItem; |
|
245 } |
|
246 } |