1 19 20 33 package org.htmlparser.scanners; 34 35 import org.htmlparser.Parser; 39 import org.htmlparser.tags.FormTag; 40 import org.htmlparser.tags.Tag; 41 import org.htmlparser.tags.data.CompositeTagData; 42 import org.htmlparser.tags.data.TagData; 43 import org.htmlparser.util.LinkProcessor; 44 import org.htmlparser.util.ParserException; 45 46 52 public class FormScanner extends CompositeTagScanner 53 { 54 private static final String [] MATCH_ID = { "FORM" }; 55 public static final String PREVIOUS_DIRTY_LINK_MESSAGE = 56 "Encountered a form tag after an open link tag.\nThere should have been an end tag for the link before the form tag began.\nCorrecting this.."; 57 private boolean linkScannerAlreadyOpen = false; 58 private static final String [] formTagEnders = { "HTML", "BODY" }; 59 62 public FormScanner(Parser parser) 63 { 64 this("", parser); 65 } 66 69 public FormScanner(String filter, Parser parser) 70 { 71 super(filter, MATCH_ID, formTagEnders, false); 72 parser.addScanner(new InputTagScanner("-i")); 73 parser.addScanner(new TextareaTagScanner("-t")); 74 parser.addScanner(new SelectTagScanner("-select")); 75 parser.addScanner(new OptionTagScanner("-option")); 76 } 77 78 84 public String extractFormLocn(Tag tag, String url) throws ParserException 85 { 86 try 87 { 88 String formURL = tag.getAttribute("ACTION"); 89 if (formURL == null) 90 return ""; 91 else 92 return (new LinkProcessor()).extract(formURL, url); 93 } 94 catch (Exception e) 95 { 96 String msg; 97 if (tag != null) 98 msg = tag.getText(); 99 else 100 msg = ""; 101 throw new ParserException( 102 "HTMLFormScanner.extractFormLocn() : Error in extracting form location, tag = " 103 + msg 104 + ", url = " 105 + url, 106 e); 107 } 108 } 109 110 public String extractFormName(Tag tag) 111 { 112 return tag.getAttribute("NAME"); 113 } 114 115 public String extractFormMethod(Tag tag) 116 { 117 String method = tag.getAttribute("METHOD"); 118 if (method == null) 119 method = FormTag.GET; 120 return method.toUpperCase(); 121 122 } 123 124 135 152 155 public String [] getID() 156 { 157 return MATCH_ID; 158 } 159 160 public boolean evaluate(String s, TagScanner previousOpenScanner) 161 { 162 if (previousOpenScanner instanceof LinkScanner) 163 { 164 linkScannerAlreadyOpen = true; 165 StringBuffer msg = new StringBuffer (); 166 msg.append("<"); 167 msg.append(s); 168 msg.append(">"); 169 msg.append(PREVIOUS_DIRTY_LINK_MESSAGE); 170 feedback.warning(msg.toString()); 171 } 177 else 178 linkScannerAlreadyOpen = false; 179 return super.evaluate(s, previousOpenScanner); 180 } 181 182 public Tag createTag(TagData tagData, CompositeTagData compositeTagData) 183 throws ParserException 184 { 185 String formUrl = 186 extractFormLocn( 187 compositeTagData.getStartTag(), 188 tagData.getUrlBeingParsed()); 189 if (formUrl != null && formUrl.length() > 0) 190 compositeTagData.getStartTag().setAttribute("ACTION", formUrl); 191 return new FormTag(tagData, compositeTagData); 192 } 193 194 } 195 | Popular Tags |