2 * Copyright (c) 2012, the Last.fm Java Project and Committers All rights
3 * reserved. Redistribution and use of this software in source and binary forms,
4 * with or without modification, are permitted provided that the following
5 * conditions are met: - Redistributions of source code must retain the above
6 * copyright notice, this list of conditions and the following disclaimer. -
7 * Redistributions in binary form must reproduce the above copyright notice,
8 * this list of conditions and the following disclaimer in the documentation
9 * and/or other materials provided with the distribution. THIS SOFTWARE IS
10 * PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
11 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
13 * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
14 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
16 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
17 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
18 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
19 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 package com.cyanogenmod.eleven.lastfm;
24 import org.w3c.dom.Element;
25 import org.w3c.dom.Node;
26 import org.w3c.dom.NodeList;
28 import java.util.ArrayList;
29 import java.util.List;
32 * <code>DomElement</code> wraps around an {@link Element} and provides
33 * convenience methods.
35 * @author Janni Kovacs
37 public class DomElement {
38 private final Element e;
41 * Creates a new wrapper around the given {@link Element}.
43 * @param elem An w3c Element
45 public DomElement(final Element elem) {
50 * @return the original Element
52 public Element getElement() {
57 * Tests if this element has an attribute with the specified name.
59 * @param name Name of the attribute.
60 * @return <code>true</code> if this element has an attribute with the
63 public boolean hasAttribute(final String name) {
64 return e.hasAttribute(name);
68 * Returns the attribute value to a given attribute name or
69 * <code>null</code> if the attribute doesn't exist.
71 * @param name The attribute's name
72 * @return Attribute value or <code>null</code>
74 public String getAttribute(final String name) {
75 return e.hasAttribute(name) ? e.getAttribute(name) : null;
79 * @return the text content of the element
81 public String getText() {
82 // XXX e.getTextContent() doesn't exsist under Android (Lukasz
84 // / getTextContent() is now available in at least Android 2.2 if not
85 // earlier, so we'll keep using that
86 // return e.hasChildNodes() ? e.getFirstChild().getNodeValue() : null;
87 return e.getTextContent();
91 * Checks if this element has a child element with the given name.
93 * @param name The child's name
94 * @return <code>true</code> if this element has a child element with the
97 public boolean hasChild(final String name) {
98 final NodeList list = e.getElementsByTagName(name);
99 for (int i = 0, j = list.getLength(); i < j; i++) {
100 final Node item = list.item(i);
101 if (item.getParentNode() == e) {
109 * Returns the child element with the given name or <code>null</code> if it
112 * @param name The child's name
113 * @return the child element or <code>null</code>
115 public DomElement getChild(final String name) {
116 final NodeList list = e.getElementsByTagName(name);
117 if (list.getLength() == 0) {
120 for (int i = 0, j = list.getLength(); i < j; i++) {
121 final Node item = list.item(i);
122 if (item.getParentNode() == e) {
123 return new DomElement((Element)item);
130 * Returns the text content of a child node with the given name. If no such
131 * child exists or the child does not have text content, <code>null</code>
134 * @param name The child's name
135 * @return the child's text content or <code>null</code>
137 public String getChildText(final String name) {
138 final DomElement child = getChild(name);
139 return child != null ? child.getText() : null;
143 * @return all children of this element
145 public List<DomElement> getChildren() {
146 return getChildren("*");
150 * Returns all children of this element with the given tag name.
152 * @param name The children's tag name
153 * @return all matching children
155 public List<DomElement> getChildren(final String name) {
156 final List<DomElement> l = new ArrayList<DomElement>();
157 final NodeList list = e.getElementsByTagName(name);
158 for (int i = 0; i < list.getLength(); i++) {
159 final Node node = list.item(i);
160 if (node.getParentNode() == e) {
161 l.add(new DomElement((Element)node));
168 * Returns this element's tag name.
170 * @return the tag name
172 public String getTagName() {
173 return e.getTagName();