1 | package org.greenstone.gsdl3.gs3build.util;
|
---|
2 |
|
---|
3 | public class HTMLTag
|
---|
4 | { String tagtext;
|
---|
5 | String name;
|
---|
6 | int docpos;
|
---|
7 | int endpos;
|
---|
8 |
|
---|
9 | public HTMLTag(String tagtext, int docpos, int endpos)
|
---|
10 | { this.tagtext = tagtext;
|
---|
11 | this.name = null;
|
---|
12 | this.docpos = docpos;
|
---|
13 | if (this.docpos < 0)
|
---|
14 | { this.endpos = -1;
|
---|
15 | }
|
---|
16 | else
|
---|
17 | { this.endpos = endpos;
|
---|
18 | }
|
---|
19 | }
|
---|
20 |
|
---|
21 | public HTMLTag(String tagtext)
|
---|
22 | { this.tagtext = tagtext;
|
---|
23 | this.docpos = -1;
|
---|
24 | this.name = null;
|
---|
25 | }
|
---|
26 |
|
---|
27 | public HTMLTag endTag()
|
---|
28 | { HTMLTag end;
|
---|
29 | String endtext;
|
---|
30 |
|
---|
31 | endtext = "</"+this.xtagName()+">";
|
---|
32 |
|
---|
33 | end = new HTMLTag(endtext);
|
---|
34 | return end;
|
---|
35 | }
|
---|
36 |
|
---|
37 | public boolean tagIsOpening()
|
---|
38 | { return (this.tagtext.charAt(1) != '/');
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Tells you if a tag is a singleton tag - TODO: complete list
|
---|
43 | */
|
---|
44 | public boolean tagIsSingleton()
|
---|
45 | { String tagName = this.tagName();
|
---|
46 |
|
---|
47 | return (tagName.equals("img"));
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * @return: the name of a tag including any leading '/'
|
---|
52 | */
|
---|
53 | public String tagName()
|
---|
54 | { int offset, start, end;
|
---|
55 | String reply;
|
---|
56 |
|
---|
57 | if (this.name != null)
|
---|
58 | { return this.name;
|
---|
59 | }
|
---|
60 |
|
---|
61 | // NB: a starting '<' and an ending '>' are guaranteed
|
---|
62 | offset = 1; // skip <
|
---|
63 | while (this.tagtext.charAt(offset) <= 32 ||
|
---|
64 | this.tagtext.charAt(offset) == 127)
|
---|
65 | { offset ++;
|
---|
66 | }
|
---|
67 | start = offset;
|
---|
68 | while (this.tagtext.charAt(offset) > 32 &&
|
---|
69 | this.tagtext.charAt(offset) != '>' &&
|
---|
70 | this.tagtext.charAt(offset) != 127)
|
---|
71 | { offset ++;
|
---|
72 | }
|
---|
73 | end = offset;
|
---|
74 |
|
---|
75 | reply = this.tagtext.substring(start, end);
|
---|
76 | reply = reply.toLowerCase();
|
---|
77 |
|
---|
78 | this.name = reply;
|
---|
79 |
|
---|
80 | return reply;
|
---|
81 | }
|
---|
82 |
|
---|
83 | // Returns the name of the tag *minus* any leading / for
|
---|
84 | // closing tags
|
---|
85 | public String xtagName()
|
---|
86 | { String reply;
|
---|
87 |
|
---|
88 | reply = this.tagName();
|
---|
89 | if (reply == null || reply.length() == 0)
|
---|
90 | { return null;
|
---|
91 | }
|
---|
92 |
|
---|
93 | if (reply.charAt(0) == '/')
|
---|
94 | { if (reply.length() > 1)
|
---|
95 | { reply = reply.substring(1, reply.length());
|
---|
96 | }
|
---|
97 | else
|
---|
98 | { reply = "";
|
---|
99 | }
|
---|
100 | }
|
---|
101 | return reply;
|
---|
102 | }
|
---|
103 |
|
---|
104 | public String endTagName()
|
---|
105 | { String reply;
|
---|
106 |
|
---|
107 | reply = "/" + xtagName();
|
---|
108 | return reply;
|
---|
109 | }
|
---|
110 |
|
---|
111 | public boolean isClosing()
|
---|
112 | { String name;
|
---|
113 |
|
---|
114 | name = this.tagName();
|
---|
115 | if (name.charAt(0) == '/')
|
---|
116 | { return true;
|
---|
117 | }
|
---|
118 | return false;
|
---|
119 | }
|
---|
120 |
|
---|
121 | public int tagLevel()
|
---|
122 | { String name;
|
---|
123 | int level;
|
---|
124 |
|
---|
125 | level = 7;
|
---|
126 |
|
---|
127 | name = this.xtagName();
|
---|
128 | if (name.charAt(0) == 'h')
|
---|
129 | { level = Integer.parseInt(name.substring(1, 2));
|
---|
130 | }
|
---|
131 |
|
---|
132 | return level;
|
---|
133 | }
|
---|
134 |
|
---|
135 | public int startPos()
|
---|
136 | { return this.docpos;
|
---|
137 | }
|
---|
138 |
|
---|
139 | public int endPos()
|
---|
140 | { return this.endpos;
|
---|
141 | }
|
---|
142 |
|
---|
143 | static public boolean isTag(String text)
|
---|
144 | { if (text.charAt(0) == '<')
|
---|
145 | { return true;
|
---|
146 | }
|
---|
147 | return false;
|
---|
148 | }
|
---|
149 |
|
---|
150 | public String toString()
|
---|
151 | { return this.tagtext;
|
---|
152 | }
|
---|
153 |
|
---|
154 | //
|
---|
155 | // Returns the value of the identifier given as a string
|
---|
156 | //
|
---|
157 | public String idValue(String idName)
|
---|
158 | { int offset, start, end;
|
---|
159 | int idstart, idend, valuestart, valueend;
|
---|
160 | boolean isvalue, isquoted, isstopped;
|
---|
161 | char quotechar;
|
---|
162 | String reply;
|
---|
163 |
|
---|
164 | // NB: a starting '<' and an ending '>' are guaranteed
|
---|
165 | offset = 1; // skip <
|
---|
166 | while (this.tagtext.charAt(offset) <= 32 ||
|
---|
167 | this.tagtext.charAt(offset) == 127)
|
---|
168 | { offset ++;
|
---|
169 | }
|
---|
170 | start = offset;
|
---|
171 | while (this.tagtext.charAt(offset) > 32 &&
|
---|
172 | this.tagtext.charAt(offset) != '>' &&
|
---|
173 | this.tagtext.charAt(offset) != 127)
|
---|
174 | { offset ++;
|
---|
175 | }
|
---|
176 | end = offset;
|
---|
177 | // End of tag name
|
---|
178 |
|
---|
179 | if (this.tagtext.charAt(offset) == '>')
|
---|
180 | { return null;
|
---|
181 | }
|
---|
182 |
|
---|
183 | isstopped = false;
|
---|
184 | while (!isstopped)
|
---|
185 | { isvalue = false;
|
---|
186 | isquoted = false;
|
---|
187 | valuestart = -1;
|
---|
188 | valueend = -1;
|
---|
189 |
|
---|
190 | while (this.tagtext.charAt(offset) <= 32 ||
|
---|
191 | this.tagtext.charAt(offset) == 127)
|
---|
192 | { offset ++;
|
---|
193 | }
|
---|
194 | idstart = offset;
|
---|
195 |
|
---|
196 | if (this.tagtext.charAt(offset) == '>')
|
---|
197 | { break;
|
---|
198 | }
|
---|
199 |
|
---|
200 | while (this.tagtext.charAt(offset) > 32 &&
|
---|
201 | this.tagtext.charAt(offset) != '=' &&
|
---|
202 | this.tagtext.charAt(offset) != '>' &&
|
---|
203 | this.tagtext.charAt(offset) != 127)
|
---|
204 | { offset ++;
|
---|
205 | }
|
---|
206 | idend = offset;
|
---|
207 |
|
---|
208 | while (this.tagtext.charAt(offset) <= 32 ||
|
---|
209 | this.tagtext.charAt(offset) == 127)
|
---|
210 | { offset ++;
|
---|
211 | }
|
---|
212 |
|
---|
213 | if (this.tagtext.charAt(offset) == '=')
|
---|
214 | { isvalue = true;
|
---|
215 | quotechar = ' ';
|
---|
216 | offset ++;
|
---|
217 |
|
---|
218 | while (this.tagtext.charAt(offset) <= 32 ||
|
---|
219 | this.tagtext.charAt(offset) == 127)
|
---|
220 | { offset ++;
|
---|
221 | }
|
---|
222 | if (this.tagtext.charAt(offset) == '"' ||
|
---|
223 | this.tagtext.charAt(offset) == '\'')
|
---|
224 | { quotechar = this.tagtext.charAt(offset);
|
---|
225 | isquoted = true;
|
---|
226 | offset ++;
|
---|
227 | }
|
---|
228 | valuestart = offset;
|
---|
229 |
|
---|
230 | while (offset < this.tagtext.length() &&
|
---|
231 | (isquoted && this.tagtext.charAt(offset) != quotechar) ||
|
---|
232 | (!isquoted && this.tagtext.charAt(offset) > 32 &&
|
---|
233 | this.tagtext.charAt(offset) != '>'))
|
---|
234 | { offset ++;
|
---|
235 | }
|
---|
236 | valueend = offset;
|
---|
237 | }
|
---|
238 |
|
---|
239 | if (offset >= this.tagtext.length() ||
|
---|
240 | this.tagtext.charAt(offset) == '>')
|
---|
241 | { isstopped = true;
|
---|
242 | }
|
---|
243 | offset ++;
|
---|
244 |
|
---|
245 | reply = this.tagtext.substring(idstart, idend);
|
---|
246 | reply = reply.toLowerCase();
|
---|
247 |
|
---|
248 | if (reply.equalsIgnoreCase(idName))
|
---|
249 | { if (isvalue)
|
---|
250 | { return this.tagtext.substring(valuestart, valueend);
|
---|
251 | }
|
---|
252 | else
|
---|
253 | { return reply;
|
---|
254 | }
|
---|
255 | }
|
---|
256 | }
|
---|
257 | return null;
|
---|
258 | }
|
---|
259 | }
|
---|