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.parsers.processors;
8   
9   import org.mule.config.spring.parsers.PreProcessor;
10  import org.mule.config.spring.parsers.assembly.configuration.PropertyConfiguration;
11  import org.mule.config.spring.util.SpringXMLUtils;
12  
13  import java.util.Arrays;
14  import java.util.HashSet;
15  import java.util.Set;
16  
17  import org.w3c.dom.Attr;
18  import org.w3c.dom.Element;
19  import org.w3c.dom.NamedNodeMap;
20  
21  /**
22   * Throws an exception if any of the disallowed attributes (after translation) is present.
23   * Designed to cooperates with
24   * {@link org.mule.config.spring.parsers.delegate.AbstractSerialDelegatingDefinitionParser#addHandledException(Class)}
25   */
26  public class BlockAttribute implements PreProcessor
27  {
28      private Set<String> disallowed;
29  
30      public BlockAttribute(String disallowed)
31      {
32          this(new String[]{ disallowed });
33      }
34  
35      public BlockAttribute(String[] disallowed)
36      {
37          this.disallowed = new HashSet<String>(Arrays.asList(disallowed));
38      }
39  
40      public void preProcess(PropertyConfiguration config, Element element)
41      {
42          NamedNodeMap attributes = element.getAttributes();
43          for (int i = 0; i < attributes.getLength(); i++)
44          {
45              String alias = SpringXMLUtils.attributeName((Attr) attributes.item(i));
46              String name = config.translateName(alias);
47              if (disallowed.contains(name))
48              {
49                  throw new BlockAttributeException("Attribute " + alias + " is not allowed here.");
50              }
51          }
52      }
53  
54      public static class BlockAttributeException extends IllegalStateException
55      {
56          BlockAttributeException(String message)
57          {
58              super(message);
59          }
60      }
61  }