View Javadoc

1   /*
2    * $Id: CheckRequiredAttributes.java 10787 2008-02-12 18:51:50Z dfeist $
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.Map;
20  
21  import org.w3c.dom.Attr;
22  import org.w3c.dom.Element;
23  import org.w3c.dom.NamedNodeMap;
24  
25  /**
26   * All attributes from at least one set must be provided
27   */
28  public class CheckRequiredAttributes implements PreProcessor
29  {
30  
31      // maps from attribute name to attribute set index (integer)
32      private Map knownAttributes = new HashMap();
33      // maps from attribute set index to number of attributes in that set
34      private Map numberOfAttributes = new HashMap();
35      // description of acceptable attributes
36      private String summary;
37  
38      public CheckRequiredAttributes(String[][] attributeSets)
39      {
40          StringBuffer buffer = new StringBuffer();
41          for (int set = 0; set < attributeSets.length; set++)
42          {
43              String[] attributes = attributeSets[set];
44              // ignore empty sets
45              if (attributes.length > 0)
46              {
47                  Integer index = new Integer(set);
48                  numberOfAttributes.put(index, new Integer(attributes.length));
49                  if (set > 0)
50                  {
51                      buffer.append("; ");
52                  }
53                  for (int attribute = 0; attribute < attributes.length; attribute++)
54                  {
55                      knownAttributes.put(attributes[attribute], index);
56                      if (attribute > 0)
57                      {
58                          buffer.append(", ");
59                      }
60                      // don't translate to alias because the error message is in terms of the attributes
61                      // the user enters - we don't want to expose the details of translations
62                      buffer.append(attributes[attribute]);
63                  }
64              }
65          }
66          summary = buffer.toString();
67      }
68  
69      public void preProcess(PropertyConfiguration config, Element element)
70      {
71          // map from attribute set index to count
72          Map foundAttributesCount = new HashMap();
73  
74          NamedNodeMap attributes = element.getAttributes();
75          for (int i = 0; i < attributes.getLength(); i++)
76          {
77              String alias = SpringXMLUtils.attributeName((Attr) attributes.item(i));
78              if (knownAttributes.containsKey(alias))
79              {
80                  Integer index = (Integer) knownAttributes.get(alias);
81                  if (!foundAttributesCount.containsKey(index))
82                  {
83                      foundAttributesCount.put(index, new Integer(0));
84                  }
85                  foundAttributesCount.put(index,
86                          new Integer(1 + ((Integer) foundAttributesCount.get(index)).intValue()));
87  
88              }
89          }
90  
91          // if there are no attributes to check for, we are ok
92          boolean ok = knownAttributes.size() == 0;
93          Iterator indices = foundAttributesCount.keySet().iterator();
94          while (indices.hasNext() && !ok)
95          {
96              Object index = indices.next();
97              Object count = foundAttributesCount.get(index);
98              ok = numberOfAttributes.get(index).equals(count);
99          }
100         if (!ok)
101         {
102             throw new CheckRequiredAttributesException(element, summary);
103         }
104     }
105 
106     public static class CheckRequiredAttributesException extends IllegalStateException
107     {
108 
109         private CheckRequiredAttributesException(Element element, String summary)
110         {
111             super("Element " + SpringXMLUtils.elementToString(element) +
112                     " must have all attributes for one of the sets: " + summary + ".");
113         }
114 
115     }
116 
117 }