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 org.apache.commons.digester3.ObjectCreateRule;
023    
024    /**
025     * Builder chained when invoking {@link LinkedRuleBuilder#createObject()}.
026     *
027     * @since 3.0
028     */
029    public final class ObjectCreateBuilder
030        extends AbstractBackToLinkedRuleBuilder<ObjectCreateRule>
031    {
032    
033        private final ClassLoader classLoader;
034    
035        private Class<?> type;
036    
037        private String attributeName;
038    
039        ObjectCreateBuilder( String keyPattern, String namespaceURI, RulesBinder mainBinder, LinkedRuleBuilder mainBuilder,
040                             ClassLoader classLoader )
041        {
042            super( keyPattern, namespaceURI, mainBinder, mainBuilder );
043            this.classLoader = classLoader;
044        }
045    
046        /**
047         * Construct an object with the specified class name.
048         *
049         * @param className Java class name of the object to be created
050         * @return this builder instance
051         */
052        public ObjectCreateBuilder ofType( String className )
053        {
054            if ( className == null )
055            {
056                reportError( "createObject().ofType( String )", "NULL Java type not allowed" );
057                return this;
058            }
059    
060            try
061            {
062                return ofType( this.classLoader.loadClass( className ) );
063            }
064            catch ( ClassNotFoundException e )
065            {
066                reportError( "createObject().ofType( String )", String.format( "class '%s' cannot be load", className ) );
067                return this;
068            }
069        }
070    
071        /**
072         * Construct an object with the specified class.
073         *
074         * @param <T> any java type
075         * @param type Java class of the object to be created
076         * @return this builder instance
077         */
078        public <T> ObjectCreateBuilder ofType( Class<T> type )
079        {
080            if ( type == null )
081            {
082                reportError( "createObject().ofType( Class<?> )", "NULL Java type not allowed" );
083                return this;
084            }
085    
086            this.type = type;
087    
088            return this;
089        }
090    
091        /**
092         * Allows specify the attribute containing an override class name if it is present.
093         *
094         * @param attributeName The attribute containing an override class name if it is present
095         * @return this builder instance
096         */
097        public ObjectCreateBuilder ofTypeSpecifiedByAttribute( /* @Nullable */String attributeName )
098        {
099            this.attributeName = attributeName;
100            return this;
101        }
102    
103        /**
104         * {@inheritDoc}
105         */
106        @Override
107        protected ObjectCreateRule createRule()
108        {
109            return new ObjectCreateRule( attributeName, type );
110        }
111    
112    }