1 | package org.greenstone.gsdl3.gs3build.util;
|
---|
2 |
|
---|
3 | public class HTMLContext
|
---|
4 | { boolean valid;
|
---|
5 | StringBuffer text;
|
---|
6 |
|
---|
7 | public static HTMLTagVector breakTags = new HTMLTagVector("a,h1,h2,h3,h4,h5,h6,table,hr,p,ul,ol,dl,dt,dd,li,tr,td,th,map");
|
---|
8 |
|
---|
9 | private int offset (String string, int direction)
|
---|
10 | { int offset;
|
---|
11 |
|
---|
12 | if (direction > 0)
|
---|
13 | { offset = string.indexOf('.');
|
---|
14 | }
|
---|
15 | else
|
---|
16 | { offset = string.lastIndexOf('.');
|
---|
17 | }
|
---|
18 | return offset;
|
---|
19 | }
|
---|
20 |
|
---|
21 | public HTMLContext(HTMLBlock parent, HTMLBlock child, int scope)
|
---|
22 | { int pos;
|
---|
23 | int start, startoffset;
|
---|
24 | int end, endoffset;
|
---|
25 | int length;
|
---|
26 | String string;
|
---|
27 | HTMLTag tag;
|
---|
28 |
|
---|
29 | this.text = null;
|
---|
30 |
|
---|
31 | // if we can't match it, give up!
|
---|
32 | pos = parent.find(child);
|
---|
33 | if (pos < 0)
|
---|
34 | { this.valid = false;
|
---|
35 | return;
|
---|
36 | }
|
---|
37 |
|
---|
38 | // get start!
|
---|
39 | if (pos > 0)
|
---|
40 | { start = pos - 1;
|
---|
41 | }
|
---|
42 | else
|
---|
43 | { start = 0;
|
---|
44 | }
|
---|
45 | startoffset = -1;
|
---|
46 | length = 0;
|
---|
47 | while ( start > 0)
|
---|
48 | {
|
---|
49 | /*
|
---|
50 | if (parent.elementAt(start) instanceof String)
|
---|
51 | { string = (String) parent.elementAt(start);
|
---|
52 |
|
---|
53 | startoffset = offset(string, -1);
|
---|
54 | if (startoffset > 0)
|
---|
55 | { break;
|
---|
56 | }
|
---|
57 |
|
---|
58 | length += string.length();
|
---|
59 |
|
---|
60 | if (length >= scope)
|
---|
61 | { break;
|
---|
62 | }
|
---|
63 | }
|
---|
64 | else
|
---|
65 | */
|
---|
66 | if (parent.elementAt(start) instanceof HTMLCText)
|
---|
67 | { string = ((HTMLCText) parent.elementAt(start)).toString();
|
---|
68 |
|
---|
69 | startoffset = offset(string, -1);
|
---|
70 | if (startoffset > 0)
|
---|
71 | { break;
|
---|
72 | }
|
---|
73 |
|
---|
74 | length += string.length();
|
---|
75 |
|
---|
76 | if (length >= scope)
|
---|
77 | { break;
|
---|
78 | }
|
---|
79 | }
|
---|
80 | else if (parent.elementAt(start) instanceof HTMLTag)
|
---|
81 | { // do some checks
|
---|
82 | tag = (HTMLTag) parent.elementAt(start);
|
---|
83 |
|
---|
84 | if (breakTags.matches(tag, false))
|
---|
85 | { startoffset = -1;
|
---|
86 | break;
|
---|
87 | }
|
---|
88 | }
|
---|
89 | start --;
|
---|
90 | }
|
---|
91 |
|
---|
92 | // get end!
|
---|
93 | pos = pos + child.size();
|
---|
94 | if (pos < parent.size())
|
---|
95 | { end = pos + 1;
|
---|
96 | }
|
---|
97 | else
|
---|
98 | { end = parent.size();
|
---|
99 | }
|
---|
100 | endoffset = -1;
|
---|
101 | length = 0;
|
---|
102 | string = null;
|
---|
103 | while (end < parent.size())
|
---|
104 | { if (parent.content.elementAt(start) instanceof String)
|
---|
105 | { string = (String) parent.content.elementAt(start);
|
---|
106 |
|
---|
107 | endoffset = offset(string, +1);
|
---|
108 | if (endoffset >= 0)
|
---|
109 | { break;
|
---|
110 | }
|
---|
111 |
|
---|
112 | length += string.length();
|
---|
113 |
|
---|
114 | if (length >= scope)
|
---|
115 | { break;
|
---|
116 | }
|
---|
117 | }
|
---|
118 | else if (parent.content.elementAt(start) instanceof HTMLCText)
|
---|
119 | { string = ((HTMLCText) parent.content.elementAt(start)).toString();
|
---|
120 |
|
---|
121 | endoffset = offset(string, +1);
|
---|
122 | if (endoffset >= 0)
|
---|
123 | { break;
|
---|
124 | }
|
---|
125 |
|
---|
126 | length += string.length();
|
---|
127 |
|
---|
128 | if (length >= scope)
|
---|
129 | { break;
|
---|
130 | }
|
---|
131 | }
|
---|
132 | else if (parent.content.elementAt(start) instanceof HTMLTag)
|
---|
133 | { tag = (HTMLTag) parent.elementAt(start);
|
---|
134 |
|
---|
135 | if (breakTags.matches(tag, false))
|
---|
136 | { endoffset = -1;
|
---|
137 | end = end - 1;
|
---|
138 | break;
|
---|
139 | }
|
---|
140 | }
|
---|
141 | System.out.println("String:"+string);
|
---|
142 | end ++;
|
---|
143 | }
|
---|
144 |
|
---|
145 | this.text = new StringBuffer();
|
---|
146 | if (startoffset >= 0)
|
---|
147 | { string = null;
|
---|
148 |
|
---|
149 | if (parent.content.elementAt(start) instanceof String)
|
---|
150 | { string = (String) parent.content.elementAt(start);
|
---|
151 | }
|
---|
152 | else if (parent.content.elementAt(start) instanceof HTMLCText)
|
---|
153 | { string = ((HTMLCText) parent.content.elementAt(start)).toString();
|
---|
154 | }
|
---|
155 |
|
---|
156 | if (string != null)
|
---|
157 | { this.text.append(string.substring(startoffset+1));
|
---|
158 | }
|
---|
159 | start ++;
|
---|
160 | }
|
---|
161 |
|
---|
162 | for (pos = start; pos <= end; pos ++)
|
---|
163 | { if (pos == end && endoffset >= 0)
|
---|
164 | { break;
|
---|
165 | }
|
---|
166 |
|
---|
167 | if (parent.content.elementAt(pos) instanceof String)
|
---|
168 | { this.text.append((String) parent.content.elementAt(pos));
|
---|
169 | }
|
---|
170 | else if (parent.content.elementAt(pos) instanceof HTMLCText)
|
---|
171 | { this.text.append(((HTMLCText) parent.content.elementAt(pos)).toString());
|
---|
172 | }
|
---|
173 | else if (parent.content.elementAt(pos) instanceof HTMLTag)
|
---|
174 | { // don't add it!
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | if (pos == end && endoffset > 0) // this deliberately ignore endoffset == 0
|
---|
179 | // 'cos nothing should happen anyhow!
|
---|
180 | { string = null;
|
---|
181 |
|
---|
182 | if (parent.content.elementAt(pos) instanceof String)
|
---|
183 | { string = (String) parent.content.elementAt(pos);
|
---|
184 | }
|
---|
185 | else if (parent.content.elementAt(pos) instanceof HTMLCText)
|
---|
186 | { string = ((HTMLCText) parent.content.elementAt(pos)).toString();
|
---|
187 | }
|
---|
188 |
|
---|
189 | if (string != null)
|
---|
190 | { this.text.append(string.substring(0, endoffset));
|
---|
191 | }
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | public String toString()
|
---|
196 | { if (this.text == null)
|
---|
197 | { return "";
|
---|
198 | }
|
---|
199 | return this.text.toString();
|
---|
200 | }
|
---|
201 | }
|
---|