1 2 package org.opencms.util; 3 4 import java.io.Serializable ; 5 6 import org.htmlparser.Node; 7 import org.htmlparser.Tag; 8 import org.htmlparser.Text; 9 import org.htmlparser.tags.LinkTag; 10 import org.htmlparser.util.Translate; 11 import org.htmlparser.visitors.NodeVisitor; 12 13 16 public class StringBean extends NodeVisitor implements Serializable { 17 18 21 private static final String NEWLINE = System.getProperty("line.separator"); 22 23 26 private static final int NEWLINE_SIZE = NEWLINE.length(); 27 28 private static final long serialVersionUID = 1596190888769126925L; 29 30 33 protected StringBuffer m_buffer; 34 35 39 protected boolean m_collapse; 40 41 44 protected boolean m_isPre; 45 46 49 protected boolean m_isScript; 50 51 54 protected boolean m_isStyle; 55 56 59 protected boolean m_links; 60 61 64 protected String m_strings; 65 66 78 public StringBean() { 79 80 super(true, true); 81 m_strings = null; 82 m_links = false; 83 m_collapse = true; 84 m_buffer = new StringBuffer (4096); 85 m_isScript = false; 86 m_isPre = false; 87 m_isStyle = false; 88 } 89 90 103 public boolean getCollapse() { 104 105 return (m_collapse); 106 } 107 108 113 public boolean getLinks() { 114 115 return (m_links); 116 } 117 118 123 public String getStrings() { 124 125 if (null == m_strings) { 126 if (0 == m_buffer.length()) { 127 setStrings(); 128 } else { 129 updateStrings(m_buffer.toString()); 130 } 131 } 132 133 return (m_strings); 134 } 135 136 143 public void setCollapse(boolean collapse) { 144 145 boolean oldValue = m_collapse; 146 if (oldValue != collapse) { 147 m_collapse = collapse; 148 setStrings(); 149 } 150 } 151 152 159 public void setLinks(boolean links) { 160 161 boolean oldValue = m_links; 162 if (oldValue != links) { 163 m_links = links; 164 setStrings(); 165 } 166 } 167 168 172 public void visitEndTag(Tag tag) { 173 174 Node parent = tag.getParent(); 175 if (parent instanceof LinkTag) { 176 if (getLinks()) { m_buffer.append(" <"); 178 m_buffer.append(((LinkTag)parent).getLink()); 179 m_buffer.append(">"); 180 } 181 } 182 183 String name = tag.getTagName().toUpperCase(); 184 if (name.equals("PRE")) { 185 m_isPre = false; 186 } else if (name.equals("SCRIPT")) { 187 m_isScript = false; 188 } else if (name.equals("STYLE")) { 189 m_isStyle = false; 190 } 191 192 if (isHeadTag(name)) { 193 carriageReturn(); 194 carriageReturn(true); 195 } 196 197 if (isTitleTag(name)) { 198 m_buffer.append(" ]"); 199 carriageReturn(); 200 carriageReturn(true); 201 } 202 } 203 204 private boolean isTitleTag(String name) { 205 206 return "TITLE".equals(name); 207 } 208 209 private boolean isHeadTag(String name) { 210 211 return "H1".equals(name) 212 || "H2".equals(name) 213 || "H3".equals(name) 214 || "H4".equals(name) 215 || "H5".equals(name) 216 || "H6".equals(name); 217 } 218 219 223 public void visitStringNode(Text string) { 224 225 if (!m_isScript && !m_isStyle) { 226 String text = string.getText(); 227 if (!m_isPre) { 228 text = Translate.decode(text); 229 text = text.replace('\u00a0', ' '); 230 if (getCollapse()) { 231 collapse(m_buffer, text); 232 } else { 233 m_buffer.append(text); 234 } 235 } else { 236 m_buffer.append(text); 237 } 238 } 239 } 240 241 246 public void visitTag(Tag tag) { 247 248 String name = tag.getTagName(); 249 if (name.equalsIgnoreCase("PRE")) { 250 m_isPre = true; 251 } else if (name.equalsIgnoreCase("SCRIPT")) { 252 m_isScript = true; 253 } else if (name.equalsIgnoreCase("STYLE")) { 254 m_isStyle = true; 255 } 256 257 if (isHeadTag(name)) { 258 carriageReturn(true); 259 m_buffer.append("* "); 260 } else if (isTitleTag(name)) { 261 m_buffer.append("[ "); 262 } else { 263 if (tag.breaksFlow()) { 264 carriageReturn(); 265 } 266 } 267 268 } 269 270 274 protected void carriageReturn() { 275 276 carriageReturn(false); 277 } 278 279 285 protected void carriageReturn(boolean check) { 286 287 int length; 288 289 length = m_buffer.length(); 290 if ((0 != length) && (check || ((NEWLINE_SIZE <= length) && (!m_buffer.substring(length - NEWLINE_SIZE, length).equals(NEWLINE))))) { 293 294 m_buffer.append(NEWLINE); 295 } 296 } 297 298 318 protected void collapse(StringBuffer buffer, String string) { 319 320 int chars; 321 int length; 322 int state; 323 char character; 324 325 chars = string.length(); 326 if (0 != chars) { 327 length = buffer.length(); 328 state = ((0 == length) || (buffer.charAt(length - 1) == ' ') || ((NEWLINE_SIZE <= length) && buffer.substring( 329 length - NEWLINE_SIZE, 330 length).equals(NEWLINE))) ? 0 : 1; 331 for (int i = 0; i < chars; i++) { 332 character = string.charAt(i); 333 switch (character) { 334 case '\u0020': 337 case '\u0009': 338 case '\u000C': 339 case '\u200B': 340 case '\r': 341 case '\n': 342 if (0 != state) { 343 state = 1; 344 } 345 break; 346 default: 347 if (1 == state) { 348 buffer.append(' '); 349 } 350 state = 2; 351 buffer.append(character); 352 } 353 } 354 } 355 } 356 357 361 protected void setStrings() { 362 363 m_strings = null; 364 m_buffer = new StringBuffer (4096); 365 } 366 367 371 protected void updateStrings(String strings) { 372 373 if ((null == m_strings) || !m_strings.equals(strings)) { 374 m_strings = strings; 375 } 376 } 377 } 378 | Popular Tags |