1 37 38 package org.htmlcleaner; 39 40 import java.util.*; 41 42 96 public class TagInfo { 97 98 static final int HEAD_AND_BODY = 0; 99 static final int HEAD = 1; 100 static final int BODY = 2; 101 102 static String CONTENT_ALL = "ALL"; 103 static String CONTENT_NONE = "NONE"; 104 static String CONTENT_TEXT = "TEXT"; 105 106 private String name; 107 private String contentType; 108 private Set mustCloseTags = new HashSet(); 109 private Set higherTags = new HashSet(); 110 private Set childTags = new HashSet(); 111 private Set permittedTags = new HashSet(); 112 private Set copyTags = new HashSet(); 113 private int belongsTo = BODY; 114 private String requiredParent = null; 115 private String fatalTag = null; 116 private boolean deprecated = false; 117 private boolean unique = false; 118 private boolean ignorePermitted = false; 119 120 public TagInfo( String name, String contentType, int belongsTo, boolean depricated, 121 boolean unique, boolean ignorePermitted, String dependancies ) { 122 this.name = name; 123 this.contentType = contentType; 124 this.belongsTo = belongsTo; 125 this.deprecated = depricated; 126 this.unique = unique; 127 this.ignorePermitted = ignorePermitted; 128 129 if (dependancies != null) { 131 StringTokenizer tokenizer = new StringTokenizer(dependancies, ",.;| "); 132 while (tokenizer.hasMoreTokens()) { 133 String currTag = tokenizer.nextToken().toLowerCase(); 134 addDependancy(currTag); 135 } 136 } 137 } 138 139 public void addDependancy(String dependantTagName) { 140 if (dependantTagName.startsWith("!")) { 141 String tagName = dependantTagName.substring(1); 142 this.fatalTag = tagName; 143 this.higherTags.add(tagName); 144 } else if (dependantTagName.startsWith("+")) { 145 String tagName = dependantTagName.substring(1); 146 this.requiredParent = dependantTagName.substring(1); 147 this.higherTags.add(tagName); 148 } else if (dependantTagName.startsWith("-")) { 149 this.permittedTags.add( dependantTagName.substring(1) ); 150 } else if (dependantTagName.startsWith("#")) { 151 this.childTags.add( dependantTagName.substring(1) ); 152 } else if (dependantTagName.startsWith("^")) { 153 this.higherTags.add( dependantTagName.substring(1) ); 154 } else if (dependantTagName.startsWith("&")) { 155 String tagName = dependantTagName.substring(1); 156 this.copyTags.add(tagName); 157 this.mustCloseTags.add(tagName); 158 } else if ( !"".equals(dependantTagName.trim()) ) { 159 this.mustCloseTags.add(dependantTagName); 160 } 161 } 162 163 165 public String getName() { 166 return name; 167 } 168 169 public void setName(String name) { 170 this.name = name; 171 } 172 173 public String getContentType() { 174 return contentType; 175 } 176 177 public void setContentType(String contentType) { 178 this.contentType = contentType; 179 } 180 181 public Set getMustCloseTags() { 182 return mustCloseTags; 183 } 184 185 public void setMustCloseTags(Set mustCloseTags) { 186 this.mustCloseTags = mustCloseTags; 187 } 188 189 public Set getHigherTags() { 190 return higherTags; 191 } 192 193 public void setHigherTags(Set higherTags) { 194 this.higherTags = higherTags; 195 } 196 197 public Set getChildTags() { 198 return childTags; 199 } 200 201 public void setChildTags(Set childTags) { 202 this.childTags = childTags; 203 } 204 205 public Set getPermittedTags() { 206 return permittedTags; 207 } 208 209 public void setPermittedTags(Set permittedTags) { 210 this.permittedTags = permittedTags; 211 } 212 213 public Set getCopyTags() { 214 return copyTags; 215 } 216 217 public void setCopyTags(Set copyTags) { 218 this.copyTags = copyTags; 219 } 220 221 public String getRequiredParent() { 222 return requiredParent; 223 } 224 225 public void setRequiredParent(String requiredParent) { 226 this.requiredParent = requiredParent; 227 } 228 229 public int getBelongsTo() { 230 return belongsTo; 231 } 232 233 public void setBelongsTo(int belongsTo) { 234 this.belongsTo = belongsTo; 235 } 236 237 public String getFatalTag() { 238 return fatalTag; 239 } 240 241 public void setFatalTag(String fatalTag) { 242 this.fatalTag = fatalTag; 243 } 244 245 public boolean isDeprecated() { 246 return deprecated; 247 } 248 249 public void setDeprecated(boolean deprecated) { 250 this.deprecated = deprecated; 251 } 252 253 public boolean isUnique() { 254 return unique; 255 } 256 257 public void setUnique(boolean unique) { 258 this.unique = unique; 259 } 260 261 public boolean isIgnorePermitted() { 262 return ignorePermitted; 263 } 264 265 public void setIgnorePermitted(boolean ignorePermitted) { 266 this.ignorePermitted = ignorePermitted; 267 } 268 269 271 boolean allowsBody() { 272 return !CONTENT_NONE.equals(contentType); 273 } 274 275 boolean isHigher(String tagName) { 276 return higherTags.contains(tagName); 277 } 278 279 boolean isCopy(String tagName) { 280 return copyTags.contains(tagName); 281 } 282 283 boolean hasCopyTags() { 284 return !copyTags.isEmpty(); 285 } 286 287 boolean hasPermittedTags() { 288 return !permittedTags.isEmpty(); 289 } 290 291 boolean isHeadTag() { 292 return belongsTo == HEAD; 293 } 294 295 boolean isHeadAndBodyTag() { 296 return belongsTo == HEAD || belongsTo == HEAD_AND_BODY; 297 } 298 299 boolean isMustCloseTag(TagInfo tagInfo) { 300 if (tagInfo != null) { 301 return mustCloseTags.contains( tagInfo.getName() ) || tagInfo.contentType == CONTENT_TEXT; 302 } 303 304 return false; 305 } 306 307 boolean allowsItem(BaseToken token) { 308 if ( contentType != CONTENT_NONE && token instanceof TagToken ) { 309 TagToken tagToken = (TagToken) token; 310 String tagName = tagToken.getName(); 311 if ( "script".equals(tagName) ) { 312 return true; 313 } 314 } 315 316 if (contentType == CONTENT_ALL) { 317 if ( !childTags.isEmpty() ) { 318 return token instanceof TagToken ? childTags.contains( ((TagToken)token).getName() ) : false; 319 } else if ( !permittedTags.isEmpty() ) { 320 return token instanceof TagToken ? !permittedTags.contains( ((TagToken)token).getName() ) : true; 321 } else { 322 return true; 323 } 324 } else if (contentType == CONTENT_TEXT) { 325 return !(token instanceof TagToken); 326 } 327 328 return false; 329 } 330 331 boolean allowsAnything() { 332 return contentType == CONTENT_ALL && childTags.size() == 0; 333 } 334 335 } | Popular Tags |