KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > render > macro > SnipTreeMacro


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

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 JavaDoc;
38 import java.io.Writer JavaDoc;
39 import java.util.Collection JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.LinkedHashMap JavaDoc;
42 import java.util.Map JavaDoc;
43
44 /*
45  * Macro for fulltext searches in SnipSnap. The macro
46  * displays the search results for the input string. Can be
47  * used in snips to "store" searches. For user defined
48  * searches use a {field} macro combined with the {search}
49  * macro.
50  *
51  * @author stephan
52  * @version $Id: SnipTreeMacro.java 1791 2004-12-14 13:20:38Z leo $
53  */

54
55 public class SnipTreeMacro extends BaseMacro {
56   private SnipSpace space;
57
58   public SnipTreeMacro() {
59     space = SnipSpaceFactory.getInstance();
60   }
61
62   public String JavaDoc getName() {
63     return "snip-tree";
64   }
65
66   public String JavaDoc[] getParamDescription() {
67     return ResourceManager.getString("i18n.messages", "macro.sniptree.params").split(";");
68   }
69
70   public String JavaDoc getDescription() {
71     return ResourceManager.getString("i18n.messages", "macro.sniptree.description");
72   }
73
74   private class Node {
75     private String JavaDoc name;
76     private boolean isSnip;
77     private Map JavaDoc children;
78     private String JavaDoc snipName;
79
80     public Node(String JavaDoc name, boolean isSnip) {
81       this.name = name;
82       this.isSnip = isSnip;
83       this.children = new LinkedHashMap JavaDoc();
84     }
85
86     public void setSnipName(String JavaDoc snipName) {
87       this.snipName = snipName;
88     }
89
90     public String JavaDoc getSnipName() {
91       return snipName;
92     }
93
94     public boolean hasChild(String JavaDoc 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 JavaDoc name) {
103       return (Node) children.get(name);
104     }
105
106     public boolean hasChildren() {
107       return children.size() > 0;
108     }
109
110     public Collection JavaDoc getChildren() {
111       return children.values();
112     }
113
114     public String JavaDoc getName() {
115       return name;
116     }
117
118     public boolean isSnip() {
119       return isSnip;
120     }
121
122     public String JavaDoc toString() {
123       return name + " " + children.values().toString();
124     }
125   }
126
127   public void execute(Writer JavaDoc writer, MacroParameter params)
128           throws IllegalArgumentException JavaDoc, IOException JavaDoc {
129
130     // Names from the namespace look like
131
// [0] = foo/
132
// [1] = foo/bar
133
// [2] = foo/barbar
134
// [3] = foo/barbar/boing
135

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 JavaDoc e) {
143           // silently ignore wrong number
144
}
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 JavaDoc elements[] = snip.getName().split("/");
152
153         // Create all nodes till leaf
154
Node lastNode = root;
155         for (int j = 0; j < elements.length; j++) {
156           String JavaDoc 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 JavaDoc("Number of arguments does not match");
178     }
179   }
180
181   public void writeTree(Writer JavaDoc writer, Node node, int currentDepth, int maxDepth) throws IOException JavaDoc {
182     Iterator JavaDoc 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