1 29 30 package nextapp.echo2.webrender; 31 32 import java.util.HashMap ; 33 import java.util.HashSet ; 34 import java.util.Map ; 35 import java.util.Set ; 36 37 import org.w3c.dom.Document ; 38 import org.w3c.dom.Element ; 39 import org.w3c.dom.NodeList ; 40 41 import nextapp.echo2.webrender.output.XmlDocument; 42 43 47 public class ServerMessage extends XmlDocument { 48 49 53 public static final int LEFT_TO_RIGHT = 0; 54 55 59 public static final int RIGHT_TO_LEFT = 1; 60 61 65 private Map itemizedDirectivesMap = new HashMap (); 66 67 73 private class ItemizedDirectiveLookupKey { 74 75 String groupId; 76 String processor; 77 String directiveName; 78 String [] keyAttributeNames; 79 String [] keyAttributeValues; 80 int hashCode; 81 82 95 private ItemizedDirectiveLookupKey(String groupId, String processor, String directiveName, String [] keyAttributeNames, 96 String [] keyAttributeValues) { 97 super(); 98 this.groupId = groupId; 99 this.processor = processor; 100 this.directiveName = directiveName; 101 this.keyAttributeNames = keyAttributeNames; 102 this.keyAttributeValues = keyAttributeValues; 103 this.hashCode = groupId.hashCode() ^ processor.hashCode() ^ directiveName.hashCode(); 104 for (int i = 0; i < keyAttributeNames.length; ++i) { 105 this.hashCode ^= keyAttributeNames[i].hashCode() ^ keyAttributeValues[i].hashCode(); 106 } 107 } 108 109 112 public boolean equals(Object o) { 113 if (!(o instanceof ItemizedDirectiveLookupKey)) { 114 return false; 115 } 116 ItemizedDirectiveLookupKey that = (ItemizedDirectiveLookupKey) o; 117 118 if (!this.groupId.equals(that.groupId)) { 119 return false; 120 } 121 if (!this.processor.equals(that.processor)) { 122 return false; 123 } 124 if (!this.directiveName.equals(that.directiveName)) { 125 return false; 126 } 127 if (this.keyAttributeNames.length != that.keyAttributeNames.length) { 128 return false; 129 } 130 for (int i = 0; i < keyAttributeValues.length; ++i) { 131 if (!(this.keyAttributeValues[i].equals(that.keyAttributeValues[i]))) { 132 return false; 133 } 134 } 135 for (int i = 0; i < keyAttributeNames.length; ++i) { 136 if (!(this.keyAttributeNames[i].equals(that.keyAttributeNames[i]))) { 137 return false; 138 } 139 } 140 141 return true; 142 } 143 144 147 public int hashCode() { 148 return hashCode; 149 } 150 } 151 152 157 public static final String GROUP_ID_INIT = "init"; 158 159 164 public static final String GROUP_ID_PREREMOVE = "preremove"; 165 166 172 public static final String GROUP_ID_REMOVE = "remove"; 173 174 180 public static final String GROUP_ID_UPDATE = "update"; 181 182 187 public static final String GROUP_ID_POSTUPDATE = "postupdate"; 188 189 190 private Set addedLibraries; 191 192 197 private Element librariesElement; 198 199 200 private Element serverMessageElement; 201 202 205 public ServerMessage() { 206 super("server-message", null, null, "http://www.nextapp.com/products/echo2/svrmsg/servermessage"); 207 Document document = getDocument(); 208 serverMessageElement = document.getDocumentElement(); 209 librariesElement = document.createElement("libraries"); 210 serverMessageElement.appendChild(librariesElement); 211 212 addPartGroup(GROUP_ID_INIT); 214 addPartGroup(GROUP_ID_PREREMOVE); 215 addPartGroup(GROUP_ID_REMOVE); 216 addPartGroup(GROUP_ID_UPDATE); 217 addPartGroup(GROUP_ID_POSTUPDATE); 218 } 219 220 226 public void addLibrary(String serviceId) { 227 if (addedLibraries == null) { 228 addedLibraries = new HashSet (); 229 } 230 if (addedLibraries.contains(serviceId)) { 231 return; 232 } 233 Element libraryElement = getDocument().createElement("library"); 234 libraryElement.setAttribute("service-id", serviceId); 235 librariesElement.appendChild(libraryElement); 236 addedLibraries.add(serviceId); 237 } 238 239 247 public Element addPartGroup(String groupId) { 248 Element messagePartGroupElement = getDocument().createElement("message-part-group"); 249 messagePartGroupElement.setAttribute("id", groupId); 250 serverMessageElement.appendChild(messagePartGroupElement); 251 return messagePartGroupElement; 252 } 253 254 260 public Element getPartGroup(String groupId) { 261 NodeList groupList = serverMessageElement.getElementsByTagName("message-part-group"); 262 int length = groupList.getLength(); 263 for (int i = 0; i < length; ++i) { 264 Element groupElement = (Element ) groupList.item(i); 265 if (groupId.equals(groupElement.getAttribute("id"))) { 266 return groupElement; 267 } 268 } 269 return null; 270 } 271 272 283 public Element addPart(String groupId, String processor) { 284 Element messagePartGroupElement = getPartGroup(groupId); 285 Element messagePartElement = getDocument().createElement("message-part"); 286 messagePartElement.setAttribute("processor", processor); 287 messagePartGroupElement.appendChild(messagePartElement); 288 return messagePartElement; 289 } 290 291 307 public Element appendPartDirective(String groupId, String processor, String directiveName) { 308 Element messagePartElement = null; 309 Element groupElement = getPartGroup(groupId); 310 311 Element lastChild = (Element ) groupElement.getLastChild(); 312 if (lastChild != null && processor.equals(lastChild.getAttribute("processor"))) { 313 messagePartElement = lastChild; 314 } else { 315 messagePartElement = addPart(groupId, processor); 316 } 317 318 Element directiveElement = getDocument().createElement(directiveName); 319 messagePartElement.appendChild(directiveElement); 320 return directiveElement; 321 } 322 323 349 public Element getItemizedDirective(String groupId, String processor, String directiveName, String [] keyAttributeNames, 350 String [] keyAttributeValues) { 351 ItemizedDirectiveLookupKey itemizedDirectiveLookupKey = new ItemizedDirectiveLookupKey(groupId, processor, directiveName, 352 keyAttributeNames, keyAttributeValues); 353 Element element = (Element ) itemizedDirectivesMap.get(itemizedDirectiveLookupKey); 354 if (element == null) { 355 element = appendPartDirective(groupId, processor, directiveName); 356 for (int i = 0; i < keyAttributeNames.length; ++i) { 357 element.setAttribute(keyAttributeNames[i], keyAttributeValues[i]); 358 } 359 itemizedDirectivesMap.put(itemizedDirectiveLookupKey, element); 360 } 361 return element; 362 } 363 364 371 public void setAsynchronousMonitorInterval(int newValue) { 372 if (newValue < 0) { 373 serverMessageElement.setAttribute("async-interval", "disable"); 374 } else { 375 serverMessageElement.setAttribute("async-interval", Integer.toString(newValue)); 376 } 377 } 378 379 387 public void setModalContextRootId(String id) { 388 if (id == null) { 389 serverMessageElement.setAttribute("modal-id", ""); 390 } else { 391 serverMessageElement.setAttribute("modal-id", id); 392 } 393 } 394 395 401 public void setRootLayoutDirection(int layoutDirection) { 402 switch (layoutDirection) { 403 case LEFT_TO_RIGHT: 404 serverMessageElement.setAttribute("root-layout-direction", "ltr"); 405 break; 406 case RIGHT_TO_LEFT: 407 serverMessageElement.setAttribute("root-layout-direction", "rtl"); 408 break; 409 default: 410 throw new IllegalArgumentException ("Illegal layout direction."); 411 } 412 } 413 414 420 public void setTransactionId(long transactionId) { 421 serverMessageElement.setAttribute("trans-id", Long.toString(transactionId)); 422 } 423 } | Popular Tags |