KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nanocontainer > script > groovy > buildernodes > AbstractBuilderNode


1 /*****************************************************************************
2  * Copyright (C) NanoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  * Original code by James Strachan *
9  *****************************************************************************/

10
11 package org.nanocontainer.script.groovy.buildernodes;
12
13 import java.io.Serializable JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.nanocontainer.script.groovy.BuilderNode;
19 import java.util.Map JavaDoc;
20 import org.nanocontainer.script.NanoContainerMarkupException;
21 import java.util.Iterator JavaDoc;
22
23 /**
24  * Abstract base class for custom nodes. Also provides basic services and
25  * construction capabilities.
26  * @author James Strachan
27  * @author Paul Hammant
28  * @author Aslak Hellesøy
29  * @author Michael Rimov
30  * @author Mauro Talevi
31  * @version $Revision: 2443 $
32  */

33 abstract public class AbstractBuilderNode implements BuilderNode, Serializable JavaDoc {
34
35     /**
36      * The name of the node we're working with.
37      */

38     private final String JavaDoc nodeName;
39
40
41     /**
42      * A set of all possible supported attribute names.
43      */

44     private Set JavaDoc supportedAttributes = new HashSet JavaDoc();
45
46
47
48     /**
49      * Constructs a custom node builder. In derived classes you would
50      * typically create a default constructor and call addPossibleParent()/addAttribute()
51      * to customize the validation capabilities of the Node.
52      * @param nodeName the name of the node we're constructing.
53      */

54     public AbstractBuilderNode(final String JavaDoc nodeName) {
55         this.nodeName = nodeName;
56
57     }
58
59
60     /**
61      * Add an attribute to the list of ones supported by this node.
62      * @param name String the name of the attribute we support.
63      * @return AbstractBuilderNode (this) to allow for method chaining.
64      */

65     protected AbstractBuilderNode addAttribute(final String JavaDoc name) {
66         supportedAttributes.add(name);
67         return this;
68     }
69
70
71     public String JavaDoc getNodeName() {
72         return nodeName;
73     }
74
75
76     public Set JavaDoc getSupportedAttributes() {
77         return Collections.unmodifiableSet(supportedAttributes);
78     }
79
80     public String JavaDoc toString() {
81         return "Nanocontainer Builder Node: " + this.getClass().getName() + " (\"" + getNodeName() + "\")";
82     }
83
84     /**
85      * Checks that an attribute actually exists in the attirbute map. (The key
86      * exists and the value is non-null)
87      * @param attributes Map the current node's attributes.
88      * @param key String the attribute key we're looking for.
89      * @return boolean true if the attribute exists for the current node.
90      */

91     protected boolean isAttribute(final Map JavaDoc attributes, final String JavaDoc key) {
92         return attributes.containsKey(key) && attributes.get(key) != null;
93     }
94
95     /**
96      * {@inheritDoc}
97      * <p>This particular implementation checks all specified attribute keynames
98      * against the names supported in the node type. It does not type checking
99      * against the values passed in via the attributes.</p>
100      * @param specifiedAttributes the attributes as passed in by the groovy
101      * script.
102      * @throws NanoContainerMarkupException if an attribute is specified that
103      * is not recognized.
104      */

105     public void validateScriptedAttributes(final Map JavaDoc specifiedAttributes) throws NanoContainerMarkupException {
106         Set JavaDoc specifiedAttributeNames = specifiedAttributes.keySet();
107         if (this.getSupportedAttributes().containsAll(specifiedAttributeNames)) {
108             return;
109         }
110
111         Set JavaDoc unknownAttributes = new HashSet JavaDoc(specifiedAttributeNames);
112         unknownAttributes.removeAll(this.getSupportedAttributes());
113
114         StringBuffer JavaDoc errorMessage = new StringBuffer JavaDoc();
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     /**
127      * Utility function that takes a set and converts it to a comma delimited
128      * String with the format: key1, key2,.....
129      * @param specifiedSet Set the set to convert. For each object in the set,
130      * its toString() is called.
131      *
132      * @return String
133      */

134     private String JavaDoc convertSetToCommaDelimitedString(final Set JavaDoc specifiedSet) {
135
136         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
137
138         boolean needComma = false;
139         for (Iterator JavaDoc 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