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.HashMap;
18  import java.util.Iterator;
19  import java.util.LinkedList;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.w3c.dom.Attr;
24  import org.w3c.dom.Element;
25  import org.w3c.dom.NamedNodeMap;
26  
27  /**
28   * Attributes from two different sets cannot appear together
29   */
30  public class CheckExclusiveAttributes implements PreProcessor
31  {
32  
33      public static final int NONE = -1;
34      private Map knownAttributes = new HashMap();
35  
36      public CheckExclusiveAttributes(String[][] attributeSets)
37      {
38          for (int set = 0; set < attributeSets.length; set++)
39          {
40              String[] attributes = attributeSets[set];
41              for (int attribute = 0; attribute < attributes.length; attribute++)
42              {
43                  knownAttributes.put(attributes[attribute], new Integer(set));
44              }
45          }
46      }
47  
48      public void preProcess(PropertyConfiguration config, Element element)
49      {
50          List foundAttributes = new LinkedList();
51          int foundSetIndex = NONE;
52  
53          NamedNodeMap attributes = element.getAttributes();
54          for (int i = 0; i < attributes.getLength(); i++)
55          {
56              String alias = SpringXMLUtils.attributeName((Attr) attributes.item(i));
57              // don't translate to alias because the error message is in terms of the attributes
58              // the user enters - we don't want to expose the details of translations
59  //            String name = null == config ? alias : config.translateName(alias);
60              if (knownAttributes.containsKey(alias))
61              {
62                  int index = ((Integer) knownAttributes.get(alias)).intValue();
63                  if (foundSetIndex != NONE && foundSetIndex != index)
64                  {
65                      StringBuffer message = new StringBuffer("The attribute '");
66                      message.append(alias);
67                      message.append("' cannot appear with the attribute");
68                      if (foundAttributes.size() > 1)
69                      {
70                          message.append("s");
71                      }
72                      Iterator found = foundAttributes.iterator();
73                      while (found.hasNext())
74                      {
75                          message.append(" '");
76                          message.append(found.next());
77                          message.append("'");
78                      }
79                      message.append(" in element ");
80                      message.append(SpringXMLUtils.elementToString(element));
81                      message.append(".");
82                      throw new CheckExclusiveAttributesException(message.toString());
83                  }
84                  else
85                  {
86                      foundSetIndex = index;
87                      foundAttributes.add(alias);
88                  }
89              }
90          }
91      }
92  
93      public static class CheckExclusiveAttributesException extends IllegalStateException
94      {
95  
96          private CheckExclusiveAttributesException(String message)
97          {
98              super(message);
99          }
100 
101     }
102 
103 }