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 */
032public class BeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
033    private final Class<?> type;
034    private final boolean assignId;
035
036    /**
037     * Bean definition parser
038     *
039     * @param type     the type, can be null
040     * @param assignId whether to allow assigning id from the id attribute on the type (there must be getter/setter id
041     *                 on type class).
042     */
043    public BeanDefinitionParser(Class<?> type, boolean assignId) {
044        this.type = type;
045        this.assignId = assignId;
046    }
047
048    @Override
049    protected Class<?> getBeanClass(Element element) {
050        return type;
051    }
052
053    protected boolean isAssignId() {
054        return assignId;
055    }
056
057    protected boolean isEligibleAttribute(String attributeName) {
058        return attributeName != null && !ID_ATTRIBUTE.equals(attributeName)
059                && !attributeName.equals("xmlns") && !attributeName.startsWith("xmlns:");
060    }
061
062    @Override
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     * Extract a JavaBean property name from the supplied attribute name.
087     * <p>
088     * The default implementation uses the {@link Conventions#attributeNameToPropertyName(String)} method to perform the
089     * extraction.
090     * <p>
091     * The name returned must obey the standard JavaBean property name conventions. For example for a class with a
092     * setter method '<code>setBingoHallFavourite(String)</code>', the name returned had better be
093     * '<code>bingoHallFavourite</code>' (with that exact casing).
094     *
095     * @param  attributeName the attribute name taken straight from the XML element being parsed (never
096     *                       <code>null</code>)
097     * @return               the extracted JavaBean property name (must never be <code>null</code>)
098     */
099    protected String extractPropertyName(String attributeName) {
100        return Conventions.attributeNameToPropertyName(attributeName);
101    }
102
103    /**
104     * Hook method that derived classes can implement to inspect/change a bean definition after parsing is complete.
105     * <p>
106     * The default implementation does nothing.
107     *
108     * @param beanDefinition the parsed (and probably totally defined) bean definition being built
109     * @param element        the XML element that was the source of the bean definition's metadata
110     */
111    protected void postProcess(BeanDefinitionBuilder beanDefinition, Element element) {
112    }
113
114}