001/* 002 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 003 * 004 * This software is open source. 005 * See the bottom of this file for the licence. 006 * 007 * $Id: Element.java,v 1.34 2002/02/01 13:04:32 jstrachan Exp $ 008 */ 009 010package org.dom4j; 011 012import java.util.Collection; 013import java.util.Collections; 014import java.util.Iterator; 015import java.util.List; 016import java.util.Map; 017 018/** <p><code>Element</code> interface defines an XML element. 019 * An element can have declared namespaces, attributes, child nodes and 020 * textual content.</p> 021 * 022 * <p>Some of this interface is optional. 023 * Some implementations may be read-only and not support being modified. 024 * Some implementations may not support the parent relationship and methods 025 * such as {@link #getParent} or {@link #getDocument}.</p> 026 * 027 * 028 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 029 * @version $Revision: 1.34 $ 030 */ 031public interface Element extends Branch { 032 033 // Name and namespace related methods 034 //------------------------------------------------------------------------- 035 036 /** <p>Returns the <code>QName</code> of this element which represents 037 * the local name, the qualified name and the <code>Namespace</code>.</p> 038 * 039 * @return the <code>QName</code> associated with this element 040 */ 041 public QName getQName(); 042 043 /** <p>Sets the <code>QName</code> of this element which represents 044 * the local name, the qualified name and the <code>Namespace</code>.</p> 045 * 046 * @param qname is the <code>QName</code> to be associated with this element 047 */ 048 public void setQName(QName qname); 049 050 /** <p>Returns the <code>Namespace</code> of this element if one exists 051 * otherwise <code>Namespace.NO_NAMESPACE</code> is returned.</p> 052 * 053 * @return the <code>Namespace</code> associated with this element 054 */ 055 public Namespace getNamespace(); 056 057 058 059 /** <p>Returns the <code>QName</code> for the given qualified name, using 060 * the namespace URI in scope for the given prefix of the qualified name 061 * or the default namespace if the qualified name has no prefix.</p> 062 * 063 * @return the <code>QName</code> for the given qualified name 064 */ 065 public QName getQName(String qualifiedName); 066 067 068 /** <p>Returns the <code>Namespace</code> which is mapped to the given 069 * prefix or null if it could not be found.</p> 070 * 071 * @return the <code>Namespace</code> associated with the given prefix 072 */ 073 public Namespace getNamespaceForPrefix(String prefix); 074 075 /** <p>Returns the <code>Namespace</code> which is mapped to the given 076 * URI or null if it could not be found.</p> 077 * 078 * @return the <code>Namespace</code> associated with the given URI 079 */ 080 public Namespace getNamespaceForURI(String uri); 081 082 /** <p>Returns the namespace prefix of this element if one exists 083 * otherwise an empty <code>String</code> is returned.</p> 084 * 085 * @return the prefix of the <code>Namespace</code> of this element 086 * or an empty <code>String</code> 087 */ 088 public String getNamespacePrefix(); 089 090 /** <p>Returns the URI mapped to the namespace of this element 091 * if one exists otherwise an empty <code>String</code> is returned.</p> 092 * 093 * @return the URI for the <code>Namespace</code> of this element 094 * or an empty <code>String</code> 095 */ 096 public String getNamespaceURI(); 097 098 /** <p>Returns the fully qualified name of this element. 099 * This will be the same as the value returned from {@link #getName} 100 * if this element has no namespace attached to this element or an 101 * expression of the form 102 * <pre> 103 * getNamespacePrefix() + ":" + getName() 104 * </pre> 105 * will be returned. 106 * 107 * @return the fully qualified name of the element. 108 */ 109 public String getQualifiedName(); 110 111 112 /** <p>Returns any additional namespaces declarations for this element 113 * other than namespace returned via the {@link #getNamespace()} method. 114 * If no additional namespace declarations are present for this 115 * element then an empty list will be returned. 116 * 117 * The list is backed by the element such that changes to the list will 118 * be reflected in the element though the reverse is not the case.</p> 119 * 120 * @return a list of any additional namespace declarations. 121 */ 122 public List additionalNamespaces(); 123 124 /** <p>Returns all the namespaces declared by this element. 125 * If no namespaces are declared for this element then 126 * an empty list will be returned. 127 * 128 * The list is backed by the element such that changes to the list will 129 * be reflected in the element though the reverse is not the case.</p> 130 * 131 * @return a list of namespaces declared for this element. 132 */ 133 public List declaredNamespaces(); 134 135 136 137 // Builder methods 138 //------------------------------------------------------------------------- 139 140 /** <p>Adds the attribute value of the given local name. 141 * If an attribute already exists for the given name it will be replaced.</p> 142 * 143 * @param name is the name of the attribute whose value is to be added 144 * or updated 145 * @param value is the attribute's value 146 * @return this <code>Element</code> instance. 147 */ 148 public Element addAttribute(String name, String value); 149 150 /** <p>Adds the attribute value of the given fully qualified name. 151 * If an attribute already exists for the given name it will be replaced.</p> 152 * 153 * @param qName is the fully qualified name of the attribute 154 * whose value is to be added or updated 155 * @param value is the attribute's value 156 * @return this <code>Element</code> instance. 157 */ 158 public Element addAttribute(QName qName, String value); 159 160 /** Adds a new <code>Comment</code> node with the given text to this element. 161 * 162 * @param comment is the text for the <code>Comment</code> node. 163 * @return this <code>Element</code> instance. 164 */ 165 public Element addComment(String comment); 166 167 168 /** Adds a new <code>CDATA</code> node with the given text to this element. 169 * 170 * @param cdata is the text for the <code>CDATA</code> node. 171 * @return this <code>Element</code> instance. 172 */ 173 public Element addCDATA(String cdata); 174 175 /** Adds a new <code>Entity</code> node with the given name and text 176 * to this element and returns a reference to the new node. 177 * 178 * @param name is the name for the <code>Entity</code> node. 179 * @param text is the text for the <code>Entity</code> node. 180 * @return this <code>Element</code> instance. 181 */ 182 public Element addEntity(String name, String text); 183 184 185 /** Adds a namespace to this element for use by its child content 186 * 187 * @param prefix is the prefix to use, which should not be null or blank 188 * @param uri is the namespace URI 189 * @return this <code>Element</code> instance. 190 */ 191 public Element addNamespace(String prefix, String uri); 192 193 /** Adds a processing instruction for the given target 194 * 195 * @param target is the target of the processing instruction 196 * @param text is the textual data (key/value pairs) of the processing instruction 197 * @return this <code>Element</code> instance. 198 */ 199 public Element addProcessingInstruction(String target, String text); 200 201 /** Adds a processing instruction for the given target 202 * 203 * @param target is the target of the processing instruction 204 * @param data is a Map of the key / value pairs of the processing instruction 205 * @return this <code>Element</code> instance. 206 */ 207 public Element addProcessingInstruction(String target, Map data); 208 209 /** Adds a new <code>Text</code> node with the given text to this element. 210 * 211 * @param text is the text for the <code>Text</code> node. 212 * @return this <code>Element</code> instance. 213 */ 214 public Element addText(String text); 215 216 217 // Typesafe modifying methods 218 //------------------------------------------------------------------------- 219 220 221 /** Adds the given <code>Attribute</code> to this element. 222 * If the given node already has a parent defined then an 223 * <code>InvalidAddNodeException</code> will be thrown. 224 * 225 * @param attribute is the attribute to be added 226 */ 227 public void add(Attribute attribute); 228 229 230 /** Adds the given <code>CDATA</code> to this element. 231 * If the given node already has a parent defined then an 232 * <code>InvalidAddNodeException</code> will be thrown. 233 * 234 * @param cdata is the CDATA to be added 235 */ 236 public void add(CDATA cdata); 237 238 /** Adds the given <code>Entity</code> to this element. 239 * If the given node already has a parent defined then an 240 * <code>InvalidAddNodeException</code> will be thrown. 241 * 242 * @param entity is the entity to be added 243 */ 244 public void add(Entity entity); 245 246 /** Adds the given <code>Text</code> to this element. 247 * If the given node already has a parent defined then an 248 * <code>InvalidAddNodeException</code> will be thrown. 249 * 250 * @param text is the text to be added 251 */ 252 public void add(Text text); 253 254 /** Adds the given <code>Namespace</code> to this element. 255 * If the given node already has a parent defined then an 256 * <code>InvalidAddNodeException</code> will be thrown. 257 * 258 * @param namespace is the namespace to be added 259 */ 260 public void add(Namespace namespace); 261 262 /** Removes the given <code>Attribute</code> from this element. 263 * 264 * @param attribute is the attribute to be removed 265 * @return true if the attribute was removed 266 */ 267 public boolean remove(Attribute attribute); 268 269 /** Removes the given <code>CDATA</code> if the node is 270 * an immediate child of this element. 271 * 272 * If the given node is not an immediate child of this element 273 * then the {@link Node#detach()} method should be used instead. 274 * 275 * @param cdata is the CDATA to be removed 276 * @return true if the cdata was removed 277 */ 278 public boolean remove(CDATA cdata); 279 280 /** Removes the given <code>Entity</code> if the node is 281 * an immediate child of this element. 282 * 283 * If the given node is not an immediate child of this element 284 * then the {@link Node#detach()} method should be used instead. 285 * 286 * @param entity is the entity to be removed 287 * @return true if the entity was removed 288 */ 289 public boolean remove(Entity entity); 290 291 /** Removes the given <code>Namespace</code> if the node is 292 * an immediate child of this element. 293 * 294 * If the given node is not an immediate child of this element 295 * then the {@link Node#detach()} method should be used instead. 296 * 297 * @param namespace is the namespace to be removed 298 * @return true if the namespace was removed 299 */ 300 public boolean remove(Namespace namespace); 301 302 /** Removes the given <code>Text</code> if the node is 303 * an immediate child of this element. 304 * 305 * If the given node is not an immediate child of this element 306 * then the {@link Node#detach()} method should be used instead. 307 * 308 * @param text is the text to be removed 309 * @return true if the text was removed 310 */ 311 public boolean remove(Text text); 312 313 314 // Text methods 315 //------------------------------------------------------------------------- 316 317 /** Returns the text value of this element without recursing through 318 * child elements. 319 * This method iterates through all {@link Text}, {@link CDATA} and 320 * {@link Entity} nodes that this element contains 321 * and appends the text values together. 322 * 323 * @return the textual content of this Element. Child elements are not navigated. 324 */ 325 public String getText(); 326 327 /** @return the trimmed text value where whitespace is trimmed and 328 * normalised into single spaces 329 */ 330 public String getTextTrim(); 331 332 333 /** Returns the XPath string-value of this node. 334 * The behaviour of this method is defined in the 335 * <a href="http://www.w3.org/TR/xpath">XPath specification</a>. 336 * 337 * This method returns the string-value of all the contained 338 * {@link Text}, {@link CDATA}, {@link Entity} and {@link Element} nodes 339 * all appended together. 340 * 341 * @return the text from all the child Text and Element nodes appended 342 * together. 343 */ 344 public String getStringValue(); 345 346 347 /** Accesses the data of this element which may implement data typing 348 * bindings such as XML Schema or 349 * Java Bean bindings or will return the same value as {@link #getText} 350 */ 351 public Object getData(); 352 353 /** Sets the data value of this element if this element supports data 354 * binding or calls {@link #setText} if it doesn't 355 */ 356 public void setData(Object data); 357 358 359 // Attribute methods 360 //------------------------------------------------------------------------- 361 362 363 /** <p>Returns the {@link Attribute} instances this element contains as 364 * a backed {@link List} so that the attributes may be modified directly 365 * using the {@link List} interface. 366 * The <code>List</code> is backed by the <code>Element</code> so that 367 * changes to the list are reflected in the element and vice versa.</p> 368 * 369 * @return the attributes that this element contains as a <code>List</code> 370 */ 371 public List attributes(); 372 373 /** Sets the attributes that this element contains 374 */ 375 public void setAttributes(List attributes); 376 377 /** @return the number of attributes this element contains 378 */ 379 public int attributeCount(); 380 381 /** @returns an iterator over the attributes of this element 382 */ 383 public Iterator attributeIterator(); 384 385 /** Returns the attribute at the specified indexGets the 386 * 387 * @return the attribute at the specified index where 388 * index >= 0 and index < number of attributes or throws 389 * an IndexOutOfBoundsException if the index is not within the 390 * allowable range 391 */ 392 public Attribute attribute(int index); 393 394 /** Returns the attribute with the given name 395 * 396 * @return the attribute for the given local name in any namespace. 397 * If there are more than one attributes with the given local name 398 * in different namespaces then the first one is returned. 399 */ 400 public Attribute attribute(String name); 401 402 /** @param qName is the fully qualified name 403 * @return the attribute for the given fully qualified name or null if 404 * it could not be found. 405 */ 406 public Attribute attribute(QName qname); 407 408 /** <p>This returns the attribute value for the attribute with the 409 * given name and any namespace or null if there is no such 410 * attribute or the empty string if the attribute value is empty.</p> 411 * 412 * @param name is the name of the attribute value to be returnd 413 * @return the value of the attribute, null if the attribute does 414 * not exist or the empty string 415 */ 416 public String attributeValue(String name); 417 418 /** <p>This returns the attribute value for the attribute with the 419 * given name and any namespace or the default value if there is 420 * no such attribute value.</p> 421 * 422 * @param name is the name of the attribute value to be returnd 423 * @param defaultValue is the default value to be returned if the 424 * attribute has no value defined. 425 * @return the value of the attribute or the defaultValue if the 426 * attribute has no value defined. 427 */ 428 public String attributeValue(String name, String defaultValue); 429 430 /** <p>This returns the attribute value for the attribute with the 431 * given fully qualified name or null if there is no such 432 * attribute or the empty string if the attribute value is empty.</p> 433 * 434 * @param qName is the fully qualified name 435 * @return the value of the attribute, null if the attribute does 436 * not exist or the empty string 437 */ 438 public String attributeValue(QName qName); 439 440 /** <p>This returns the attribute value for the attribute with the 441 * given fully qualified name or the default value if 442 * there is no such attribute value.</p> 443 * 444 * @param qName is the fully qualified name 445 * @param defaultValue is the default value to be returned if the 446 * attribute has no value defined. 447 * @return the value of the attribute or the defaultValue if the 448 * attribute has no value defined. 449 */ 450 public String attributeValue(QName qName, String defaultValue); 451 452 453 /** <p>Sets the attribute value of the given local name.</p> 454 * 455 * @param name is the name of the attribute whose value is to be added 456 * or updated 457 * @param value is the attribute's value 458 * 459 * @deprecated As of version 0.5. Please use 460 * {@link #addAttribute(String,String)} instead. 461 */ 462 public void setAttributeValue(String name, String value); 463 464 /** <p>Sets the attribute value of the given fully qualified name.</p> 465 * 466 * @param qName is the fully qualified name of the attribute 467 * whose value is to be added or updated 468 * @param value is the attribute's value 469 * 470 * @deprecated As of version 0.5. Please use 471 * {@link #addAttribute(QName,String)} instead. 472 */ 473 public void setAttributeValue(QName qName, String value); 474 475 476 // Content methods 477 //------------------------------------------------------------------------- 478 479 480 /** Returns the first element for the given local name and any namespace. 481 * 482 * @return the first element with the given local name 483 */ 484 public Element element(String name); 485 486 /** Returns the first element for the given fully qualified name. 487 * 488 * @param qName is the fully qualified name to search for 489 * @return the first element with the given fully qualified name 490 */ 491 public Element element(QName qname); 492 493 /** <p>Returns the elements contained in this element. 494 * If this element does not contain any elements then this method returns 495 * an empty list. 496 * 497 * The list is backed by the element such that changes to the list will 498 * be reflected in the element though the reverse is not the case.</p> 499 * 500 * @return a list of all the elements in this element. 501 */ 502 public List elements(); 503 504 /** <p>Returns the elements contained in this element with the given 505 * local name and any namespace. 506 * If no elements are found then this method returns an empty list. 507 * 508 * The list is backed by the element such that changes to the list will 509 * be reflected in the element though the reverse is not the case.</p> 510 * 511 * @return a list of all the elements in this element for the given 512 * local name 513 */ 514 public List elements(String name); 515 516 /** <p>Returns the elements contained in this element with the given 517 * fully qualified name. 518 * If no elements are found then this method returns an empty list. 519 * 520 * The list is backed by the element such that changes to the list will 521 * be reflected in the element though the reverse is not the case.</p> 522 * 523 * @param qName is the fully qualified name to search for 524 * @return a list of all the elements in this element for the 525 * given fully qualified name. 526 */ 527 public List elements(QName qName); 528 529 /** Returns an iterator over all this elements child elements. 530 * 531 * @return an iterator over the contained elements 532 */ 533 public Iterator elementIterator(); 534 535 /** Returns an iterator over the elements contained in this element 536 * which match the given local name and any namespace. 537 * 538 * @return an iterator over the contained elements matching the given 539 * local name 540 */ 541 public Iterator elementIterator(String name); 542 543 /** Returns an iterator over the elements contained in this element 544 * which match the given fully qualified name. 545 * 546 * @param qName is the fully qualified name to search for 547 * @return an iterator over the contained elements matching the given 548 * fully qualified name 549 */ 550 public Iterator elementIterator(QName qname); 551 552 553 554 // Helper methods 555 //------------------------------------------------------------------------- 556 557 /** @return true if this element is the root element of a document 558 * and this element supports the parent relationship else false. 559 */ 560 public boolean isRootElement(); 561 562 /** <p>Returns true if this <code>Element</code> has mixed content. 563 * Mixed content means that an element contains both textual data and 564 * child elements. 565 * 566 * @return true if this element contains mixed content. 567 */ 568 public boolean hasMixedContent(); 569 570 /** <p>Returns true if this <code>Element</code> has text only content. 571 * 572 * @return true if this element is empty or only contains text content. 573 */ 574 public boolean isTextOnly(); 575 576 577 /** Appends the attributes of the given element to me. 578 * This method behaves like the {@link Collection#addAll(java.util.Collection)} 579 * method. 580 * 581 * @param element is the element whose attributes will be added to me. 582 */ 583 public void appendAttributes(Element element); 584 585 /** <p>Creates a deep copy of this element 586 * The new element is detached from its parent, and getParent() on the 587 * clone will return null.</p> 588 * 589 * @return a new deep copy Element 590 */ 591 public Element createCopy(); 592 593 /** <p>Creates a deep copy of this element with the given local name 594 * The new element is detached from its parent, and getParent() on the 595 * clone will return null.</p> 596 * 597 * @return a new deep copy Element 598 */ 599 public Element createCopy(String name); 600 601 /** <p>Creates a deep copy of this element with the given fully qualified name. 602 * The new element is detached from its parent, and getParent() on the 603 * clone will return null.</p> 604 * 605 * @return a new deep copy Element 606 */ 607 public Element createCopy(QName qName); 608 609 public String elementText(String name); 610 public String elementText(QName qname); 611 public String elementTextTrim(String name); 612 public String elementTextTrim(QName qname); 613 614 /** Returns a node at the given index suitable for an XPath result set. 615 * This means the resulting Node will either be null or it will support 616 * the parent relationship. 617 * 618 * @return the Node for the given index which will support the parent 619 * relationship or null if there is not a node at the given index. 620 */ 621 public Node getXPathResult(int index); 622 623 624} 625 626 627 628 629/* 630 * Redistribution and use of this software and associated documentation 631 * ("Software"), with or without modification, are permitted provided 632 * that the following conditions are met: 633 * 634 * 1. Redistributions of source code must retain copyright 635 * statements and notices. Redistributions must also contain a 636 * copy of this document. 637 * 638 * 2. Redistributions in binary form must reproduce the 639 * above copyright notice, this list of conditions and the 640 * following disclaimer in the documentation and/or other 641 * materials provided with the distribution. 642 * 643 * 3. The name "DOM4J" must not be used to endorse or promote 644 * products derived from this Software without prior written 645 * permission of MetaStuff, Ltd. For written permission, 646 * please contact dom4j-info@metastuff.com. 647 * 648 * 4. Products derived from this Software may not be called "DOM4J" 649 * nor may "DOM4J" appear in their names without prior written 650 * permission of MetaStuff, Ltd. DOM4J is a registered 651 * trademark of MetaStuff, Ltd. 652 * 653 * 5. Due credit should be given to the DOM4J Project 654 * (http://dom4j.org/). 655 * 656 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS 657 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 658 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 659 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 660 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 661 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 662 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 663 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 664 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 665 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 666 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 667 * OF THE POSSIBILITY OF SUCH DAMAGE. 668 * 669 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 670 * 671 * $Id: Element.java,v 1.34 2002/02/01 13:04:32 jstrachan Exp $ 672 */