/
ImportCandidates.java
784 lines (699 loc) · 24.6 KB
/
ImportCandidates.java
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
/*
* Copyright 2009 Glencoe Software, Inc. All rights reserved.
* Use is subject to license terms supplied in LICENSE.txt
*/
package ome.formats.importer;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static omero.rtypes.rint;
import static omero.rtypes.rstring;
import loci.formats.FileInfo;
import loci.formats.FormatTools;
import loci.formats.IFormatReader;
import loci.formats.MissingLibraryException;
import loci.formats.UnknownFormatException;
import loci.formats.UnsupportedCompressionException;
import loci.formats.in.DefaultMetadataOptions;
import loci.formats.in.MetadataLevel;
import ome.formats.ImageNameMetadataStore;
import ome.formats.importer.util.ErrorHandler;
import org.apache.commons.io.DirectoryWalker;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Utility class which given any {@link File} object will determine the correct
* number and members of a given import. This facility permits iterating over a
* directory.
*
* This class is NOT thread-safe.
*
* @since Beta4.1
*/
public class ImportCandidates extends DirectoryWalker
{
/**
* Event raised during a pass through the directory structure given to
* {@link ImportCandidates}. A {@link SCANNING} event will not necessarily
* be raised for every file or directory, but the values will be valid for
* each event.
*
* If {@link #totalFiles} is less than 0, then the directory is currently being
* scanned and the count is unknown. Once {@link #totalFiles} is positive,
* it will remain constant.
*
* If {@link #cancel()} is called, then directory searching will cease. The
* {@link ImportCandidates} instance will be left with <em>no</em>
* {@link ImportContainer}s.
*/
public static class SCANNING extends ImportEvent
{
public final File file;
public final int depth;
public final int numFiles;
public final int totalFiles;
private boolean cancel = false;
public SCANNING(File file, int depth, int numFiles, int totalFiles)
{
this.file = file;
this.depth = depth;
this.numFiles = numFiles;
this.totalFiles = totalFiles;
}
/**
* Can be called to cancel the current action.
*/
public void cancel()
{
this.cancel = true;
}
public String toLog()
{
int l = file.toString().length() - 16;
if (l < 0)
{
l = 0;
}
String f = file.toString().substring(l);
return super.toLog() + String.format(": Depth:%s Num: %4s Tot: %4s File: %s",
depth, numFiles, (totalFiles < 0 ? "n/a" : totalFiles), f);
}
}
/**
* Marker exception raised if the {@link SCANNING#cancel()} method is
* called by an {@link IObserver} instance.
*/
public static class CANCEL extends RuntimeException {
private static final long serialVersionUID = 1L;};
final private static Logger log = LoggerFactory.getLogger(ImportCandidates.class);
final public static int DEPTH = Integer.valueOf(
System.getProperty("omero.import.depth","4"));
final public static MetadataLevel METADATA_LEVEL =
MetadataLevel.valueOf(System.getProperty(
"omero.import.metadata.level","MINIMUM"));
final private IObserver observer;
final private OMEROWrapper reader;
final private Set<String> allFiles = new HashSet<String>();
final private Map<String, List<String>> usedBy = new LinkedHashMap<String, List<String>>();
final private List<ImportContainer> containers = new ArrayList<ImportContainer>();
final private long start = System.currentTimeMillis();
/**
* Time taken for {@link IFormatReader#setId()}
*/
long readerTime = 0;
/**
* Current count of calls to {@link IFormatReader#setId()}.
*/
int setids = 0;
/**
* Number of times UNKNOWN_EVENT was raised
*/
int unknown = 0;
/**
* Current count of files processed. This will be incremented in two phases:
* once during directory counting, and once during parsing.
*/
int count = 0;
/**
* Total number of files which have been / will be examined. During the
* first pass, this value is negative.
*/
int total = -1;
/**
* Whether or not one of the {@link SCANNING} events had {@link SCANNING#cancel()}
* called.
*/
boolean cancelled = false;
/**
* Calls {@link #ImportCandidates(int, OMEROWrapper, String[], IObserver)}
* with {@link #DEPTH} as the first argument.
*
* @param reader
* instance used for parsing each of the paths. Not used once the
* constructor completes.
* @param paths
* file paths which are searched. May be directories.
* @param observer
* {@link IObserver} which will monitor any exceptions during
* {@link OMEROWrapper#setId(String)}. Otherwise no error
* reporting takes place.
*/
public ImportCandidates(OMEROWrapper reader, String[] paths,
IObserver observer)
{
this(DEPTH, reader, paths, observer);
}
/**
* Main constructor which starts depth-first descent into all the paths
* and permits a descent to the given depth.
*
* @param depth
* number of directory levels to search down.
* @param reader
* instance used for parsing each of the paths. Not used once the
* constructor completes.
* @param paths
* file paths which are searched. May be directories.
* @param observer
* {@link IObserver} which will monitor any exceptions during
* {@link OMEROWrapper#setId(String)}. Otherwise no error
* reporting takes place.
*/
public ImportCandidates(int depth, OMEROWrapper reader, String[] paths,
IObserver observer)
{
super(TrueFileFilter.INSTANCE, depth);
this.reader = reader;
this.observer = observer;
log.info(String.format("Depth: %s Metadata Level: %s", depth,
METADATA_LEVEL));
if (paths != null && paths.length == 2 && "".equals(paths[0])
&& "".equals(paths[1]))
{
// Easter egg for testing.
// groups is not null, therefore usage() won't need to be
// called.
System.exit(0);
return;
}
if (paths == null || paths.length == 0)
{
return;
}
Groups g;
try {
execute(paths);
total = count;
count = 0;
execute(paths);
g = new Groups(usedBy);
g.parse(containers);
long totalElapsed = System.currentTimeMillis() - start;
log.info(String.format("%s file(s) parsed into "
+ "%s group(s) with %s call(s) to setId in "
+ "%sms. (%sms total) [%s unknowns]", this.total, size(), this.setids,
readerTime, totalElapsed, unknown));
} catch (CANCEL c)
{
log.info(String.format("Cancelling search after %sms "
+ "with %s containers found (%sms in %s calls to setIds)",
(System.currentTimeMillis() - start), containers.size(),
readerTime, setids));
containers.clear();
cancelled = true;
g = null;
total = -1;
count = -1;
}
}
/**
* Prints the "standard" representation of the groups, which is parsed by
* other software layers. The format is: 1) any empty lines are ignored, 2)
* any blocks of comments separate groups, 3) each group is begun by the
* "key", 4) all other files in a group will also be imported.
*
* The ordering of the used files is taken into account.
*/
/*
* Similar logic is contained in Groups.print() below but that does not
* take the ordering of the used files into account.
*/
public void print()
{
if (containers == null)
{
return;
}
for (ImportContainer container : containers)
{
System.out.println("#======================================");
System.out.println(String.format(
"# Group: %s SPW: %s Reader: %s", container.getFile(),
container.getIsSPW(), container.getReader()));
for (String file : container.getUsedFiles())
{
System.out.println(file);
}
}
}
public void printYaml() {
if (containers == null) {
return;
}
System.out.println("---");
for (ImportContainer container : containers) {
System.out.println("- group: " + container.getFile());
System.out.println(" spw: " + container.getIsSPW());
System.out.println(" reader: " + container.getReader());
System.out.println(" files:");
for (String file : container.getUsedFiles())
{
System.out.println(" - " + file);
}
}
}
/**
* @return containers size
*/
public int size()
{
return containers.size();
}
/**
* @return if import was cancelled
*/
public boolean wasCancelled()
{
return cancelled;
}
/**
* @return array of string paths for files in containers
*/
public List<String> getPaths()
{
List<String> paths = new ArrayList<String>();
for (ImportContainer i : containers)
{
paths.add(i.getFile().getAbsolutePath());
}
return paths;
}
/**
* Retrieve reader type for file specified in path
*
* @param path - absolute path for container
* @return reader type
*/
public String getReaderType(String path)
{
for (ImportContainer i : containers) {
if (i.getFile().getAbsolutePath().equals(path)) {
return i.getReader();
}
}
throw new RuntimeException("Unfound reader for: " + path);
}
/**
* Return string of files used by container item at path
*
* @param path - absolute path for container
* @return string array of used files
*/
public String[] getUsedFiles(String path)
{
for (ImportContainer i : containers)
{
if (i.getFile().getAbsolutePath().equals(path))
{
return i.getUsedFiles();
}
}
throw new RuntimeException("Unfound reader for: " + path);
}
/**
* @return all containers as an array list
*/
public List<ImportContainer> getContainers()
{
return new ArrayList<ImportContainer>(containers);
}
/**
* Method called during
* {@link ImportCandidates#ImportCandidates(OMEROWrapper, String[], IObserver)}
* to operate on all the given paths. This will be called twice: once
* without reading the files, and once (with the known total) using
* {@link #reader}
*
* @param paths
*/
protected void execute(String[] paths)
{
for (String string : paths)
{
try {
File f = new File(string);
if (f.isDirectory())
{
walk(f, null);
} else
{
handleFile(f, 0, null);
}
// Forcing an event for each path, so that at least one
// event is raised per file despite the count of handlefile.
scanWithCancel(f, 0);
} catch (IOException e)
{
throw new RuntimeException(e);
}
}
}
/**
* Return an import container for a single file
* @param file - single file
* @return importer container
*/
protected ImportContainer singleFile(File file, ImportConfig config)
{
if (file == null) {
// Can't do anything about it.
return null;
}
final String path = file.getAbsolutePath();
if (!file.exists() || !file.canRead()) {
safeUpdate(new ErrorHandler.UNREADABLE_FILE(path,
new java.io.FileNotFoundException(path), this));
return null;
}
String format = null;
String[] usedFiles = new String[] { path };
long start = System.currentTimeMillis();
try {
try {
setids++;
reader.close();
reader.setMetadataStore(new ImageNameMetadataStore());
reader.setMetadataOptions(
new DefaultMetadataOptions(METADATA_LEVEL));
reader.setId(path);
format = reader.getFormat();
usedFiles = getOrderedFiles();
String[] domains = reader.getReader().getDomains();
boolean isSPW = Arrays.asList(domains).contains(FormatTools.HCS_DOMAIN);
final String readerClassName = reader.unwrap().getClass().getCanonicalName();
ImportContainer ic = new ImportContainer(config,
file, null, null,
readerClassName, usedFiles, isSPW);
ic.setDoThumbnails(config.doThumbnails.get());
ic.setNoStatsInfo(config.noStatsInfo.get());
String configImageName = config.userSpecifiedName.get();
ic.setUserSpecifiedName(configImageName);
ic.setUserSpecifiedDescription(config.userSpecifiedDescription.get());
ic.setCustomAnnotationList(config.annotations.get());
return ic;
} finally
{
readerTime += (System.currentTimeMillis() - start);
reader.close();
}
} catch (UnsupportedCompressionException uce)
{
unknown++;
// Handling as UNKNOWN_FORMAT for 4.3.0
safeUpdate(new ErrorHandler.UNKNOWN_FORMAT(path, uce, this));
} catch (UnknownFormatException ufe)
{
unknown++;
safeUpdate(new ErrorHandler.UNKNOWN_FORMAT(path, ufe, this));
} catch (MissingLibraryException mle)
{
safeUpdate(new ErrorHandler.MISSING_LIBRARY(path, mle, usedFiles, format));
} catch (Throwable t)
{
Exception e = null;
if (t instanceof Exception) {
e = (Exception) t;
}
else {
e = new Exception(t);
}
safeUpdate(new ErrorHandler.FILE_EXCEPTION(path, e, usedFiles, format));
}
return null;
}
/**
* Retrieves Image names for each image that Bio-Formats has detected.
* @return a list of Image names, in the order of <i>series</i>.
*/
private List<String> getImageNames() {
List<String> toReturn = new ArrayList<String>();
Map<Integer, String> imageNames = ((ImageNameMetadataStore)
reader.getMetadataStore()).getImageNames();
for (int i = 0; i < reader.getSeriesCount(); i++) {
toReturn.add(imageNames.get(i));
}
return toReturn;
}
/**
* This method uses the {@link FileInfo#usedToInitialize} flag to re-order
* used files. All files which can be used to initialize a fileset are
* returned first.
*/
private String[] getOrderedFiles() {
FileInfo[] infos = reader.getAdvancedUsedFiles(false);
String[] usedFiles = new String[infos.length];
int count = 0;
for (int i = 0; i < usedFiles.length; i++) {
if (infos[i].usedToInitialize) {
usedFiles[count++] = infos[i].filename;
}
}
for (int i = 0; i < usedFiles.length; i++) {
if (!infos[i].usedToInitialize) {
usedFiles[count++] = infos[i].filename;
}
}
return usedFiles;
}
/**
* @param f
* @param d
* @throws CANCEL
*/
private void scanWithCancel(File f, int d) throws CANCEL{
SCANNING s = new SCANNING(f, d, count, total);
safeUpdate(s);
if (s.cancel) {
throw new CANCEL();
}
}
/**
* Update observers with event
*
* @param event
*/
private void safeUpdate(ImportEvent event) {
try {
observer.update(null, event);
} catch (Exception ex) {
log.error(
String.format("Error on %s with %s", observer, event),
ex);
}
}
/**
* Handle a file import
*
* @param file the selected file
* @param depth the depth of the scan
* @param collection the result objects, ignored
*/
@Override
public void handleFile(File file, int depth, Collection collection) {
count++;
// Our own filtering
if (file.getName().startsWith(".")) {
return; // Omitting dot files.
}
// If this is the 100th file, publish an event
if (count%100 == 0) {
scanWithCancel(file, depth);
}
// If this is just a count, return
if (total < 0) {
return;
}
// Optimization.
if (allFiles.contains(file.getAbsolutePath())) {
return;
}
ImportContainer info = singleFile(file, reader.getConfig());
if (info == null) {
return;
}
containers.add(info);
allFiles.addAll(Arrays.asList(info.getUsedFiles()));
for (String string : info.getUsedFiles()) {
List<String> users = usedBy.get(string);
if (users == null) {
users = new ArrayList<String>();
usedBy.put(string, users);
}
users.add(file.getAbsolutePath());
}
}
/**
* The {@link Groups} class serves as an algorithm for sorting the usedBy
* map from the {@link ImportCandidates#walk(File, Collection)} method.
* These objects should never leave the outer class.
*
* It is important that the Groups keep their used files ordered.
* @see ImportCandidates#getOrderedFiles()
*/
private static class Groups {
private class Group {
String key;
List<String> theyUseMe;
List<String> iUseThem;
public Group(String key) {
this.key = key;
this.theyUseMe = new ArrayList<String>(usedBy.get(key));
this.theyUseMe.remove(key);
this.iUseThem = new ArrayList<String>();
for (Map.Entry<String, List<String>> entry : usedBy.entrySet()) {
if (entry.getValue().contains(key)) {
iUseThem.add(entry.getKey());
}
}
iUseThem.remove(key);
}
public void removeSelfIfSingular() {
int users = theyUseMe.size();
int used = iUseThem.size();
if (used <= 1 && users > 0) {
groups.remove(key);
}
}
public String toShortString() {
StringBuilder sb = new StringBuilder();
sb.append(key);
sb.append("\n");
for (String val : iUseThem) {
sb.append(val);
sb.append("\n");
}
return sb.toString();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("#======================================\n");
sb.append("# Group: " + key);
sb.append("\n");
// sb.append("# Used by: ");
// for (String key : theyUseMe) {
// sb.append(" " + key + " ");
// }
// sb.append("\n");
sb.append(key);
sb.append("\n");
for (String val : iUseThem) {
sb.append(val);
sb.append("\n");
}
return sb.toString();
}
}
private final Map<String, List<String>> usedBy;
private final Map<String, Group> groups = new LinkedHashMap<String, Group>();
private List<String> ordering;
Groups(Map<String, List<String>> usedBy) {
this.usedBy = usedBy;
for (String key : usedBy.keySet()) {
groups.put(key, new Group(key));
}
}
public int size() {
return ordering.size();
}
@SuppressWarnings("unused")
public List<String> getPaths() {
size(); // Check.
return ordering;
}
Groups parse(List<ImportContainer> containers) {
if (ordering != null) {
throw new RuntimeException("Already ordered");
}
for (Group g : new ArrayList<Group>(groups.values())) {
g.removeSelfIfSingular();
}
ordering = new ArrayList<String>(groups.keySet());
// Here we remove all the superfluous import containers.
List<ImportContainer> copy = new ArrayList<ImportContainer>(
containers);
containers.clear();
for (String key : ordering) {
for (ImportContainer importContainer : copy) {
if (importContainer.getFile().getAbsolutePath().equals(key)) {
containers.add(importContainer);
}
}
}
// Now rewrite the filename chosen based on the first file in the
// getUsedFiles.
for (ImportContainer c : containers) {
c.setFile(new File(c.getUsedFiles()[0]));
c.updateUsedFilesTotalSize();
}
return this;
}
@SuppressWarnings("unused")
void print() {
Collection<Group> values = groups.values();
if (values.size() == 1) {
System.out.println(values.iterator().next().toShortString());
} else {
for (Group g : values) {
System.out.println(g);
}
}
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (Group g : groups.values()) {
sb.append(g.toString());
sb.append("\n");
}
return sb.toString();
}
static void line(String s) {
System.out.println("\n# ************ " + s + " ************ \n");
}
static Groups test(int count, Map<String, List<String>> t) {
System.out.println("\n\n");
line("TEST " + count);
Groups g = new Groups(t);
System.out.println(g);
g.parse(new ArrayList<ImportContainer>());
line("RESULT " + count);
System.out.println(g);
return g;
}
@SuppressWarnings("unused")
static Groups test() {
System.out.println("\n");
line("NOTICE");
System.out
.println("# You have entered \"\" \"\" as the path to import.");
System.out
.println("# This runs the test suite. If you would like to");
System.out.println("# import the current directory use \"\".");
Map<String, List<String>> t = new LinkedHashMap<String, List<String>>();
t.put("a.dv.log", Arrays.asList("b.dv"));
t.put("b.dv", Arrays.asList("b.dv"));
test(1, t);
t = new LinkedHashMap<String, List<String>>();
t.put("a.png", Arrays.asList("a.png"));
test(2, t);
t = new LinkedHashMap<String, List<String>>();
t.put("a.tiff", Arrays.asList("a.tiff", "c.lei"));
t.put("b.tiff", Arrays.asList("b.tiff", "c.lei"));
t.put("c.lei", Arrays.asList("c.lei"));
test(3, t);
t = new LinkedHashMap<String, List<String>>();
t.put("overlay.tiff", Arrays.asList("overlay.tiff"));
t.put("b.tiff", Arrays.asList("b.tiff", "overlay.tiff"));
return test(4, t);
}
}
}