KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002-2004 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 package org.snipsnap.render.macro;
27
28 import org.radeox.util.i18n.ResourceManager;
29 import org.snipsnap.container.Components;
30 import org.snipsnap.render.macro.parameter.SnipMacroParameter;
31 import org.snipsnap.snip.Snip;
32 import org.snipsnap.snip.SnipSpace;
33 import org.snipsnap.snip.label.Label;
34 import org.snipsnap.snip.label.Labels;
35
36 import java.io.IOException JavaDoc;
37 import java.io.Writer JavaDoc;
38 import java.text.MessageFormat JavaDoc;
39 import java.util.ArrayList JavaDoc;
40 import java.util.Collection JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.List JavaDoc;
43
44 /*
45  * Macro that displays all Snips with a certain label
46  *
47  * @author stephan
48  * @version $Id: LabelSearchMacro.java 1688 2004-06-25 12:42:31Z leo $
49  */

50
51 public class LabelSearchMacro extends ListOutputMacro {
52   public String JavaDoc getName() {
53     return "label-search";
54   }
55
56   public String JavaDoc getDescription() {
57     return ResourceManager.getString("i18n.messages", "macro.labelsearch.description");
58   }
59
60   public String JavaDoc[] getParamDescription() {
61     return ResourceManager.getString("i18n.messages", "macro.labelsearch.params").split(";");
62   }
63
64   public void execute(Writer JavaDoc writer, SnipMacroParameter params)
65           throws IllegalArgumentException JavaDoc, IOException JavaDoc {
66
67     // {label-search:Category=Snip}
68
// Map pairs = params.getParams();
69
String JavaDoc type = params.get("type");
70     String JavaDoc name = params.get("name");
71     String JavaDoc value = params.get("value");
72
73     SnipSpace snipspace = (SnipSpace) Components.getComponent(SnipSpace.class);
74     List JavaDoc snipList = snipspace.getAll();
75
76     List JavaDoc result = new ArrayList JavaDoc();
77
78     Iterator JavaDoc iterator = snipList.iterator();
79     while (iterator.hasNext()) {
80       Snip snip = (Snip) iterator.next();
81       Labels labels = snip.getLabels();
82       boolean noLabelsAll = labels.getAll().isEmpty();
83
84       if (!noLabelsAll) {
85         Collection JavaDoc LabelsCat;
86 // Search for all type labels
87
Label label = labels.getLabel(name, value);
88         if (label != null) {
89           result.add(snip);
90         }
91       }
92     }
93
94 // String type = null;
95
// boolean showSize = true;
96
// if (params.getLength() > 1) {
97
// type = params.get("1");
98
// }
99
//
100
if (params.getLength() > 1) {
101       MessageFormat JavaDoc mf = new MessageFormat JavaDoc(ResourceManager.getString("i18n.messages", "macro.labelsearch.title"),
102                                            ResourceManager.getLocale("i18n.messages"));
103       output(writer, params.getSnipRenderContext().getSnip(),
104              mf.format(new Object JavaDoc[]{name, value}), result,
105              ResourceManager.getString("i18n.messages", "macro.labelsearch.notfound"), type, true);
106     } else {
107       throw new IllegalArgumentException JavaDoc("Number of arguments does not match");
108     }
109   }
110 }
111
Popular Tags