1 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 41 42 public class Labels { 43 private Map labels; 44 private String cache; 45 46 public Labels() { 47 this.labels = new HashMap(); 48 } 49 50 public Labels(Snip snip, String labelString) { 51 cache = labelString; 52 deserialize(snip, labelString); 53 } 54 55 public void addLabel(Label label) { 56 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 name, String value) { 69 cache = null; 73 Label label = createDefaultLabel(name, value); 74 addLabel(label); 75 } 76 77 public Label getLabel(String 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 name, String 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 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 name, String 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 name, String 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 type, String name, String value) { 139 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 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 labelToken = tokenizer.nextToken(); 157 String [] data = StringUtil.split(labelToken, ":"); 158 if (data.length >= 3) { 160 String 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 serialize() { 175 if (null == this.labels || this.labels.isEmpty()) { 176 return ""; 177 } 178 179 StringBuffer linkBuffer = new StringBuffer (); 180 Iterator iterator = this.labels.entrySet().iterator(); 181 while (iterator.hasNext()) { 182 Map.Entry entry = (Map.Entry) iterator.next(); 183 String name = (String ) 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 type = label.getType(); 189 String 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 if (linkBuffer.length() > 0) { 202 linkBuffer.setLength(linkBuffer.length() - 1); 203 } 204 return linkBuffer.toString(); 205 } 206 207 public String toString() { 208 return (cache = serialize()); 210 } 211 } 212 | Popular Tags |