1 19 20 33 package org.htmlparser.scanners; 34 import java.util.Hashtable ; 38 39 import org.htmlparser.tags.ImageTag; 40 import org.htmlparser.tags.Tag; 41 import org.htmlparser.tags.data.TagData; 42 import org.htmlparser.util.LinkProcessor; 43 import org.htmlparser.util.ParserException; 44 import org.htmlparser.util.ParserUtils; 45 51 public class ImageScanner extends TagScanner 52 { 53 public static final String IMAGE_SCANNER_ID = "IMG"; 54 private Hashtable table; 55 private LinkProcessor processor; 56 59 public ImageScanner() 60 { 61 super(); 62 processor = new LinkProcessor(); 63 } 64 67 public ImageScanner(String filter, LinkProcessor processor) 68 { 69 super(filter); 70 this.processor = processor; 71 } 72 78 public String extractImageLocn(Tag tag, String url) throws ParserException 79 { 80 String relativeLink = null; 81 try 82 { 83 table = tag.getAttributes(); 84 relativeLink = (String ) table.get("SRC"); 85 86 if (relativeLink != null) 87 { 88 relativeLink = ParserUtils.removeChars(relativeLink, '\n'); 89 relativeLink = ParserUtils.removeChars(relativeLink, '\r'); 90 } 91 if (relativeLink == null || relativeLink.length() == 0) 92 { 93 String tagText = tag.getText().toUpperCase(); 95 int indexSrc = tagText.indexOf("SRC"); 96 if (indexSrc != -1) 97 { 98 tag.setText( 100 tag.getText().substring(0, indexSrc + 3) 101 + "=" 102 + tag.getText().substring( 103 indexSrc + 3, 104 tag.getText().length())); 105 table = tag.redoParseAttributes(); 106 relativeLink = (String ) table.get("SRC"); 107 108 } 109 } 110 if (relativeLink == null) 111 return ""; 112 else 113 return processor.extract(relativeLink, url); 114 } 115 catch (Exception e) 116 { 117 throw new ParserException( 118 "HTMLImageScanner.extractImageLocn() : Error in extracting image location, relativeLink = " 119 + relativeLink 120 + ", url = " 121 + url, 122 e); 123 } 124 } 125 126 public String [] getID() 127 { 128 String [] ids = new String [1]; 129 ids[0] = IMAGE_SCANNER_ID; 130 return ids; 131 } 132 133 protected Tag createTag(TagData tagData, Tag tag, String url) 134 throws ParserException 135 { 136 String link = extractImageLocn(tag, url); 137 return new ImageTag(tagData, link); 138 } 139 140 } 141 | Popular Tags |