OSDN Git Service

3f09d8c4f3c3f26ca1813c356d88d8d35797f6b2
[android-x86/packages-apps-Eleven.git] / src / com / andrew / apollo / lastfm / DomElement.java
1 /*
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.
20  */
21
22 package com.andrew.apollo.lastfm;
23
24 import org.w3c.dom.Element;
25 import org.w3c.dom.Node;
26 import org.w3c.dom.NodeList;
27
28 import java.util.ArrayList;
29 import java.util.List;
30
31 /**
32  * <code>DomElement</code> wraps around an {@link Element} and provides
33  * convenience methods.
34  * 
35  * @author Janni Kovacs
36  */
37 public class DomElement {
38     private final Element e;
39
40     /**
41      * Creates a new wrapper around the given {@link Element}.
42      * 
43      * @param elem An w3c Element
44      */
45     public DomElement(final Element elem) {
46         e = elem;
47     }
48
49     /**
50      * @return the original Element
51      */
52     public Element getElement() {
53         return e;
54     }
55
56     /**
57      * Tests if this element has an attribute with the specified name.
58      * 
59      * @param name Name of the attribute.
60      * @return <code>true</code> if this element has an attribute with the
61      *         specified name.
62      */
63     public boolean hasAttribute(final String name) {
64         return e.hasAttribute(name);
65     }
66
67     /**
68      * Returns the attribute value to a given attribute name or
69      * <code>null</code> if the attribute doesn't exist.
70      * 
71      * @param name The attribute's name
72      * @return Attribute value or <code>null</code>
73      */
74     public String getAttribute(final String name) {
75         return e.hasAttribute(name) ? e.getAttribute(name) : null;
76     }
77
78     /**
79      * @return the text content of the element
80      */
81     public String getText() {
82         // XXX e.getTextContent() doesn't exsist under Android (Lukasz
83         // Wisniewski)
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();
88     }
89
90     /**
91      * Checks if this element has a child element with the given name.
92      * 
93      * @param name The child's name
94      * @return <code>true</code> if this element has a child element with the
95      *         given name
96      */
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) {
102                 return true;
103             }
104         }
105         return false;
106     }
107
108     /**
109      * Returns the child element with the given name or <code>null</code> if it
110      * doesn't exist.
111      * 
112      * @param name The child's name
113      * @return the child element or <code>null</code>
114      */
115     public DomElement getChild(final String name) {
116         final NodeList list = e.getElementsByTagName(name);
117         if (list.getLength() == 0) {
118             return null;
119         }
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);
124             }
125         }
126         return null;
127     }
128
129     /**
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>
132      * is returned.
133      * 
134      * @param name The child's name
135      * @return the child's text content or <code>null</code>
136      */
137     public String getChildText(final String name) {
138         final DomElement child = getChild(name);
139         return child != null ? child.getText() : null;
140     }
141
142     /**
143      * @return all children of this element
144      */
145     public List<DomElement> getChildren() {
146         return getChildren("*");
147     }
148
149     /**
150      * Returns all children of this element with the given tag name.
151      * 
152      * @param name The children's tag name
153      * @return all matching children
154      */
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));
162             }
163         }
164         return l;
165     }
166
167     /**
168      * Returns this element's tag name.
169      * 
170      * @return the tag name
171      */
172     public String getTagName() {
173         return e.getTagName();
174     }
175 }