View Javadoc

1   /*
2    * Copyright (C) 2005-2006 Christian Siefkes <christian@siefkes.net>.
3    * Development of this software is supported by the German Research Society,
4    * Berlin-Brandenburg Graduate School in Distributed Information Systems
5    * (DFG grant no. GRK 316).
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, visit
19   * http://www.gnu.org/licenses/gpl.html or write to the Free Software
20   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21   */
22  package de.fu_berlin.ties.classify.feature;
23  
24  import de.fu_berlin.ties.ProcessingException;
25  import de.fu_berlin.ties.TiesConfiguration;
26  import de.fu_berlin.ties.util.Util;
27  
28  /***
29   * Static factory for initializing {@linkplain
30   * de.fu_berlin.ties.classify.feature.FeatureExtractor feature extractors}.
31   *
32   * @author Christian Siefkes
33   * @version $Revision: 1.4 $, $Date: 2006/10/21 16:03:57 $, $Author: siefkes $
34   */
35  public final class FeatureExtractorFactory {
36  
37  
38      /***
39       * Factory method that creates a feature extractor based on the
40       * provided configuration. The "feature.extractor" key in the configuration
41       * (optionally adapted by appending the provided <code>suffix</code>) must
42       * give the qualified name of a {@link FeatureExtractor} implementation
43       * accepting a {@link TiesConfiguration} as first argument and a
44       * <code>suffix</code> String as second argument.
45       *
46       * @param conf used to configure this instance
47       * @param suffix optional suffix for
48       * {@linkplain de.fu_berlin.ties.TiesConfiguration#adaptKey(String, String)
49       * adapting configuration keys} if not <code>null</code>
50       * @return the created extractor
51       * @throws ProcessingException if an error occurred while creating the
52       * extractor
53       */
54      public static FeatureExtractor createExtractor(final TiesConfiguration conf,
55              final String suffix) throws ProcessingException {
56          final FeatureExtractor result;
57          final String className =
58              conf.getString(conf.adaptKey("feature.extractor", suffix));
59  
60          // should be the qualified name of a FeatureExtractor implementation
61          // accepting a TiesConfiguration as 1st argument and a suffix String
62          // as second argument
63          try {
64              result = (FeatureExtractor) Util.createObject(
65                  Class.forName(className),
66                  new Object[] {conf, suffix},
67                  new Class[] {TiesConfiguration.class, String.class});
68          } catch (ClassNotFoundException cnfe) {
69              // convert and rethrow exception
70              throw new ProcessingException(
71                  "Cannot create feature extractor for class "
72                  + className + ": " + cnfe.toString());
73          } catch (InstantiationException ie) {
74              // convert and rethrow exception
75              throw new ProcessingException(
76                  "Cannot create feature extractor for class "
77                  + className, ie);
78          }
79          return result;
80      }
81  
82  
83      /***
84       * Private constructor prevents creation of instances.
85       */
86      private FeatureExtractorFactory() {
87          super();
88      }
89  
90  }