1 19 20 33 package org.htmlparser.parserHelper; 34 35 import java.util.Hashtable ; 36 import java.util.StringTokenizer ; 37 38 import org.htmlparser.tags.Tag; 39 40 41 49 public class AttributeParser 50 { 51 private final String delima = " \t\r\n\f=\"'>"; 52 private final String delimb = " \t\r\n\f\"'>"; 53 private final char doubleQuote = '\"'; 54 private final char singleQuote = '\''; 55 private String delim; 56 57 96 public Hashtable parseAttributes(Tag tag) 97 { 98 Hashtable h = new Hashtable (); 99 String element, name, value, nextPart = null; 100 String empty = null; 101 name = null; 102 value = null; 103 element = null; 104 boolean waitingForEqual = false; 105 delim = delima; 106 StringTokenizer tokenizer = 107 new StringTokenizer (tag.getText(), delim, true); 108 while (true) 109 { 110 nextPart = getNextPart(tokenizer, delim); 111 delim = delima; 112 if (element == null && nextPart != null && !nextPart.equals("=")) 113 { 114 element = nextPart; 115 putDataIntoTable(h, element, null, true); 116 } 117 else 118 { 119 if (nextPart != null) 120 { 121 if (name == null) 122 { 123 if (0 < nextPart.length() && !nextPart.substring(0, 1).equals(" ")) 124 { 125 name = nextPart; 126 waitingForEqual = true; 127 } 128 } 129 else 130 { 131 if (waitingForEqual) 132 { 133 if (nextPart.equals("=")) 134 { 135 waitingForEqual = false; 136 delim = delimb; 137 } 138 else 139 { 140 putDataIntoTable(h, name, "", false); 141 name = nextPart; 142 value = null; 143 } 144 } 145 if (!waitingForEqual && !nextPart.equals("=")) 146 { 147 value = nextPart; 148 putDataIntoTable(h, name, value, false); 149 name = null; 150 value = null; 151 } 152 } 153 } 154 else 155 { 156 if (name != null) 157 { 158 if (name.equals("/")) 159 { 160 putDataIntoTable(h, Tag.EMPTYTAG, "", false); 161 } 162 else 163 { 164 putDataIntoTable(h, name, "", false); 165 } 166 name = null; 167 value = null; 168 } 169 break; 170 } 171 } 172 } 173 if (null == element) putDataIntoTable(h, "", null, true); 175 return h; 176 } 177 178 private String getNextPart(StringTokenizer tokenizer, String deli) 179 { 180 String tokenAccumulator = null; 181 boolean isDoubleQuote = false; 182 boolean isSingleQuote = false; 183 boolean isDataReady = false; 184 String currentToken; 185 while (isDataReady == false && tokenizer.hasMoreTokens()) 186 { 187 currentToken = tokenizer.nextToken(deli); 188 if (isDoubleQuote || isSingleQuote) 192 { 193 if (isDoubleQuote && currentToken.charAt(0) == doubleQuote) 194 { 195 isDoubleQuote = false; 196 isDataReady = true; 197 } 198 else if ( 199 isSingleQuote && currentToken.charAt(0) == singleQuote) 200 { 201 isSingleQuote = false; 202 isDataReady = true; 203 } 204 else 205 { 206 tokenAccumulator += currentToken; 207 continue; 208 } 209 } 210 else if (currentToken.charAt(0) == doubleQuote) 211 { 212 isDoubleQuote = true; 213 tokenAccumulator = ""; 214 continue; 215 } 216 else if (currentToken.charAt(0) == singleQuote) 217 { 218 isSingleQuote = true; 219 tokenAccumulator = ""; 220 continue; 221 } 222 else 223 tokenAccumulator = currentToken; 224 225 if (tokenAccumulator.equals(currentToken)) 226 { 227 228 if (delim.indexOf(tokenAccumulator) >= 0) 229 { 230 if (tokenAccumulator.equals("=")) 231 { 232 isDataReady = true; 233 } 234 } 235 else 236 { 237 238 isDataReady = true; 239 } 240 } 241 else 242 isDataReady = true; 243 244 } 245 return tokenAccumulator; 246 } 247 248 private void putDataIntoTable( 249 Hashtable h, 250 String name, 251 String value, 252 boolean isName) 253 { 254 if (isName && value == null) 255 value = Tag.TAGNAME; 256 else if (value == null) 257 value = ""; if (isName) 259 { 260 h.put(value, name.toUpperCase()); 262 } 263 else 264 { 265 h.put(name.toUpperCase(), value); 267 } 268 } 269 } 270 | Popular Tags |