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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
|
// **********************************************************************
//
// Copyright (c) 2003-2012 ZeroC, Inc. All rights reserved.
//
// This plug-in is provided to you under the terms and conditions
// of the Eclipse Public License Version 1.0 ("EPL"). A copy of
// the EPL is available at http://www.eclipse.org/legal/epl-v10.html.
//
// **********************************************************************
package com.zeroc.slice2javaplugin.internal;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.ui.console.MessageConsoleStream;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;
import com.zeroc.slice2javaplugin.Activator;
public class Dependencies
{
public Dependencies(IProject project, Set<IFile> LprojectResources, MessageConsoleStream err)
{
_project = project;
_projectResources = LprojectResources;
_err = err;
// Build a map of location to project resource.
for(IFile f : _projectResources)
{
_locationToResource.put(f.getLocation(), f);
}
}
/**
*
* @param allDependencies The string of all dependencies.
* @throws CoreException
*/
public void updateDependencies(String allDependencies)
throws CoreException
{
Slice2JavaDependenciesParser parser = new Slice2JavaDependenciesParser();
try
{
InputStream in = new ByteArrayInputStream(allDependencies.getBytes());
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new BufferedInputStream(in));
parser.visit(doc);
}
catch(SAXException e)
{
throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
"internal error reading dependencies", e));
}
catch(ParserConfigurationException e)
{
throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
"internal error reading dependencies", e));
}
catch(IOException e)
{
throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
"internal error reading dependencies", e));
}
for(Map.Entry<String, List<String>> entry : parser.dependencies.entrySet())
{
Path sourcePath = new Path(entry.getKey());
assert sourcePath.isAbsolute();
IFile sourceFile = _locationToResource.get(sourcePath);
if(sourceFile == null)
{
if(_err != null)
{
_err.println("Dependencies: ignoring non-project resource " + sourcePath.toString());
}
// This should not occur.
continue;
}
for(String s : entry.getValue())
{
IFile f = getProjectResource(new Path(s));
// Ignore any resources not in the project.
if(f != null)
{
Set<IFile> dependents = reverseSliceSliceDependencies.get(f);
if(dependents == null)
{
dependents = new HashSet<IFile>();
reverseSliceSliceDependencies.put(f, dependents);
}
dependents.add(sourceFile);
}
}
Set<IFile> dependents = new HashSet<IFile>();
sliceSliceDependencies.put(sourceFile, dependents);
for(String s : entry.getValue())
{
IFile f = getProjectResource(new Path(s));
// Ignore any resources not in the project.
if(f != null)
{
dependents.add(f);
}
}
}
}
private IFile getProjectResource(Path path)
{
IFile f = null;
if(path.isAbsolute())
{
f = _locationToResource.get(path);
}
else
{
f = _project.getFile(path.toString());
if(!f.exists())
{
f = null;
}
}
if(_projectResources.contains(f))
{
return f;
}
return null;
}
public void read()
throws CoreException
{
IFileStore dependencies = getDependenciesStore();
if(!dependencies.fetchInfo(EFS.NONE, null).exists())
{
return;
}
InputStream in = dependencies.openInputStream(EFS.NONE, null);
try
{
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new BufferedInputStream(in));
DependenciesParser parser = new DependenciesParser(_project);
parser.visit(doc);
sliceSliceDependencies = parser.sliceSliceDependencies;
reverseSliceSliceDependencies = parser.reverseSliceSliceDependencies;
sliceJavaDependencies = parser.sliceJavaDependencies;
errorSliceFiles = parser.errorSliceFiles;
}
catch(SAXException e)
{
throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
"internal error reading dependencies", e));
}
catch(ParserConfigurationException e)
{
throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
"internal error reading dependencies", e));
}
catch(IOException e)
{
throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
"internal error reading dependencies", e));
}
}
public void write()
throws CoreException
{
// Create a DOM of the map.
Document doc = null;
try
{
doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
}
catch(ParserConfigurationException e)
{
throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
"internal error writing dependencies", e));
}
Element root = doc.createElement("dependencies");
doc.appendChild(root);
writeDependencies(sliceSliceDependencies, doc, "sliceSliceDependencies", root);
writeDependencies(reverseSliceSliceDependencies, doc, "reverseSliceSliceDependencies", root);
writeDependencies(sliceJavaDependencies, doc, "sliceJavaDependencies", root);
writeErrorSliceFiles(errorSliceFiles, doc, "errorSliceFiles", root);
// Write the DOM to the dependencies.xml file.
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = null;
try
{
trans = transfac.newTransformer();
}
catch(TransformerConfigurationException e)
{
throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
"internal error writing dependencies", e));
}
// tf.setAttribute("indent-number", 4);
// trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
trans.setOutputProperty(OutputKeys.ENCODING, "UTF8");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
trans.setOutputProperty(OutputKeys.METHOD, "XML");
IFileStore dependencies = getDependenciesStore();
OutputStream out = dependencies.openOutputStream(EFS.NONE, null);
StreamResult result = new StreamResult(new BufferedOutputStream(out));
DOMSource source = new DOMSource(doc);
try
{
trans.transform(source, result);
}
catch(TransformerException e)
{
throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
"internal error writing dependencies", e));
}
try
{
out.close();
}
catch(IOException e)
{
throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
"internal error writing dependencies", e));
}
}
private void writeErrorSliceFiles(Set<IFile> s, Document doc, String name, Element root)
{
Element jsd = doc.createElement(name);
root.appendChild(jsd);
for(IFile f : s)
{
Element elem = doc.createElement("file");
jsd.appendChild(elem);
Text text = doc.createTextNode(f.getProjectRelativePath().toString());
elem.appendChild(text);
}
}
private void writeDependencies(Map<IFile, Set<IFile>> map, Document doc, String name, Element root)
{
Element jsd = doc.createElement(name);
root.appendChild(jsd);
Iterator<Map.Entry<IFile, Set<IFile>>> p = map.entrySet().iterator();
while(p.hasNext())
{
Map.Entry<IFile, Set<IFile>> e = p.next();
Element entry = doc.createElement("entry");
jsd.appendChild(entry);
Element key = doc.createElement("key");
entry.appendChild(key);
Text text = doc.createTextNode(e.getKey().getProjectRelativePath().toString());
key.appendChild(text);
Element value = doc.createElement("value");
entry.appendChild(value);
Iterator<IFile> q = e.getValue().iterator();
while(q.hasNext())
{
IFile f = q.next();
Element elem = doc.createElement("file");
value.appendChild(elem);
text = doc.createTextNode(f.getProjectRelativePath().toString());
elem.appendChild(text);
}
}
}
private IFileStore getDependenciesStore()
throws CoreException
{
IPath name = new Path(_project.getName());
IFileStore store = EFS.getLocalFileSystem().getStore(Activator.getDefault().getStateLocation()).getFileStore(
name);
if(!store.fetchInfo(EFS.NONE, null).exists())
{
store.mkdir(EFS.NONE, null);
}
return store.getFileStore(new Path("dependencies.xml"));
}
private static class Slice2JavaDependenciesParser
{
Map<String, List<String>> dependencies = new java.util.HashMap<String, List<String>>();
private Node findNode(Node n, String qName)
throws SAXException
{
NodeList children = n.getChildNodes();
for(int i = 0; i < children.getLength(); ++i)
{
Node child = children.item(i);
if(child.getNodeType() == Node.ELEMENT_NODE && child.getNodeName().equals(qName))
{
return child;
}
}
throw new SAXException("no such node: " + qName);
}
private void visitDependencies(Node n) throws SAXException
{
NodeList children = n.getChildNodes();
for(int i = 0; i < children.getLength(); ++i)
{
if(children.item(i).getNodeType() == Node.ELEMENT_NODE && children.item(i).getNodeName().equals("source"))
{
String source = ((Element)children.item(i)).getAttribute("name");
if(source.length() == 0)
{
throw new SAXException("empty name attribute");
}
List<String> dependsOn = visitDependsOn(children.item(i));
dependencies.put(source, dependsOn);
}
}
}
private List<String> visitDependsOn(Node source) throws SAXException
{
List<String> depends = new ArrayList<String>();
NodeList dependencies = source.getChildNodes();
for(int j = 0; j < dependencies.getLength(); ++j)
{
if(dependencies.item(j).getNodeType() == Node.ELEMENT_NODE && dependencies.item(j).getNodeName().equals("dependsOn"))
{
Element dependsOn = (Element)dependencies.item(j);
String name = dependsOn.getAttribute("name");
if(name.length() == 0)
{
throw new SAXException("empty name attribute");
}
depends.add(name);
}
}
return depends;
}
public void visit(Node doc) throws SAXException
{
Node n = findNode(doc, "dependencies");
visitDependencies(n);
}
}
private static class DependenciesParser
{
private IProject _project;
Map<IFile, Set<IFile>> sliceSliceDependencies = new java.util.HashMap<IFile, Set<IFile>>();
Map<IFile, Set<IFile>> reverseSliceSliceDependencies = new java.util.HashMap<IFile, Set<IFile>>();
Map<IFile, Set<IFile>> sliceJavaDependencies = new java.util.HashMap<IFile, Set<IFile>>();
Set<IFile> errorSliceFiles = new java.util.HashSet<IFile>();
private Node findNode(Node n, String qName)
throws SAXException
{
NodeList children = n.getChildNodes();
for(int i = 0; i < children.getLength(); ++i)
{
Node child = children.item(i);
if(child.getNodeType() == Node.ELEMENT_NODE && child.getNodeName().equals(qName))
{
return child;
}
}
throw new SAXException("no such node: " + qName);
}
private String getText(Node n)
throws SAXException
{
NodeList children = n.getChildNodes();
if(children.getLength() == 1 && children.item(0).getNodeType() == Node.TEXT_NODE)
{
return children.item(0).getNodeValue();
}
throw new SAXException("no text element");
}
private List<String> processFiles(Node n)
throws SAXException
{
List<String> files = new ArrayList<String>();
NodeList children = n.getChildNodes();
for(int i = 0; i < children.getLength(); ++i)
{
Node child = children.item(i);
if(child.getNodeType() == Node.ELEMENT_NODE && child.getNodeName().equals("file"))
{
files.add(getText(child));
}
}
return files;
}
public void visitDependencies(Map<IFile, Set<IFile>> map, Node n)
throws SAXException
{
NodeList children = n.getChildNodes();
for(int i = 0; i < children.getLength(); ++i)
{
Node child = children.item(i);
if(child.getNodeType() == Node.ELEMENT_NODE && child.getNodeName().equals("entry"))
{
IFile key = _project.getFile(new Path(getText(findNode(child, "key"))));
Node value = findNode(child, "value");
List<String> files = processFiles(value);
Set<IFile> f = new HashSet<IFile>();
for(String s : files)
{
f.add(_project.getFile(new Path(s)));
}
map.put(key, f);
}
}
}
public void visitErrorList(Set<IFile> s, Node n) throws SAXException
{
NodeList children = n.getChildNodes();
for(int i = 0; i < children.getLength(); ++i)
{
Node child = children.item(i);
if(child.getNodeType() == Node.ELEMENT_NODE && child.getNodeName().equals("file"))
{
s.add(_project.getFile(new Path(getText(child))));
}
}
}
public void visit(Node doc)
throws SAXException
{
Node dependencies = findNode(doc, "dependencies");
visitDependencies(sliceSliceDependencies, findNode(dependencies, "sliceSliceDependencies"));
visitDependencies(reverseSliceSliceDependencies, findNode(dependencies, "reverseSliceSliceDependencies"));
visitDependencies(sliceJavaDependencies, findNode(dependencies, "sliceJavaDependencies"));
try
{
visitErrorList(errorSliceFiles, findNode(dependencies, "errorSliceFiles"));
}
catch(SAXException e)
{
// Optional.
}
}
DependenciesParser(IProject project)
{
_project = project;
}
}
// A map of slice to dependencies.
//
// sliceSliceDependencies is the set of slice files that depend on the IFile
// (the output of slice2java --depend).
//
// _reverseSliceSliceDependencies is the reverse.
public Map<IFile, Set<IFile>> sliceSliceDependencies = new java.util.HashMap<IFile, Set<IFile>>();
public Map<IFile, Set<IFile>> reverseSliceSliceDependencies = new java.util.HashMap<IFile, Set<IFile>>();
// A map of slice file to java source files.
public Map<IFile, Set<IFile>> sliceJavaDependencies = new java.util.HashMap<IFile, Set<IFile>>();
// A set of slice files that have not, or cannot be built.
public Set<IFile> errorSliceFiles = new java.util.HashSet<IFile>();
private IProject _project;
private MessageConsoleStream _err;
private Set<IFile> _projectResources;
private Map<IPath, IFile> _locationToResource = new HashMap<IPath, IFile>();
}
|