001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.spring.handler;
018
019import org.w3c.dom.Attr;
020import org.w3c.dom.Element;
021import org.w3c.dom.NamedNodeMap;
022
023import org.springframework.beans.factory.support.BeanDefinitionBuilder;
024import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
025import org.springframework.core.Conventions;
026import org.springframework.util.Assert;
027import org.springframework.util.StringUtils;
028
029/**
030 * A base class for a parser for a bean.
031 *
032 * @version 
033 */
034public class BeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
035    private final Class<?> type;
036    private final boolean assignId;
037
038    /**
039     * Bean definition parser
040     *
041     * @param type     the type, can be null
042     * @param assignId whether to allow assigning id from the id attribute on the type
043     *                 (there must be getter/setter id on type class).
044     */
045    public BeanDefinitionParser(Class<?> type, boolean assignId) {
046        this.type = type;
047        this.assignId = assignId;
048    }
049
050    protected Class<?> getBeanClass(Element element) {
051        return type;
052    }
053
054    protected boolean isAssignId() {
055        return assignId;
056    }
057
058    protected boolean isEligibleAttribute(String attributeName) {
059        return attributeName != null && !ID_ATTRIBUTE.equals(attributeName)
060                && !attributeName.equals("xmlns") && !attributeName.startsWith("xmlns:");
061    }
062
063    protected void doParse(Element element, BeanDefinitionBuilder builder) {
064        NamedNodeMap attributes = element.getAttributes();
065        for (int x = 0; x < attributes.getLength(); x++) {
066            Attr attribute = (Attr) attributes.item(x);
067            String name = attribute.getLocalName();
068            String fullName = attribute.getName();
069            // assign id if we want them
070            if (fullName.equals("id") && isAssignId()) {
071                if (attribute.getValue() != null) {
072                    builder.addPropertyValue("id", attribute.getValue());
073                }
074            // assign other attributes if eligible
075            } else if (!fullName.startsWith("xmlns:") && !fullName.equals("xmlns") && isEligibleAttribute(name)) {
076                String propertyName = extractPropertyName(name);
077                Assert.state(StringUtils.hasText(propertyName),
078                        "Illegal property name returned from 'extractPropertyName(String)': cannot be null or empty.");
079                builder.addPropertyValue(propertyName, attribute.getValue());
080            }
081        }
082        postProcess(builder, element);
083    }
084
085
086    /**
087     * Extract a JavaBean property name from the supplied attribute name.
088     * <p>The default implementation uses the
089     * {@link Conventions#attributeNameToPropertyName(String)}
090     * method to perform the extraction.
091     * <p>The name returned must obey the standard JavaBean property name
092     * conventions. For example for a class with a setter method
093     * '<code>setBingoHallFavourite(String)</code>', the name returned had
094     * better be '<code>bingoHallFavourite</code>' (with that exact casing).
095     *
096     * @param attributeName the attribute name taken straight from the
097     *                      XML element being parsed (never <code>null</code>)
098     * @return the extracted JavaBean property name (must never be <code>null</code>)
099     */
100    protected String extractPropertyName(String attributeName) {
101        return Conventions.attributeNameToPropertyName(attributeName);
102    }
103
104    /**
105     * Hook method that derived classes can implement to inspect/change a
106     * bean definition after parsing is complete.
107     * <p>The default implementation does nothing.
108     *
109     * @param beanDefinition the parsed (and probably totally defined) bean definition being built
110     * @param element        the XML element that was the source of the bean definition's metadata
111     */
112    protected void postProcess(BeanDefinitionBuilder beanDefinition, Element element) {
113    }
114
115}