1 25 26 27 package org.snipsnap.render.macro; 28 29 import org.radeox.macro.BaseMacro; 30 import org.radeox.macro.parameter.MacroParameter; 31 import org.radeox.util.i18n.ResourceManager; 32 import org.snipsnap.snip.Snip; 33 import org.snipsnap.snip.SnipLink; 34 import org.snipsnap.snip.SnipSpace; 35 import org.snipsnap.snip.SnipSpaceFactory; 36 37 import java.io.IOException ; 38 import java.io.Writer ; 39 import java.util.Collection ; 40 import java.util.Iterator ; 41 import java.util.LinkedHashMap ; 42 import java.util.Map ; 43 44 54 55 public class SnipTreeMacro extends BaseMacro { 56 private SnipSpace space; 57 58 public SnipTreeMacro() { 59 space = SnipSpaceFactory.getInstance(); 60 } 61 62 public String getName() { 63 return "snip-tree"; 64 } 65 66 public String [] getParamDescription() { 67 return ResourceManager.getString("i18n.messages", "macro.sniptree.params").split(";"); 68 } 69 70 public String getDescription() { 71 return ResourceManager.getString("i18n.messages", "macro.sniptree.description"); 72 } 73 74 private class Node { 75 private String name; 76 private boolean isSnip; 77 private Map children; 78 private String snipName; 79 80 public Node(String name, boolean isSnip) { 81 this.name = name; 82 this.isSnip = isSnip; 83 this.children = new LinkedHashMap (); 84 } 85 86 public void setSnipName(String snipName) { 87 this.snipName = snipName; 88 } 89 90 public String getSnipName() { 91 return snipName; 92 } 93 94 public boolean hasChild(String name) { 95 return children.containsKey(name); 96 } 97 98 public void addChild(Node node) { 99 children.put(node.getName(), node); 100 }; 101 102 public Node getChild(String name) { 103 return (Node) children.get(name); 104 } 105 106 public boolean hasChildren() { 107 return children.size() > 0; 108 } 109 110 public Collection getChildren() { 111 return children.values(); 112 } 113 114 public String getName() { 115 return name; 116 } 117 118 public boolean isSnip() { 119 return isSnip; 120 } 121 122 public String toString() { 123 return name + " " + children.values().toString(); 124 } 125 } 126 127 public void execute(Writer writer, MacroParameter params) 128 throws IllegalArgumentException , IOException { 129 130 136 if (params.getLength() < 3) { 137 Snip[] snips = space.match(params.get("0")); 138 int maxDepth = -1; 139 if (params.getLength() == 2) { 140 try { 141 maxDepth = Integer.parseInt(params.get("1")); 142 } catch (NumberFormatException e) { 143 } 145 } 146 147 Node root = new Node("root", false); 148 149 for (int i = 0; i < snips.length; i++) { 150 Snip snip = snips[i]; 151 String elements[] = snip.getName().split("/"); 152 153 Node lastNode = root; 155 for (int j = 0; j < elements.length; j++) { 156 String name = elements[j]; 157 if (!lastNode.hasChild(name)) { 158 boolean isSnip = (j == elements.length - 1); 159 Node node = new Node(name, isSnip); 160 if (isSnip) { 161 node.setSnipName(snip.getName()); 162 } 163 lastNode.addChild(node); 164 lastNode = node; 165 } else { 166 lastNode = lastNode.getChild(name); 167 } 168 } 169 } 170 171 writer.write("<div class=\"snip-tree\">"); 172 writeTree(writer, root, 1, maxDepth); 173 writer.write("</div>"); 174 } else if (params.getLength() == 3) { 175 writer.write("<img SRC=\"/exec/namespace?name=" + params.get(0) + "\"/>"); 176 } else { 177 throw new IllegalArgumentException ("Number of arguments does not match"); 178 } 179 } 180 181 public void writeTree(Writer writer, Node node, int currentDepth, int maxDepth) throws IOException { 182 Iterator children = node.getChildren().iterator(); 183 writer.write("<ul>"); 184 while (children.hasNext()) { 185 Node child = (Node) children.next(); 186 writer.write("<li>"); 187 if (child.isSnip()) { 188 SnipLink.appendLink(writer, child.getSnipName(), child.getName()); 189 } else { 190 writer.write(child.getName()); 191 } 192 writer.write("</li>"); 193 if (child.hasChildren() && (maxDepth == -1 || currentDepth < maxDepth)) { 194 writeTree(writer, child, currentDepth + 1, maxDepth); 195 } 196 } 197 writer.write("</ul>"); 198 } 199 } 200 | Popular Tags |