1 package org.catacomb.serial.om;
2
3 import org.catacomb.interlish.structure.Attribute;
4 import org.catacomb.interlish.structure.Element;
5 import org.catacomb.serial.ElementSerializer;
6
7
8 import java.util.ArrayList;
9 import java.util.List;
10
11
12 public class OmElement implements Element {
13
14 String name;
15
16 String body;
17
18 List<Attribute> attributes;
19 List<Element> elements;
20
21
22 private Attribute p_bufAtt;
23
24 public OmElement() {
25
26 }
27
28 public OmElement(String s) {
29 name = s;
30 }
31
32
33 public String toString() {
34 String sret = "OmElemnt:" + name;
35
36 if (attributes != null) {
37 for (Attribute oma : attributes) {
38 sret += " " + oma.getName() + "=" + oma.getValue();
39 }
40 }
41
42 int nel = (elements != null ? elements.size() : 0);
43 sret += " [" + nel + " elements]";
44 sret += " body=" + body;
45 return sret;
46 }
47
48 public void addToBody(String s) {
49 if (body == null) {
50 body = s;
51 } else {
52 body += " " + s;
53 }
54 }
55
56
57 public OmElement(ArrayList<Attribute> ala, ArrayList<Element> ale) {
58 name = null;
59 attributes = ala;
60 elements = ale;
61 body = null;
62 }
63
64
65 public void setName(String s) {
66 name = s;
67 }
68
69
70 public String getName() {
71 return name;
72 }
73
74
75 public void setBody(String s) {
76 body = s;
77 }
78
79 public String getBody() {
80 return body;
81 }
82
83 public boolean hasBody() {
84 return (body != null);
85 }
86
87 public boolean hasText() {
88 return (body != null);
89 }
90
91 public String getText() {
92 return body;
93 }
94
95
96 public void copyAttributes(Attribute[] atta) {
97 if (atta != null) {
98 for (int i = 0; i < atta.length; i++) {
99 Attribute att = atta[i];
100 addAttribute(att.getName(), att.getValue());
101 }
102 }
103 }
104
105
106
107 public Attribute[] getAttributeArray() {
108 Attribute[] ret = null;
109 if (attributes != null) {
110 ret = new Attribute[attributes.size()];
111 int na = 0;
112
113 for (Attribute att : attributes) {
114 ret[na++] = att;
115 }
116 }
117 return ret;
118 }
119
120
121
122 public boolean hasAttribute(String nm) {
123 return (getAttribute(nm) != null);
124 }
125
126
127 public void setAttribute(String nm, String val) {
128 OmAttribute oma = getOmAttribute(nm);
129 oma.setValue(val);
130 }
131
132
133 public OmAttribute getOmAttribute(String nm) {
134 OmAttribute oma = null;
135 if (attributes != null) {
136 for (Attribute att : attributes) {
137 if (att.getName().equals(nm)) {
138 oma = (OmAttribute)att;
139 break;
140 }
141 }
142 }
143 return oma;
144 }
145
146
147
148 public String getAttribute(String nm) {
149 String ret = null;
150
151
152 if (p_bufAtt != null && p_bufAtt.getName().equals(nm)) {
153 ret = p_bufAtt.getValue();
154
155 } else {
156 p_bufAtt = getOmAttribute(nm);
157 if (p_bufAtt != null) {
158 ret = p_bufAtt.getValue();
159 }
160
161 }
162
163 return ret;
164 }
165
166
167 public boolean hasAttributes() {
168 return (attributes != null && attributes.size() > 0);
169 }
170
171 public boolean hasElements() {
172 return (elements != null && elements.size() > 0);
173 }
174
175
176
177 public Element[] getElementArray() {
178 Element[] ret = null;
179 if (elements != null) {
180 ret = new Element[elements.size()];
181 int na = 0;
182
183 for (Element elt : elements) {
184 ret[na++] = elt;
185 }
186 }
187 return ret;
188 }
189
190
191 public Element getElement(String s) {
192 Element ret = null;
193 if (elements != null) {
194
195 for (Element elt : elements) {
196 if (elt.getName().equals(s)) {
197 ret = elt;
198 break;
199 }
200 }
201 }
202 return ret;
203 }
204
205
206
207 public void addElement(Element elt) {
208 if (elements == null) {
209 elements = new ArrayList<Element>();
210 }
211 elements.add(elt);
212 }
213
214
215 public void addAttribute(String n, String v) {
216 Attribute att = new OmAttribute(n, v);
217 addAttribute(att);
218 }
219
220
221 public void addAttribute(Attribute att) {
222 if (attributes == null) {
223 attributes = new ArrayList<Attribute>();
224 }
225 attributes.add(att);
226 }
227
228
229
230
231 public void setAttributes(ArrayList<Attribute> ala) {
232 if (attributes != null && attributes.size() > 0) {
233 System.out.println("sand.state.element error - overwriting " +
234 "non empty att array ");
235 (new Exception()).printStackTrace();
236 }
237 attributes = ala;
238 }
239
240
241
242
243
244 public void setElements(ArrayList<Element> ale) {
245 if (elements != null && elements.size() > 0) {
246 System.out.println("sand.state.element error - overwriting " +
247 "non empty elt array ");
248 (new Exception()).printStackTrace();
249 }
250
251 elements = ale;
252 }
253
254
255
256
257 public List<Attribute> getAttributes() {
258 return attributes;
259 }
260
261 public List<Element> getElements() {
262 return elements;
263 }
264
265 public String serialize() {
266 return ElementSerializer.serializeContent(this);
267 }
268
269
270
271 }
272
273