1 27 package org.htmlparser.tags; 28 29 import java.util.Locale ; 30 import java.util.Vector ; 31 32 import org.htmlparser.Attribute; 33 import org.htmlparser.nodes.TagNode; 34 import org.htmlparser.util.ParserUtils; 35 36 39 public class ImageTag 40 extends 41 TagNode 42 { 43 46 private static final String [] mIds = new String [] {"IMG"}; 47 48 53 protected String imageURL; 54 55 58 public ImageTag () 59 { 60 imageURL = null; 61 } 62 63 67 public String [] getIds () 68 { 69 return (mIds); 70 } 71 72 84 public String extractImageLocn () 85 { 86 Vector attributes; 87 int size; 88 Attribute attribute; 89 String string; 90 String data; 91 int state; 92 String name; 93 String ret; 94 95 97 ret = ""; 98 state = 0; 99 attributes = getAttributesEx (); 100 size = attributes.size (); 101 for (int i = 0; (i < size) && (state < 3); i++) 102 { 103 attribute = (Attribute)attributes.elementAt (i); 104 string = attribute.getName (); 105 data = attribute.getValue (); 106 switch (state) 107 { 108 case 0: if (null != string) 110 { 111 name = string.toUpperCase (Locale.ENGLISH); 112 if (name.equals ("SRC")) 113 { 114 state = 1; 115 if (null != data) 116 { 117 if ("".equals (data)) 118 state = 2; else 120 { 121 ret = data; 122 i = size; } 124 } 125 126 } 127 else if (name.startsWith ("SRC")) 128 { 129 string = string.substring (3); 131 if (string.startsWith ("\"") && string.endsWith ("\"") && (1 < string.length ())) 133 string = string.substring (1, string.length () - 1); 134 if (string.startsWith ("'") && string.endsWith ("'") && (1 < string.length ())) 136 string = string.substring (1, string.length () - 1); 137 ret = string; 138 state = 0; } 142 } 143 break; 144 case 1: if (null != string) 146 { 147 if (string.startsWith ("=")) 148 { 149 state = 2; 150 if (1 < string.length ()) 151 { 152 ret = string.substring (1); 153 state = 0; } 155 else if (null != data) 156 { 157 ret = string.substring (1); 158 state = 0; } 160 } 161 } 162 break; 163 case 2: if (null != string) 165 { 166 if (null == data) 167 ret = string; 168 state = 0; } 171 break; 172 default: 173 throw new IllegalStateException ("we're not supposed to in state " + state); 174 } 175 } 176 ret = ParserUtils.removeChars (ret, '\n'); 177 ret = ParserUtils.removeChars (ret, '\r'); 178 179 return (ret); 180 } 181 182 185 public String getImageURL() 186 { 187 if (null == imageURL) 188 if (null != getPage ()) 189 imageURL = getPage ().getAbsoluteURL (extractImageLocn ()); 190 191 return (imageURL); 192 } 193 194 public void setImageURL (String url) 195 { 196 imageURL = url; 197 setAttribute ("SRC", imageURL); 198 } 199 } 200 | Popular Tags |