1 | package genius.core.xml;
|
---|
2 | /*
|
---|
3 | * @(#)SimpleElement.java
|
---|
4 | */
|
---|
5 |
|
---|
6 | import java.util.HashMap;
|
---|
7 | import java.util.Iterator;
|
---|
8 | import java.util.LinkedList;
|
---|
9 | import java.util.ListIterator;
|
---|
10 | import java.util.Map;
|
---|
11 | import java.util.Vector;
|
---|
12 |
|
---|
13 | import java.io.BufferedWriter;
|
---|
14 | import java.io.FileWriter;
|
---|
15 | import java.io.IOException;
|
---|
16 | import java.io.Serializable;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * <code>SimpleElement</code> is the only node type for
|
---|
20 | * simplified DOM model.
|
---|
21 | */
|
---|
22 | public class SimpleElement implements Serializable {
|
---|
23 | /**
|
---|
24 | *
|
---|
25 | */
|
---|
26 | private static final long serialVersionUID = -575978416803618689L;
|
---|
27 | protected String tagName;
|
---|
28 | protected String text;
|
---|
29 | protected HashMap<String, String> attributes;
|
---|
30 | protected LinkedList<SimpleElement> childElements;
|
---|
31 |
|
---|
32 | public SimpleElement(String tagName) {
|
---|
33 | this.tagName = tagName;
|
---|
34 | attributes = new HashMap<String, String>();
|
---|
35 | childElements = new LinkedList<SimpleElement>();
|
---|
36 | }
|
---|
37 |
|
---|
38 | public String getTagName() {
|
---|
39 | return tagName;
|
---|
40 | }
|
---|
41 |
|
---|
42 | public void setTagName(String tagName) {
|
---|
43 | this.tagName = tagName;
|
---|
44 | }
|
---|
45 |
|
---|
46 | public String getText() {
|
---|
47 | return text;
|
---|
48 | }
|
---|
49 |
|
---|
50 | public void setText(String text) {
|
---|
51 | this.text = text;
|
---|
52 | }
|
---|
53 |
|
---|
54 | public String getAttribute(String name) {
|
---|
55 | return attributes.get(name);
|
---|
56 | }
|
---|
57 |
|
---|
58 | public HashMap<String, String> getAttributes() {
|
---|
59 | return attributes;
|
---|
60 | }
|
---|
61 |
|
---|
62 | public void setAttribute(String name, String value) {
|
---|
63 | attributes.put(name, value);
|
---|
64 | }
|
---|
65 |
|
---|
66 | public void addChildElement(SimpleElement element) {
|
---|
67 | childElements.add(element);
|
---|
68 | }
|
---|
69 |
|
---|
70 | public Object[] getChildElements() {
|
---|
71 | return childElements.toArray();
|
---|
72 | }
|
---|
73 |
|
---|
74 | public LinkedList<SimpleElement> getChildElementsAsList() {
|
---|
75 | return childElements;
|
---|
76 | }
|
---|
77 |
|
---|
78 | public boolean isEmpty()
|
---|
79 | {
|
---|
80 | return attributes.isEmpty() && childElements.isEmpty();
|
---|
81 | }
|
---|
82 |
|
---|
83 | public void combineLists(HashMap<String,String> element){
|
---|
84 | //System.out.println("Testing");
|
---|
85 | //setAttribute("Key", "String");
|
---|
86 | attributes.put("Testing", "Here");
|
---|
87 | Iterator it = element.entrySet().iterator();
|
---|
88 | while (it.hasNext()) {
|
---|
89 | Map.Entry pairs = (Map.Entry)it.next();
|
---|
90 | setAttribute(pairs.getKey().toString(), pairs.getValue().toString());
|
---|
91 |
|
---|
92 |
|
---|
93 | //System.out.println(pairs.getKey() + " = " + pairs.getValue());
|
---|
94 | it.remove(); // avoids a ConcurrentModificationException
|
---|
95 | }
|
---|
96 |
|
---|
97 | //HashMap<String, String> temp = attributes;
|
---|
98 | //attributes.putAll(temp);
|
---|
99 | //attributes.putAll(element);
|
---|
100 | }
|
---|
101 |
|
---|
102 | public Object[] getChildByTagName(String tagName) {
|
---|
103 | // LinkedList<Object> result = new LinkedList<Object>();
|
---|
104 | Vector<Object> result = new Vector<Object>();
|
---|
105 | ListIterator<SimpleElement> iter = childElements.listIterator();
|
---|
106 | while(iter.hasNext()) {
|
---|
107 | SimpleElement se = iter.next();
|
---|
108 | String seTagName = se.getTagName();
|
---|
109 | if (seTagName.equals(tagName))
|
---|
110 | result.add(se);
|
---|
111 | }
|
---|
112 | Object[] resultArray = new Object[result.size()];//for some reason the toArray gave me a direct reference to the last element of the returned array, not the array itself. - Hdv.
|
---|
113 | for(int ind=0; ind < result.size(); ind++){
|
---|
114 | resultArray[ind] = result.elementAt(ind);
|
---|
115 | }
|
---|
116 | return resultArray;
|
---|
117 | }
|
---|
118 |
|
---|
119 | public String toString()
|
---|
120 | {
|
---|
121 | StringBuffer s;
|
---|
122 | if (childElements.isEmpty())
|
---|
123 | s = new StringBuffer(64);
|
---|
124 | else
|
---|
125 | s = new StringBuffer(1024);
|
---|
126 |
|
---|
127 | s.append("<");
|
---|
128 | s.append(tagName);
|
---|
129 | //save all attributes
|
---|
130 | for(int i=0;i<attributes.size();i++) {
|
---|
131 | String lAttrName = (String)(attributes.keySet().toArray()[i]);
|
---|
132 | String lAttrValue="";
|
---|
133 | if (attributes.entrySet().toArray()[i]!=null)
|
---|
134 | lAttrValue= (attributes.get(lAttrName));
|
---|
135 |
|
---|
136 | s.append(" ");
|
---|
137 | s.append(lAttrName);
|
---|
138 | s.append("=\"");
|
---|
139 | s.append(lAttrValue);
|
---|
140 | s.append("\"");
|
---|
141 | }
|
---|
142 | s.append("> \n");
|
---|
143 | //save all children
|
---|
144 | for(int i=0;i<childElements.size();i++) {
|
---|
145 | SimpleElement lSE = (SimpleElement)getChildElements()[i];
|
---|
146 | s.append(lSE.toString());
|
---|
147 | }
|
---|
148 | if(text!=null) {
|
---|
149 | s.append(text);
|
---|
150 | s.append(" \n");
|
---|
151 | }
|
---|
152 | s.append("</");
|
---|
153 | s.append(tagName);
|
---|
154 | s.append("> \n");
|
---|
155 |
|
---|
156 | return s.toString();
|
---|
157 | }
|
---|
158 | public void saveToFile(String pFileName) {
|
---|
159 | try {
|
---|
160 | BufferedWriter out = new BufferedWriter(new FileWriter(pFileName));
|
---|
161 | String lXML = toString();
|
---|
162 | out.write(lXML);
|
---|
163 | out.close();
|
---|
164 | } catch (IOException e) {
|
---|
165 | e.printStackTrace();
|
---|
166 | }
|
---|
167 |
|
---|
168 | }
|
---|
169 | }
|
---|