source: trunk/greenstone3-extensions/gs3build/src/org/greenstone/gsdl3/gs3build/util/HTMLTag.java@ 12188

Last change on this file since 12188 was 12188, checked in by kjdon, 18 years ago

Initial revision

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