View Javadoc

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