1 21 package au.id.jericho.lib.html; 22 23 import java.util.*; 24 25 61 public final class EndTag extends Tag { 62 private final EndTagType endTagType; 63 64 73 EndTag(final Source source, final int begin, final int end, final EndTagType endTagType, final String name) { 74 super(source,begin,end,name); 75 this.endTagType=endTagType; 76 } 77 78 94 public Element getElement() { 95 if (element!=Element.NOT_CACHED) return element; 96 int pos=begin; 97 while (pos!=0) { 98 StartTag startTag=source.findPreviousStartTag(pos-1); 99 if (startTag==null) break; 100 Element foundElement=startTag.getElement(); if (foundElement.getEndTag()==this) return foundElement; pos=startTag.begin; 103 } 104 return element=null; 105 } 106 107 114 public EndTagType getEndTagType() { 115 return endTagType; 116 } 117 118 public TagType getTagType() { 120 return endTagType; 121 } 122 123 public boolean isUnregistered() { 125 return endTagType==EndTagType.UNREGISTERED; 126 } 127 128 136 public String tidy() { 137 return toString(); 138 } 139 140 156 public static String generateHTML(final String tagName) { 157 return EndTagType.NORMAL.generateHTML(tagName); 158 } 159 160 public String getDebugInfo() { 161 final StringBuffer sb=new StringBuffer (); 162 sb.append("\"/").append(name).append("\" "); 163 if (endTagType!=EndTagType.NORMAL) sb.append('(').append(endTagType.getDescription()).append(") "); 164 sb.append(super.getDebugInfo()); 165 return sb.toString(); 166 } 167 168 176 public String regenerateHTML() { 177 return toString(); 178 } 179 180 188 public static boolean isForbidden(String name) { 189 return HTMLElements.getEndTagForbiddenElementNames().contains(name.toLowerCase()); 190 } 191 192 200 public static boolean isOptional(String name) { 201 return HTMLElements.getEndTagOptionalElementNames().contains(name.toLowerCase()); 202 } 203 204 212 public static boolean isRequired(String name) { 213 return HTMLElements.getEndTagRequiredElementNames().contains(name.toLowerCase()); 214 } 215 216 229 static EndTag findPreviousOrNext(final Source source, final int pos, final String searchName, final EndTagType endTagType, final boolean previous) { 230 if (searchName==null) return (EndTag)Tag.findPreviousOrNextTag(source,pos,endTagType,previous); 231 if (searchName.length()==0) throw new IllegalArgumentException ("searchName argument must not be zero length"); 232 final String searchString=endTagType.generateHTML(searchName); 233 try { 234 final ParseText parseText=source.getParseText(); 235 int begin=pos; 236 do { 237 begin=previous?parseText.lastIndexOf(searchString,begin):parseText.indexOf(searchString,begin); 238 if (begin==-1) return null; 239 final EndTag endTag=(EndTag)source.getTagAt(begin); 240 if (endTag!=null && endTag.getEndTagType()==endTagType) return endTag; 241 } while (previous ? (begin-=1)>=0 : (begin+=1)<source.end); 242 } catch (IndexOutOfBoundsException ex) { 243 } 246 return null; 247 } 248 249 static EndTag findPreviousOrNext(final Source source, int pos, final boolean previous) { 250 while (true) { 251 final Tag tag=Tag.findPreviousOrNextTag(source,pos,previous); 252 if (tag==null) return null; 253 if (tag instanceof EndTag) return (EndTag)tag; 254 pos+=previous?-1:1; 255 } 256 } 257 } 258 259 | Popular Tags |