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.HashMap;
23  import java.util.Map;
24  
25  import org.apache.commons.digester3.SetPropertiesRule;
26  
27  /**
28   * Builder chained when invoking {@link LinkedRuleBuilder#setProperties()}.
29   */
30  public final class SetPropertiesBuilder
31      extends AbstractBackToLinkedRuleBuilder<SetPropertiesRule>
32  {
33  
34      private final Map<String, String> aliases = new HashMap<String, String>();
35  
36      private boolean ignoreMissingProperty = true;
37  
38      SetPropertiesBuilder( String keyPattern, String namespaceURI, RulesBinder mainBinder,
39                            LinkedRuleBuilder mainBuilder )
40      {
41          super( keyPattern, namespaceURI, mainBinder, mainBuilder );
42      }
43  
44      /**
45       * Add an additional attribute name to property name mapping.
46       *
47       * @param attributeName The attribute to match
48       * @param propertyName The java bean property to be assigned the value
49       * @return this builder instance
50       * @deprecated
51       */
52      @Deprecated
53      public SetPropertiesBuilder addAlias( String attributeName, String propertyName )
54      {
55          return addAlias( attributeName ).forProperty( propertyName );
56      }
57  
58      /**
59       * Add an additional attribute name to property name mapping.
60       *
61       * @param attributeName The attribute to match
62       * @return the property alias builder
63       * @since 3.2
64       */
65      public AddAliasBuilder<SetPropertiesBuilder> addAlias( String attributeName )
66      {
67          if ( attributeName == null )
68          {
69              reportError( "setProperties().addAlias( String )", "empty 'attributeName' not allowed" );
70          }
71          return new AddAliasBuilder<SetPropertiesBuilder>( this, aliases, attributeName );
72      }
73  
74      /**
75       * Add an attribute name to the ignore list.
76       *
77       * @param attributeName The attribute to match has to be ignored
78       * @return this builder instance
79       */
80      public SetPropertiesBuilder ignoreAttribute( String attributeName )
81      {
82          if ( attributeName == null )
83          {
84              reportError( "setProperties().ignoreAttribute( String )", "empty 'attributeName' not allowed" );
85          }
86          return addAlias( attributeName ).forProperty( null );
87      }
88  
89      /**
90       * Sets whether attributes found in the XML without matching properties should be ignored.
91       *
92       * If set to false, the parsing will throw an {@code NoSuchMethodException}
93       * if an unmatched attribute is found.
94       * This allows to trap misspellings in the XML file.
95       *
96       * @param ignoreMissingProperty false to stop the parsing on unmatched attributes
97       * @return this builder instance
98       */
99      public SetPropertiesBuilder ignoreMissingProperty( boolean ignoreMissingProperty )
100     {
101         this.ignoreMissingProperty = ignoreMissingProperty;
102         return this;
103     }
104 
105     /**
106      * {@inheritDoc}
107      */
108     @Override
109     protected SetPropertiesRule createRule()
110     {
111         SetPropertiesRule rule = new SetPropertiesRule( aliases );
112         rule.setIgnoreMissingProperty( ignoreMissingProperty );
113         return rule;
114     }
115 
116 }