001package org.apache.commons.digester3.binder;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.HashMap;
023import java.util.Map;
024
025import org.apache.commons.digester3.SetPropertiesRule;
026
027/**
028 * Builder chained when invoking {@link LinkedRuleBuilder#setProperties()}.
029 */
030public final class SetPropertiesBuilder
031    extends AbstractBackToLinkedRuleBuilder<SetPropertiesRule>
032{
033
034    private final Map<String, String> aliases = new HashMap<String, String>();
035
036    private boolean ignoreMissingProperty = true;
037
038    SetPropertiesBuilder( String keyPattern, String namespaceURI, RulesBinder mainBinder,
039                          LinkedRuleBuilder mainBuilder )
040    {
041        super( keyPattern, namespaceURI, mainBinder, mainBuilder );
042    }
043
044    /**
045     * Add an additional attribute name to property name mapping.
046     *
047     * @param attributeName The attribute to match
048     * @param propertyName The java bean property to be assigned the value
049     * @return this builder instance
050     * @deprecated
051     */
052    @Deprecated
053    public SetPropertiesBuilder addAlias( String attributeName, String propertyName )
054    {
055        return addAlias( attributeName ).forProperty( propertyName );
056    }
057
058    /**
059     * Add an additional attribute name to property name mapping.
060     *
061     * @param attributeName The attribute to match
062     * @return the property alias builder
063     * @since 3.2
064     */
065    public AddAliasBuilder<SetPropertiesBuilder> addAlias( String attributeName )
066    {
067        if ( attributeName == null )
068        {
069            reportError( "setProperties().addAlias( String )", "empty 'attributeName' not allowed" );
070        }
071        return new AddAliasBuilder<SetPropertiesBuilder>( this, aliases, attributeName );
072    }
073
074    /**
075     * Add an attribute name to the ignore list.
076     *
077     * @param attributeName The attribute to match has to be ignored
078     * @return this builder instance
079     */
080    public SetPropertiesBuilder ignoreAttribute( String attributeName )
081    {
082        if ( attributeName == null )
083        {
084            reportError( "setProperties().ignoreAttribute( String )", "empty 'attributeName' not allowed" );
085        }
086        return addAlias( attributeName ).forProperty( null );
087    }
088
089    /**
090     * Sets whether attributes found in the XML without matching properties should be ignored.
091     *
092     * If set to false, the parsing will throw an {@code NoSuchMethodException}
093     * if an unmatched attribute is found.
094     * This allows to trap misspellings in the XML file.
095     *
096     * @param ignoreMissingProperty false to stop the parsing on unmatched attributes
097     * @return this builder instance
098     */
099    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}