View Javadoc

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.extractor;
19  
20  import org.apache.any23.mime.MIMEType;
21  import org.apache.any23.rdf.Prefixes;
22  
23  import java.util.ArrayList;
24  import java.util.Collection;
25  
26  /**
27   * This class is a simple and default-like implementation of {@link ExtractorFactory}.
28   *
29   * @param <T> the type of the {@link Extractor} served by this factory.
30   */
31  public class SimpleExtractorFactory<T extends Extractor<?>> implements ExtractorFactory<T> {
32  
33      private final String name;
34  
35      private final Prefixes prefixes;
36  
37      private final Collection<MIMEType> supportedMIMETypes = new ArrayList<MIMEType>();
38  
39      private final String exampleInput;
40      
41      private final Class<T> extractorClass;
42  
43      /**
44       * Creates an instance of a {@link ExtractorFactory} serving concrete implementation
45       * instances of {@link Extractor}.
46       *
47       * @param name of the {@link Extractor}.
48       * @param prefixes handled {@link org.apache.any23.rdf.Prefixes}.
49       * @param supportedMIMETypes collection of supported MIME Types.
50       * @param exampleInput a string acting as a input example.
51       * @param extractorClass concrete implementation class of the {@link Extractor}.
52       * @param <S> the concrete type of the {@link Extractor}.
53       * @return an {@link ExtractorFactory}.
54       */
55      public static <S extends Extractor<?>> ExtractorFactory<S> create(
56              String name,
57              Prefixes prefixes,
58              Collection<String> supportedMIMETypes,
59              String exampleInput,
60              Class<S> extractorClass
61      ) {
62          return new SimpleExtractorFactory<S>(name, prefixes, supportedMIMETypes, exampleInput, extractorClass);
63      }
64  
65      /**
66       * @return the name of the {@link Extractor}
67       */
68      public String getExtractorName() {
69          return name;
70      }
71  
72      /**
73       * @return the handled {@link org.apache.any23.rdf.Prefixes}
74       */
75      public Prefixes getPrefixes() {
76          return prefixes;
77      }
78  
79      /**
80       * @return the supported {@link org.apache.any23.mime.MIMEType}
81       */
82      public Collection<MIMEType> getSupportedMIMETypes() {
83          return supportedMIMETypes;
84      }
85  
86      @Override
87      public Class<T> getExtractorType() {
88          return extractorClass;
89      }
90  
91      /**
92       * @return an instance of type T concrete implementation of {@link Extractor}
93       */
94      @Override
95      public T createExtractor() {
96          try {
97              return extractorClass.newInstance();
98          } catch (IllegalAccessException ex) {
99              throw new RuntimeException("Zero-argument constructor not public?", ex);
100         } catch (InstantiationException ex) {
101             throw new RuntimeException("Non-instantiable type?", ex);
102         }
103     }
104 
105     /**
106      * @return an input example
107      */
108     @Override
109     public String getExampleInput() {
110         return exampleInput;
111     }
112 
113     private SimpleExtractorFactory(
114             String name,
115             Prefixes prefixes,
116             Collection<String> supportedMIMETypes,
117             String exampleInput,
118             Class<T> extractorClass
119     ) {
120         this.name = name;
121         this.prefixes = (prefixes == null) ? Prefixes.EMPTY : prefixes;
122         for (String type : supportedMIMETypes) {
123             this.supportedMIMETypes.add(MIMEType.parse(type));
124         }
125         this.exampleInput = exampleInput;
126         this.extractorClass = extractorClass;
127     }
128 
129 }