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 041 * (there must be getter/setter id on type class). 042 */ 043 public BeanDefinitionParser(Class<?> type, boolean assignId) { 044 this.type = type; 045 this.assignId = assignId; 046 } 047 048 protected Class<?> getBeanClass(Element element) { 049 return type; 050 } 051 052 protected boolean isAssignId() { 053 return assignId; 054 } 055 056 protected boolean isEligibleAttribute(String attributeName) { 057 return attributeName != null && !ID_ATTRIBUTE.equals(attributeName) 058 && !attributeName.equals("xmlns") && !attributeName.startsWith("xmlns:"); 059 } 060 061 protected void doParse(Element element, BeanDefinitionBuilder builder) { 062 NamedNodeMap attributes = element.getAttributes(); 063 for (int x = 0; x < attributes.getLength(); x++) { 064 Attr attribute = (Attr) attributes.item(x); 065 String name = attribute.getLocalName(); 066 String fullName = attribute.getName(); 067 // assign id if we want them 068 if (fullName.equals("id") && isAssignId()) { 069 if (attribute.getValue() != null) { 070 builder.addPropertyValue("id", attribute.getValue()); 071 } 072 // assign other attributes if eligible 073 } else if (!fullName.startsWith("xmlns:") && !fullName.equals("xmlns") && isEligibleAttribute(name)) { 074 String propertyName = extractPropertyName(name); 075 Assert.state(StringUtils.hasText(propertyName), 076 "Illegal property name returned from 'extractPropertyName(String)': cannot be null or empty."); 077 builder.addPropertyValue(propertyName, attribute.getValue()); 078 } 079 } 080 postProcess(builder, element); 081 } 082 083 084 /** 085 * Extract a JavaBean property name from the supplied attribute name. 086 * <p>The default implementation uses the 087 * {@link Conventions#attributeNameToPropertyName(String)} 088 * method to perform the extraction. 089 * <p>The name returned must obey the standard JavaBean property name 090 * conventions. For example for a class with a setter method 091 * '<code>setBingoHallFavourite(String)</code>', the name returned had 092 * better be '<code>bingoHallFavourite</code>' (with that exact casing). 093 * 094 * @param attributeName the attribute name taken straight from the 095 * XML element being parsed (never <code>null</code>) 096 * @return the extracted JavaBean property name (must never be <code>null</code>) 097 */ 098 protected String extractPropertyName(String attributeName) { 099 return Conventions.attributeNameToPropertyName(attributeName); 100 } 101 102 /** 103 * Hook method that derived classes can implement to inspect/change a 104 * bean definition after parsing is complete. 105 * <p>The default implementation does nothing. 106 * 107 * @param beanDefinition the parsed (and probably totally defined) bean definition being built 108 * @param element the XML element that was the source of the bean definition's metadata 109 */ 110 protected void postProcess(BeanDefinitionBuilder beanDefinition, Element element) { 111 } 112 113}