View Javadoc

1   /*
2    * $Id: CheckRequiredAttributes.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.ArrayList;
18  import java.util.Arrays;
19  import java.util.Collection;
20  import java.util.HashSet;
21  import java.util.List;
22  import java.util.Set;
23  
24  import org.w3c.dom.Attr;
25  import org.w3c.dom.Element;
26  import org.w3c.dom.NamedNodeMap;
27  
28  /**
29   * All attributes from at least one set must be provided
30   */
31  public class CheckRequiredAttributes implements PreProcessor
32  {
33      Collection<List<String>> attributeSets;
34      
35      public CheckRequiredAttributes(String[][] attributeNames)
36      {
37          super();
38  
39          attributeSets = new ArrayList<List<String>>();
40          for (int i = 0; i < attributeNames.length; i++)
41          {
42              String[] currentSet = attributeNames[i];
43              if (currentSet.length > 0)
44              {
45                  List<String> list = Arrays.asList(currentSet);
46                  attributeSets.add(list);
47              }
48          }
49      }
50  
51      public void preProcess(PropertyConfiguration config, Element element)
52      {
53          Collection<List<String>> matchingSets = new ArrayList<List<String>>();
54          
55          for (List<String> currentSet : attributeSets)
56          {
57              if (containsAllRequiredAttributes(currentSet, element))
58              {
59                  matchingSets.add(currentSet);
60              }
61          }
62          
63          if (matchingSets.size() == 0)
64          {
65              throw new CheckRequiredAttributesException(element, attributeSets);
66          }
67      }
68  
69      private boolean containsAllRequiredAttributes(List<String> currentSet, Element element)
70      {
71          Set<String> attributes = collectAttributes(element);
72          if (attributes.size() == 0)
73          {
74              return false;
75          }
76          
77          // Clone the set of attribute names and subtract all the element's attribute names from it.
78          // If the remaining set is empty, all required attributes of this set were present.
79          Set<String> remainingElementNames = new HashSet<String>(currentSet);
80          remainingElementNames.removeAll(attributes);
81          return (remainingElementNames.size() == 0);
82      }
83      
84      private Set<String> collectAttributes(Element element)
85      {
86          Set<String> attributeNames = new HashSet<String>();
87          
88          NamedNodeMap attributes = element.getAttributes();
89          for (int i = 0; i < attributes.getLength(); i++)
90          {
91              String alias = SpringXMLUtils.attributeName((Attr) attributes.item(i));
92              attributeNames.add(alias);
93          }
94          
95          return attributeNames;
96      }
97  
98      public static class CheckRequiredAttributesException extends IllegalStateException
99      {
100         private static String summary(Collection<List<String>> attributeSets)
101         {
102             StringBuilder buf = new StringBuilder();
103             for (List<String> set : attributeSets)
104             {
105                 if (buf.length() > 0)
106                 {
107                     buf.append(" ");
108                 }
109                 
110                 if (set.isEmpty())
111                 {
112                     continue;
113                 }
114                 
115                 buf.append(set.toString());
116             }
117             return buf.toString();
118         }
119 
120         private CheckRequiredAttributesException(Element element, Collection<List<String>> attributeSets)
121         {
122             super("Element " + SpringXMLUtils.elementToString(element) +
123                     " must have all attributes for one of the sets: " + summary(attributeSets) + ".");
124         }
125     }
126 }