Desktop tool for browsing account info from EVE-Online
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Element.java 56KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564
  1. /*--
  2. $Id: Element.java,v 1.159 2007/11/14 05:02:08 jhunter Exp $
  3. Copyright (C) 2000-2007 Jason Hunter & Brett McLaughlin.
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. 1. Redistributions of source code must retain the above copyright
  9. notice, this list of conditions, and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions, and the disclaimer that follows
  12. these conditions in the documentation and/or other materials
  13. provided with the distribution.
  14. 3. The name "JDOM" must not be used to endorse or promote products
  15. derived from this software without prior written permission. For
  16. written permission, please contact <request_AT_jdom_DOT_org>.
  17. 4. Products derived from this software may not be called "JDOM", nor
  18. may "JDOM" appear in their name, without prior written permission
  19. from the JDOM Project Management <request_AT_jdom_DOT_org>.
  20. In addition, we request (but do not require) that you include in the
  21. end-user documentation provided with the redistribution and/or in the
  22. software itself an acknowledgement equivalent to the following:
  23. "This product includes software developed by the
  24. JDOM Project (http://www.jdom.org/)."
  25. Alternatively, the acknowledgment may be graphical using the logos
  26. available at http://www.jdom.org/images/logos.
  27. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  28. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  29. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30. DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
  31. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  34. USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  35. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  36. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  38. SUCH DAMAGE.
  39. This software consists of voluntary contributions made by many
  40. individuals on behalf of the JDOM Project and was originally
  41. created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
  42. Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
  43. on the JDOM Project, please see <http://www.jdom.org/>.
  44. */
  45. package org.jdom;
  46. import java.io.*;
  47. import java.util.*;
  48. import org.jdom.filter.*;
  49. /**
  50. * An XML element. Methods allow the user to get and manipulate its child
  51. * elements and content, directly access the element's textual content,
  52. * manipulate its attributes, and manage namespaces.
  53. *
  54. * @version $Revision: 1.159 $, $Date: 2007/11/14 05:02:08 $
  55. * @author Brett McLaughlin
  56. * @author Jason Hunter
  57. * @author Lucas Gonze
  58. * @author Kevin Regan
  59. * @author Dan Schaffer
  60. * @author Yusuf Goolamabbas
  61. * @author Kent C. Johnson
  62. * @author Jools Enticknap
  63. * @author Alex Rosen
  64. * @author Bradley S. Huffman
  65. * @author Victor Toni
  66. */
  67. public class Element extends Content implements Parent {
  68. private static final String CVS_ID =
  69. "@(#) $RCSfile: Element.java,v $ $Revision: 1.159 $ $Date: 2007/11/14 05:02:08 $ $Name: jdom_1_1 $";
  70. private static final int INITIAL_ARRAY_SIZE = 5;
  71. /** The local name of the element */
  72. protected String name;
  73. /** The namespace of the element */
  74. protected transient Namespace namespace;
  75. /** Additional namespace declarations to store on this element; useful
  76. * during output */
  77. protected transient List additionalNamespaces;
  78. // See http://lists.denveronline.net/lists/jdom-interest/2000-September/003030.html
  79. // for a possible memory optimization here (using a RootElement subclass)
  80. /**
  81. * The attributes of the element. Subclassers have to
  82. * track attributes using their own mechanism.
  83. */
  84. AttributeList attributes = new AttributeList(this);
  85. /**
  86. * The content of the element. Subclassers have to
  87. * track content using their own mechanism.
  88. */
  89. ContentList content = new ContentList(this);
  90. /**
  91. * This protected constructor is provided in order to support an Element
  92. * subclass that wants full control over variable initialization. It
  93. * intentionally leaves all instance variables null, allowing a lightweight
  94. * subclass implementation. The subclass is responsible for ensuring all the
  95. * get and set methods on Element behave as documented.
  96. * <p>
  97. * When implementing an Element subclass which doesn't require full control
  98. * over variable initialization, be aware that simply calling super() (or
  99. * letting the compiler add the implicit super() call) will not initialize
  100. * the instance variables which will cause many of the methods to throw a
  101. * NullPointerException. Therefore, the constructor for these subclasses
  102. * should call one of the public constructors so variable initialization is
  103. * handled automatically.
  104. */
  105. protected Element() { }
  106. /**
  107. * Creates a new element with the supplied (local) name and namespace. If
  108. * the provided namespace is null, the element will have no namespace.
  109. *
  110. * @param name local name of the element
  111. * @param namespace namespace for the element
  112. * @throws IllegalNameException if the given name is illegal as an element
  113. * name
  114. */
  115. public Element(final String name, final Namespace namespace) {
  116. setName(name);
  117. setNamespace(namespace);
  118. }
  119. /**
  120. * Create a new element with the supplied (local) name and no namespace.
  121. *
  122. * @param name local name of the element
  123. * @throws IllegalNameException if the given name is illegal as an element
  124. * name.
  125. */
  126. public Element(final String name) {
  127. this(name, (Namespace) null);
  128. }
  129. /**
  130. * Creates a new element with the supplied (local) name and a namespace
  131. * given by a URI. The element will be put into the unprefixed (default)
  132. * namespace.
  133. *
  134. * @param name name of the element
  135. * @param uri namespace URI for the element
  136. * @throws IllegalNameException if the given name is illegal as an element
  137. * name or the given URI is illegal as a
  138. * namespace URI
  139. */
  140. public Element(final String name, final String uri) {
  141. this(name, Namespace.getNamespace("", uri));
  142. }
  143. /**
  144. * Creates a new element with the supplied (local) name and a namespace
  145. * given by the supplied prefix and URI combination.
  146. *
  147. * @param name local name of the element
  148. * @param prefix namespace prefix
  149. * @param uri namespace URI for the element
  150. * @throws IllegalNameException if the given name is illegal as an element
  151. * name, the given prefix is illegal as a
  152. * namespace prefix, or the given URI is
  153. * illegal as a namespace URI
  154. */
  155. public Element(final String name, final String prefix, final String uri) {
  156. this(name, Namespace.getNamespace(prefix, uri));
  157. }
  158. /**
  159. * Returns the (local) name of the element (without any namespace prefix).
  160. *
  161. * @return local element name
  162. */
  163. public String getName() {
  164. return name;
  165. }
  166. /**
  167. * Sets the (local) name of the element.
  168. *
  169. * @param name the new (local) name of the element
  170. * @return the target element
  171. * @throws IllegalNameException if the given name is illegal as an Element
  172. * name
  173. */
  174. public Element setName(final String name) {
  175. final String reason = Verifier.checkElementName(name);
  176. if (reason != null) {
  177. throw new IllegalNameException(name, "element", reason);
  178. }
  179. this.name = name;
  180. return this;
  181. }
  182. /**
  183. * Returns the element's {@link Namespace}.
  184. *
  185. * @return the element's namespace
  186. */
  187. public Namespace getNamespace() {
  188. return namespace;
  189. }
  190. /**
  191. * Sets the element's {@link Namespace}. If the provided namespace is null,
  192. * the element will have no namespace.
  193. *
  194. * @param namespace the new namespace
  195. * @return the target element
  196. */
  197. public Element setNamespace(Namespace namespace) {
  198. if (namespace == null) {
  199. namespace = Namespace.NO_NAMESPACE;
  200. }
  201. this.namespace = namespace;
  202. return this;
  203. }
  204. /**
  205. * Returns the namespace prefix of the element or an empty string if none
  206. * exists.
  207. *
  208. * @return the namespace prefix
  209. */
  210. public String getNamespacePrefix() {
  211. return namespace.getPrefix();
  212. }
  213. /**
  214. * Returns the namespace URI mapped to this element's prefix (or the
  215. * in-scope default namespace URI if no prefix). If no mapping is found, an
  216. * empty string is returned.
  217. *
  218. * @return the namespace URI for this element
  219. */
  220. public String getNamespaceURI() {
  221. return namespace.getURI();
  222. }
  223. /**
  224. * Returns the {@link Namespace} corresponding to the given prefix in scope
  225. * for this element. This involves searching up the tree, so the results
  226. * depend on the current location of the element. Returns null if there is
  227. * no namespace in scope with the given prefix at this point in the
  228. * document.
  229. *
  230. * @param prefix namespace prefix to look up
  231. * @return the Namespace for this prefix at this
  232. * location, or null if none
  233. */
  234. public Namespace getNamespace(final String prefix) {
  235. if (prefix == null) {
  236. return null;
  237. }
  238. if ("xml".equals(prefix)) {
  239. // Namespace "xml" is always bound.
  240. return Namespace.XML_NAMESPACE;
  241. }
  242. // Check if the prefix is the prefix for this element
  243. if (prefix.equals(getNamespacePrefix())) {
  244. return getNamespace();
  245. }
  246. // Scan the additional namespaces
  247. if (additionalNamespaces != null) {
  248. for (int i = 0; i < additionalNamespaces.size(); i++) {
  249. final Namespace ns = (Namespace) additionalNamespaces.get(i);
  250. if (prefix.equals(ns.getPrefix())) {
  251. return ns;
  252. }
  253. }
  254. }
  255. // If we still don't have a match, ask the parent
  256. if (parent instanceof Element) {
  257. return ((Element)parent).getNamespace(prefix);
  258. }
  259. return null;
  260. }
  261. /**
  262. * Returns the full name of the element, in the form
  263. * [namespacePrefix]:[localName]. If the element does not have a namespace
  264. * prefix, then the local name is returned.
  265. *
  266. * @return qualified name of the element (including
  267. * namespace prefix)
  268. */
  269. public String getQualifiedName() {
  270. // Note: Any changes here should be reflected in
  271. // XMLOutputter.printQualifiedName()
  272. if ("".equals(namespace.getPrefix())) {
  273. return getName();
  274. }
  275. return new StringBuffer(namespace.getPrefix())
  276. .append(':')
  277. .append(name)
  278. .toString();
  279. }
  280. /**
  281. * Adds a namespace declarations to this element. This should <i>not</i> be
  282. * used to add the declaration for this element itself; that should be
  283. * assigned in the construction of the element. Instead, this is for adding
  284. * namespace declarations on the element not relating directly to itself.
  285. * It's used during output to for stylistic reasons move namespace
  286. * declarations higher in the tree than they would have to be.
  287. *
  288. * @param additionalNamespace namespace to add
  289. * @throws IllegalAddException if the namespace prefix collides with another
  290. * namespace prefix on the element
  291. */
  292. public void addNamespaceDeclaration(final Namespace additionalNamespace) {
  293. // Verify the new namespace prefix doesn't collide with another
  294. // declared namespace, an attribute prefix, or this element's prefix
  295. final String reason = Verifier.checkNamespaceCollision(additionalNamespace, this);
  296. if (reason != null) {
  297. throw new IllegalAddException(this, additionalNamespace, reason);
  298. }
  299. if (additionalNamespaces == null) {
  300. additionalNamespaces = new ArrayList(INITIAL_ARRAY_SIZE);
  301. }
  302. additionalNamespaces.add(additionalNamespace);
  303. }
  304. /**
  305. * Removes an additional namespace declarations from this element. This
  306. * should <i>not</i> be used to remove the declaration for this element
  307. * itself; that should be handled in the construction of the element.
  308. * Instead, this is for removing namespace declarations on the element not
  309. * relating directly to itself. If the declaration is not present, this
  310. * method does nothing.
  311. *
  312. * @param additionalNamespace namespace to remove
  313. */
  314. public void removeNamespaceDeclaration(final Namespace additionalNamespace) {
  315. if (additionalNamespaces == null) {
  316. return;
  317. }
  318. additionalNamespaces.remove(additionalNamespace);
  319. }
  320. /**
  321. * Returns a list of the additional namespace declarations on this element.
  322. * This includes only additional namespace, not the namespace of the element
  323. * itself, which can be obtained through {@link #getNamespace()}. If there
  324. * are no additional declarations, this returns an empty list. Note, the
  325. * returned list is unmodifiable.
  326. *
  327. * @return a List of the additional namespace
  328. * declarations
  329. */
  330. public List getAdditionalNamespaces() {
  331. // Not having the returned list be live allows us to avoid creating a
  332. // new list object when XMLOutputter calls this method on an element
  333. // with an empty list.
  334. if (additionalNamespaces == null) {
  335. return Collections.EMPTY_LIST;
  336. }
  337. return Collections.unmodifiableList(additionalNamespaces);
  338. }
  339. /**
  340. * Returns the XPath 1.0 string value of this element, which is the
  341. * complete, ordered content of all text node descendants of this element
  342. * (i&#46;e&#46; the text that's left after all references are resolved
  343. * and all other markup is stripped out.)
  344. *
  345. * @return a concatentation of all text node descendants
  346. */
  347. public String getValue() {
  348. final StringBuffer buffer = new StringBuffer();
  349. final Iterator iter = getContent().iterator();
  350. while (iter.hasNext()) {
  351. final Content child = (Content) iter.next();
  352. if (child instanceof Element || child instanceof Text) {
  353. buffer.append(child.getValue());
  354. }
  355. }
  356. return buffer.toString();
  357. }
  358. /**
  359. * Returns whether this element is a root element. This can be used in
  360. * tandem with {@link #getParent} to determine if an element has any
  361. * "attachments" to a parent element or document.
  362. *
  363. * @return whether this is a root element
  364. */
  365. public boolean isRootElement() {
  366. return parent instanceof Document;
  367. }
  368. public int getContentSize() {
  369. return content.size();
  370. }
  371. public int indexOf(final Content child) {
  372. return content.indexOf(child);
  373. }
  374. // private int indexOf(int start, Filter filter) {
  375. // int size = getContentSize();
  376. // for (int i = start; i < size; i++) {
  377. // if (filter.matches(getContent(i))) {
  378. // return i;
  379. // }
  380. // }
  381. // return -1;
  382. // }
  383. /**
  384. * Returns the textual content directly held under this element as a string.
  385. * This includes all text within this single element, including whitespace
  386. * and CDATA sections if they exist. It's essentially the concatenation of
  387. * all {@link Text} and {@link CDATA} nodes returned by {@link #getContent}.
  388. * The call does not recurse into child elements. If no textual value exists
  389. * for the element, an empty string is returned.
  390. *
  391. * @return text content for this element, or empty
  392. * string if none
  393. */
  394. public String getText() {
  395. if (content.size() == 0) {
  396. return "";
  397. }
  398. // If we hold only a Text or CDATA, return it directly
  399. if (content.size() == 1) {
  400. final Object obj = content.get(0);
  401. if (obj instanceof Text) {
  402. return ((Text) obj).getText();
  403. }
  404. else {
  405. return "";
  406. }
  407. }
  408. // Else build String up
  409. final StringBuffer textContent = new StringBuffer();
  410. boolean hasText = false;
  411. for (int i = 0; i < content.size(); i++) {
  412. final Object obj = content.get(i);
  413. if (obj instanceof Text) {
  414. textContent.append(((Text) obj).getText());
  415. hasText = true;
  416. }
  417. }
  418. if (!hasText) {
  419. return "";
  420. }
  421. else {
  422. return textContent.toString();
  423. }
  424. }
  425. /**
  426. * Returns the textual content of this element with all surrounding
  427. * whitespace removed. If no textual value exists for the element, or if
  428. * only whitespace exists, the empty string is returned.
  429. *
  430. * @return trimmed text content for this element, or
  431. * empty string if none
  432. */
  433. public String getTextTrim() {
  434. return getText().trim();
  435. }
  436. /**
  437. * Returns the textual content of this element with all surrounding
  438. * whitespace removed and internal whitespace normalized to a single space.
  439. * If no textual value exists for the element, or if only whitespace exists,
  440. * the empty string is returned.
  441. *
  442. * @return normalized text content for this element, or
  443. * empty string if none
  444. */
  445. public String getTextNormalize() {
  446. return Text.normalizeString(getText());
  447. }
  448. /**
  449. * Returns the textual content of the named child element, or null if
  450. * there's no such child. This method is a convenience because calling
  451. * <code>getChild().getText()</code> can throw a NullPointerException.
  452. *
  453. * @param name the name of the child
  454. * @return text content for the named child, or null if
  455. * no such child
  456. */
  457. public String getChildText(final String name) {
  458. final Element child = getChild(name);
  459. if (child == null) {
  460. return null;
  461. }
  462. return child.getText();
  463. }
  464. /**
  465. * Returns the trimmed textual content of the named child element, or null
  466. * if there's no such child. See <code>{@link #getTextTrim()}</code> for
  467. * details of text trimming.
  468. *
  469. * @param name the name of the child
  470. * @return trimmed text content for the named child, or
  471. * null if no such child
  472. */
  473. public String getChildTextTrim(final String name) {
  474. final Element child = getChild(name);
  475. if (child == null) {
  476. return null;
  477. }
  478. return child.getTextTrim();
  479. }
  480. /**
  481. * Returns the normalized textual content of the named child element, or
  482. * null if there's no such child. See <code>{@link
  483. * #getTextNormalize()}</code> for details of text normalizing.
  484. *
  485. * @param name the name of the child
  486. * @return normalized text content for the named child,
  487. * or null if no such child
  488. */
  489. public String getChildTextNormalize(final String name) {
  490. final Element child = getChild(name);
  491. if (child == null) {
  492. return null;
  493. }
  494. return child.getTextNormalize();
  495. }
  496. /**
  497. * Returns the textual content of the named child element, or null if
  498. * there's no such child.
  499. *
  500. * @param name the name of the child
  501. * @param ns the namespace of the child
  502. * @return text content for the named child, or null if
  503. * no such child
  504. */
  505. public String getChildText(final String name, final Namespace ns) {
  506. final Element child = getChild(name, ns);
  507. if (child == null) {
  508. return null;
  509. }
  510. return child.getText();
  511. }
  512. /**
  513. * Returns the trimmed textual content of the named child element, or null
  514. * if there's no such child.
  515. *
  516. * @param name the name of the child
  517. * @param ns the namespace of the child
  518. * @return trimmed text content for the named child, or
  519. * null if no such child
  520. */
  521. public String getChildTextTrim(final String name, final Namespace ns) {
  522. final Element child = getChild(name, ns);
  523. if (child == null) {
  524. return null;
  525. }
  526. return child.getTextTrim();
  527. }
  528. /**
  529. * Returns the normalized textual content of the named child element, or
  530. * null if there's no such child.
  531. *
  532. * @param name the name of the child
  533. * @param ns the namespace of the child
  534. * @return normalized text content for the named child,
  535. * or null if no such child
  536. */
  537. public String getChildTextNormalize(final String name, final Namespace ns) {
  538. final Element child = getChild(name, ns);
  539. if (child == null) {
  540. return null;
  541. }
  542. return child.getTextNormalize();
  543. }
  544. /**
  545. * Sets the content of the element to be the text given. All existing text
  546. * content and non-text context is removed. If this element should have both
  547. * textual content and nested elements, use <code>{@link #setContent}</code>
  548. * instead. Setting a null text value is equivalent to setting an empty
  549. * string value.
  550. *
  551. * @param text new text content for the element
  552. * @return the target element
  553. * @throws IllegalDataException if the assigned text contains an illegal
  554. * character such as a vertical tab (as
  555. * determined by {@link
  556. * org.jdom.Verifier#checkCharacterData})
  557. */
  558. public Element setText(final String text) {
  559. content.clear();
  560. if (text != null) {
  561. addContent(new Text(text));
  562. }
  563. return this;
  564. }
  565. /**
  566. * This returns the full content of the element as a List which
  567. * may contain objects of type <code>Text</code>, <code>Element</code>,
  568. * <code>Comment</code>, <code>ProcessingInstruction</code>,
  569. * <code>CDATA</code>, and <code>EntityRef</code>.
  570. * The List returned is "live" in document order and modifications
  571. * to it affect the element's actual contents. Whitespace content is
  572. * returned in its entirety.
  573. *
  574. * <p>
  575. * Sequential traversal through the List is best done with an Iterator
  576. * since the underlying implement of List.size() may require walking the
  577. * entire list.
  578. * </p>
  579. *
  580. * @return a <code>List</code> containing the mixed content of the
  581. * element: may contain <code>Text</code>,
  582. * <code>{@link Element}</code>, <code>{@link Comment}</code>,
  583. * <code>{@link ProcessingInstruction}</code>,
  584. * <code>{@link CDATA}</code>, and
  585. * <code>{@link EntityRef}</code> objects.
  586. */
  587. public List getContent() {
  588. return content;
  589. }
  590. /**
  591. * Return a filter view of this <code>Element</code>'s content.
  592. *
  593. * <p>
  594. * Sequential traversal through the List is best done with a Iterator
  595. * since the underlying implement of List.size() may require walking the
  596. * entire list.
  597. * </p>
  598. *
  599. * @param filter <code>Filter</code> to apply
  600. * @return <code>List</code> - filtered Element content
  601. */
  602. public List getContent(final Filter filter) {
  603. return content.getView(filter);
  604. }
  605. /**
  606. * Removes all child content from this parent.
  607. *
  608. * @return list of the old children detached from this parent
  609. */
  610. public List removeContent() {
  611. final List old = new ArrayList(content);
  612. content.clear();
  613. return old;
  614. }
  615. /**
  616. * Remove all child content from this parent matching the supplied filter.
  617. *
  618. * @param filter filter to select which content to remove
  619. * @return list of the old children detached from this parent
  620. */
  621. public List removeContent(final Filter filter) {
  622. final List old = new ArrayList();
  623. final Iterator iter = content.getView(filter).iterator();
  624. while (iter.hasNext()) {
  625. final Content child = (Content) iter.next();
  626. old.add(child);
  627. iter.remove();
  628. }
  629. return old;
  630. }
  631. /**
  632. * This sets the content of the element. The supplied List should
  633. * contain only objects of type <code>Element</code>, <code>Text</code>,
  634. * <code>CDATA</code>, <code>Comment</code>,
  635. * <code>ProcessingInstruction</code>, and <code>EntityRef</code>.
  636. *
  637. * <p>
  638. * When all objects in the supplied List are legal and before the new
  639. * content is added, all objects in the old content will have their
  640. * parentage set to null (no parent) and the old content list will be
  641. * cleared. This has the effect that any active list (previously obtained
  642. * with a call to {@link #getContent} or {@link #getChildren}) will also
  643. * change to reflect the new content. In addition, all objects in the
  644. * supplied List will have their parentage set to this element, but the
  645. * List itself will not be "live" and further removals and additions will
  646. * have no effect on this elements content. If the user wants to continue
  647. * working with a "live" list, then a call to setContent should be
  648. * followed by a call to {@link #getContent} or {@link #getChildren} to
  649. * obtain a "live" version of the content.
  650. * </p>
  651. *
  652. * <p>
  653. * Passing a null or empty List clears the existing content.
  654. * </p>
  655. *
  656. * <p>
  657. * In event of an exception the original content will be unchanged and
  658. * the objects in the supplied content will be unaltered.
  659. * </p>
  660. *
  661. * @param newContent <code>Collection</code> of content to set
  662. * @return this element modified
  663. * @throws IllegalAddException if the List contains objects of
  664. * illegal types or with existing parentage.
  665. */
  666. public Element setContent(final Collection newContent) {
  667. content.clearAndSet(newContent);
  668. return this;
  669. }
  670. /**
  671. * Replace the current child the given index with the supplied child.
  672. * <p>
  673. * In event of an exception the original content will be unchanged and
  674. * the supplied child will be unaltered.
  675. * </p>
  676. *
  677. * @param index - index of child to replace.
  678. * @param child - child to add.
  679. * @return element on which this method was invoked
  680. * @throws IllegalAddException if the supplied child is already attached
  681. * or not legal content for this parent.
  682. * @throws IndexOutOfBoundsException if index is negative or greater
  683. * than the current number of children.
  684. */
  685. public Element setContent(final int index, final Content child) {
  686. content.set(index, child);
  687. return this;
  688. }
  689. /**
  690. * Replace the child at the given index whith the supplied
  691. * collection.
  692. * <p>
  693. * In event of an exception the original content will be unchanged and
  694. * the content in the supplied collection will be unaltered.
  695. * </p>
  696. *
  697. * @param index - index of child to replace.
  698. * @param newContent - <code>Collection</code> of content to replace child.
  699. * @return object on which this method was invoked
  700. * @throws IllegalAddException if the collection contains objects of
  701. * illegal types.
  702. * @throws IndexOutOfBoundsException if index is negative or greater
  703. * than the current number of children.
  704. */
  705. public Parent setContent(final int index, final Collection newContent) {
  706. content.remove(index);
  707. content.addAll(index, newContent);
  708. return this;
  709. }
  710. /**
  711. * This adds text content to this element. It does not replace the
  712. * existing content as does <code>setText()</code>.
  713. *
  714. * @param str <code>String</code> to add
  715. * @return this element modified
  716. * @throws IllegalDataException if <code>str</code> contains an
  717. * illegal character such as a vertical tab (as determined
  718. * by {@link org.jdom.Verifier#checkCharacterData})
  719. */
  720. public Element addContent(final String str) {
  721. return addContent(new Text(str));
  722. }
  723. /**
  724. * Appends the child to the end of the element's content list.
  725. *
  726. * @param child child to append to end of content list
  727. * @return the element on which the method was called
  728. * @throws IllegalAddException if the given child already has a parent. */
  729. public Element addContent(final Content child) {
  730. content.add(child);
  731. return this;
  732. }
  733. /**
  734. * Appends all children in the given collection to the end of
  735. * the content list. In event of an exception during add the
  736. * original content will be unchanged and the objects in the supplied
  737. * collection will be unaltered.
  738. *
  739. * @param newContent <code>Collection</code> of content to append
  740. * @return the element on which the method was called
  741. * @throws IllegalAddException if any item in the collection
  742. * already has a parent or is of an inappropriate type.
  743. */
  744. public Element addContent(final Collection newContent) {
  745. content.addAll(newContent);
  746. return this;
  747. }
  748. /**
  749. * Inserts the child into the content list at the given index.
  750. *
  751. * @param index location for adding the collection
  752. * @param child child to insert
  753. * @return the parent on which the method was called
  754. * @throws IndexOutOfBoundsException if index is negative or beyond
  755. * the current number of children
  756. * @throws IllegalAddException if the given child already has a parent.
  757. */
  758. public Element addContent(final int index, final Content child) {
  759. content.add(index, child);
  760. return this;
  761. }
  762. /**
  763. * Inserts the content in a collection into the content list
  764. * at the given index. In event of an exception the original content
  765. * will be unchanged and the objects in the supplied collection will be
  766. * unaltered.
  767. *
  768. * @param index location for adding the collection
  769. * @param newContent <code>Collection</code> of content to insert
  770. * @return the parent on which the method was called
  771. * @throws IndexOutOfBoundsException if index is negative or beyond
  772. * the current number of children
  773. * @throws IllegalAddException if any item in the collection
  774. * already has a parent or is of an inappropriate type.
  775. */
  776. public Element addContent(final int index, final Collection newContent) {
  777. content.addAll(index, newContent);
  778. return this;
  779. }
  780. public List cloneContent() {
  781. final int size = getContentSize();
  782. final List list = new ArrayList(size);
  783. for (int i = 0; i < size; i++) {
  784. final Content child = getContent(i);
  785. list.add(child.clone());
  786. }
  787. return list;
  788. }
  789. public Content getContent(final int index) {
  790. return (Content) content.get(index);
  791. }
  792. // public Content getChild(Filter filter) {
  793. // int i = indexOf(0, filter);
  794. // return (i < 0) ? null : getContent(i);
  795. // }
  796. public boolean removeContent(final Content child) {
  797. return content.remove(child);
  798. }
  799. public Content removeContent(final int index) {
  800. return (Content) content.remove(index);
  801. }
  802. /**
  803. * Set this element's content to be the supplied child.
  804. * <p>
  805. * If the supplied child is legal content for this parent and before
  806. * it is added, all content in the current content list will
  807. * be cleared and all current children will have their parentage set to
  808. * null.
  809. * <p>
  810. * This has the effect that any active list (previously obtained with
  811. * a call to one of the {@link #getContent} methods will also change
  812. * to reflect the new content. In addition, all content in the supplied
  813. * collection will have their parentage set to this parent. If the user
  814. * wants to continue working with a <b>"live"</b> list of this parent's
  815. * child, then a call to setContent should be followed by a call to one
  816. * of the {@link #getContent} methods to obtain a <b>"live"</b>
  817. * version of the children.
  818. * <p>
  819. * Passing a null child clears the existing content.
  820. * <p>
  821. * In event of an exception the original content will be unchanged and
  822. * the supplied child will be unaltered.
  823. *
  824. * @param child new content to replace existing content
  825. * @return the parent on which the method was called
  826. * @throws IllegalAddException if the supplied child is already attached
  827. * or not legal content for an Element
  828. */
  829. public Element setContent(final Content child) {
  830. content.clear();
  831. content.add(child);
  832. return this;
  833. }
  834. /**
  835. * Determines if this element is the ancestor of another element.
  836. *
  837. * @param element <code>Element</code> to check against
  838. * @return <code>true</code> if this element is the ancestor of the
  839. * supplied element
  840. */
  841. public boolean isAncestor(final Element element) {
  842. Parent p = element.getParent();
  843. while (p instanceof Element) {
  844. if (p == this) {
  845. return true;
  846. }
  847. p = p.getParent();
  848. }
  849. return false;
  850. }
  851. /**
  852. * <p>
  853. * This returns the complete set of attributes for this element, as a
  854. * <code>List</code> of <code>Attribute</code> objects in no particular
  855. * order, or an empty list if there are none.
  856. * The returned list is "live" and changes to it affect the
  857. * element's actual attributes.
  858. * </p>
  859. *
  860. * @return attributes for the element
  861. */
  862. public List getAttributes() {
  863. return attributes;
  864. }
  865. /**
  866. * <p>
  867. * This returns the attribute for this element with the given name
  868. * and within no namespace, or null if no such attribute exists.
  869. * </p>
  870. *
  871. * @param name name of the attribute to return
  872. * @return attribute for the element
  873. */
  874. public Attribute getAttribute(final String name) {
  875. return getAttribute(name, Namespace.NO_NAMESPACE);
  876. }
  877. /**
  878. * <p>
  879. * This returns the attribute for this element with the given name
  880. * and within the given Namespace, or null if no such attribute exists.
  881. * </p>
  882. *
  883. * @param name name of the attribute to return
  884. * @param ns <code>Namespace</code> to search within
  885. * @return attribute for the element
  886. */
  887. public Attribute getAttribute(final String name, final Namespace ns) {
  888. return (Attribute) attributes.get(name, ns);
  889. }
  890. /**
  891. * <p>
  892. * This returns the attribute value for the attribute with the given name
  893. * and within no namespace, null if there is no such attribute, and the
  894. * empty string if the attribute value is empty.
  895. * </p>
  896. *
  897. * @param name name of the attribute whose value to be returned
  898. * @return the named attribute's value, or null if no such attribute
  899. */
  900. public String getAttributeValue(final String name) {
  901. return getAttributeValue(name, Namespace.NO_NAMESPACE);
  902. }
  903. /**
  904. * <p>
  905. * This returns the attribute value for the attribute with the given name
  906. * and within no namespace, or the passed-in default if there is no
  907. * such attribute.
  908. * </p>
  909. *
  910. * @param name name of the attribute whose value to be returned
  911. * @param def a default value to return if the attribute does not exist
  912. * @return the named attribute's value, or the default if no such attribute
  913. */
  914. public String getAttributeValue(final String name, final String def) {
  915. return getAttributeValue(name, Namespace.NO_NAMESPACE, def);
  916. }
  917. /**
  918. * <p>
  919. * This returns the attribute value for the attribute with the given name
  920. * and within the given Namespace, null if there is no such attribute, and
  921. * the empty string if the attribute value is empty.
  922. * </p>
  923. *
  924. * @param name name of the attribute whose valud is to be returned
  925. * @param ns <code>Namespace</code> to search within
  926. * @return the named attribute's value, or null if no such attribute
  927. */
  928. public String getAttributeValue(final String name, final Namespace ns) {
  929. return getAttributeValue(name, ns, null);
  930. }
  931. /**
  932. * <p>
  933. * This returns the attribute value for the attribute with the given name
  934. * and within the given Namespace, or the passed-in default if there is no
  935. * such attribute.
  936. * </p>
  937. *
  938. * @param name name of the attribute whose valud is to be returned
  939. * @param ns <code>Namespace</code> to search within
  940. * @param def a default value to return if the attribute does not exist
  941. * @return the named attribute's value, or the default if no such attribute
  942. */
  943. public String getAttributeValue(final String name, final Namespace ns, final String def) {
  944. final Attribute attribute = (Attribute) attributes.get(name, ns);
  945. if (attribute == null) {
  946. return def;
  947. }
  948. return attribute.getValue();
  949. }
  950. /**
  951. * <p>
  952. * This sets the attributes of the element. The supplied Collection should
  953. * contain only objects of type <code>Attribute</code>.
  954. * </p>
  955. *
  956. * <p>
  957. * When all objects in the supplied List are legal and before the new
  958. * attributes are added, all old attributes will have their
  959. * parentage set to null (no parent) and the old attribute list will be
  960. * cleared. This has the effect that any active attribute list (previously
  961. * obtained with a call to {@link #getAttributes}) will also change to
  962. * reflect the new attributes. In addition, all attributes in the supplied
  963. * List will have their parentage set to this element, but the List itself
  964. * will not be "live" and further removals and additions will have no
  965. * effect on this elements attributes. If the user wants to continue
  966. * working with a "live" attribute list, then a call to setAttributes
  967. * should be followed by a call to {@link #getAttributes} to obtain a
  968. * "live" version of the attributes.
  969. * </p>
  970. *
  971. * <p>
  972. * Passing a null or empty List clears the existing attributes.
  973. * </p>
  974. *
  975. * <p>
  976. * In cases where the List contains duplicate attributes, only the last
  977. * one will be retained. This has the same effect as calling
  978. * {@link #setAttribute(Attribute)} sequentially.
  979. * </p>
  980. *
  981. * <p>
  982. * In event of an exception the original attributes will be unchanged and
  983. * the attributes in the supplied attributes will be unaltered.
  984. * </p>
  985. *
  986. * @param newAttributes <code>Collection</code> of attributes to set
  987. * @return this element modified
  988. * @throws IllegalAddException if the List contains objects
  989. * that are not instances of <code>Attribute</code>,
  990. * or if any of the <code>Attribute</code> objects have
  991. * conflicting namespace prefixes.
  992. */
  993. public Element setAttributes(final Collection newAttributes) {
  994. attributes.clearAndSet(newAttributes);
  995. return this;
  996. }
  997. /**
  998. * <p>
  999. * This sets the attributes of the element. It's an alternate form of
  1000. * the method, accepting a <code>List</code> instead of a
  1001. * <code>Collection</code>, for backward compatibility.
  1002. * </p>
  1003. */
  1004. public Element setAttributes(final List newAttributes) {
  1005. return setAttributes((Collection)newAttributes);
  1006. }
  1007. /**
  1008. * <p>
  1009. * This sets an attribute value for this element. Any existing attribute
  1010. * with the same name and namespace URI is removed.
  1011. * </p>
  1012. *
  1013. * @param name name of the attribute to set
  1014. * @param value value of the attribute to set
  1015. * @return this element modified
  1016. * @throws IllegalNameException if the given name is illegal as an
  1017. * attribute name.
  1018. * @throws IllegalDataException if the given attribute value is
  1019. * illegal character data (as determined by
  1020. * {@link org.jdom.Verifier#checkCharacterData}).
  1021. */
  1022. public Element setAttribute(final String name, final String value) {
  1023. final Attribute attribute = getAttribute(name);
  1024. if (attribute == null) {
  1025. final Attribute newAttribute = new Attribute(name, value);
  1026. setAttribute(newAttribute);
  1027. } else {
  1028. attribute.setValue(value);
  1029. }
  1030. return this;
  1031. }
  1032. /**
  1033. * <p>
  1034. * This sets an attribute value for this element. Any existing attribute
  1035. * with the same name and namespace URI is removed.
  1036. * </p>
  1037. *
  1038. * @param name name of the attribute to set
  1039. * @param value value of the attribute to set
  1040. * @param ns namespace of the attribute to set
  1041. * @return this element modified
  1042. * @throws IllegalNameException if the given name is illegal as an
  1043. * attribute name, or if the namespace is an unprefixed default
  1044. * namespace
  1045. * @throws IllegalDataException if the given attribute value is
  1046. * illegal character data (as determined by
  1047. * {@link org.jdom.Verifier#checkCharacterData}).
  1048. * @throws IllegalAddException if the attribute namespace prefix
  1049. * collides with another namespace prefix on the element.
  1050. */
  1051. public Element setAttribute(final String name, final String value, final Namespace ns) {
  1052. final Attribute attribute = getAttribute(name, ns);
  1053. if (attribute == null) {
  1054. final Attribute newAttribute = new Attribute(name, value, ns);
  1055. setAttribute(newAttribute);
  1056. } else {
  1057. attribute.setValue(value);
  1058. }
  1059. return this;
  1060. }
  1061. /**
  1062. * <p>
  1063. * This sets an attribute value for this element. Any existing attribute
  1064. * with the same name and namespace URI is removed.
  1065. * </p>
  1066. *
  1067. * @param attribute <code>Attribute</code> to set
  1068. * @return this element modified
  1069. * @throws IllegalAddException if the attribute being added already has a
  1070. * parent or if the attribute namespace prefix collides with another
  1071. * namespace prefix on the element.
  1072. */
  1073. public Element setAttribute(final Attribute attribute) {
  1074. attributes.add(attribute);
  1075. return this;
  1076. }
  1077. /**
  1078. * <p>
  1079. * This removes the attribute with the given name and within no
  1080. * namespace. If no such attribute exists, this method does nothing.
  1081. * </p>
  1082. *
  1083. * @param name name of attribute to remove
  1084. * @return whether the attribute was removed
  1085. */
  1086. public boolean removeAttribute(final String name) {
  1087. return removeAttribute(name, Namespace.NO_NAMESPACE);
  1088. }
  1089. /**
  1090. * <p>
  1091. * This removes the attribute with the given name and within the
  1092. * given Namespace. If no such attribute exists, this method does
  1093. * nothing.
  1094. * </p>
  1095. *
  1096. * @param name name of attribute to remove
  1097. * @param ns namespace URI of attribute to remove
  1098. * @return whether the attribute was removed
  1099. */
  1100. public boolean removeAttribute(final String name, final Namespace ns) {
  1101. return attributes.remove(name, ns);
  1102. }
  1103. /**
  1104. * <p>
  1105. * This removes the supplied Attribute should it exist.
  1106. * </p>
  1107. *
  1108. * @param attribute Reference to the attribute to be removed.
  1109. * @return whether the attribute was removed
  1110. */
  1111. public boolean removeAttribute(final Attribute attribute) {
  1112. return attributes.remove(attribute);
  1113. }
  1114. /**
  1115. * <p>
  1116. * This returns a <code>String</code> representation of the
  1117. * <code>Element</code>, suitable for debugging. If the XML
  1118. * representation of the <code>Element</code> is desired,
  1119. * {@link org.jdom.output.XMLOutputter#outputString(Element)}
  1120. * should be used.
  1121. * </p>
  1122. *
  1123. * @return <code>String</code> - information about the
  1124. * <code>Element</code>
  1125. */
  1126. public String toString() {
  1127. final StringBuffer stringForm = new StringBuffer(64)
  1128. .append("[Element: <")
  1129. .append(getQualifiedName());
  1130. final String nsuri = getNamespaceURI();
  1131. if (!"".equals(nsuri)) {
  1132. stringForm
  1133. .append(" [Namespace: ")
  1134. .append(nsuri)
  1135. .append("]");
  1136. }
  1137. stringForm.append("/>]");
  1138. return stringForm.toString();
  1139. }
  1140. /**
  1141. * <p>
  1142. * This returns a deep clone of this element.
  1143. * The new element is detached from its parent, and getParent()
  1144. * on the clone will return null.
  1145. * </p>
  1146. *
  1147. * @return the clone of this element
  1148. */
  1149. public Object clone() {
  1150. // Ken Rune Helland <kenh@csc.no> is our local clone() guru
  1151. final Element element = (Element) super.clone();
  1152. // name and namespace are references to immutable objects
  1153. // so super.clone() handles them ok
  1154. // Reference to parent is copied by super.clone()
  1155. // (Object.clone()) so we have to remove it
  1156. // Actually, super is a Content, which has already detached in the
  1157. // clone().
  1158. // element.parent = null;
  1159. // Reference to content list and attribute lists are copyed by
  1160. // super.clone() so we set it new lists if the original had lists
  1161. element.content = new ContentList(element);
  1162. element.attributes = new AttributeList(element);
  1163. // Cloning attributes
  1164. if (attributes != null) {
  1165. for(int i = 0; i < attributes.size(); i++) {
  1166. final Attribute attribute = (Attribute) attributes.get(i);
  1167. element.attributes.add(attribute.clone());
  1168. }
  1169. }
  1170. // Cloning additional namespaces
  1171. if (additionalNamespaces != null) {
  1172. element.additionalNamespaces = new ArrayList(additionalNamespaces);
  1173. }
  1174. // Cloning content
  1175. if (content != null) {
  1176. for(int i = 0; i < content.size(); i++) {
  1177. final Content c = (Content) content.get(i);
  1178. element.content.add(c.clone());
  1179. }
  1180. }
  1181. return element;
  1182. }
  1183. // Support a custom Namespace serialization so no two namespace
  1184. // object instances may exist for the same prefix/uri pair
  1185. private void writeObject(final ObjectOutputStream out) throws IOException {
  1186. out.defaultWriteObject();
  1187. // We use writeObject() and not writeUTF() to minimize space
  1188. // This allows for writing pointers to already written strings
  1189. out.writeObject(namespace.getPrefix());
  1190. out.writeObject(namespace.getURI());
  1191. if (additionalNamespaces == null) {
  1192. out.write(0);
  1193. }
  1194. else {
  1195. final int size = additionalNamespaces.size();
  1196. out.write(size);
  1197. for (int i = 0; i < size; i++) {
  1198. final Namespace additional = (Namespace) additionalNamespaces.get(i);
  1199. out.writeObject(additional.getPrefix());
  1200. out.writeObject(additional.getURI());
  1201. }
  1202. }
  1203. }
  1204. private void readObject(final ObjectInputStream in)
  1205. throws IOException, ClassNotFoundException {
  1206. in.defaultReadObject();
  1207. namespace = Namespace.getNamespace(
  1208. (String)in.readObject(), (String)in.readObject());
  1209. final int size = in.read();
  1210. if (size != 0) {
  1211. additionalNamespaces = new ArrayList(size);
  1212. for (int i = 0; i < size; i++) {
  1213. final Namespace additional = Namespace.getNamespace(
  1214. (String)in.readObject(), (String)in.readObject());
  1215. additionalNamespaces.add(additional);
  1216. }
  1217. }
  1218. }
  1219. /**
  1220. * Returns an iterator that walks over all descendants in document order.
  1221. *
  1222. * @return an iterator to walk descendants
  1223. */
  1224. public Iterator getDescendants() {
  1225. return new DescendantIterator(this);
  1226. }
  1227. /**
  1228. * Returns an iterator that walks over all descendants in document order
  1229. * applying the Filter to return only elements that match the filter rule.
  1230. * With filters you can match only Elements, only Comments, Elements or
  1231. * Comments, only Elements with a given name and/or prefix, and so on.
  1232. *
  1233. * @param filter filter to select which descendants to see
  1234. * @return an iterator to walk descendants within a filter
  1235. */
  1236. public Iterator getDescendants(final Filter filter) {
  1237. final Iterator iterator = new DescendantIterator(this);
  1238. return new FilterIterator(iterator, filter);
  1239. }
  1240. /**
  1241. * This returns a <code>List</code> of all the child elements
  1242. * nested directly (one level deep) within this element, as
  1243. * <code>Element</code> objects. If this target element has no nested
  1244. * elements, an empty List is returned. The returned list is "live"
  1245. * in document order and changes to it affect the element's actual
  1246. * contents.
  1247. *
  1248. * <p>
  1249. * Sequential traversal through the List is best done with a Iterator
  1250. * since the underlying implement of List.size() may not be the most
  1251. * efficient.
  1252. * </p>
  1253. *
  1254. * <p>
  1255. * No recursion is performed, so elements nested two levels deep
  1256. * would have to be obtained with:
  1257. * <pre>
  1258. * <code>
  1259. * Iterator itr = (currentElement.getChildren()).iterator();
  1260. * while(itr.hasNext()) {
  1261. * Element oneLevelDeep = (Element)itr.next();
  1262. * List twoLevelsDeep = oneLevelDeep.getChildren();
  1263. * // Do something with these children
  1264. * }
  1265. * </code>
  1266. * </pre>
  1267. * </p>
  1268. *
  1269. * @return list of child <code>Element</code> objects for this element
  1270. */
  1271. public List getChildren() {
  1272. return content.getView(new ElementFilter());
  1273. }
  1274. /**
  1275. * This returns a <code>List</code> of all the child elements
  1276. * nested directly (one level deep) within this element with the given
  1277. * local name and belonging to no namespace, returned as
  1278. * <code>Element</code> objects. If this target element has no nested
  1279. * elements with the given name outside a namespace, an empty List
  1280. * is returned. The returned list is "live" in document order
  1281. * and changes to it affect the element's actual contents.
  1282. * <p>
  1283. * Please see the notes for <code>{@link #getChildren}</code>
  1284. * for a code example.
  1285. * </p>
  1286. *
  1287. * @param name local name for the children to match
  1288. * @return all matching child elements
  1289. */
  1290. public List getChildren(final String name) {
  1291. return getChildren(name, Namespace.NO_NAMESPACE);
  1292. }
  1293. /**
  1294. * This returns a <code>List</code> of all the child elements
  1295. * nested directly (one level deep) within this element with the given
  1296. * local name and belonging to the given Namespace, returned as
  1297. * <code>Element</code> objects. If this target element has no nested
  1298. * elements with the given name in the given Namespace, an empty List
  1299. * is returned. The returned list is "live" in document order
  1300. * and changes to it affect the element's actual contents.
  1301. * <p>
  1302. * Please see the notes for <code>{@link #getChildren}</code>
  1303. * for a code example.
  1304. * </p>
  1305. *
  1306. * @param name local name for the children to match
  1307. * @param ns <code>Namespace</code> to search within
  1308. * @return all matching child elements
  1309. */
  1310. public List getChildren(final String name, final Namespace ns) {
  1311. return content.getView(new ElementFilter(name, ns));
  1312. }
  1313. /**
  1314. * This returns the first child element within this element with the
  1315. * given local name and belonging to the given namespace.
  1316. * If no elements exist for the specified name and namespace, null is
  1317. * returned.
  1318. *
  1319. * @param name local name of child element to match
  1320. * @param ns <code>Namespace</code> to search within
  1321. * @return the first matching child element, or null if not found
  1322. */
  1323. public Element getChild(final String name, final Namespace ns) {
  1324. final List elements = content.getView(new ElementFilter(name, ns));
  1325. final Iterator iter = elements.iterator();
  1326. if (iter.hasNext()) {
  1327. return (Element) iter.next();
  1328. }
  1329. return null;
  1330. }
  1331. /**
  1332. * This returns the first child element within this element with the
  1333. * given local name and belonging to no namespace.
  1334. * If no elements exist for the specified name and namespace, null is
  1335. * returned.
  1336. *
  1337. * @param name local name of child element to match
  1338. * @return the first matching child element, or null if not found
  1339. */
  1340. public Element getChild(final String name) {
  1341. return getChild(name, Namespace.NO_NAMESPACE);
  1342. }
  1343. /**
  1344. * <p>
  1345. * This removes the first child element (one level deep) with the
  1346. * given local name and belonging to no namespace.
  1347. * Returns true if a child was removed.
  1348. * </p>
  1349. *
  1350. * @param name the name of child elements to remove
  1351. * @return whether deletion occurred
  1352. */
  1353. public boolean removeChild(final String name) {
  1354. return removeChild(name, Namespace.NO_NAMESPACE);
  1355. }
  1356. /**
  1357. * <p>
  1358. * This removes the first child element (one level deep) with the
  1359. * given local name and belonging to the given namespace.
  1360. * Returns true if a child was removed.
  1361. * </p>
  1362. *
  1363. * @param name the name of child element to remove
  1364. * @param ns <code>Namespace</code> to search within
  1365. * @return whether deletion occurred
  1366. */
  1367. public boolean removeChild(final String name, final Namespace ns) {
  1368. final Filter filter = new ElementFilter(name, ns);
  1369. final List old = content.getView(filter);
  1370. final Iterator iter = old.iterator();
  1371. if (iter.hasNext()) {
  1372. iter.next();
  1373. iter.remove();
  1374. return true;
  1375. }
  1376. return false;
  1377. }
  1378. /**
  1379. * <p>
  1380. * This removes all child elements (one level deep) with the
  1381. * given local name and belonging to no namespace.
  1382. * Returns true if any were removed.
  1383. * </p>
  1384. *
  1385. * @param name the name of child elements to remove
  1386. * @return whether deletion occurred
  1387. */
  1388. public boolean removeChildren(final String name) {
  1389. return removeChildren(name, Namespace.NO_NAMESPACE);
  1390. }
  1391. /**
  1392. * <p>
  1393. * This removes all child elements (one level deep) with the
  1394. * given local name and belonging to the given namespace.
  1395. * Returns true if any were removed.
  1396. * </p>
  1397. *
  1398. * @param name the name of child elements to remove
  1399. * @param ns <code>Namespace</code> to search within
  1400. * @return whether deletion occurred
  1401. */
  1402. public boolean removeChildren(final String name, final Namespace ns) {
  1403. boolean deletedSome = false;
  1404. final Filter filter = new ElementFilter(name, ns);
  1405. final List old = content.getView(filter);
  1406. final Iterator iter = old.iterator();
  1407. while (iter.hasNext()) {
  1408. iter.next();
  1409. iter.remove();
  1410. deletedSome = true;
  1411. }
  1412. return deletedSome;
  1413. }
  1414. }