View Javadoc

1   // Copyright 2006-2007, by the California Institute of Technology.
2   // ALL RIGHTS RESERVED. United States Government Sponsorship acknowledged.
3   // Any commercial use must be negotiated with the Office of Technology Transfer
4   // at the California Institute of Technology.
5   //
6   // This software is subject to U. S. export control laws and regulations
7   // (22 C.F.R. 120-130 and 15 C.F.R. 730-774). To the extent that the software
8   // is subject to U.S. export control laws and regulations, the recipient has
9   // the responsibility to obtain export licenses or other export authority as
10  // may be required before exporting such information to foreign countries or
11  // providing access to foreign nationals.
12  //
13  // $Id: DataObjectValidatorFactory.java 2606 2007-04-18 19:35:09Z pramirez $ 
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          //TODO: Load type to class mappings
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  }