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.config.spring.util;
8   
9   import org.mule.config.spring.parsers.AbstractMuleBeanDefinitionParser;
10  import org.mule.util.StringUtils;
11  import org.mule.util.XMLUtils;
12  
13  import org.apache.commons.logging.Log;
14  import org.apache.commons.logging.LogFactory;
15  import org.springframework.beans.factory.xml.BeanDefinitionParserDelegate;
16  import org.w3c.dom.Element;
17  
18  /**
19   * These only depend on standard (JSE) XML classes and are used by Spring config code.
20   * For a more extensive (sub-)class, see the XMLUtils class in the XML module.
21   */
22  public class SpringXMLUtils extends XMLUtils
23  {
24  
25      private static final Log logger = LogFactory.getLog(SpringXMLUtils.class);
26  
27      public static final String MULE_DEFAULT_NAMESPACE = "http://www.mulesoft.org/schema/mule/core";
28      public static final String MULE_NAMESPACE_PREFIX = "http://www.mulesoft.org/schema/mule/";
29  
30      public static boolean isMuleNamespace(Element element)
31      {
32          String ns = element.getNamespaceURI();
33          return ns != null && ns.startsWith(MULE_NAMESPACE_PREFIX);
34      }
35  
36      public static boolean isBeansNamespace(Element element)
37      {
38          String ns = element.getNamespaceURI();
39          return ns != null && ns.equals(BeanDefinitionParserDelegate.BEANS_NAMESPACE_URI);
40      }
41  
42      public static String getNameOrId(Element element)
43      {
44          String id = element.getAttribute(AbstractMuleBeanDefinitionParser.ATTRIBUTE_ID);
45          String name = element.getAttribute(AbstractMuleBeanDefinitionParser.ATTRIBUTE_NAME);
46          if (StringUtils.isBlank(id))
47          {
48              if (StringUtils.isBlank(name))
49              {
50                  return "";
51              }
52              else
53              {
54                  return name;
55              }
56          }
57          else
58          {
59              if (!StringUtils.isBlank(name) && !name.equals(id))
60              {
61                  logger.warn("Id (" + id + ") and name (" + name + ") differ for " + elementToString(element));
62              }
63              return id;
64          }
65      }
66  
67  }