001    package org.apache.commons.digester3.annotations.handlers;
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.lang.reflect.AnnotatedElement;
023    import java.lang.reflect.Constructor;
024    
025    import org.apache.commons.digester3.annotations.AnnotationHandler;
026    import org.apache.commons.digester3.annotations.rules.ObjectCreate;
027    import org.apache.commons.digester3.binder.ObjectCreateBuilder;
028    import org.apache.commons.digester3.binder.RulesBinder;
029    
030    /**
031     * {@link ObjectCreateHandler} handler.
032     *
033     * @since 3.0
034     */
035    public final class ObjectCreateHandler
036        implements AnnotationHandler<ObjectCreate, AnnotatedElement>
037    {
038    
039        /**
040         * {@inheritDoc}
041         */
042        public void handle( ObjectCreate annotation, AnnotatedElement element, RulesBinder rulesBinder )
043        {
044            Class<?> type = null;
045            if ( element instanceof Class<?> )
046            {
047                type = (Class<?>) element;
048            }
049            else if ( element instanceof Constructor<?> )
050            {
051                type = ( (Constructor<?>) element ).getDeclaringClass();
052            }
053            else
054            {
055                rulesBinder.addError( "Misplaced @ObjectCreate annotation to %s, Class and Constructor only supported",
056                                      element );
057                return;
058            }
059    
060            ObjectCreateBuilder builder = rulesBinder
061                    .forPattern( annotation.pattern() )
062                    .withNamespaceURI( annotation.namespaceURI() )
063                    .createObject()
064                    .ofType( type )
065                    .ofTypeSpecifiedByAttribute( annotation.attributeName() != null ? annotation.attributeName() : null );
066    
067            if ( element instanceof Constructor<?> )
068            {
069                Constructor<?> method = (Constructor<?>) element;
070                builder.usingConstructor( method.getParameterTypes() );
071            }
072        }
073    
074    }