1 20 21 package org.xmpp.packet; 22 23 import org.dom4j.*; 24 import org.dom4j.io.XMLWriter; 25 import org.dom4j.io.OutputFormat; 26 27 import java.util.Iterator ; 28 import java.io.StringWriter ; 29 30 36 public class StreamError { 37 38 private static final String ERROR_NAMESPACE = "urn:ietf:params:xml:ns:xmpp-streams"; 39 40 private static DocumentFactory docFactory = DocumentFactory.getInstance(); 41 42 private Element element; 43 44 49 public StreamError(Condition condition) { 50 this.element = docFactory.createElement(docFactory.createQName("error", "stream", 51 "http://etherx.jabber.org/streams")); 52 setCondition(condition); 53 } 54 55 61 public StreamError(Condition condition, String text) { 62 this.element = docFactory.createElement(docFactory.createQName("error", "stream", 63 "http://etherx.jabber.org/streams")); 64 setCondition(condition); 65 setText(text, null); 66 } 67 68 75 public StreamError(Condition condition, String text, String language) { 76 this.element = docFactory.createElement(docFactory.createQName("error", "stream", 77 "http://etherx.jabber.org/streams")); 78 setCondition(condition); 79 setText(text, language); 80 } 81 82 88 public StreamError(Element element) { 89 this.element = element; 90 } 91 92 98 public Condition getCondition() { 99 for (Iterator i=element.elementIterator(); i.hasNext(); ) { 100 Element el = (Element)i.next(); 101 if (el.getNamespaceURI().equals(ERROR_NAMESPACE) && 102 !el.getName().equals("text")) 103 { 104 return Condition.fromXMPP(el.getName()); 105 } 106 } 107 return null; 108 } 109 110 116 public void setCondition(Condition condition) { 117 if (condition == null) { 118 throw new NullPointerException ("Condition cannot be null"); 119 } 120 Element conditionElement = null; 121 for (Iterator i=element.elementIterator(); i.hasNext(); ) { 122 Element el = (Element)i.next(); 123 if (el.getNamespaceURI().equals(ERROR_NAMESPACE) && 124 !el.getName().equals("text")) 125 { 126 conditionElement = el; 127 } 128 } 129 if (conditionElement != null) { 130 element.remove(conditionElement); 131 } 132 133 conditionElement = docFactory.createElement(condition.toXMPP(), ERROR_NAMESPACE); 134 element.add(conditionElement); 135 } 136 137 143 public String getText() { 144 return element.elementText("text"); 145 } 146 147 152 public void setText(String text) { 153 setText(text, null); 154 } 155 156 164 public void setText(String text, String language) { 165 Element textElement = element.element("text"); 166 if (text == null) { 168 if (textElement != null) { 169 element.remove(textElement); 170 } 171 return; 172 } 173 174 if (textElement == null) { 175 textElement = docFactory.createElement("text", ERROR_NAMESPACE); 176 if (language != null) { 177 textElement.addAttribute(QName.get("lang", "xml", 178 "http://www.w3.org/XML/1998/namespace"), language); 179 } 180 element.add(textElement); 181 } 182 textElement.setText(text); 183 } 184 185 191 public String getTextLanguage() { 192 Element textElement = element.element("text"); 193 if (textElement != null) { 194 return textElement.attributeValue(QName.get("lang", "xml", 195 "http://www.w3.org/XML/1998/namespace")); 196 } 197 return null; 198 } 199 200 207 public Element getElement() { 208 return element; 209 } 210 211 216 public String toXML() { 217 return element.asXML(); 218 } 219 220 public String toString() { 221 StringWriter out = new StringWriter (); 222 XMLWriter writer = new XMLWriter(out, OutputFormat.createPrettyPrint()); 223 try { 224 writer.write(element); 225 } 226 catch (Exception e) { } 227 return out.toString(); 228 } 229 230 240 public enum Condition { 241 242 248 bad_format("bad-format"), 249 250 254 bad_namespace_prefix("bad-namespace-prefix"), 255 256 260 conflict("conflict"), 261 262 266 connection_timeout("connection-timeout"), 267 268 272 host_gone("host-gone"), 273 274 278 host_unknown("host-unknown"), 279 280 284 improper_addressing("improper-addressing"), 285 286 290 internal_server_error("internal-server-error"), 291 292 297 invalid_from("invalid-from"), 298 299 302 invalid_id("invalid-id"), 303 304 308 invalid_namespace("invalid-namespace"), 309 310 313 invalid_xml("invalid-xml"), 314 315 321 not_authorized("not-authorized"), 322 323 328 policy_violation("policy-violation"), 329 330 334 remote_connection_failed("remote-connection-failed"), 335 336 339 resource_constraint("resource-constraint"), 340 341 345 restricted_xml("restricted-xml"), 346 347 353 see_other_host("see-other-host"), 354 355 358 system_shutdown("system-shutdown"), 359 360 365 undefined_condition("undefined-condition"), 366 367 371 unsupported_encoding("unsupported-encoding"), 372 373 377 unsupported_stanza_type("unsupported-stanza-type"), 378 379 384 unsupported_version("unsupported-version"), 385 386 389 xml_not_well_formed("xml-not-well-formed"); 390 391 397 public static Condition fromXMPP(String condition) { 398 if (condition == null) { 399 throw new NullPointerException (); 400 } 401 condition = condition.toLowerCase(); 402 if (bad_format.toXMPP().equals(condition)) { 403 return bad_format; 404 } 405 else if (bad_namespace_prefix.toXMPP().equals(condition)) { 406 return bad_namespace_prefix; 407 } 408 else if (conflict.toXMPP().equals(condition)) { 409 return conflict; 410 } 411 else if (connection_timeout.toXMPP().equals(condition)) { 412 return connection_timeout; 413 } 414 else if (host_gone.toXMPP().equals(condition)) { 415 return host_gone; 416 } 417 else if (host_unknown.toXMPP().equals(condition)) { 418 return host_unknown; 419 } 420 else if (improper_addressing.toXMPP().equals(condition)) { 421 return improper_addressing; 422 } 423 else if (internal_server_error.toXMPP().equals(condition)) { 424 return internal_server_error; 425 } 426 else if (invalid_from.toXMPP().equals(condition)) { 427 return invalid_from; 428 } 429 else if (invalid_id.toXMPP().equals(condition)) { 430 return invalid_id; 431 } 432 else if (invalid_namespace.toXMPP().equals(condition)) { 433 return invalid_namespace; 434 } 435 else if (invalid_xml.toXMPP().equals(condition)) { 436 return invalid_xml; 437 } 438 else if (not_authorized.toXMPP().equals(condition)) { 439 return not_authorized; 440 } 441 else if (policy_violation.toXMPP().equals(condition)) { 442 return policy_violation; 443 } 444 else if (remote_connection_failed.toXMPP().equals(condition)) { 445 return remote_connection_failed; 446 } 447 else if (resource_constraint.toXMPP().equals(condition)) { 448 return resource_constraint; 449 } 450 else if (restricted_xml.toXMPP().equals(condition)) { 451 return restricted_xml; 452 } 453 else if (see_other_host.toXMPP().equals(condition)) { 454 return see_other_host; 455 } 456 else if (system_shutdown.toXMPP().equals(condition)) { 457 return system_shutdown; 458 } 459 else if (undefined_condition.toXMPP().equals(condition)) { 460 return undefined_condition; 461 } 462 else if (unsupported_encoding.toXMPP().equals(condition)) { 463 return unsupported_encoding; 464 } 465 else if (unsupported_stanza_type.toXMPP().equals(condition)) { 466 return unsupported_stanza_type; 467 } 468 else if (unsupported_version.toXMPP().equals(condition)) { 469 return unsupported_version; 470 } 471 else if (xml_not_well_formed.toXMPP().equals(condition)) { 472 return xml_not_well_formed; 473 } 474 else { 475 throw new IllegalArgumentException ("Condition invalid:" + condition); 476 } 477 } 478 479 private String value; 480 481 private Condition(String value) { 482 this.value = value; 483 } 484 485 490 public String toXMPP() { 491 return value; 492 } 493 } 494 } 495 | Popular Tags |