Softwaredesign: Beispiel: XML-Strukturbaum als Kompositum: Java Implementierung |
1abstract
2public
3class XMLTree {
4 abstract
5 public
6 String toString();
7}
|
1public
2class PlainText extends XMLTree {
3 protected
4 String text;
5
6 public
7 String toString() {
8 return
9 text;
10 }
11}
|
1public
2class SimpleElem extends XMLTree {
3
4 // auxiliary class for attributes
5 static class Map {
6 public
7 String toString() {
8 String res = "";
9
10 // foreach attr in map do ...
11
12 return res;
13 }
14 }
15
16
17 String elemName;
18
19 Map attributes;
20
21 public
22 String toString() {
23 return
24 "<" + elemName + attributes.toString() + ">";
25 }
26}
|
1public
2class CompoundElem extends XMLTree {
3
4 protected
5 SimpleElem elem;
6
7 protected
8 XMLTree [] body;
9
10 public
11 String toString() {
12 String res = "";
13 for ( int i = 0;
14 i < body.length;
15 ++i ) {
16 res += body[i].toString();
17 }
18
19 return
20 elem.toString() +
21 res +
22 "</" + elem.tag + ">";
23 }
24}
|
1public
2class XMLComment extends XMLTree {
3 protected
4 String comment;
5
6 public
7 String toString() {
8 return
9 "<!--" + comment + "-->";
10 }
11}
|
Letzte Änderung: 13.04.2012 | © Prof. Dr. Uwe Schmidt |