1 /*
2 * @(#)$Id: AlgorithmFactory.java 736 2009-08-14 09:20:17Z kurmannu $
3 *
4 * Author : Ueli Kurmann, igesture@uelikurmann.ch
5 *
6 * Purpose : The factory used to create algorithms.
7 *
8 * -----------------------------------------------------------------------
9 *
10 * Revision Information:
11 *
12 * Date Who Reason
13 *
14 * Dec 26, 2006 ukurmann Initial Release
15 * Mar 14, 2007 bsigner Cleanup
16 *
17 * -----------------------------------------------------------------------
18 *
19 * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
20 *
21 * This software is the proprietary information of ETH Zurich.
22 * Use is subject to license terms.
23 *
24 */
25
26
27 package org.ximtec.igesture.algorithm;
28
29 import java.io.InputStream;
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.logging.Level;
33 import java.util.logging.Logger;
34
35 import org.ximtec.igesture.configuration.Configuration;
36 import org.ximtec.igesture.core.GestureSet;
37 import org.ximtec.igesture.util.XMLTool;
38
39
40 /**
41 * The factory used to create algorithms.
42 *
43 * @version 1.0 Dec 2006
44 * @author Ueli Kurmann, igesture@uelikurmann.ch
45 * @author Beat Signer, signer@inf.ethz.ch
46 */
47 public class AlgorithmFactory {
48
49 private static final Logger LOGGER = Logger.getLogger(AlgorithmFactory.class
50 .getName());
51
52
53 /**
54 * Creates a list of algorithms and initialises them with the parameters
55 * provided in the configuration object.
56 *
57 * @param configuration the input stream from where the XML configuration can
58 * be read from.
59 * @return the list of algorithms.
60 * @throws AlgorithmException if the algorithms could not be created.
61 */
62 public static List<Algorithm> createAlgorithms(InputStream configuration)
63 throws AlgorithmException {
64 return createAlgorithms(XMLTool.importConfiguration(configuration));
65 } // createAlgorithms
66
67
68 /**
69 * Creates a list of algorithms and initialises them with the parameters
70 * provided in the configuration object.
71 *
72 * @param config the configuration object.
73 * @return the list of algorithms.
74 * @throws AlgorithmException if the algorithms could not be created.
75 */
76 public static List<Algorithm> createAlgorithms(Configuration config)
77 throws AlgorithmException {
78 final List<Algorithm> algorithms = new ArrayList<Algorithm>();
79
80 for (final String algorithmName : config.getAlgorithms()) {
81 try {
82 final Algorithm algorithm = createAlgorithmInstance(algorithmName);
83 algorithm.init(config);
84 algorithms.add(algorithm);
85 }
86 catch (final NullPointerException e) {
87 LOGGER.log(Level.SEVERE, null, e);
88 throw new AlgorithmException(
89 AlgorithmException.ExceptionType.Initialisation);
90 }
91 }
92
93 return algorithms;
94 } // createAlgorithms
95
96
97 /**
98 * Instantiates an algorithm with a specific configuration object.
99 *
100 * @param configuration the input stream from which the XML configuration can
101 * be read from.
102 * @return the initialised algorithm.
103 * @throws AlgorithmException if the algorithm could not be created.
104 */
105 public static Algorithm createAlgorithm(InputStream configuration)
106 throws AlgorithmException {
107 return createAlgorithm(XMLTool.importConfiguration(configuration));
108 } // createAlgorithm
109
110
111 /**
112 * Creates a new algorithm instance.
113 *
114 * @param config the input stream from which the XML configuration can be read
115 * from.
116 * @param set the input stream from which the XML gesture set can be read
117 * from.
118 * @return the initialised algorithm.
119 * @throws AlgorithmException if the algorithm could not be created.
120 */
121 public static Algorithm createAlgorithm(InputStream config, InputStream set)
122 throws AlgorithmException {
123 Configuration configuration = XMLTool.importConfiguration(config);
124 GestureSet gset = XMLTool.importGestureSet(set);
125
126 if (gset != null) {
127 configuration.addGestureSet(gset);
128 }
129
130 return createAlgorithm(configuration);
131 } // createAlgorithm
132
133
134 /**
135 * Instantiates an algorithm with a given configuration object.
136 *
137 * @param config the configuration of the algorithm.
138 * @return the initialised algorithm.
139 * @throws AlgorithmException if the algorithm could not be created.
140 */
141 public static Algorithm createAlgorithm(Configuration config)
142 throws AlgorithmException {
143 Algorithm algorithm = null;
144
145 try {
146 final String algorithmName = config.getAlgorithms().get(0);
147 algorithm = createAlgorithmInstance(algorithmName);
148 algorithm.init(config);
149 }
150 catch (final NullPointerException e) {
151 LOGGER.log(Level.SEVERE, null, e);
152 throw new AlgorithmException(
153 AlgorithmException.ExceptionType.Initialisation);
154 }
155 catch (final IndexOutOfBoundsException e) {
156 LOGGER.log(Level.SEVERE, null, e);
157 throw new AlgorithmException(
158 AlgorithmException.ExceptionType.Initialisation);
159 }
160
161 return algorithm;
162 } // createAlgorithm
163
164
165 /**
166 * Creates an algorithm instance.
167 *
168 * @param className the full qualified name of the algorithm.
169 * @return the instance of the algorithm.
170 */
171 @SuppressWarnings("unchecked")
172 public static Algorithm createAlgorithmInstance(String className) {
173 Algorithm algorithm = null;
174 try {
175 final Class< ? extends Algorithm> clazz = (Class< ? extends Algorithm>)Class
176 .forName(className);
177 algorithm = clazz.newInstance();
178 }
179 catch (final ClassNotFoundException e) {
180 LOGGER.log(Level.SEVERE, null, e);
181 }
182 catch (final InstantiationException e) {
183 LOGGER.log(Level.SEVERE, null, e);
184 }
185 catch (final IllegalAccessException e) {
186 LOGGER.log(Level.SEVERE, null, e);
187 }
188
189 return algorithm;
190 } // createAlgorithmInstance
191
192 }