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 /**
23 * A support class for RulesModule which reduces repetition and results in a more readable configuration.
24 *
25 * @since 3.0
26 */
27 public abstract class AbstractRulesModule
28 implements RulesModule
29 {
30
31 private RulesBinder rulesBinder;
32
33 /**
34 * {@inheritDoc}
35 */
36 public final void configure( RulesBinder rulesBinder )
37 {
38 if ( this.rulesBinder != null )
39 {
40 throw new IllegalStateException( "Re-entry is not allowed." );
41 }
42
43 this.rulesBinder = rulesBinder;
44 try
45 {
46 configure();
47 }
48 finally
49 {
50 this.rulesBinder = null;
51 }
52 }
53
54 /**
55 * Configures a {@link RulesBinder} via the exposed methods.
56 */
57 protected abstract void configure();
58
59 /**
60 * Records an error message which will be presented to the user at a later time.
61 *
62 * Uses {@link java.lang.String#format(String, Object...)} to insert the arguments into the message.
63 *
64 * @param messagePattern A
65 * <a href="http://download.oracle.com/javase/6/docs/api/java/util/Formatter.html#syntax">format string</a>
66 * @param arguments Arguments referenced by the format specifiers in the format string
67 * @see RulesBinder#addError(String, Object...)
68 */
69 protected void addError( String messagePattern, Object... arguments )
70 {
71 rulesBinder.addError( messagePattern, arguments );
72 }
73
74 /**
75 * Records an exception, the full details of which will be logged, and the message of which will be presented to
76 * the user at a later time.
77 *
78 * @param t The exception has to be recorded
79 * @see RulesBinder#addError(Throwable)
80 */
81 protected void addError( Throwable t )
82 {
83 rulesBinder.addError( t );
84 }
85
86 /**
87 * Uses the given module to configure more bindings.
88 *
89 * @param rulesModule The module used to configure more bindings
90 * @see RulesBinder#install(RulesModule)
91 */
92 protected void install( RulesModule rulesModule )
93 {
94 rulesBinder.install( rulesModule );
95 }
96
97 /**
98 * Allows user binding one or more Digester rules to the input pattern.
99 *
100 * @param pattern The pattern used to bind rules
101 * @return The Digester rules builder
102 * @see RulesBinder#forPattern(String)
103 */
104 protected LinkedRuleBuilder forPattern( String pattern )
105 {
106 return rulesBinder.forPattern( pattern );
107 }
108
109 /**
110 * Return the wrapped {@link RulesBinder}.
111 *
112 * @return The wrapped {@link RulesBinder}
113 */
114 protected RulesBinder rulesBinder()
115 {
116 return rulesBinder;
117 }
118
119 }