1
2
3
4
5
6
7
8
9
10
11
12
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
26
27
28
29
30
31
32 public class JClassPrinterFactoryRegistry {
33
34
35
36
37
38 private final Log _log = LogFactory.getLog(JClassPrinterFactoryRegistry.class);
39
40
41 private Map<String, JClassPrinterFactory> _factories =
42 new HashMap<String, JClassPrinterFactory>();
43
44
45
46
47
48
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
68
69
70
71
72
73 public String[] getJClassPrinterFactoryNames() {
74 String[] names = new String[_factories.size()];
75 return _factories.keySet().toArray(names);
76 }
77
78
79
80
81
82
83
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 }