001    package 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    
022    import java.util.HashMap;
023    import java.util.Map;
024    
025    import org.apache.commons.digester3.SetNestedPropertiesRule;
026    
027    /**
028     * Builder chained when invoking {@link LinkedRuleBuilder#setNestedProperties()}.
029     *
030     * @since 3.0
031     */
032    public final class NestedPropertiesBuilder
033        extends AbstractBackToLinkedRuleBuilder<SetNestedPropertiesRule>
034    {
035    
036        private final Map<String, String> elementNames = new HashMap<String, String>();
037    
038        private boolean trimData = true;
039    
040        private boolean allowUnknownChildElements = false;
041    
042        NestedPropertiesBuilder( String keyPattern, String namespaceURI, RulesBinder mainBinder,
043                                        LinkedRuleBuilder mainBuilder )
044        {
045            super( keyPattern, namespaceURI, mainBinder, mainBuilder );
046        }
047    
048        /**
049         * Allows ignore a matching element.
050         *
051         * @param elementName The child xml element to be ignored
052         * @return this builder instance
053         */
054        public NestedPropertiesBuilder ignoreElement( String elementName )
055        {
056            if ( elementName == null )
057            {
058                reportError( "setNestedProperties().ignoreElement( String )", "empty 'elementName' not allowed" );
059            }
060            else
061            {
062                addAlias( elementName, null );
063            }
064            return this;
065        }
066    
067        /**
068         * Allows element2property mapping to be overridden.
069         *
070         * @param elementName The child xml element to match
071         * @param propertyName The java bean property to be assigned the value
072         * @return this builder instance
073         */
074        public NestedPropertiesBuilder addAlias( String elementName, String propertyName )
075        {
076            if ( elementName == null )
077            {
078                reportError( "setNestedProperties().addAlias( String,String )", "empty 'elementName' not allowed" );
079            }
080            else
081            {
082                elementNames.put( elementName, propertyName );
083            }
084            return this;
085        }
086    
087        /**
088         * When set to true, any text within child elements will have leading
089         * and trailing whitespace removed before assignment to the target
090         * object.
091         *
092         * @param trimData Flag to set any text within child elements will have leading
093         *                 and trailing whitespace removed
094         * @return this builder instance
095         */
096        public NestedPropertiesBuilder trimData( boolean trimData )
097        {
098            this.trimData = trimData;
099            return this;
100        }
101    
102        /**
103         * Determines whether an error is reported when a nested element is encountered for which there is no corresponding
104         * property-setter method.
105         *
106         * @param allowUnknownChildElements flag to ignore any child element for which there is no corresponding
107         *        object property
108         * @return this builder instance
109         */
110        public NestedPropertiesBuilder allowUnknownChildElements( boolean allowUnknownChildElements )
111        {
112            this.allowUnknownChildElements = allowUnknownChildElements;
113            return this;
114        }
115    
116        /**
117         * {@inheritDoc}
118         */
119        @Override
120        protected SetNestedPropertiesRule createRule()
121        {
122            SetNestedPropertiesRule rule = new SetNestedPropertiesRule( elementNames );
123            rule.setTrimData( trimData );
124            rule.setAllowUnknownChildElements( allowUnknownChildElements );
125            return rule;
126        }
127    
128    }