1 23 package org.apache.slide.util; 24 import org.jdom.*; 25 26 import java.io.IOException ; 27 import java.io.StringReader ; 28 import java.io.StringWriter ; 29 import java.util.ArrayList ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import org.apache.slide.content.NodeProperty; 33 import org.apache.slide.content.NodeRevisionDescriptor; 34 import org.apache.slide.structure.SubjectNode; 35 import org.jdom.input.SAXBuilder; 36 import org.jdom.output.XMLOutputter; 37 import org.xml.sax.Attributes ; 38 import org.xml.sax.SAXException ; 39 import org.xml.sax.helpers.XMLFilterImpl ; 40 41 49 public class XMLValue implements Cloneable , Comparable { 50 51 55 56 public static final String ELEMENT_MUST_NOT_BE_NULL = "'null' Element is not allowed"; 57 58 62 public static final String TYPE_MISMATCH = "Only objects of type Element are allowed"; 63 64 68 protected static final String START_TAG = "<root>"; 69 70 74 protected static final String END_TAG = "</root>"; 75 76 protected static final String E_HREF = "href"; 77 78 79 82 protected ArrayList elementList = null; 83 84 85 88 public XMLValue() { 89 this((List )null); 90 } 91 92 100 public XMLValue(Element element) throws IllegalArgumentException { 101 this((List )null); 102 add(element); 103 } 104 105 115 public XMLValue(List elementList) throws IllegalArgumentException { 116 this.elementList = new ArrayList (); 117 add(elementList); 118 } 119 120 128 public XMLValue(String xmlString) throws JDOMException { 129 this(xmlString, null); 130 } 131 132 145 public XMLValue(String xmlString, Namespace defaultNamespace) throws JDOMException { 146 this((List )null); 147 add(xmlString, defaultNamespace); 148 } 149 150 151 159 public void add(Element element) throws IllegalArgumentException { 160 if (element == null) { 161 throw new IllegalArgumentException (ELEMENT_MUST_NOT_BE_NULL); 162 } 163 elementList.add(element); 164 } 165 166 174 public void add(Text text) throws IllegalArgumentException { 175 if (text == null) { 176 throw new IllegalArgumentException (ELEMENT_MUST_NOT_BE_NULL); 177 } 178 elementList.add(text); 179 } 180 181 189 public void add(Comment comment) throws IllegalArgumentException { 190 if (comment == null) { 191 throw new IllegalArgumentException (ELEMENT_MUST_NOT_BE_NULL); 192 } 193 elementList.add(comment); 194 } 195 196 204 public void add(ProcessingInstruction pi) throws IllegalArgumentException { 205 if (pi == null) { 206 throw new IllegalArgumentException (ELEMENT_MUST_NOT_BE_NULL); 207 } 208 elementList.add(pi); 209 } 210 211 219 public void add(CDATA cdata) throws IllegalArgumentException { 220 if (cdata == null) { 221 throw new IllegalArgumentException (ELEMENT_MUST_NOT_BE_NULL); 222 } 223 elementList.add(cdata); 224 } 225 226 234 public void add(EntityRef eref) throws IllegalArgumentException { 235 if (eref == null) { 236 throw new IllegalArgumentException (ELEMENT_MUST_NOT_BE_NULL); 237 } 238 elementList.add(eref); 239 } 240 241 250 public void add(List content) throws IllegalArgumentException { 251 252 if (content != null) { 253 Iterator iterator = content.iterator(); 254 while (iterator.hasNext()) { 255 Object o = iterator.next(); 256 if( o == null ) { 257 throw new IllegalArgumentException (ELEMENT_MUST_NOT_BE_NULL); 258 } 259 else if( o instanceof Element ) { 260 add((Element)o); 261 } 262 else if( o instanceof Text ) { 263 Text t = (Text)o; 264 t.setText( t.getTextTrim() ); 265 add(t); 266 } 267 else if( o instanceof Comment ) { 268 add((Comment)o); 269 } 270 else if( o instanceof ProcessingInstruction ) { 271 add((ProcessingInstruction)o); 272 } 273 else if( o instanceof CDATA ) { 274 add((CDATA)o); 275 } 276 else if( o instanceof EntityRef ) { 277 add((EntityRef)o); 278 } 279 else { 280 throw new IllegalArgumentException (TYPE_MISMATCH); 281 } 282 } 283 } 284 } 285 286 293 public void add(String xmlString) throws JDOMException { 294 add(xmlString, null); 295 } 296 297 309 public void add(String xmlString, Namespace defaultNamespace) throws JDOMException { 310 311 if (xmlString != null && xmlString.length() > 0) { 312 if (NodeRevisionDescriptor.COLLECTION_TYPE.equals(xmlString)) { 313 add(new Element("collection", defaultNamespace)); 315 } 316 else { 317 StringBuffer buffer = new StringBuffer (START_TAG.length() + 318 xmlString.length() + 319 END_TAG.length()); 320 buffer.append(START_TAG); 321 buffer.append(xmlString); 322 buffer.append(END_TAG); 323 SAXBuilder builder = new SAXBuilder(); 324 if (defaultNamespace != null) { 325 builder.setXMLFilter(new DefaultNamespaceXMLFilter(defaultNamespace)); 326 } 327 try { 328 Document document = builder.build(new StringReader (buffer.toString())); 329 List content = document.getRootElement().getContent(); 330 add( content ); 331 content.clear(); 333 } 334 catch (Exception e) { 335 throw new JDOMException(e.getMessage()); 338 } 339 } 340 } 341 } 342 343 public void addHref(String path) { 344 Element href = new Element(E_HREF, NodeProperty.NamespaceCache.DEFAULT_NAMESPACE); 345 href.setText(path); 346 add(href); 347 } 348 349 355 public List getHrefStrings() { 356 List result = new ArrayList (); 357 Iterator i = iterator(); 358 while (i.hasNext()) { 359 Object o = i.next(); 360 if (o instanceof Element && E_HREF.equals(((Element)o).getName())) { 361 result.add(((Element)o).getTextTrim()); 362 } 363 } 364 return result; 365 } 366 367 373 public List getHrefNodes() { 374 List result = new ArrayList (); 375 Iterator i = iterator(); 376 while (i.hasNext()) { 377 Object o = i.next(); 378 if (o instanceof Element && E_HREF.equals(((Element)o).getName())) { 379 result.add(SubjectNode.getSubjectNode(((Element)o).getTextTrim())); 380 } 381 } 382 return result; 383 } 384 385 public void stripServletContext(String servletContext) { 386 Iterator i = iterator(); 387 while (i.hasNext()) { 388 Object o = i.next(); 389 if (o instanceof Element && E_HREF.equals(((Element)o).getName())) { 390 Element hrefElm = (Element)o; 391 String href = hrefElm.getTextTrim(); 392 if (href.startsWith(servletContext)) { 393 hrefElm.setContent(java.util.Arrays.asList( 394 new Text[]{new Text(href.substring(servletContext.length()))})); 395 } 396 } 397 } 398 } 399 400 405 public Iterator iterator() { 406 return elementList.iterator(); 407 } 408 409 414 public int size() { 415 return elementList.size(); 416 } 417 418 423 public List getList() { 424 return (List )elementList.clone(); 425 } 426 427 436 public boolean equals(Object other) { 437 if ( ! (other instanceof XMLValue) ) { 438 return false; 439 } 440 return (this.toString().equals(other.toString())); 441 } 442 443 448 public int hashCode() { 449 return toString().hashCode(); 450 } 451 452 457 public Object clone() { 458 return new XMLValue(elementList); 459 } 460 461 466 public String toString() { 467 468 XMLOutputter outputter = new XMLOutputter(); 469 StringWriter stringWriter = new StringWriter (); 470 Iterator iterator = elementList.iterator(); 471 while (iterator.hasNext()) { 472 try { 473 Object o = iterator.next(); 474 if( o instanceof Element ) { 475 outputter.output((Element)o, stringWriter); 476 } 477 else if( o instanceof Text ) { 478 outputter.output((Text)o, stringWriter); 479 } 480 else if( o instanceof Comment ) { 481 outputter.output((Comment)o, stringWriter); 482 } 483 else if( o instanceof ProcessingInstruction ) { 484 outputter.output((ProcessingInstruction)o, stringWriter); 485 } 486 else if( o instanceof CDATA ) { 487 outputter.output((CDATA)o, stringWriter); 488 } 489 else if( o instanceof EntityRef ) { 490 outputter.output((EntityRef)o, stringWriter); 491 } 492 } 493 catch (IOException e) { 494 throw new RuntimeException ("IOException occurred: " + e.getMessage()); 497 } 498 } 499 return stringWriter.toString(); 500 } 501 502 506 protected static class DefaultNamespaceXMLFilter extends XMLFilterImpl { 507 508 511 Namespace defaultNamespace = null; 512 513 521 public DefaultNamespaceXMLFilter(Namespace defaultNamespace) { 522 this.defaultNamespace = defaultNamespace; 523 } 524 525 539 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 540 541 if ( (namespaceURI == null) || (namespaceURI.length() == 0) ) { 542 namespaceURI = defaultNamespace.getURI(); 543 qName = defaultNamespace.getPrefix() + ":" + qName; 544 } 545 super.startElement(namespaceURI, localName, qName, atts); 546 } 547 548 561 public void endElement(String namespaceURI, String localName, String qName) throws SAXException { 562 563 if ( (namespaceURI == null) || (namespaceURI.length() == 0) ) { 564 namespaceURI = defaultNamespace.getURI(); 565 qName = defaultNamespace.getPrefix() + ":" + qName; 566 } 567 super.endElement(namespaceURI, localName, qName); 568 } 569 570 } 571 572 578 public String getTextValue() { 579 Iterator it = elementList.iterator(); 580 StringBuffer sb = new StringBuffer (); 581 582 while (it.hasNext()) { 583 Object o = it.next(); 584 if (o instanceof Element) { 585 sb.append (((Element)o).getTextTrim()); 586 } 587 } 588 return sb.toString(); 589 } 590 591 599 public int compareTo (Object o) { 600 601 String s1 = getTextValue(); 602 String s2 = ((XMLValue)o).getTextValue(); 603 return s1.compareTo (s2); 604 } 605 } 606 607 | Popular Tags |