1
2
3 package org.catacomb.serial.xml;
4
5 import org.catacomb.interlish.structure.Attribute;
6
7
8 public class XMLToken {
9
10 final static int NONE = 0;
11 final static int OPEN = 1;
12 final static int CLOSE = 2;
13
14 final static int NUMBER = 3;
15 final static int STRING = 4;
16
17 final static int OPENCLOSE = 5;
18
19 final static int INTRO = 6;
20 final static int COMMENT = 7;
21
22
23 String[] types = {"NONE", "OPEN", "CLOSE", "NUMBER", "STRING",
24 "OPENCLOSE", "INTRO", "COMMENT"
25 };
26
27
28 int type;
29
30 String svalue;
31 double dvalue;
32
33 int natt;
34 String[] attNV;
35
36
37
38 public XMLToken() {
39 type = NONE;
40 }
41
42
43
44
45
46
47
48
49
50
51
52
53
54 public boolean isOpen() {
55 return (type == OPEN || type == OPENCLOSE);
56 }
57
58 public boolean isClose() {
59 return (type == OPENCLOSE || type == CLOSE);
60 }
61
62 public boolean isNumber() {
63 return (type == NUMBER);
64 }
65
66 public boolean isString() {
67 return (type == STRING);
68 }
69
70 public boolean isNone() {
71 return (type == NONE);
72 }
73
74 public boolean isIntro() {
75 return (type == INTRO);
76 }
77
78 public boolean isComment() {
79 return (type == COMMENT);
80 }
81
82
83
84 public String toString() {
85 String s = types[type] + " " ;
86 if (type == OPEN ||
87 type == STRING ||
88 type == INTRO ||
89 type == COMMENT ||
90 type == CLOSE ||
91 type == OPENCLOSE) {
92 s += svalue;
93
94 if (type == OPEN || type==OPENCLOSE) {
95 if (natt > 0) {
96 for (int i = 0; i < natt; i++) {
97 s += "\n " + attNV[2*i] + "=" + attNV[2*i+1];
98 }
99 }
100 }
101 } else if (type == NUMBER) {
102 s += " " + dvalue;
103 }
104 return s;
105 }
106
107 public void setType(int itype) {
108 type = itype;
109 }
110
111
112 public void setStringValue(String s) {
113 svalue = s;
114 }
115
116
117 public void setDValue(double d) {
118 dvalue = d;
119 }
120
121
122 public void setAttributes(String[] sa) {
123 attNV = sa;
124 natt = sa.length / 2;
125 }
126
127
128
129 public boolean hasAttribute(String sat) {
130 boolean bret = false;
131 for (int i = 0; i < natt; i++) {
132 if (attNV[2*i].equals(sat)) bret = true;
133 }
134 return bret;
135 }
136
137 public Attribute[] getAttributes() {
138 NamedString[] nvpa = new NamedString[natt];
139 for (int i = 0; i < natt; i++) {
140 nvpa[i] = new NamedString(attNV[2*i], attNV[2*i+1]);
141 }
142 return nvpa;
143 }
144
145
146
147 public String getAttribute(String sat) {
148 String sret = null;
149 for (int i = 0; i < natt; i++) {
150 if (attNV[2*i].equals(sat)) sret = attNV[2*i+1];
151 }
152 return sret;
153 }
154
155
156 public String getName() {
157 return svalue;
158 }
159
160 public String getOpenTagString() {
161 return ("<" + svalue + ">");
162 }
163
164 public String getCloseTagString() {
165 return ("</" + svalue + ">");
166 }
167
168
169
170 public boolean closes(XMLToken start) {
171 return (svalue.equals(start.getName()) && isClose());
172 }
173
174
175 public int getNumAttributes() {
176 return natt;
177 }
178
179
180 public String getAttributeName(int i) {
181 return attNV[2*i];
182 }
183
184 public String getAttributeValue(int i) {
185 return attNV[2*i+1];
186 }
187
188 }