##### Coverage Report - org.apache.any23.cli.ExtractorDocumentation

Classes in this File | Line Coverage | Branch Coverage | Complexity
ExtractorDocumentation |
0% | 0/102
0% | 0/44
| 4.7;4.7

1 |  |
     /*
2 |  |
      * Licensed to the Apache Software Foundation (ASF) under one or more
3 |  |
      * contributor license agreements.  See the NOTICE file distributed with
4 |  |
      * this work for additional information regarding copyright ownership.
5 |  |
      * The ASF licenses this file to You under the Apache License, Version 2.0
6 |  |
      * (the "License"); you may not use this file except in compliance with
7 |  |
      * the License.  You may obtain a copy of the License at
8 |  |
      *
9 |  |
      *  http://www.apache.org/licenses/LICENSE-2.0
10 |  |
      *
11 |  |
      * Unless required by applicable law or agreed to in writing, software
12 |  |
      * distributed under the License is distributed on an "AS IS" BASIS,
13 |  |
      * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 |  |
      * See the License for the specific language governing permissions and
15 |  |
      * limitations under the License.
16 |  |
      */
17 |  |
18 |  |
     package org.apache.any23.cli;
19 |  |
20 |  |
     import org.apache.any23.extractor.ExampleInputOutput;
21 |  |
     import org.apache.any23.extractor.ExtractionException;
22 |  |
     import org.apache.any23.extractor.ExtractorFactory;
23 |  |
     import org.apache.any23.extractor.ExtractorRegistry;
24 |  |
     import org.apache.any23.util.LogUtils;
25 |  |
     import org.apache.any23.extractor.Extractor;
26 |  |
     import org.apache.any23.extractor.Extractor.BlindExtractor;
27 |  |
     import org.apache.any23.extractor.Extractor.ContentExtractor;
28 |  |
     import org.apache.any23.extractor.Extractor.TagSoupDOMExtractor;
29 |  |
30 |  |
     import java.io.IOException;
31 |  |
32 |  |
     /**
33 |  |
      * This class provides some command-line documentation
34 |  |
      * about available extractors and their usage.
35 |  |
      */
36 |  |
     @ToolRunner.Description("Utility for obtaining documentation about metadata extractors.")
