KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > snip > label > Labels


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 package org.snipsnap.snip.label;
27
28 import org.snipsnap.util.StringUtil;
29 import org.snipsnap.snip.Snip;
30 import org.snipsnap.container.Components;
31 import org.snipsnap.app.Application;
32
33 import java.util.*;
34
35 /**
36  * Stores Label information for a snip
37  *
38  * @author stephan
39  * @version $Id: Labels.java 1834 2005-10-04 09:26:52Z leo $
40  */

41
42 public class Labels {
43   private Map labels;
44   private String JavaDoc cache;
45
46   public Labels() {
47     this.labels = new HashMap();
48   }
49
50   public Labels(Snip snip, String JavaDoc labelString) {
51     cache = labelString;
52     deserialize(snip, labelString);
53   }
54
55   public void addLabel(Label label) {
56     // TODO: check if label with same name exists
57
// additional parameter 'overwrite' or exception or return value?
58
// (decision should to be made by user)
59
cache = null;
60     Map map = (Map) this.labels.get(label.getName());
61     if (map == null) {
62       map = new HashMap();
63       this.labels.put(label.getName(), map);
64     }
65     map.put(label.getValue(), label);
66   }
67
68   public void addLabel(String JavaDoc name, String JavaDoc value) {
69     // TODO: check if label with same name exists
70
// additional parameter 'overwrite' or exception or return value?
71
// (decision should to be made by user)
72
cache = null;
73     Label label = createDefaultLabel(name, value);
74     addLabel(label);
75   }
76
77   public Label getLabel(String JavaDoc name) {
78     Map map = (Map) this.labels.get(name);
79     if (map == null) return null;
80     Iterator it = map.values().iterator();
81     return it.hasNext() ? (Label) it.next() : null;
82   }
83
84   public Label getLabel(String JavaDoc name, String JavaDoc value) {
85     Map map = (Map) this.labels.get(name);
86     if (map == null) return null;
87     return (Label) map.get(value);
88   }
89
90   public Collection getAll() {
91     Collection result = new ArrayList();
92
93     Iterator iterator = this.labels.values().iterator();
94     while (iterator.hasNext()) {
95       Map map = (Map) iterator.next();
96       result.addAll(map.values());
97     }
98     return result;
99   }
100
101   public Collection getLabels(String JavaDoc type) {
102     ArrayList result = new ArrayList();
103     if (null == type) {
104       return result;
105     }
106
107     Iterator iterator = this.labels.values().iterator();
108     while (iterator.hasNext()) {
109       Map map = (Map) iterator.next();
110       Iterator it = map.values().iterator();
111       while (it.hasNext()) {
112         Label label = (Label) it.next();
113         if (null != label && type.equals(label.getType())) {
114           result.add(label);
115         }
116       }
117     }
118     return result;
119   }
120
121   public void removeLabel(String JavaDoc name, String JavaDoc value) {
122     cache = null;
123     Map map = (Map) labels.get(name);
124     if (map != null) {
125       Label label = (Label) map.get(value);
126       label.remove();
127       map.remove(value);
128     }
129   }
130
131   private Label createDefaultLabel(String JavaDoc name, String JavaDoc value) {
132     Label label = ((LabelManager) Components.getComponent(LabelManager.class)).getDefaultLabel();
133     label.setName(name);
134     label.setValue(value);
135     return label;
136   }
137
138   private Label createLabel(String JavaDoc type, String JavaDoc name, String JavaDoc value) {
139     // TODO: ? throw an exception (e.g. LabelTypeUnkownException ) ?
140
Label label = ((LabelManager) Components.getComponent(LabelManager.class)).getLabel(type);
141     if (label != null) {
142       label.setName(name);
143       label.setValue(value);
144     }
145     return label;
146   }
147
148   private void deserialize(Snip snip, String JavaDoc labelString) {
149     labels = new HashMap();
150     if (null == labelString || "".equals(labelString)) {
151       return;
152     }
153
154     StringTokenizer tokenizer = new StringTokenizer(labelString, "|");
155     while (tokenizer.hasMoreTokens()) {
156       String JavaDoc labelToken = tokenizer.nextToken();
157       String JavaDoc[] data = StringUtil.split(labelToken, ":");
158       //System.out.println("Data="+data);
159
if (data.length >= 3) {
160         String JavaDoc value = data[2];
161         for(int i = 3; i < data.length; i++) {
162           value += ":" + data[i];
163         }
164         Label label = createLabel(data[0], data[1], value);
165         label.setSnip(snip);
166         addLabel(label);
167       } else {
168         System.err.println("Labels: Broken Label: '" + labelToken + "' ignored");
169       }
170     }
171     return;
172   }
173
174   private String JavaDoc serialize() {
175     if (null == this.labels || this.labels.isEmpty()) {
176       return "";
177     }
178
179     StringBuffer JavaDoc linkBuffer = new StringBuffer JavaDoc();
180     Iterator iterator = this.labels.entrySet().iterator();
181     while (iterator.hasNext()) {
182       Map.Entry entry = (Map.Entry) iterator.next();
183       String JavaDoc name = (String JavaDoc) entry.getKey();
184       Map map = (Map) entry.getValue();
185       Iterator it = map.values().iterator();
186       while (it.hasNext()) {
187         Label label = (Label) it.next();
188         String JavaDoc type = label.getType();
189         String JavaDoc value = label.getValue();
190         linkBuffer.append(type);
191         linkBuffer.append(":");
192         linkBuffer.append(name);
193         linkBuffer.append(":");
194         linkBuffer.append(value);
195
196         linkBuffer.append("|");
197       }
198     }
199     //System.out.println("serialize = "+linkBuffer.toString());
200
// remove last '|'
201
if (linkBuffer.length() > 0) {
202       linkBuffer.setLength(linkBuffer.length() - 1);
203     }
204     return linkBuffer.toString();
205   }
206
207   public String JavaDoc toString() {
208     // always force serialization to ensure changed labels are reflected
209
return (cache = serialize());
210   }
211 }
212
Popular Tags