Line 0
Link Here
|
|
|
1 |
package org.eclipse.mtj.ui.internal.preferences; |
2 |
|
3 |
import java.io.File; |
4 |
import java.io.FileInputStream; |
5 |
import java.io.IOException; |
6 |
import java.io.InputStream; |
7 |
import java.lang.reflect.InvocationTargetException; |
8 |
import java.util.Enumeration; |
9 |
import java.util.HashMap; |
10 |
import java.util.HashMap; |
11 |
import java.util.Iterator; |
12 |
import java.util.StringTokenizer; |
13 |
import java.util.Vector; |
14 |
import java.util.jar.JarEntry; |
15 |
import java.util.jar.JarFile; |
16 |
|
17 |
import javax.xml.parsers.DocumentBuilder; |
18 |
import javax.xml.parsers.DocumentBuilderFactory; |
19 |
import javax.xml.parsers.ParserConfigurationException; |
20 |
|
21 |
import org.eclipse.core.runtime.CoreException; |
22 |
import org.eclipse.core.runtime.IProgressMonitor; |
23 |
import org.eclipse.core.runtime.IStatus; |
24 |
import org.eclipse.jface.operation.IRunnableWithProgress; |
25 |
import org.eclipse.jface.preference.IPreferenceStore; |
26 |
import org.eclipse.jface.wizard.IWizardContainer; |
27 |
import org.eclipse.jface.wizard.Wizard; |
28 |
import org.eclipse.mtj.core.IMTJCoreConstants; |
29 |
import org.eclipse.mtj.core.internal.MTJCorePlugin; |
30 |
import org.eclipse.mtj.core.model.device.DeviceFactory; |
31 |
import org.eclipse.mtj.core.model.device.preprocess.DeviceSymbolDefinitionSetFactory; |
32 |
import org.eclipse.mtj.core.model.preprocessor.SymbolDefinitionSet; |
33 |
import org.eclipse.mtj.core.model.preprocessor.SymbolDefinitionSetRegistry; |
34 |
import org.eclipse.mtj.ui.internal.MTJUIPlugin; |
35 |
import org.eclipse.ui.PlatformUI; |
36 |
import org.eclipse.ui.progress.IProgressService; |
37 |
import org.w3c.dom.Document; |
38 |
import org.w3c.dom.Element; |
39 |
import org.w3c.dom.NodeList; |
40 |
import org.xml.sax.SAXException; |
41 |
|
42 |
public class SymbolDefinitionsImportWizard extends Wizard { |
43 |
|
44 |
|
45 |
private static final String J2MEPOLISH_CAPABILITY_SEPARATOR = ","; |
46 |
|
47 |
private SymbolDefinitionsImportWizardPage wizardPage; |
48 |
|
49 |
private String importDirectory = null; |
50 |
|
51 |
public SymbolDefinitionsImportWizard() { |
52 |
|
53 |
super(); |
54 |
setNeedsProgressMonitor(true); |
55 |
setWindowTitle("Import Devices"); |
56 |
|
57 |
} |
58 |
|
59 |
|
60 |
@Override |
61 |
public boolean performFinish() { |
62 |
IProgressService progressService = PlatformUI.getWorkbench().getProgressService(); |
63 |
switch(wizardPage.getImportType()) { |
64 |
case SymbolDefinitionsImportWizardPage.IMPORT_FROM_ANTENNA_JAR: |
65 |
try { |
66 |
progressService.run(true, true, getProcessImportFromAntennaJarFile()); |
67 |
} |
68 |
catch (InvocationTargetException e) { |
69 |
e.printStackTrace(); |
70 |
} |
71 |
catch (InterruptedException e) { |
72 |
e.printStackTrace(); |
73 |
} |
74 |
break; |
75 |
|
76 |
case SymbolDefinitionsImportWizardPage.IMPORT_J2MEPOLISH_FORMAT: |
77 |
importDirectory = wizardPage.getDirectory(); |
78 |
try { |
79 |
progressService.run(true, true, getProcessImportFromXMLFiles()); |
80 |
} |
81 |
catch (InvocationTargetException e) { |
82 |
e.printStackTrace(); |
83 |
} |
84 |
catch (InterruptedException e) { |
85 |
e.printStackTrace(); |
86 |
} |
87 |
break; |
88 |
case SymbolDefinitionsImportWizardPage.IMPORT_MTJ_FORMAT: |
89 |
|
90 |
break; |
91 |
} |
92 |
|
93 |
return true; |
94 |
} |
95 |
|
96 |
|
97 |
@Override |
98 |
public void addPages() { |
99 |
wizardPage = new SymbolDefinitionsImportWizardPage(); |
100 |
addPage(wizardPage); |
101 |
} |
102 |
|
103 |
|
104 |
private IRunnableWithProgress getProcessImportFromAntennaJarFile() { |
105 |
|
106 |
// The runnable to do import definitions |
107 |
return new IRunnableWithProgress() { |
108 |
public void run(IProgressMonitor monitor) { |
109 |
JarFile antennaFileJar = null; |
110 |
try { |
111 |
IPreferenceStore preferenceStore = MTJUIPlugin.getDefault().getCorePreferenceStore(); |
112 |
String antennaFile = preferenceStore.getString(IMTJCoreConstants.PREF_ANTENNA_JAR); |
113 |
|
114 |
// search devices.xml & groups.xml from Antenna Jar file |
115 |
InputStream devicesInputStream = null; |
116 |
InputStream groupsInputStream = null; |
117 |
antennaFileJar = new JarFile(new File(antennaFile)); |
118 |
Enumeration<JarEntry> e = antennaFileJar.entries(); |
119 |
while(e.hasMoreElements()) { |
120 |
JarEntry entry = e.nextElement(); |
121 |
if(entry.getName().toLowerCase().equals("devices.xml")) { |
122 |
devicesInputStream = antennaFileJar.getInputStream(entry); |
123 |
} |
124 |
if(entry.getName().toLowerCase().equals("groups.xml")) { |
125 |
groupsInputStream = antennaFileJar.getInputStream(entry); |
126 |
} |
127 |
} |
128 |
if(devicesInputStream != null && groupsInputStream != null) { |
129 |
try { |
130 |
importFromJ2MEPolishFormat(devicesInputStream, groupsInputStream); |
131 |
} |
132 |
catch(Exception ex) { |
133 |
ex.printStackTrace(); |
134 |
} |
135 |
} |
136 |
} |
137 |
catch (IOException e) { |
138 |
e.printStackTrace(); |
139 |
} |
140 |
finally { |
141 |
try { |
142 |
antennaFileJar.close(); |
143 |
} |
144 |
catch(Exception e) { |
145 |
} |
146 |
} |
147 |
|
148 |
} |
149 |
}; |
150 |
|
151 |
} |
152 |
|
153 |
private IRunnableWithProgress getProcessImportFromXMLFiles() { |
154 |
|
155 |
// The runnable to do import definitions |
156 |
return new IRunnableWithProgress() { |
157 |
public void run(IProgressMonitor monitor) { |
158 |
InputStream devicesInputStream = null; |
159 |
InputStream groupsInputStream = null; |
160 |
try { |
161 |
// search devices.xml & groups.xml from Antenna Jar file |
162 |
devicesInputStream = new FileInputStream(new File(importDirectory + File.separator + "devices.xml")); |
163 |
groupsInputStream = new FileInputStream(new File(importDirectory + File.separator + "groups.xml")); |
164 |
if(devicesInputStream != null && groupsInputStream != null) { |
165 |
try { |
166 |
importFromJ2MEPolishFormat(devicesInputStream, groupsInputStream); |
167 |
} |
168 |
catch(Exception ex) { |
169 |
ex.printStackTrace(); |
170 |
} |
171 |
} |
172 |
} |
173 |
catch (IOException e) { |
174 |
e.printStackTrace(); |
175 |
} |
176 |
finally { |
177 |
try { |
178 |
devicesInputStream.close(); |
179 |
} |
180 |
catch(Exception e) { |
181 |
} |
182 |
try { |
183 |
groupsInputStream.close(); |
184 |
} |
185 |
catch(Exception e) { |
186 |
} |
187 |
} |
188 |
|
189 |
} |
190 |
}; |
191 |
|
192 |
} |
193 |
|
194 |
|
195 |
/* |
196 |
* Functions for importFromJ2MEPolishFormat: |
197 |
* - importFromJ2MEPolishFormat |
198 |
* - prepareGroups |
199 |
* - getGroupCapabilities |
200 |
* - copyCapabilities |
201 |
* - copyCapability |
202 |
* - mergeCapabilities |
203 |
*/ |
204 |
private void importFromJ2MEPolishFormat(InputStream devicesInputStream, InputStream groupsInputStream) { |
205 |
|
206 |
try { |
207 |
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
208 |
DocumentBuilder db = dbf.newDocumentBuilder(); |
209 |
|
210 |
Document devicesDOM = db.parse(devicesInputStream); |
211 |
Document groupsDOM = db.parse(groupsInputStream); |
212 |
|
213 |
HashMap<String, Element> groups = prepareGroups(groupsDOM); |
214 |
|
215 |
NodeList nl = devicesDOM.getElementsByTagName("device"); |
216 |
for(int i = 0; i < nl.getLength(); i++) { |
217 |
|
218 |
HashMap<String, String> capabilities = new HashMap<String, String>(); |
219 |
HashMap<String, String> groupCapabilities; |
220 |
String features; |
221 |
|
222 |
Element deviceElement = (Element) nl.item(i); |
223 |
String name = ((Element) deviceElement.getElementsByTagName("identifier").item(0)).getTextContent(); |
224 |
|
225 |
Element featuresElement = (Element) deviceElement.getElementsByTagName("features").item(0); |
226 |
if(featuresElement != null) { |
227 |
features = featuresElement.getTextContent(); |
228 |
capabilities.put("features", features); |
229 |
} |
230 |
|
231 |
Element groupElement = (Element) deviceElement.getElementsByTagName("groups").item(0); |
232 |
if(groupElement != null) { |
233 |
StringTokenizer st = new StringTokenizer(groupElement.getTextContent(), J2MEPOLISH_CAPABILITY_SEPARATOR); |
234 |
while(st.hasMoreTokens()) { |
235 |
String group = st.nextToken().trim(); |
236 |
groupCapabilities = getGroupCapabilities(groups, group); |
237 |
copyCapabilities(capabilities, groupCapabilities); |
238 |
} |
239 |
capabilities.put("groups", groupElement.getTextContent().toLowerCase()); |
240 |
} |
241 |
|
242 |
NodeList capabilitiesNL = deviceElement.getElementsByTagName("capability"); |
243 |
for(int j = 0; j < capabilitiesNL.getLength(); j++) { |
244 |
Element capabilityElement = (Element) capabilitiesNL.item(j); |
245 |
copyCapability(capabilities, capabilityElement.getAttribute("name"), capabilityElement.getAttribute("value")); |
246 |
} |
247 |
|
248 |
capabilities.put(name, "true"); |
249 |
|
250 |
StringTokenizer st = new StringTokenizer(name, J2MEPOLISH_CAPABILITY_SEPARATOR); |
251 |
while(st.hasMoreTokens()) { |
252 |
String device = st.nextToken().trim(); |
253 |
device = device.replace('/', '_'); |
254 |
device = device.replace('(', '_'); |
255 |
device = device.replace(')', '_'); |
256 |
|
257 |
SymbolDefinitionSet definitionSet = DeviceSymbolDefinitionSetFactory.createSymbolDefinitionSet(device, capabilities); |
258 |
SymbolDefinitionSetRegistry.singleton.addDefinitionSet(definitionSet); |
259 |
} |
260 |
} |
261 |
SymbolDefinitionSetRegistry.singleton.store(); |
262 |
|
263 |
} |
264 |
catch(Exception e) { |
265 |
MTJCorePlugin.log(IStatus.ERROR, e); |
266 |
} |
267 |
|
268 |
} |
269 |
|
270 |
private HashMap<String, Element> prepareGroups(Document groupsDOM) { |
271 |
|
272 |
HashMap<String, Element> result = new HashMap<String, Element>(); |
273 |
|
274 |
NodeList nl = groupsDOM.getElementsByTagName("group"); |
275 |
for(int i = 0; i < nl.getLength(); i++) { |
276 |
Element groupElement = (Element) nl.item(i); |
277 |
String name = ((Element) groupElement.getElementsByTagName("name").item(0)).getTextContent(); |
278 |
result.put(name, groupElement); |
279 |
} |
280 |
|
281 |
return result; |
282 |
} |
283 |
|
284 |
private HashMap<String, String> getGroupCapabilities(HashMap<String, Element> groups, String name) { |
285 |
|
286 |
Element groupElement = groups.get(name); |
287 |
|
288 |
HashMap<String, String> result = new HashMap<String, String>(); |
289 |
|
290 |
NodeList nl = groupElement.getElementsByTagName("parent"); |
291 |
if(nl != null && nl.getLength() > 0) { |
292 |
Element parent = (Element) nl.item(0); |
293 |
String parentValue = parent.getTextContent(); |
294 |
if(parentValue != null && !parent.getTextContent().equals("")) { |
295 |
HashMap<String, String> parentCapabilities = getGroupCapabilities(groups, parent.getTextContent()); |
296 |
copyCapabilities(result, parentCapabilities); |
297 |
} |
298 |
} |
299 |
|
300 |
|
301 |
nl = groupElement.getElementsByTagName("capability"); |
302 |
for(int i = 0; i < nl.getLength(); i++) { |
303 |
Element capabilityElement = (Element) nl.item(i); |
304 |
result.put(capabilityElement.getAttribute("name"), capabilityElement.getAttribute("value")); |
305 |
} |
306 |
|
307 |
return result; |
308 |
|
309 |
} |
310 |
|
311 |
private void copyCapabilities(HashMap<String, String> capabilities, HashMap<String, String> parentCapabilities) { |
312 |
Iterator<String> i = parentCapabilities.keySet().iterator(); |
313 |
while(i.hasNext()) { |
314 |
String capability = i.next(); |
315 |
copyCapability(capabilities, capability, parentCapabilities.get(capability)); |
316 |
} |
317 |
} |
318 |
|
319 |
|
320 |
private void copyCapability(HashMap<String, String> capabilities, String name, String capability) { |
321 |
|
322 |
String currentCapabilityValue = capabilities.get(name); |
323 |
if(currentCapabilityValue == null || currentCapabilityValue.equals("")) { |
324 |
capabilities.put(name, capability); |
325 |
} |
326 |
else { |
327 |
String mergedCapabilities = mergeCapabilities(currentCapabilityValue, capability); |
328 |
capabilities.put(name, mergedCapabilities); |
329 |
} |
330 |
|
331 |
} |
332 |
|
333 |
private String mergeCapabilities(String currentCapability, String newCapability) { |
334 |
|
335 |
Vector<String> v = new Vector<String>(); |
336 |
StringTokenizer st = new StringTokenizer(currentCapability, J2MEPOLISH_CAPABILITY_SEPARATOR); |
337 |
while(st.hasMoreTokens()) { |
338 |
v.add(st.nextToken().trim()); |
339 |
} |
340 |
st = new StringTokenizer(newCapability, J2MEPOLISH_CAPABILITY_SEPARATOR); |
341 |
while(st.hasMoreTokens()) { |
342 |
String capability = st.nextToken().trim(); |
343 |
if(v.indexOf(capability) == -1) { |
344 |
v.add(capability); |
345 |
} |
346 |
} |
347 |
String result = ""; |
348 |
for(int i = 0; i < v.size(); i++) { |
349 |
if(i > 0) { |
350 |
result = result.concat(","); |
351 |
} |
352 |
result = result.concat(v.elementAt(i)); |
353 |
} |
354 |
|
355 |
return result; |
356 |
} |
357 |
} |