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 static java.lang.String.format;
23  
24  import org.apache.commons.digester3.AbstractMethodRule;
25  
26  /**
27   * Builder chained when invoking {@link LinkedRuleBuilder#setNext(String)},
28   * {@link LinkedRuleBuilder#setRoot(String)} or {@link LinkedRuleBuilder#setTop(String)}.
29   *
30   * @param <R> any {@link AbstractMethodRule} concrete implementation, typically
31   *        {@link org.apache.commons.digester3.SetNextRule}, {@link org.apache.commons.digester3.SetRootRule}
32   *        and {@link org.apache.commons.digester3.SetTopRule}
33   * @since 3.0
34   */
35  public abstract class AbstractParamTypeBuilder<R extends AbstractMethodRule>
36      extends AbstractBackToLinkedRuleBuilder<R>
37  {
38  
39      private final String methodName;
40  
41      private final ClassLoader classLoader;
42  
43      private boolean useExactMatch = false;
44  
45      private Class<?> paramType;
46  
47      private boolean fireOnBegin = false;
48  
49      AbstractParamTypeBuilder( String keyPattern, String namespaceURI, RulesBinder mainBinder,
50                                LinkedRuleBuilder mainBuilder, String methodName, ClassLoader classLoader )
51      {
52          super( keyPattern, namespaceURI, mainBinder, mainBuilder );
53          this.methodName = methodName;
54          this.classLoader = classLoader;
55      }
56  
57      /**
58       * Sets the Java class of the method's argument.
59       * 
60       * If you wish to use a primitive type, specify the corresonding
61       * Java wrapper class instead, such as {@code java.lang.Boolean}
62       * for a {@code boolean} parameter.
63       *
64       * @param paramType The Java class of the method's argument
65       * @return this builder instance
66       */
67      public final AbstractParamTypeBuilder<R> withParameterType( Class<?> paramType )
68      {
69          if ( paramType == null )
70          {
71              reportError( format( ".%s.withParameterType( Class<?> )", methodName ), "NULL Java type not allowed" );
72              return this;
73          }
74          this.paramType = paramType;
75          return withParameterType( paramType.getName() );
76      }
77  
78      /**
79       * Sets the Java class name of the method's argument.
80       * 
81       * If you wish to use a primitive type, specify the corresonding
82       * Java wrapper class instead, such as {@code java.lang.Boolean}
83       * for a {@code boolean} parameter.
84       *
85       * @param paramType The Java class name of the method's argument
86       * @return this builder instance
87       */
88      public final AbstractParamTypeBuilder<R> withParameterType( String paramType )
89      {
90          if ( paramType == null )
91          {
92              reportError( format( ".%s.withParameterType( Class<?> )", methodName ), "NULL Java type not allowed" );
93              return this;
94          }
95  
96          if ( this.paramType == null )
97          {
98              try
99              {
100                 this.paramType = classLoader.loadClass( paramType );
101             }
102             catch ( ClassNotFoundException e )
103             {
104                 this.reportError( format( ".%s.withParameterType( Class<?> )", methodName ),
105                                   format( "class '%s' cannot be load", paramType ) );
106             }
107         }
108         return this;
109     }
110 
111     /**
112      * Sets exact matching being used.
113      *
114      * @param useExactMatch The exact matching being used
115      * @return this builder instance
116      */
117     public final AbstractParamTypeBuilder<R> useExactMatch( boolean useExactMatch )
118     {
119         this.useExactMatch = useExactMatch;
120         return this;
121     }
122 
123     /**
124      * Marks the rule be invoked when {@code begin} or {@code end} events match.
125      *
126      * @param fireOnBegin true, to invoke the rule at {@code begin}, false for {@code end}
127      * @return this builder instance
128      */
129     public final AbstractParamTypeBuilder<R> fireOnBegin( boolean fireOnBegin )
130     {
131         this.fireOnBegin = fireOnBegin;
132         return this;
133     }
134 
135     final String getMethodName()
136     {
137         return methodName;
138     }
139 
140     final Class<?> getParamType()
141     {
142         return paramType;
143     }
144 
145     final boolean isUseExactMatch()
146     {
147         return useExactMatch;
148     }
149 
150     final boolean isFireOnBegin()
151     {
152         return fireOnBegin;
153     }
154 
155 }