Changed license to LGPLv3 or later.
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / data / xml / XmlElement.java
1 /*
2  * Svjatoslav Commons - shared library of common functionality.
3  * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 3 of the GNU Lesser General Public License
7  * or later as published by the Free Software Foundation.
8  */
9
10 package eu.svjatoslav.commons.data.xml;
11
12 import java.util.ArrayList;
13 import java.util.List;
14
15 import org.w3c.dom.Element;
16 import org.w3c.dom.NamedNodeMap;
17 import org.w3c.dom.Node;
18
19 public class XmlElement {
20
21         Element element;
22
23         public XmlElement(final Element element) {
24                 this.element = element;
25         }
26
27         public List<String> getAttributeNames() {
28                 final ArrayList<String> result = new ArrayList<String>();
29
30                 final NamedNodeMap attributes = element.getAttributes();
31
32                 for (int i = 0; i < attributes.getLength(); i++) {
33                         final Node node = attributes.item(i);
34
35                         result.add(node.getNodeName());
36                 }
37
38                 return result;
39         }
40
41         public String getAttributeValue(final String attributeName) {
42                 return element.getAttribute(attributeName);
43         }
44
45         @Override
46         public String toString() {
47                 final StringBuffer result = new StringBuffer();
48
49                 result.append("node name: " + element.getNodeName() + "\n");
50
51                 final NamedNodeMap attributes = element.getAttributes();
52
53                 for (int i = 0; i < attributes.getLength(); i++) {
54                         final Node node = attributes.item(i);
55
56                         result.append("    " + node.getNodeName() + "\n");
57                 }
58
59                 return result.toString();
60         }
61
62 }