001    /*
002     $Id: Node.java,v 1.10 2005/08/10 09:53:58 hmeling Exp $
003    
004     Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
005    
006     Redistribution and use of this software and associated documentation
007     ("Software"), with or without modification, are permitted provided
008     that the following conditions are met:
009    
010     1. Redistributions of source code must retain copyright
011        statements and notices.  Redistributions must also contain a
012        copy of this document.
013    
014     2. Redistributions in binary form must reproduce the
015        above copyright notice, this list of conditions and the
016        following disclaimer in the documentation and/or other
017        materials provided with the distribution.
018    
019     3. The name "groovy" must not be used to endorse or promote
020        products derived from this Software without prior written
021        permission of The Codehaus.  For written permission,
022        please contact info@codehaus.org.
023    
024     4. Products derived from this Software may not be called "groovy"
025        nor may "groovy" appear in their names without prior written
026        permission of The Codehaus. "groovy" is a registered
027        trademark of The Codehaus.
028    
029     5. Due credit should be given to The Codehaus -
030        http://groovy.codehaus.org/
031    
032     THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
033     ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
034     NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
035     FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
036     THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
037     INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
038     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
039     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
040     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
041     STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
042     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
043     OF THE POSSIBILITY OF SUCH DAMAGE.
044    
045     */
046    package groovy.util;
047    
048    import java.io.PrintWriter;
049    import java.util.ArrayList;
050    import java.util.Collection;
051    import java.util.Collections;
052    import java.util.Iterator;
053    import java.util.List;
054    import java.util.Map;
055    
056    import org.codehaus.groovy.runtime.InvokerHelper;
057    
058    /**
059     * Represents an arbitrary tree node which can be used for structured  metadata which can be any arbitrary XML-like tree.
060     * A node can have a name, a value and an optional Map of attributes.
061     * Typically the name is a String and a value is either a String or a List of other Nodes.
062     * Though the types are extensible to provide a flexible structure. 
063     * e.g. you could use a QName as the name which includes a namespace URI and a local name. Or a JMX ObjectName etc.
064     * So this class can represent metadata like {foo a=1 b="abc"} or nested metadata like {foo a=1 b="123" { bar x=12 text="hello" }}
065     * 
066     * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
067     * @version $Revision: 1.10 $
068     */
069    public class Node implements java.io.Serializable {
070    
071        private Node parent;
072        private Object name;
073        private Map attributes;
074        private Object value;
075    
076        public Node(Node parent, Object name) {
077            this(parent, name, Collections.EMPTY_MAP, Collections.EMPTY_LIST);
078        }
079    
080        public Node(Node parent, Object name, Object value) {
081            this(parent, name, Collections.EMPTY_MAP, value);
082        }
083    
084        public Node(Node parent, Object name, Map attributes) {
085            this(parent, name, attributes, Collections.EMPTY_LIST);
086        }
087    
088        public Node(Node parent, Object name, Map attributes, Object value) {
089            this.parent = parent;
090            this.name = name;
091            this.attributes = attributes;
092            this.value = value;
093            
094            if (parent != null) {
095                Object parentValue = parent.value();
096                List parentList = null;
097                if (parentValue instanceof List) {
098                    parentList = (List) parentValue;
099                }
100                else {
101                    parentList = new ArrayList();
102                    parentList.add(parentValue);
103                    parent.setValue(parentList);
104                }
105                parentList.add(this);
106            }
107        }
108    
109        public String text() {
110            if (value instanceof String) {
111                return (String) value;
112            }
113            else if (value instanceof Collection) {
114                Collection coll = (Collection) value;
115                String previousText = null;
116                StringBuffer buffer = null;
117                for (Iterator iter = coll.iterator(); iter.hasNext();) {
118                    Object child = iter.next();
119                    if (child instanceof String) {
120                        String childText = (String) child;
121                        if (previousText == null) {
122                            previousText = childText;
123                        }
124                        else {
125                            if (buffer == null) {
126                                buffer = new StringBuffer();
127                                buffer.append(previousText);
128                            }
129                            buffer.append(childText);
130                        }
131                    }
132                }
133                if (buffer != null) {
134                    return buffer.toString();
135                }
136                else {
137                    if (previousText != null) {
138                        return previousText;
139                    }
140                }
141            }
142            return "";
143        }
144    
145        public Iterator iterator() {
146            return children().iterator();
147        }
148        
149        public List children() {
150            if (value == null) {
151                return Collections.EMPTY_LIST;
152            }
153            else if (value instanceof List) {
154                return (List) value;
155            }
156            else {
157                // we're probably just a String
158                return Collections.singletonList(value);
159            }
160        }
161    
162        public Map attributes() {
163            return attributes;
164        }
165    
166        public Object attribute(Object key) {
167            return (attributes != null) ? attributes.get(key) : null;
168        }
169        
170        public Object name() {
171            return name;
172        }
173    
174        public Object value() {
175            return value;
176        }
177    
178        public void setValue(Object value) {
179            this.value = value;
180        }
181    
182        public Node parent() {
183            return parent;
184        }
185    
186        public Object get(String key) {
187            if (key.charAt(0) == '@') {
188                String attributeName = key.substring(1);
189                return attributes().get(attributeName);
190            }
191            else {
192                // iterate through list looking for node with name 'key'
193                List answer = new ArrayList();
194                for (Iterator iter = children().iterator(); iter.hasNext();) {
195                    Object child = iter.next();
196                    if (child instanceof Node) {
197                        Node childNode = (Node) child;
198                        Object childNodeName = childNode.name();
199                        if (childNodeName != null && childNodeName.equals(key)) {
200                            answer.add(childNode);
201                        }
202                    }
203                }
204                return answer;
205            }
206        }
207    
208    //    public Object get(int idx) {
209    //        return children().get(idx);
210    //    }
211    
212    
213    
214        /**
215         * Provide a collection of all the nodes in the tree
216         * using a depth first traversal
217         */
218        public List depthFirst() {
219            List answer = new ArrayList();
220            answer.add(this);
221            answer.addAll(depthFirstRest());
222            return answer;
223        }
224        
225        private  List depthFirstRest() {
226            List answer = new ArrayList();
227            for (Iterator iter = InvokerHelper.asIterator(value); iter.hasNext(); ) {
228                Object child = iter.next();
229                if (child instanceof Node) {
230                    Node childNode = (Node) child;
231                    List children = childNode.depthFirstRest();
232                    answer.add(childNode);
233                    answer.addAll(children);
234                }
235            }
236            return answer;
237        }
238    
239        /**
240         * Provide a collection of all the nodes in the tree
241         * using a bredth first traversal
242         */
243        public List breadthFirst() {
244            List answer = new ArrayList();
245            answer.add(this);
246            answer.addAll(breadthFirstRest());
247            return answer;
248        }
249        
250        private  List breadthFirstRest() {
251            List answer = new ArrayList();
252            for (Iterator iter = InvokerHelper.asIterator(value); iter.hasNext(); ) {
253                Object child = iter.next();
254                if (child instanceof Node) {
255                    Node childNode = (Node) child;
256                    answer.add(childNode);
257                }
258            }
259            List copy = new ArrayList(answer);
260            for (Iterator iter = copy.iterator(); iter.hasNext(); ) {
261                Node childNode = (Node) iter.next();
262                List children = childNode.breadthFirstRest();
263                answer.addAll(children);
264            }
265            return answer;
266        }
267    
268        public String toString() {
269            return name + "[attributes=" + attributes + "; value=" + value + "]";
270        }
271    
272        public void print(PrintWriter out) {
273            new NodePrinter(out).print(this);
274        }
275    }