View Javadoc

1   /*
2    * $Id: NamespaceManagerDefinitionParser.java 20321 2010-11-24 15:21:24Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  package org.mule.module.xml.config;
11  
12  import org.mule.api.config.MuleProperties;
13  import org.mule.config.spring.parsers.AbstractMuleBeanDefinitionParser;
14  import org.mule.config.spring.parsers.generic.OrphanDefinitionParser;
15  import org.mule.module.xml.util.NamespaceManager;
16  
17  import java.util.HashMap;
18  import java.util.Map;
19  
20  import org.springframework.beans.factory.support.BeanDefinitionBuilder;
21  import org.springframework.beans.factory.xml.ParserContext;
22  import org.w3c.dom.Element;
23  import org.w3c.dom.Node;
24  
25  /**
26   * TODO
27   */
28  public class NamespaceManagerDefinitionParser extends OrphanDefinitionParser
29  {
30      public NamespaceManagerDefinitionParser()
31      {
32          super(NamespaceManager.class, true);
33      }
34  
35      /**
36       * Parse the supplied {@link org.w3c.dom.Element} and populate the supplied
37       * {@link org.springframework.beans.factory.support.BeanDefinitionBuilder} as required.
38       * <p>The default implementation delegates to the <code>doParse</code>
39       * version without ParserContext argument.
40       *
41       * @param element       the XML element being parsed
42       * @param parserContext the object encapsulating the current state of the parsing process
43       * @param builder       used to define the <code>BeanDefinition</code>
44       */
45      @Override
46      protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder)
47      {
48          Map<String, String> ns = new HashMap<String, String>();
49  
50          for (int i = 0; i < element.getParentNode().getAttributes().getLength(); i++)
51          {
52              Node node = element.getParentNode().getAttributes().item(i);
53              String prefix = node.getNodeName();
54              if (prefix.startsWith("xmlns"))
55              {
56                  if (prefix.indexOf(":") > 0)
57                  {
58                      prefix = prefix.substring(prefix.indexOf(":") + 1);
59                  }
60                  else
61                  {
62                      prefix = "";
63                  }
64                  ns.put(prefix, node.getNodeValue());
65              }
66          }
67          builder.addPropertyValue("configNamespaces", ns);
68  
69          // this id must match the bean name
70          element.setAttribute(AbstractMuleBeanDefinitionParser.ATTRIBUTE_ID, MuleProperties.OBJECT_MULE_NAMESPACE_MANAGER);
71  
72          super.doParse(element, parserContext, builder);
73      }
74  
75      @Override
76      public String getBeanName(Element element)
77      {
78          return MuleProperties.OBJECT_MULE_NAMESPACE_MANAGER;
79      }
80  }