1 10 11 package org.nanocontainer.script.groovy.buildernodes; 12 13 import java.io.Serializable ; 14 import java.util.Collections ; 15 import java.util.HashSet ; 16 import java.util.Set ; 17 18 import org.nanocontainer.script.groovy.BuilderNode; 19 import java.util.Map ; 20 import org.nanocontainer.script.NanoContainerMarkupException; 21 import java.util.Iterator ; 22 23 33 abstract public class AbstractBuilderNode implements BuilderNode, Serializable { 34 35 38 private final String nodeName; 39 40 41 44 private Set supportedAttributes = new HashSet (); 45 46 47 48 54 public AbstractBuilderNode(final String nodeName) { 55 this.nodeName = nodeName; 56 57 } 58 59 60 65 protected AbstractBuilderNode addAttribute(final String name) { 66 supportedAttributes.add(name); 67 return this; 68 } 69 70 71 public String getNodeName() { 72 return nodeName; 73 } 74 75 76 public Set getSupportedAttributes() { 77 return Collections.unmodifiableSet(supportedAttributes); 78 } 79 80 public String toString() { 81 return "Nanocontainer Builder Node: " + this.getClass().getName() + " (\"" + getNodeName() + "\")"; 82 } 83 84 91 protected boolean isAttribute(final Map attributes, final String key) { 92 return attributes.containsKey(key) && attributes.get(key) != null; 93 } 94 95 105 public void validateScriptedAttributes(final Map specifiedAttributes) throws NanoContainerMarkupException { 106 Set specifiedAttributeNames = specifiedAttributes.keySet(); 107 if (this.getSupportedAttributes().containsAll(specifiedAttributeNames)) { 108 return; 109 } 110 111 Set unknownAttributes = new HashSet (specifiedAttributeNames); 112 unknownAttributes.removeAll(this.getSupportedAttributes()); 113 114 StringBuffer errorMessage = new StringBuffer (); 115 errorMessage.append("Found one or more unknown attributes for builder node '"); 116 errorMessage.append(this.getNodeName()); 117 errorMessage.append("': "); 118 errorMessage.append(convertSetToCommaDelimitedString(unknownAttributes)); 119 errorMessage.append(". Recognized Attributes For this node are ["); 120 errorMessage.append(convertSetToCommaDelimitedString(this.getSupportedAttributes())); 121 errorMessage.append("]."); 122 123 throw new NanoContainerMarkupException(errorMessage.toString()); 124 } 125 126 134 private String convertSetToCommaDelimitedString(final Set specifiedSet) { 135 136 StringBuffer result = new StringBuffer (); 137 138 boolean needComma = false; 139 for (Iterator i = specifiedSet.iterator(); i.hasNext();) { 140 if (needComma) { 141 result.append(","); 142 } else { 143 needComma = true; 144 } 145 146 result.append(i.next().toString()); 147 } 148 return result.toString(); 149 } 150 151 } 152 | Popular Tags |