1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package gov.nasa.pds.tools.label.validate;
17
18 import gov.nasa.pds.tools.label.parser.UnsupportedDataObjectException;
19
20 /***
21 * This class will dynamically load data object validators from property settings.
22 * The property mapping should take the form object.validator.{TYPE}
23 * @author pramirez
24 * @version $Revision: 2606 $
25 *
26 */
27 public class DataObjectValidatorFactory {
28 private static DataObjectValidatorFactory factory = null;
29
30 /***
31 * Constructs a parser factory following the Singleton pattern
32 */
33 private DataObjectValidatorFactory() {
34
35 }
36
37 /***
38 * This will provide access to a {@link DataObjectValidatorFactory}
39 * @return factory to generate data object validators
40 */
41 public synchronized static DataObjectValidatorFactory getInstance() {
42 if (factory == null)
43 factory = new DataObjectValidatorFactory();
44 return factory;
45 }
46
47 /***
48 * Retrieves a validator for the given type
49 * @param type The type of data object to be validated
50 * @return a validator for the given type
51 * @throws UnsupportedTypeException if there is no mapping to a validator for the type
52 */
53 public DataObjectValidator newInstance(String type) throws UnsupportedDataObjectException {
54 String className = System.getProperty("object.validator." + type);
55
56 if (className == null)
57 throw new UnsupportedDataObjectException("There is no mapping available. Property "
58 + "object.validator." + type + " was not set.");
59
60 DataObjectValidator validator = null;
61
62 try {
63 validator = (DataObjectValidator) Class.forName(className).newInstance();
64 } catch (InstantiationException e) {
65 throw new UnsupportedDataObjectException(e.getMessage());
66 } catch (IllegalAccessException e) {
67 throw new UnsupportedDataObjectException(e.getMessage());
68 } catch (ClassNotFoundException e) {
69 throw new UnsupportedDataObjectException(e.getMessage());
70 }
71
72
73 if (validator == null)
74 throw new UnsupportedDataObjectException("Validator could not be loaded for type " + type
75 + " with class " + className);
76
77 return validator;
78 }
79 }