001    /* 
002     * Copyright 2008-2009 the original author or authors.
003     * The contents of this file are subject to the Mozilla Public License
004     * Version 1.1 (the "License"); you may not use this file except in
005     * compliance with the License. You may obtain a copy of the License at
006     * http://www.mozilla.org/MPL/
007     *
008     * Software distributed under the License is distributed on an "AS IS"
009     * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
010     * License for the specific language governing rights and limitations
011     * under the License.
012     */
013     
014    package com.mtgi.analytics.aop.config.v11;
015    
016    import org.springframework.beans.factory.config.BeanDefinition;
017    import org.springframework.beans.factory.config.BeanDefinitionHolder;
018    import org.springframework.beans.factory.xml.BeanDefinitionParser;
019    import org.springframework.beans.factory.xml.ParserContext;
020    import org.springframework.util.StringUtils;
021    import org.w3c.dom.Element;
022    
023    import com.mtgi.analytics.BehaviorTrackingManagerImpl;
024    
025    /**
026     * Generic parser for inner bean definitions enclosed by a <code>bt:manager</code> tag.  These bean definitions
027     * are generally the same as standard Spring XML bean definitions, with a <code>class</code> attribute and nested
028     * <code>property</code> elements rather than custom XML attributes.  At time of writing, this parser handles both
029     * <code>bt:custom-persister</code> and <code>bt:session-context</code> tags.
030     */
031    public class BtInnerBeanDefinitionParser implements BeanDefinitionParser {
032    
033            private String property;
034    
035            /** @param property the property of {@link BehaviorTrackingManagerImpl} that should receive this bean definition. */
036            public BtInnerBeanDefinitionParser(String property) {
037                    this.property = property;
038            }
039    
040            public BeanDefinition parse(Element element, ParserContext parserContext) {
041                    //no custom attributes, delegate to default definition parser.
042                    BeanDefinitionHolder ret = parserContext.getDelegate().parseBeanDefinitionElement(element);
043                    if (ret != null) {
044                            //add parsed inner bean to containing manager definition if applicable
045                            if (!BtManagerBeanDefinitionParser.registerNestedBean(ret, property, parserContext)) {
046                                    
047                                    //add bean to global registry
048                                    BeanDefinition def = ret.getBeanDefinition();
049                                    
050                                    String id = element.getAttribute("id");
051                                    if (StringUtils.hasText(id))
052                                            parserContext.getRegistry().registerBeanDefinition(id, def);
053                                    else
054                                            parserContext.getReaderContext().registerWithGeneratedName(def);
055    
056                                    return def;
057                            }
058                                    
059                    }
060                    //global bean definition not created, probably some parse error.
061                    return null;
062            }
063    
064    }