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 org.apache.commons.digester3.Rule;
23
24 /**
25 * Builder invoked to back to main {@link LinkedRuleBuilder}.
26 *
27 * @since 3.0
28 */
29 abstract class AbstractBackToLinkedRuleBuilder<R extends Rule>
30 implements RuleProvider<R>
31 {
32
33 private final String keyPattern;
34
35 private final String namespaceURI;
36
37 private final RulesBinder mainBinder;
38
39 private final LinkedRuleBuilder mainBuilder;
40
41 AbstractBackToLinkedRuleBuilder( final String keyPattern, final String namespaceURI,
42 final RulesBinder mainBinder, final LinkedRuleBuilder mainBuilder )
43 {
44 this.keyPattern = keyPattern;
45 this.namespaceURI = namespaceURI;
46 this.mainBinder = mainBinder;
47 this.mainBuilder = mainBuilder;
48 }
49
50 /**
51 * Come back to the main {@link LinkedRuleBuilder}.
52 *
53 * @return the main {@link LinkedRuleBuilder}
54 */
55 public final LinkedRuleBuilder then()
56 {
57 return this.mainBuilder;
58 }
59
60 /**
61 * Returns the namespace URI for which this Rule is relevant, if any.
62 *
63 * @return The namespace URI for which this Rule is relevant, if any
64 */
65 public final String getNamespaceURI()
66 {
67 return this.namespaceURI;
68 }
69
70 /**
71 * {@inheritDoc}
72 */
73 public final R get()
74 {
75 R rule = this.createRule();
76 if ( rule != null && this.namespaceURI != null )
77 {
78 rule.setNamespaceURI( this.namespaceURI );
79 }
80 return rule;
81 }
82
83 protected final void reportError( String methodChain, String message )
84 {
85 this.mainBinder.addError( "{ forPattern( \"%s\" ).%s } %s", this.keyPattern, methodChain, message );
86 }
87
88 /**
89 * Returns the rule pattern associated to this builder.
90 *
91 * @return The rule pattern associated to this builder
92 */
93 public final String getPattern()
94 {
95 return keyPattern;
96 }
97
98 /**
99 * Provides an instance of {@link Rule}. Must never return null.
100 *
101 * @return an instance of {@link Rule}.
102 * @see #get()
103 */
104 protected abstract R createRule();
105
106 }