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.Iterator;
14  import java.util.LinkedList;
15  import java.util.List;
16  
17  import org.w3c.dom.Attr;
18  import org.w3c.dom.Element;
19  import org.w3c.dom.NamedNodeMap;
20  
21  /**
22   * If this attribute is present, no other can be
23   */
24  public class CheckExclusiveAttribute implements PreProcessor
25  {
26  
27      public static final int NONE = -1;
28      private String attribute;
29  
30      public CheckExclusiveAttribute(String attribute)
31      {
32          this.attribute = attribute;
33      }
34  
35      public void preProcess(PropertyConfiguration config, Element element)
36      {
37          List foundAttributes = new LinkedList();
38          boolean found = false;
39  
40          NamedNodeMap attributes = element.getAttributes();
41          for (int i = 0; i < attributes.getLength(); i++)
42          {
43              String alias = SpringXMLUtils.attributeName((Attr) attributes.item(i));
44              if (! config.isIgnored(alias))
45              {
46                  if (attribute.equals(alias))
47                  {
48                      found = true;
49                  }
50                  else
51                  {
52                      foundAttributes.add(alias);
53                  }
54              }
55          }
56  
57          if (found && foundAttributes.size() > 0)
58          {
59              StringBuffer message = new StringBuffer("The attribute '");
60              message.append(attribute);
61              message.append("' cannot appear with the attribute");
62              if (foundAttributes.size() > 1)
63              {
64                  message.append("s");
65              }
66              Iterator others = foundAttributes.iterator();
67              while (others.hasNext())
68              {
69                  message.append(" '");
70                  message.append(others.next());
71                  message.append("'");
72              }
73              message.append(" in element ");
74              message.append(SpringXMLUtils.elementToString(element));
75              message.append(".");
76              throw new CheckExclusiveAttributeException(message.toString());
77          }
78      }
79  
80      public static class CheckExclusiveAttributeException extends IllegalStateException
81      {
82  
83          private CheckExclusiveAttributeException(String message)
84          {
85              super(message);
86          }
87  
88      }
89  
90  }