37 | 0 |
     public class ExtractorDocumentation implements Tool {
38 |  |
39 |  |
         /**
40 |  |
          * Main method to access the class functionality.
41 |  |
          *
42 |  |
          * Usage:
43 |  |
          *     ExtractorDocumentation -list
44 |  |
          *       shows the names of all available extractors
45 |  |
          *
46 |  |
          *     ExtractorDocumentation -i extractor-name
47 |  |
          *       shows example input for the given extractor
48 |  |
          *
49 |  |
          *     ExtractorDocumentation -o extractor-name
50 |  |
          *       shows example output for the given extractor
51 |  |
          *
52 |  |
          *     ExtractorDocumentation -all
53 |  |
          *       shows a report about all available extractors
54 |  |
          *
55 |  |
          * @param args allowed arguments
56 |  |
          * @throws ExtractionException
57 |  |
          * @throws IOException
58 |  |
          */
59 |  |
         public static void main(String[] args) throws ExtractionException, IOException {
60 | 0 |
             System.exit( new ExtractorDocumentation().run(args) );
61 | 0 |
         }
62 |  |
63 |  |
         public int run(String[] args) {
64 | 0 |
             LogUtils.setDefaultLogging();
65 |  |
             try {
66 | 0 |
                 if (args.length == 0) {
67 | 0 |
                     printUsage();
68 | 0 |
                     return 1;
69 |  |
                 }
70 |  |
71 | 0 |
                 final String option = args[0];
72 | 0 |
                 if ("-list".equals(option)) {
73 | 0 |
                     if (args.length > 1) {
74 | 0 |
                         printUsage();
75 | 0 |
                         return 2;
76 |  |
                     }
77 | 0 |
                     printExtractorList();
78 |  |
                 }
79 | 0 |
                 else if ("-i".equals(option)) {
80 | 0 |
                     if (args.length > 2) {
81 | 0 |
                         printUsage();
82 | 0 |
                         return 3;
83 |  |
                     }
84 | 0 |
                     if (args.length < 2) {
85 | 0 |
                         printError("Required argument for -i: extractor name");
86 | 0 |
                         return 4;
87 |  |
                     }
88 | 0 |
                     printExampleInput(args[1]);
89 |  |
                 }
90 | 0 |
                 else if ("-o".equals(option)) {
91 | 0 |
                     if (args.length > 2) {
92 | 0 |
                         printUsage();
93 | 0 |
                         return 5;
94 |  |
                     }
95 | 0 |
                     if (args.length < 2) {
96 | 0 |
                         printError("Required argument for -o: extractor name");
97 | 0 |
                         return 6;
98 |  |
                     }
99 | 0 |
                     printExampleOutput(args[1]);
100 |  |
                 }
101 | 0 |
                 else if ("-all".equals(option)) {
102 | 0 |
                     if (args.length > 1) {
103 | 0 |
                         printUsage();
104 | 0 |
                         return 7;
105 |  |
                     }
106 | 0 |
                     printReport();
107 |  |
                 } else {
108 | 0 |
                     printUsage();
109 |  |
                 }
110 | 0 |
             } catch (Exception e) {
111 | 0 |
                 e.printStackTrace(System.err);
112 | 0 |
                 return 8;
113 | 0 |
             }
114 | 0 |
             return 0;
115 |  |
         }
116 |  |
117 |  |
         /**
118 |  |
          * Prints the command line usage help.
119 |  |
          */
120 |  |
         public void printUsage() {
121 | 0 |
             System.out.println("Usage:");
122 | 0 |
             System.out.println("  " + ExtractorDocumentation.class.getSimpleName() + " -list");
123 | 0 |
             System.out.println("      shows the names of all available extractors");
124 | 0 |
             System.out.println();
125 | 0 |
             System.out.println("  " + ExtractorDocumentation.class.getSimpleName() + " -i extractor-name");
126 | 0 |
             System.out.println("      shows example input for the given extractor");
127 | 0 |
             System.out.println();
128 | 0 |
             System.out.println("  " + ExtractorDocumentation.class.getSimpleName() + " -o extractor-name");
129 | 0 |
             System.out.println("      shows example output for the given extractor");
130 | 0 |
             System.out.println();
131 | 0 |
             System.out.println("  " + ExtractorDocumentation.class.getSimpleName() + " -all");
132 | 0 |
             System.out.println("      shows a report about all available extractors");
133 | 0 |
             System.out.println();
134 | 0 |
         }
135 |  |
136 |  |
         /**
137 |  |
          * Print an error message.
138 |  |
          *
139 |  |
          * @param msg the error message to be printed
140 |  |
          */
141 |  |
         public void printError(String msg) {
142 | 0 |
             System.err.println(msg);
143 | 0 |
         }
144 |  |
145 |  |
         /**
146 |  |
          * Prints the list of all the available extractors.
147 |  |
          */
148 |  |
         public void printExtractorList() {
149 | 0 |
             for(ExtractorFactory factory : ExtractorRegistry.getInstance().getExtractorGroup()) {
150 | 0 |
                 System.out.println( String.format("%25s [%15s]", factory.getExtractorName(), factory.getExtractorType()));
151 |  |
             }
152 | 0 |
         }
153 |  |
154 |  |
         /**
155 |  |
          * Prints an example of input for the provided extractor.
156 |  |
          *
157 |  |
          * @param extractorName the name of the extractor
158 |  |
          * @throws IOException raised if no extractor is found with that name
159 |  |
          */
160 |  |
         public void printExampleInput(String extractorName) throws IOException {
161 | 0 |
             ExtractorFactory<?> factory = getFactory(extractorName);
162 | 0 |
             ExampleInputOutput example = new ExampleInputOutput(factory);
163 | 0 |
             String input = example.getExampleInput();
164 | 0 |
             if (input == null) {
165 | 0 |
                 throw new IllegalArgumentException("Extractor " + extractorName + " provides no example input");
166 |  |
             }
167 | 0 |
             System.out.println(input);
168 | 0 |
         }
169 |  |
170 |  |
         /**
171 |  |
          * Prints an output example for the given extractor.
172 |  |
          *
173 |  |
          * @param extractorName the extractor name
174 |  |
          * @throws IOException raised if no extractor is found with that name
175 |  |
          * @throws ExtractionException
176 |  |
          */
177 |  |
         public void printExampleOutput(String extractorName) throws IOException, ExtractionException {
178 | 0 |
             ExtractorFactory<?> factory = getFactory(extractorName);
179 | 0 |
             ExampleInputOutput example = new ExampleInputOutput(factory);
180 | 0 |
             String output = example.getExampleOutput();
181 | 0 |
             if (output == null) {
182 | 0 |
                 throw new IllegalArgumentException("Extractor " + extractorName + " provides no example output");
183 |  |
             }
184 | 0 |
             System.out.println(output);
185 | 0 |
         }
186 |  |
187 |  |
         /**
188 |  |
          * Prints a complete report on all the available extractors.
189 |  |
          *
190 |  |
          * @throws IOException
191 |  |
          * @throws ExtractionException
192 |  |
          */
193 |  |
         public void printReport() throws IOException, ExtractionException {
194 | 0 |
             for (String extractorName : ExtractorRegistry.getInstance().getAllNames()) {
195 | 0 |
                 ExtractorFactory<?> factory = ExtractorRegistry.getInstance().getFactory(extractorName);
196 | 0 |
                 ExampleInputOutput example = new ExampleInputOutput(factory);
197 | 0 |
                 System.out.println("Extractor: " + extractorName);
198 | 0 |
                 System.out.println("\ttype: " + getType(factory));
199 | 0 |
                 System.out.println();
200 | 0 |
                 final String exampleInput = example.getExampleInput();
201 | 0 |
                 if(exampleInput == null) {
202 | 0 |
                     System.out.println("(No Example Available)");
203 |  |
                 } else {
204 | 0 |
                     System.out.println("-------- Example Input  --------");
205 | 0 |
                     System.out.println(exampleInput);
206 | 0 |
                     System.out.println("-------- Example Output --------");
207 | 0 |
                     String output = example.getExampleOutput();
208 | 0 |
                     System.out.println(output == null || output.trim().length() == 0 ? "(No Output Generated)" : output);
209 |  |
                 }
210 | 0 |
                 System.out.println("================================");
211 | 0 |
                 System.out.println();
212 | 0 |
             }
213 | 0 |
         }
214 |  |
215 |  |
         private ExtractorFactory<?> getFactory(String name) {
216 | 0 |
             if (!ExtractorRegistry.getInstance().isRegisteredName(name)) {
217 | 0 |
                 throw new IllegalArgumentException("Unknown extractor name: " + name);
218 |  |
             }
219 | 0 |
             return ExtractorRegistry.getInstance().getFactory(name);
220 |  |
         }
221 |  |
222 |  |
         private String getType(ExtractorFactory<?> factory) {
223 | 0 |
             Extractor<?> extractor = factory.createExtractor();
224 | 0 |
             if (extractor instanceof BlindExtractor) {
225 | 0 |
                 return BlindExtractor.class.getSimpleName();
226 |  |
             }
227 | 0 |
             if (extractor instanceof TagSoupDOMExtractor) {
228 | 0 |
                 return TagSoupDOMExtractor.class.getSimpleName();
229 |  |
             }
230 | 0 |
             if (extractor instanceof ContentExtractor) {
231 | 0 |
                 return ContentExtractor.class.getSimpleName();
232 |  |
             }
233 | 0 |
             return "?";
234 |  |
         }
235 |  |
236 |  |
     }