1 package org.apache.commons.digester3.binder;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.digester3.FactoryCreateRule;
23 import org.apache.commons.digester3.ObjectCreationFactory;
24
25
26
27
28
29
30 public final class FactoryCreateBuilder
31 extends AbstractBackToLinkedRuleBuilder<FactoryCreateRule>
32 {
33
34 private final ClassLoader classLoader;
35
36 private Class<? extends ObjectCreationFactory<?>> type;
37
38 private String attributeName;
39
40 private boolean ignoreCreateExceptions;
41
42 private ObjectCreationFactory<?> creationFactory;
43
44 FactoryCreateBuilder( String keyPattern, String namespaceURI, RulesBinder mainBinder,
45 LinkedRuleBuilder mainBuilder, ClassLoader classLoader )
46 {
47 super( keyPattern, namespaceURI, mainBinder, mainBuilder );
48 this.classLoader = classLoader;
49 }
50
51
52
53
54
55
56
57
58 @SuppressWarnings( "unchecked" )
59 public FactoryCreateBuilder ofType( String className )
60 {
61 if ( className == null )
62 {
63 reportError( "factoryCreate().ofType( String )", "NULL Java type not allowed" );
64 }
65
66 try
67 {
68 Class<?> type = this.classLoader.loadClass( className );
69 if ( !ObjectCreationFactory.class.isAssignableFrom( type ) )
70 {
71 reportError( "factoryCreate().ofType( String )", "NULL Java type not allowed" );
72 return this;
73 }
74
75 this.type = (Class<? extends ObjectCreationFactory<?>>) type;
76 }
77 catch ( ClassNotFoundException e )
78 {
79 reportError( "factoryCreate().ofType( String )", String.format( "class '%s' cannot be load", className ) );
80 }
81
82 return this;
83 }
84
85
86
87
88
89
90
91
92 public FactoryCreateBuilder ofType( Class<? extends ObjectCreationFactory<?>> type )
93 {
94 if ( type == null )
95 {
96 reportError( "factoryCreate().ofType( Class<? extends ObjectCreationFactory<?>> )",
97 "NULL Java type not allowed" );
98 }
99
100 this.type = type;
101
102 return this;
103 }
104
105
106
107
108
109
110
111
112 public <T> FactoryCreateBuilder usingFactory(
113 {
114 this.creationFactory = creationFactory;
115 return this;
116 }
117
118
119
120
121
122
123
124 public FactoryCreateBuilder overriddenByAttribute(
125 {
126 this.attributeName = attributeName;
127 return this;
128 }
129
130
131
132
133
134
135
136 public FactoryCreateBuilder ignoreCreateExceptions( boolean ignoreCreateExceptions )
137 {
138 this.ignoreCreateExceptions = ignoreCreateExceptions;
139 return this;
140 }
141
142
143
144
145 @Override
146 protected FactoryCreateRule createRule()
147 {
148 if ( type == null && attributeName == null && creationFactory == null )
149 {
150 reportError( "factoryCreate()",
151 "at least one between 'className', 'attributeName' or 'creationFactory' has to be specified" );
152 }
153
154 if ( type != null || attributeName != null )
155 {
156 return new FactoryCreateRule( type, attributeName, ignoreCreateExceptions );
157 }
158
159 return new FactoryCreateRule( creationFactory, ignoreCreateExceptions );
160 }
161
162 }