/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.xbean.spring.context.v2;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.springframework.beans.factory.xml.BeanDefinitionParserDelegate;
import org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader;
import org.springframework.beans.factory.xml.XmlReaderContext;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;

public class XBeanBeanDefinitionDocumentReader extends DefaultBeanDefinitionDocumentReader {

    protected BeanDefinitionParserDelegate createHelper(XmlReaderContext readerContext, Element root) {
        BeanDefinitionParserDelegate delegate = XBeanV2Helper.createParser(readerContext);
        delegate.initDefaults(root);
        return delegate;
    }

    protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) {
        String namespaceUri = root.getNamespaceURI();
        if (!DomUtils.nodeNameEquals(root, "beans") && 
            !delegate.isDefaultNamespace(namespaceUri)) {
            try {
                try {
                    Method m = BeanDefinitionParserDelegate.class.getMethod("parseCustomElement", new Class[] { Element.class });
                    m.invoke(delegate, new Object[] { root });
                } catch (NoSuchMethodException e) {
                    try {
                        Method m = BeanDefinitionParserDelegate.class.getMethod("parseCustomElement", new Class[] { Element.class, boolean.class });
                        m.invoke(delegate, new Object[] { root, Boolean.FALSE });
                    } catch (NoSuchMethodException e2) {
                        throw new IllegalStateException(e);
                    }
                }
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (IllegalArgumentException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                if (e.getCause() instanceof RuntimeException) {
                    throw (RuntimeException) e.getCause();
                }
                throw new RuntimeException(e);
            }
        } else {
            super.parseBeanDefinitions(root, delegate);
        }
    }
    
}

@@ -1247,16 +1247,72 @@
 lement;%0D
+%0Aimport org.w3c.dom.Node;%0D%0Aimport org.w3c.dom.NodeList;%0D
 %0A%0D%0Apubli
@@ -3127,90 +3127,1238 @@
 lse 
-%7B%0D%0A            super.parseBeanDefinitions(root, delegate);%0D%0A        %7D%0D%0A    %7D%0D%0A    
+if (DomUtils.nodeNameEquals(root, %22beans%22)) %7B%0D%0A            NodeList nl = root.getChildNodes();%0D%0A            for (int i = 0; i %3C nl.getLength(); i++) %7B%0D%0A                Node node = nl.item(i);%0D%0A                if (node instanceof Element) %7B%0D%0A                    Element ele = (Element) node;%0D%0A                    String childNamespaceUri = ele.getNamespaceURI();%0D%0A                    if (delegate.isDefaultNamespace(childNamespaceUri)) %7B%0D%0A                        parseDefaultElement(ele, delegate);%0D%0A                    %7D%0D%0A                    else %7B%0D%0A                        delegate.parseCustomElement(ele);%0D%0A                    %7D%0D%0A                %7D%0D%0A            %7D%0D%0A        %7D else %7B%0D%0A            super.parseBeanDefinitions(root, delegate);%0D%0A        %7D%0D%0A    %7D%0D%0A    %0D%0A    private void parseDefaultElement(Element ele, BeanDefinitionParserDelegate delegate) %7B%0D%0A        if (DomUtils.nodeNameEquals(ele, IMPORT_ELEMENT)) %7B%0D%0A            importBeanDefinitionResource(ele);%0D%0A        %7D%0D%0A        else if (DomUtils.nodeNameEquals(ele, ALIAS_ELEMENT)) %7B%0D%0A            processAliasRegistration(ele);%0D%0A        %7D%0D%0A        else if (DomUtils.nodeNameEquals(ele, BEAN_ELEMENT)) %7B%0D%0A            processBeanDefinition(ele, delegate);%0D%0A        %7D%0D%0A    %7D%0D%0A
 %0D%0A%7D%0D

Fix parsing of root "beans" in a namespace. Not sure why test case were not failing before ...