KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > spring > factory > NamedXmlBeanDefinitionParser


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22
23 package org.jboss.spring.factory;
24
25 import java.util.regex.Matcher JavaDoc;
26 import java.util.regex.Pattern JavaDoc;
27
28 import org.jboss.util.naming.Util;
29 import org.springframework.beans.factory.BeanDefinitionStoreException;
30 import org.springframework.beans.factory.BeanFactory;
31 import org.springframework.beans.factory.config.ConfigurableBeanFactory;
32 import org.springframework.beans.factory.xml.BeanDefinitionParserDelegate;
33 import org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader;
34 import org.w3c.dom.Element JavaDoc;
35 import org.w3c.dom.Node JavaDoc;
36 import org.w3c.dom.NodeList JavaDoc;
37
38 /**
39  * @author <a HREF="mailto:ales.justin@genera-lynx.com">Ales Justin</a>
40  */

41 public class NamedXmlBeanDefinitionParser extends DefaultBeanDefinitionDocumentReader
42       implements Nameable, Instantiable
43 {
44
45    public static final String JavaDoc BEAN_FACTORY_ELEMENT = "BeanFactory=\\(([^)]+)\\)";
46    public static final String JavaDoc PARENT_BEAN_FACTORY_ELEMENT = "ParentBeanFactory=\\(([^)]+)\\)";
47    public static final String JavaDoc INSTANTIATION_ELEMENT = "Instantiate=\\(([^)]+)\\)";
48
49    private ConfigurableBeanFactory beanFactory;
50    private String JavaDoc beanFactoryName;
51    private boolean instantiate;
52
53    public NamedXmlBeanDefinitionParser(ConfigurableBeanFactory beanFactory)
54    {
55       this.beanFactory = beanFactory;
56    }
57
58    protected void preProcessXml(Element JavaDoc root) throws BeanDefinitionStoreException
59    {
60       NodeList JavaDoc nl = root.getChildNodes();
61       for (int i = 0; i < nl.getLength(); i++)
62       {
63          Node JavaDoc node = nl.item(i);
64          if (node instanceof Element JavaDoc)
65          {
66             Element JavaDoc ele = (Element JavaDoc) node;
67             if (BeanDefinitionParserDelegate.DESCRIPTION_ELEMENT.equals(node.getNodeName()))
68             {
69                String JavaDoc nodeValue = ele.getFirstChild().getNodeValue();
70                logger.info("Bean names [description tag]: " + nodeValue);
71                Matcher JavaDoc bfm = parse(nodeValue, BEAN_FACTORY_ELEMENT);
72                if (bfm.find())
73                {
74                   beanFactoryName = bfm.group(1);
75                }
76                Matcher JavaDoc pbfm = parse(nodeValue, PARENT_BEAN_FACTORY_ELEMENT);
77                if (pbfm.find())
78                {
79                   String JavaDoc parentBeanFactoryName = pbfm.group(1);
80                   try
81                   {
82                      beanFactory.setParentBeanFactory((BeanFactory) Util.lookup(parentBeanFactoryName, BeanFactory.class));
83                   }
84                   catch (Exception JavaDoc e)
85                   {
86                      throw new BeanDefinitionStoreException("Failure during parent bean factory JNDI lookup: " + parentBeanFactoryName, e);
87                   }
88                }
89                Matcher JavaDoc inst = parse(nodeValue, INSTANTIATION_ELEMENT);
90                if (inst.find())
91                {
92                   instantiate = Boolean.parseBoolean(inst.group(1));
93                }
94             }
95          }
96       }
97    }
98
99    public String JavaDoc getName()
100    {
101       return beanFactoryName;
102    }
103
104    public boolean doInstantiate()
105    {
106       return instantiate;
107    }
108
109    private static Matcher JavaDoc parse(String JavaDoc value, String JavaDoc regexp)
110    {
111       Pattern JavaDoc pattern = Pattern.compile(regexp);
112       return pattern.matcher(value);
113    }
114
115 }
116
Popular Tags