001 /* 002 $Id: NodePrinter.java,v 1.4 2005/06/18 07:49:15 cstein 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 049 import org.codehaus.groovy.runtime.InvokerHelper; 050 051 import java.io.OutputStreamWriter; 052 import java.io.PrintWriter; 053 import java.util.Iterator; 054 import java.util.List; 055 import java.util.Map; 056 057 /** 058 * A helper class for creating nested trees of data 059 * 060 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a> 061 * @author Christian Stein 062 * @version $Revision: 1.4 $ 063 */ 064 public class NodePrinter { 065 066 protected final IndentPrinter out; 067 068 public NodePrinter() { 069 this(new IndentPrinter(new PrintWriter(new OutputStreamWriter(System.out)))); 070 } 071 072 public NodePrinter(PrintWriter out) { 073 this(new IndentPrinter(out)); 074 } 075 076 public NodePrinter(IndentPrinter out) { 077 if (out == null) { 078 throw new NullPointerException("IndentPrinter 'out' must not be null!"); 079 } 080 this.out = out; 081 } 082 083 public void print(Node node) { 084 out.printIndent(); 085 printName(node); 086 Map attributes = node.attributes(); 087 boolean hasAttributes = attributes != null && !attributes.isEmpty(); 088 if (hasAttributes) { 089 printAttributes(attributes); 090 } 091 Object value = node.value(); 092 if (value instanceof List) { 093 if (!hasAttributes) { 094 out.print("()"); 095 } 096 printList((List) value); 097 } 098 else { 099 if (value instanceof String) { 100 out.print("('"); 101 out.print((String) value); 102 out.println("')"); 103 } 104 else { 105 out.println("()"); 106 } 107 } 108 out.flush(); 109 } 110 111 protected void printName(Node node) { 112 Object name = node.name(); 113 if (name != null) { 114 out.print(name.toString()); 115 } 116 else { 117 out.print("null"); 118 } 119 } 120 121 protected void printList(List list) { 122 if (list.isEmpty()) { 123 out.println(""); 124 } 125 else { 126 out.println(" {"); 127 out.incrementIndent(); 128 for (Iterator iter = list.iterator(); iter.hasNext();) { 129 Object value = iter.next(); 130 if (value instanceof Node) { 131 print((Node) value); 132 } 133 else { 134 out.printIndent(); 135 out.print("builder.append("); 136 out.print(InvokerHelper.toString(value)); 137 out.println(")"); 138 } 139 } 140 out.decrementIndent(); 141 out.printIndent(); 142 out.println("}"); 143 } 144 } 145 146 147 protected void printAttributes(Map attributes) { 148 out.print("("); 149 boolean first = true; 150 for (Iterator iter = attributes.entrySet().iterator(); iter.hasNext();) { 151 Map.Entry entry = (Map.Entry) iter.next(); 152 if (first) { 153 first = false; 154 } 155 else { 156 out.print(", "); 157 } 158 out.print(entry.getKey().toString()); 159 out.print(":"); 160 if (entry.getValue() instanceof String) { 161 out.print("'" + entry.getValue() + "'"); 162 } 163 else { 164 out.print(InvokerHelper.toString(entry.getValue())); 165 } 166 } 167 out.print(")"); 168 } 169 170 }