Added README.txt and GPL Header to Source Files
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / network / XmlEntry.java
1 /* XmlEntry.java
2  *
3  * This program is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 package de.dhbwloe.campusapp.network;
17 import android.os.Bundle;
18 import android.util.Log;
19 import android.util.Xml;
20
21 import java.util.ArrayList;
22
23 /**
24  * Created by pk910 on 22.01.2016.
25  */
26 public class XmlEntry {
27     public static XmlEntry FindXmlEntryByName(XmlEntry root, String name) {
28         if(root.getName().equalsIgnoreCase(name))
29             return root;
30         for(XmlEntry entry : root.getChildren()) {
31             entry = FindXmlEntryByName(entry, name);
32             if(entry != null)
33                 return entry;
34         }
35         return null;
36     }
37
38     public static XmlEntry[] FindXmlEntriesByName(XmlEntry root, String name) {
39         Log.i("XMLFind", "Search "+name+"  have: "+root.getName());
40         if(root.getName().equalsIgnoreCase(name))
41             return new XmlEntry[] { root };
42
43         ArrayList<XmlEntry> entries = new ArrayList<XmlEntry>();
44         for(XmlEntry entry : root.getChildren()) {
45             XmlEntry[] centries = FindXmlEntriesByName(entry, name);
46             for(XmlEntry centry : centries)
47                 entries.add(centry);
48         }
49         XmlEntry[] entriesArr = new XmlEntry[entries.size()];
50         entriesArr = entries.toArray(entriesArr);
51         return entriesArr;
52     }
53
54
55     private String name;
56     private String value;
57     private ArrayList<XmlEntry> children = new ArrayList<XmlEntry>();
58     private Bundle attributes = new Bundle();
59
60     public XmlEntry(String name) {
61         this.name = name;
62     }
63
64     public void setValue(String value) {
65         this.value = value;
66     }
67
68     public String getName() {
69         return name;
70     }
71
72     public String getValue() {
73         return value;
74     }
75
76     public void addChild(XmlEntry child) {
77         children.add(child);
78     }
79
80     public XmlEntry[] getChildren() {
81         XmlEntry childs[] = new XmlEntry[children.size()];
82         childs = children.toArray(childs);
83         return childs;
84     }
85
86     public void setAttribute(String name, String value) {
87         attributes.putString(name, value);
88     }
89
90     public void setAttributes(Bundle bundle) {
91         attributes.putAll(bundle);
92     }
93
94     public Bundle getAttributes() {
95         return attributes;
96     }
97
98     public String getAttribute(String name) {
99         return attributes.getString(name);
100     }
101
102 }