View Javadoc

1   package org.apache.commons.digester3.binder;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.LinkedHashMap;
23  import java.util.Map;
24  import java.util.Map.Entry;
25  
26  import org.apache.commons.digester3.plugins.PluginCreateRule;
27  import org.apache.commons.digester3.plugins.RuleLoader;
28  
29  /**
30   * Builder chained when invoking {@link LinkedRuleBuilder#createPlugin()}.
31   *
32   * @since 3.0
33   */
34  public final class PluginCreateRuleBuilder
35      extends AbstractBackToLinkedRuleBuilder<PluginCreateRule>
36  {
37  
38      private final Map<String, String> pluginClassAttributes = new LinkedHashMap<String, String>();
39  
40      private final Map<String, String> pluginIdAttributes = new LinkedHashMap<String, String>();
41  
42      private Class<?> baseClass;
43  
44      private Class<?> dfltPluginClass;
45  
46      private RuleLoader dfltPluginRuleLoader;
47  
48      PluginCreateRuleBuilder( String keyPattern, String namespaceURI, RulesBinder mainBinder,
49                                      LinkedRuleBuilder mainBuilder )
50      {
51          super( keyPattern, namespaceURI, mainBinder, mainBuilder );
52      }
53  
54      /**
55       * Set the class which any specified plugin <i>must</i> be descended from.
56       *
57       * @param <T> Any Java type
58       * @param type the class which any specified plugin <i>must</i> be descended from
59       * @return this builder instance
60       */
61      public <T> PluginCreateRuleBuilder ofType( Class<T> type )
62      {
63          if ( type == null )
64          {
65              reportError( "createPlugin().ofType( Class<?> )", "NULL Java type not allowed" );
66              return this;
67          }
68  
69          this.baseClass = type;
70  
71          return this;
72      }
73  
74      /**
75       * Set the class which will be used if the user doesn't specify any plugin-class or plugin-id.
76       *
77       * @param <T> Any Java type
78       * @param type the class which will be used if the user doesn't specify any plugin-class or plugin-id.
79       * @return this builder instance
80       */
81      public <T> PluginCreateRuleBuilder usingDefaultPluginClass( /* @Nullable */Class<T> type )
82      {
83          this.dfltPluginClass = type;
84          return this;
85      }
86  
87      /**
88       * Set RuleLoader instance which knows how to load the custom rules associated with the default plugin.
89       *
90       * @param <RL> Any {@link RuleLoader} extension.
91       * @param ruleLoader the RuleLoader instance which knows how to load the custom rules associated with
92       *        the default plugin.
93       * @return this builder instance
94       */
95      public <RL extends RuleLoader> PluginCreateRuleBuilder usingRuleLoader( /* @Nullable */RL ruleLoader )
96      {
97          this.dfltPluginRuleLoader = ruleLoader;
98          return this;
99      }
100 
101     /**
102      * Sets the xml attribute which the input xml uses to indicate to a
103      * PluginCreateRule which class should be instantiated.
104      *
105      * @param attrName the xml attribute which the input xml uses to indicate to a
106      *                 PluginCreateRule which class should be instantiated.
107      * @return this builder instance
108      */
109     public PluginCreateRuleBuilder setPluginClassAttribute( String attrName )
110     {
111         if ( attrName == null )
112         {
113             reportError( "createPlugin().setPluginClassAttribute( String )", "NULL attribute name not allowed" );
114             return this;
115         }
116 
117         return this.setPluginClassAttribute( null, attrName );
118     }
119 
120     /**
121      * Sets the xml attribute which the input xml uses to indicate to a
122      * PluginCreateRule which class should be instantiated.
123      *
124      * @param namespaceUri The attribute NameSpace
125      * @param attrName The attribute name
126      * @return this builder instance
127      */
128     public PluginCreateRuleBuilder setPluginClassAttribute( /* @Nullable */String namespaceUri, String attrName )
129     {
130         if ( attrName == null )
131         {
132             reportError( "createPlugin().setPluginClassAttribute( String, String )",
133                          "NULL attribute name not allowed" );
134             return this;
135         }
136 
137         return addToMap( pluginClassAttributes, namespaceUri, attrName );
138     }
139 
140     /**
141      * Sets the xml attribute which the input xml uses to indicate to a 
142      * PluginCreateRule which plugin declaration is being referenced.
143      *
144      * @param attrName The attribute name
145      * @return this builder instance
146      */
147     public PluginCreateRuleBuilder setPluginIdAttribute( String attrName )
148     {
149         if ( attrName == null )
150         {
151             reportError( "createPlugin().setPluginIdAttribute( String )", "NULL attribute name not allowed" );
152             return this;
153         }
154 
155         return setPluginIdAttribute( null, attrName );
156     }
157 
158     /**
159      * Sets the xml attribute which the input xml uses to indicate to a 
160      * PluginCreateRule which plugin declaration is being referenced.
161      *
162      * @param namespaceUri The attribute NameSpace
163      * @param attrName The attribute name
164      * @return this builder instance
165      */
166     public PluginCreateRuleBuilder setPluginIdAttribute( /* @Nullable */String namespaceUri, String attrName )
167     {
168         if ( attrName == null )
169         {
170             reportError( "createPlugin().setPluginIdAttribute( String, String )", "NULL attribute name not allowed" );
171             return this;
172         }
173 
174         return addToMap( pluginIdAttributes, namespaceUri, attrName );
175     }
176 
177     /**
178      * Private internal method to set values to a {@link Map} instance and return the current builder.
179      *
180      * @param map The target {@link Map}
181      * @param namespaceUri The attribute NameSpace
182      * @param attrName The attribute name
183      * @return this builder instance
184      */
185     private PluginCreateRuleBuilder addToMap( Map<String, String> map, String namespaceUri, String attrName )
186     {
187         map.put( namespaceUri, attrName );
188         return this;
189     }
190 
191     /**
192      * {@inheritDoc}
193      */
194     @Override
195     protected PluginCreateRule createRule()
196     {
197         if ( baseClass == null )
198         {
199             reportError( "createPlugin()", "'baseClass' has to be specified" );
200         }
201 
202         PluginCreateRule rule;
203         if ( dfltPluginClass != null )
204         {
205             if ( dfltPluginRuleLoader != null )
206             {
207                 rule = new PluginCreateRule( baseClass, dfltPluginClass, dfltPluginRuleLoader );
208             }
209             else
210             {
211                 rule = new PluginCreateRule( baseClass, dfltPluginClass );
212             }
213         }
214         else
215         {
216             rule = new PluginCreateRule( baseClass );
217         }
218 
219         for ( Entry<String, String> entry : pluginClassAttributes.entrySet() )
220         {
221             rule.setPluginClassAttribute( entry.getKey(), entry.getValue() );
222         }
223 
224         for ( Entry<String, String> entry : pluginIdAttributes.entrySet() )
225         {
226             rule.setPluginIdAttribute( entry.getKey(), entry.getValue() );
227         }
228 
229         return rule;
230     }
231 
232 }