-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathElement.java
More file actions
676 lines (572 loc) · 18.3 KB
/
Copy pathElement.java
File metadata and controls
676 lines (572 loc) · 18.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
/*******************************************************************************
* Copyright (c) 2013, 2025 James M. ZHOU
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package org.tinystruct.dom;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Vector;
public class Element implements Cloneable {
private String name;
private String data;
private ElementType elementType = ElementType.NORMAL;
private List<Attribute> attributes;
private List<Element> childNodes;
private Element parent;
private boolean haveData = false;
/**
* Default Constructor
*/
public Element() {
this.name = "";
this.childNodes = new Vector<Element>();
this.attributes = new Vector<Attribute>();
this.data = "";
}
/**
* Constructor
*
* @param name name
*/
public Element(String name) {
this.name = name;
this.attributes = new Vector<Attribute>();
this.childNodes = new Vector<Element>();
this.data = "";
}
/**
* Constructor
*
* @param name name
* @param attributes list of attribute
*/
public Element(String name, List<Attribute> attributes) {
this.name = name;
this.attributes = attributes;
this.childNodes = new Vector<Element>();
this.data = "";
}
/**
* Constructor
*
* @param name name
* @param data data
*/
public Element(String name, String data) {
this.name = name;
this.attributes = new Vector<Attribute>();
this.childNodes = new Vector<Element>();
this.data = data;
}
public void setElementType(ElementType type) {
this.elementType = type;
}
public ElementType getElementType() {
return this.elementType;
}
/**
* Add attribute to this xml element.
*
* @param keyName name of key
* @param value new attribute value
* @return old attribute value
*/
public Attribute setAttribute(String keyName, Object value) {
if ((value != null) && (keyName != null)) {
boolean exists = false;
Iterator<Attribute> iterator = this.attributes.iterator();
Attribute attribute;
while (iterator.hasNext()) {
attribute = iterator.next();
if (attribute.name.equalsIgnoreCase(keyName)) {
exists = true;
break;
}
}
if (!exists) {
Attribute returnValue = new Attribute(keyName, String.valueOf(value));
this.attributes.add(returnValue);
return returnValue;
}
}
return null;
}
/**
* String getAttribute(String keyName)
*
* @param attributeName attribute name
* @return attribute
*/
public String getAttribute(String attributeName) {
Iterator<Attribute> iterator = this.attributes.iterator();
Attribute attribute;
while (iterator.hasNext()) {
attribute = iterator.next();
if (attribute.name.equalsIgnoreCase(attributeName)) {
return attribute.value;
}
}
return "";
}
public void removeAttribute(String attributeName) {
Iterator<Attribute> iterator = this.attributes.iterator();
Attribute attribute;
while (iterator.hasNext()) {
attribute = iterator.next();
if (attribute.name.equalsIgnoreCase(attributeName)) {
this.attributes.remove(attribute);
break;
}
}
}
/**
* Get attributes.
*
* @return attributes
*/
public List<Attribute> getAttributes() {
return this.attributes;
}
/**
* @param attributes List to use as the attributes
*/
public void setAttributes(List<Attribute> attributes) {
this.attributes = attributes;
}
/**
* @return Enumeration
*/
public Vector<String> getAttributeNames() {
int i = 0, size = this.attributes.size();
Vector<String> keys = new Vector<String>(size);
while (i < size) {
keys.add(this.attributes.get(i++).name);
}
return keys;
}
/**
* @param element element
* @return boolean
*/
public boolean addElement(Element element) {
element.setParent(this);
element.level = this.level + 1;
return this.childNodes.add(element);
}
public Element addElement(String elementName) {
Element element = new Element(elementName);
if (this.addElement(element)) {
return element;
}
return null;
}
public boolean removeElement(Element element) {
return this.childNodes.remove(element);
}
public boolean removeElement(String elementName) {
int i = 0;
Element the_target_element = null;
while (i < this.childNodes.size()) {
the_target_element = this.childNodes.get(i);
if (the_target_element.name.equalsIgnoreCase(elementName))
break;
}
return this.childNodes.remove(the_target_element);
}
public Element removeElement(int index) {
return this.childNodes.remove(index);
}
public void removeAllElements() {
this.childNodes = new Vector<Element>();
}
public void removeFromParent() {
if (this.parent != null) {
this.parent.removeElement(this);
this.parent = null;
}
}
public boolean contains(Element element) {
return this.childNodes.contains(element);
}
public void append(Element element) {
if (this.parent != null && this.parent.contains(element)) {
this.parent.removeElement(element);
}
this.addElement(element);
}
/**
* Insert element to a specific position
*
* @param e element
* @param index index
*/
public void insertElement(Element e, int index) {
e.removeFromParent();
this.childNodes.add(index, e);
e.setParent(this);
}
/**
* @return child nodes
*/
public List<Element> getChildNodes() {
return this.childNodes;
}
public int size() {
return this.childNodes.size();
}
public Vector<String> getElementsTagNames() {
Vector<String> keys = new Vector<String>(this.size());
int i = 0;
while (i < this.size()) {
keys.add(this.getElementByIndex(i++).name);
}
return keys;
}
public Element getElementByIndex(int index) {
return this.childNodes.get(index);
}
private boolean containsAttribute(List<Attribute> attributes, String attributeName) {
int i = 0, size = attributes.size();
while (i < size) {
if (this.attributes.get(i++).name.equalsIgnoreCase(attributeName))
return true;
}
return false;
}
private String getAttribute(List<Attribute> attributes, String attributeName) {
Iterator<Attribute> iterator = attributes.iterator();
Attribute attribute;
while (iterator.hasNext()) {
attribute = iterator.next();
if (attribute.name.equalsIgnoreCase(attributeName)) {
return attribute.value;
}
}
return "";
}
public Element getElementById(String Id) {
int i = 0;
Element currentElement = null;
while (i < this.childNodes.size()) {
if (containsAttribute(this.childNodes.get(i).attributes, "id")) {
if (getAttribute(this.childNodes.get(i).attributes, "id")
.equalsIgnoreCase(Id)) {
currentElement = this.childNodes.get(i);
break;
} else
currentElement = this.childNodes.get(i++).getElementById(Id);
} else
currentElement = this.childNodes.get(i++).getElementById(Id);
}
return currentElement;
}
private List<Element> getChildElementsByTagName(String tagName) {
List<Element> found = new ArrayList<>();
findElementsByTagName(this.childNodes, tagName, found);
return found;
}
private void findElementsByTagName(List<Element> nodes, String tagName, List<Element> found) {
for (Element currentElement : nodes) {
if (currentElement.name.equalsIgnoreCase(tagName)) {
found.add(currentElement);
}
// Recurse into child nodes
findElementsByTagName(currentElement.childNodes, tagName, found);
}
}
public List<Element> getElementsByTagName(String tagName) {
return this.getChildElementsByTagName(tagName);
}
/**
* Set the parent element.
*
* @param parent The element that contains this one
*/
public void setParent(Element parent) {
this.parent = parent;
}
/**
* Gives the element containing the current element
*
* @return parent
*/
public Element getParent() {
return parent;
}
/**
* Set the data for this element
*
* @param data The String representation of the data
* @return this
*/
public Element setData(String data) {
this.data = data;
this.haveData = true;
return this;
}
/**
* Return the data associated with the current Xml element
*
* @return data
*/
public String getData() {
return this.data;
}
/**
* Return the name of the current element
*
* @return name
*/
public String getName() {
return this.name;
}
public int level = 0;
public String getSpace() {
StringBuffer space = new StringBuffer();
int n = this.level;
while (n-- > 0) {
space.append(" ");
}
if (space.length() == 0)
return space + "\r\n";
return "\r\n" + space;
}
@Override
public String toString() {
if (this.elementType == ElementType.TEXT) {
return this.name;
}
boolean valid = this.name != null && !this.name.trim().isEmpty();
if (!valid)
return "Invalid Tag Name";
StringBuffer buffer = new StringBuffer();
StringBuffer nodes = new StringBuffer();
buffer.append(this.getSpace());
buffer.append("<");
buffer.append(this.name);
Attribute currentAttribute;
Iterator<Attribute> iterator = this.attributes.iterator();
while (iterator.hasNext()) {
currentAttribute = iterator.next();
buffer.append(" ").append(currentAttribute.name).append("=\"").append(currentAttribute.value).append("\"");
}
if (this.data != null) {
nodes.append(this.data);
}
if (!this.childNodes.isEmpty()) {
Iterator<Element> childList = this.childNodes.iterator();
while (childList.hasNext()) {
nodes.append(childList.next().toString());
}
nodes.append(this.getSpace());
}
if (this.haveData || nodes.length() > 0) {
buffer.append(">");
buffer.append(nodes);
buffer.append("</");
buffer.append(this.name);
buffer.append(">");
nodes.delete(0, nodes.length());
} else {
buffer.append("/>");
}
String s = buffer.toString();
buffer.delete(0, buffer.length());
return s;
}
public static void printNode(Element node, String indent) {
String data = node.getData();
if ((data == null) || data.isEmpty()) {
System.out.println(indent + node.getName());
} else {
System.out.println(indent + node.getName() + " = '" + data + "'");
}
// print attributes
List<Attribute> attributes = node.getAttributes();
for (Attribute attribute : attributes) {
System.out.println(indent + attribute.name + ":" + attribute.value);
}
List<Element> subs = node.getChildNodes();
for (Element sub : subs) {
printNode(sub, indent + " ");
}
}
/**
* {@inheritDoc}
*/
@Override
public Object clone() {
try {
Element clone = (Element) super.clone(); // creates a shallow
// copy of this object
if (this.attributes != null) {
clone.attributes = new Vector<Attribute>();
List<Attribute> attribs = this.attributes;
Attribute attribute;
for (Attribute attrib : attribs) {
attribute = attrib;
clone.setAttribute((Attribute) attribute.clone());
}
}
if (this.data != null) {
clone.setData(this.data);
}
if (this.childNodes != null) {
clone.childNodes = new Vector<Element>();
List<Element> childs = this.childNodes;
Element child;
for (Element element : childs) {
child = element;
clone.addElement((Element) child.clone());
}
}
return clone;
} catch (CloneNotSupportedException cnse) {
throw new InternalError("Could not clone Element: " + cnse);
}
}
public Attribute setAttribute(Attribute attribute) {
return this.setAttribute(attribute.name, attribute.value);
}
/**
* Sets the name.
*
* @param name The name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* Returns true if the specified objects are equal. They are equal if they
* are both null OR if the <code>equals()</code> method return true. (
* <code>obj1.equals(obj2)</code>).
*
* @param obj1 first object to compare with.
* @param obj2 second object to compare with.
* @return true if they represent the same object; false if one of them is
* null or the <code>equals()</code> method returns false.
*/
private boolean equals(Object obj1, Object obj2) {
boolean equal = false;
if ((obj1 == null) && (obj2 == null)) {
equal = true;
} else if ((obj1 != null) && (obj2 != null)) {
equal = obj1.equals(obj2);
}
return equal;
}
/**
* {@inheritDoc} Recursive comparison.
*/
@Override
public boolean equals(Object obj) {
boolean equal = false;
if ((obj instanceof Element)) {
Element other = (Element) obj;
if (equals(attributes, other.attributes)
&& equals(data, other.data) && equals(name, other.name)
&& equals(childNodes, other.childNodes)) {
equal = true;
}
}
return equal;
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
// Hashcode value should be buffered.
int hashCode = 23;
if (attributes != null) {
hashCode += (attributes.hashCode() * 13);
}
if (data != null) {
hashCode += (data.hashCode() * 17);
}
if (name != null) {
hashCode += (name.hashCode() * 29);
}
if (childNodes != null) {
hashCode += (childNodes.hashCode() * 57);
}
return hashCode;
}
/**
* Convert the element to HTML format
*
* @param docType The HTML document type
* @param voidElements Set of void elements that don't need closing tags
* @return HTML string representation
*/
public String toHtml(Document.DocumentType docType, Set<String> voidElements) {
StringBuilder buffer = new StringBuilder();
String tagName = name.toLowerCase();
// Opening tag
buffer.append('<').append(tagName);
// Attributes
if (!attributes.isEmpty()) {
for (Attribute attr : attributes) {
String attrName = attr.name.toLowerCase();
String attrValue = attr.value;
buffer.append(' ').append(attrName);
if (attrValue != null && !attrValue.isEmpty()) {
buffer.append("=\"").append(escapeHtml(attrValue)).append('"');
}
}
}
// Handle void elements
if (voidElements.contains(tagName)) {
if (docType == Document.DocumentType.XHTML1_STRICT ||
docType == Document.DocumentType.XHTML1_TRANSITIONAL) {
buffer.append(" />");
} else {
buffer.append('>');
}
return buffer.toString();
}
buffer.append('>');
// Content
if (data != null && !data.isEmpty()) {
buffer.append(escapeHtml(data));
}
// Child elements
if (!childNodes.isEmpty()) {
for (Element child : childNodes) {
buffer.append(child.toHtml(docType, voidElements));
}
}
// Closing tag
buffer.append("</").append(tagName).append('>');
return buffer.toString();
}
/**
* Escape special characters for HTML output
*
* @param text The text to escape
* @return Escaped HTML text
*/
private String escapeHtml(String text) {
if (text == null) {
return "";
}
return text.replace("&", "&")
.replace("<", "<")
.replace(">", ">")
.replace("\"", """)
.replace("'", "'");
}
}