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