View Javadoc
1   /*
2    * Copyright 2005-2008 Werner Guttmann
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  package org.exolab.castor.builder.printing;
15  
16  import java.util.HashMap;
17  import java.util.Map;
18  
19  import org.apache.commons.lang3.StringUtils;
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.exolab.castor.builder.BuilderConfiguration;
23  
24  /**
25   * Registry for {@link JClassPrinterFactory} implementations obtained from the Castor XML code
26   * generator property file and used by the XML code generator during its operation.
27   * 
28   * @author <a href="mailto:Werner.Guttmann@morganstanley.com">Werner Guttmann</a>
29   * @version $Revision: 7134 $ $Date: 2006-04-08 08:58:10 -0600 (Sat, 08 Apr 2006) $
30   * @since 1.2.1
31   */
32  public class JClassPrinterFactoryRegistry {
33  
34    /**
35     * The <a href="http://jakarta.apache.org/commons/logging/">Jakarta Commons Logging</a> instance
36     * used for all logging.
37     */
38    private final Log _log = LogFactory.getLog(JClassPrinterFactoryRegistry.class);
39  
40    /** Association between name of implementation and JClassPrinterFactory instance. */
41    private Map<String, JClassPrinterFactory> _factories =
42        new HashMap<String, JClassPrinterFactory>();
43  
44    /**
45     * Construct an instance of JClassPrinterFactoryRegistry that loads the
46     * {@link JClassPrinterFactory} implementations specified in the given Configuration.
47     * 
48     * @param config The LocalConfiguration.
49     */
50    public JClassPrinterFactoryRegistry(final BuilderConfiguration config) {
51      String jClassPrinterFactories = config.getJClassPrinterFactories();
52      String[] factoryClassNames = StringUtils.split(jClassPrinterFactories, ',');
53  
54      for (String factoryClassName : factoryClassNames) {
55        JClassPrinterFactory factory;
56        try {
57          factory = (JClassPrinterFactory) Class.forName(factoryClassName).newInstance();
58        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
59          throw new IllegalStateException("Property entry '" + factoryClassName + "' does"
60              + " not represent a valid class name.");
61        }
62        _factories.put(factory.getName(), factory);
63      }
64    }
65  
66    /**
67     * Returns the names of all the configured {@link JClassPrinterFactory} implementations. A
68     * {@link JClassPrinterFactory} instance can be obtained by the {@link #getJClassPrinterFactory}
69     * method.
70     *
71     * @return Names of {@link JClassPrinterFactory} implementations
72     */
73    public String[] getJClassPrinterFactoryNames() {
74      String[] names = new String[_factories.size()];
75      return _factories.keySet().toArray(names);
76    }
77  
78    /**
79     * Returns a {@link JClassPrinterFactory} with the specified name. Returns null if the named
80     * factory is not supported.
81     *
82     * @param name The name of the JClassPrinterFactory.
83     * @return The {@link JClassPrinterFactory} or null if none exists.
84     */
85    public JClassPrinterFactory getJClassPrinterFactory(final String name) {
86      Object factory = _factories.get(name);
87      if (factory == null) {
88        String msg = "The JClassPrinterFactory '" + name + "' "
89            + "does not exist in the Castor XML code generator properties file "
90            + "and is therefore not supported.";
91        _log.error(msg);
92        throw new IllegalArgumentException(msg);
93      }
94  
95      if (_log.isDebugEnabled()) {
96        _log.debug("Returning JClassPrinterFactory with name " + name);
97      }
98      return (JClassPrinterFactory) factory;
99    }
100 
101 }