4646import java .util .HashSet ;
4747import java .util .Set ;
4848
49+ import org .scijava .util .Combiner ;
4950import org .scijava .util .FileUtils ;
5051
5152/**
5253 * Combines SezPoz annotations from all JAR files on the classpath.
5354 *
5455 * @author Curtis Rueden
5556 */
56- public class CombineAnnotations extends AbstractIndexWriter {
57+ public class AnnotationCombiner extends AbstractIndexWriter implements Combiner
58+ {
5759
5860 private static final String PREFIX = "META-INF/json/" ;
5961 private static final String LEGACY_PREFIX = "META-INF/annotations/" ;
60- private final String OUTPUT_DIR ;
6162
62- private final Set <String > annotationFiles ;
63-
64- public CombineAnnotations () throws IOException {
65- this (null );
66- }
67-
68- public CombineAnnotations (final String outputDir ) throws IOException {
69- if (outputDir != null ) {
70- OUTPUT_DIR = outputDir ;
71- } else {
72- OUTPUT_DIR = "src/main/assembly/all" ;
63+ /** Reads in annotations from all available resources and combines them. */
64+ @ Override
65+ public void combine (File outputDirectory ) throws Exception {
66+ if (outputDirectory == null ) {
67+ outputDirectory = new File ("src/main/assembly/all" );
7368 }
69+ final Set <String > annotationFiles = getAnnotationFiles ();
7470
75- annotationFiles = getAnnotationFiles ();
76- }
77-
78- /** Reads in annotations from all available resources and combines them. */
79- public void combine () throws IOException , ClassNotFoundException {
8071 final ClassLoader loader = Thread .currentThread ().getContextClassLoader ();
8172
8273 log ("" );
83- log ("Writing annotations to " + new File ( OUTPUT_DIR ) .getAbsolutePath ());
74+ log ("Writing annotations to " + outputDirectory .getAbsolutePath ());
8475
85- new File (OUTPUT_DIR , PREFIX ).mkdirs ();
76+ new File (outputDirectory , PREFIX ).mkdirs ();
8677 for (final String annotationFile : annotationFiles ) {
8778 final String annotationName = annotationFile .substring (PREFIX .length ());
8879 @ SuppressWarnings ("unchecked" )
8980 final Class <? extends Annotation > annotation =
9081 (Class <? extends Annotation >) loader .loadClass (annotationName );
91- for (IndexItem <? extends Annotation > item : Index .load (annotation , loader )) {
82+ for (IndexItem <? extends Annotation > item : Index
83+ .load (annotation , loader ))
84+ {
9285 add (adapt (item .annotation ()), annotationName , item .className ());
9386 }
9487 }
9588
96- write (new StreamFactory () {
97-
98- @ Override
99- public InputStream openInput (String annotationName )
100- throws IOException {
101- return null ;
102- }
103-
104- @ Override
105- public OutputStream openOutput (String annotationName )
106- throws IOException {
107- final File file = new File (OUTPUT_DIR , PREFIX + annotationName );
108- return new FileOutputStream (file );
109- }
110-
111- @ Override
112- public boolean isClassObsolete (String className ) {
113- return false ;
114- }
115-
116- });
89+ write (new AnnotationStreamFactory (outputDirectory ));
11790 }
11891
11992 /** Scans for annotations files in every resource on the classpath. */
12093 public Set <String > getAnnotationFiles () throws IOException {
12194 final HashSet <String > files = new HashSet <String >();
12295
12396 for (final String prefix : new String [] { PREFIX , LEGACY_PREFIX }) {
124- final Enumeration <URL > directories = Thread . currentThread ()
125- .getContextClassLoader ().getResources (prefix );
97+ final Enumeration <URL > directories =
98+ Thread . currentThread () .getContextClassLoader ().getResources (prefix );
12699 while (directories .hasMoreElements ()) {
127100 final URL url = directories .nextElement ();
128101 for (final URL annotationIndexURL : FileUtils .listContents (url )) {
@@ -131,18 +104,14 @@ public Set<String> getAnnotationFiles() throws IOException {
131104 continue ;
132105 }
133106 final int length = string .length ();
134- add (files , PREFIX + string . substring (
135- string .lastIndexOf ('/' , length - 1 ) + 1 , length ));
107+ add (files , PREFIX +
108+ string . substring ( string .lastIndexOf ('/' , length - 1 ) + 1 , length ));
136109 }
137110 }
138111 }
139112 return files ;
140113 }
141114
142- public static void main (final String [] args ) throws Exception {
143- new CombineAnnotations (args .length > 0 ? args [0 ] : null ).combine ();
144- }
145-
146115 // -- Helper methods --
147116
148117 private void add (final HashSet <String > set , final String item ) {
@@ -154,4 +123,41 @@ private void log(final String msg) {
154123 System .out .println (msg );
155124 }
156125
126+ // -- Helper Class --
127+
128+ /**
129+ * {@link StreamFactory} implementation for writing an annotation.
130+ *
131+ */
132+ private static class AnnotationStreamFactory implements StreamFactory {
133+
134+ private final File outputDirectory ;
135+
136+ public AnnotationStreamFactory (final File outputDirectory ) {
137+ this .outputDirectory = outputDirectory ;
138+ }
139+
140+ @ Override
141+ public InputStream openInput (String annotationName ) throws IOException {
142+ return null ;
143+ }
144+
145+ @ Override
146+ public OutputStream openOutput (String annotationName ) throws IOException {
147+ final File file = new File (outputDirectory , PREFIX + annotationName );
148+ return new FileOutputStream (file );
149+ }
150+
151+ @ Override
152+ public boolean isClassObsolete (String className ) {
153+ return false ;
154+ }
155+
156+ }
157+
158+ // -- Main method --
159+
160+ public static void main (final String [] args ) throws Exception {
161+ new AnnotationCombiner ().combine (args .length > 0 ? new File (args [0 ]) : null );
162+ }
157163}
0 commit comments