1 19 20 33 package org.htmlparser.tags; 34 35 import org.htmlparser.Node; 36 import org.htmlparser.tags.data.TagData; 37 import org.htmlparser.visitors.NodeVisitor; 38 41 public class EndTag extends Tag 42 { 43 public final static String TYPE = "END_TAG"; 44 public final static int ENDTAG_BEFORE_PARSING_STATE = 0; 45 public final static int ENDTAG_WAIT_FOR_SLASH_STATE = 1; 46 public final static int ENDTAG_BEGIN_PARSING_STATE = 2; 47 public final static int ENDTAG_FINISHED_PARSING_STATE = 3; 48 49 55 public EndTag(TagData tagData) 56 { 57 super(tagData); 58 } 59 64 public static Node find(String input, int position) 65 { 66 int state = ENDTAG_BEFORE_PARSING_STATE; 67 StringBuffer tagContents = new StringBuffer (); 68 int tagBegin = 0; 69 int tagEnd = 0; 70 int inputLen = input.length(); 71 char ch; 72 int i; 73 for (i = position; 74 (i < inputLen && state != ENDTAG_FINISHED_PARSING_STATE); 75 i++) 76 { 77 ch = input.charAt(i); 78 if (ch == '>' && state == ENDTAG_BEGIN_PARSING_STATE) 79 { 80 state = ENDTAG_FINISHED_PARSING_STATE; 81 tagEnd = i; 82 } 83 if (state == ENDTAG_BEGIN_PARSING_STATE) 84 { 85 tagContents.append(ch); 86 } 87 if (state == ENDTAG_WAIT_FOR_SLASH_STATE) 88 { 89 if (ch == '/') 90 { 91 state = ENDTAG_BEGIN_PARSING_STATE; 92 } 93 else 94 return null; 95 } 96 97 if (ch == '<') 98 { 99 if (state == ENDTAG_BEFORE_PARSING_STATE) 100 { 101 tagBegin = i; 103 state = ENDTAG_WAIT_FOR_SLASH_STATE; 104 } 105 else if (state == ENDTAG_BEGIN_PARSING_STATE) 106 { 107 state = ENDTAG_FINISHED_PARSING_STATE; 108 tagEnd = i; 109 } 110 } 111 else if (state == ENDTAG_BEFORE_PARSING_STATE) 112 return (null); 114 } 115 if (state == ENDTAG_BEGIN_PARSING_STATE) 117 { 118 tagEnd = i; 119 state = ENDTAG_FINISHED_PARSING_STATE; 120 } 121 if (state == ENDTAG_FINISHED_PARSING_STATE) 122 return new EndTag( 123 new TagData(tagBegin, tagEnd, tagContents.toString(), input)); 124 else 125 return null; 126 } 127 public String toPlainTextString() 128 { 129 return ""; 130 } 131 public String toHtml() 132 { 133 return "</" + getTagName() + ">"; 134 } 135 public String toString() 136 { 137 return "EndTag : " 138 + tagContents 139 + "; begins at : " 140 + elementBegin() 141 + "; ends at : " 142 + elementEnd(); 143 } 144 145 public void accept(NodeVisitor visitor) 146 { 147 visitor.visitEndTag(this); 148 } 149 150 public String getType() 151 { 152 return TYPE; 153 } 154 155 } 156 | Popular Tags |