1 27 28 package org.objectweb.clif.protocol.http.lib; 29 30 import org.htmlparser.HTMLNode; 32 import org.htmlparser.HTMLParser; 33 import org.htmlparser.HTMLReader; 34 import org.htmlparser.scanners.HTMLLinkScanner; 35 import org.htmlparser.tags.HTMLFormTag; 36 import org.htmlparser.tags.HTMLInputTag; 37 import org.htmlparser.tags.HTMLLinkTag; 38 import org.htmlparser.util.DefaultHTMLParserFeedback; 39 import org.htmlparser.util.HTMLEnumeration; 40 41 import java.io.StringReader; 42 import java.util.Enumeration; 43 import java.util.Hashtable; 44 import java.util.Vector; 45 46 54 public class HtmlParser { 55 56 private HTMLReader reader; 57 private HTMLParser parser; 58 private DefaultHTMLParserFeedback feedback; 59 private Vector links = new Vector(); 60 private Vector formNodes = new Vector(); 61 private Vector form_inputs = new Vector(); 62 private HTMLEnumeration e; 63 private Enumeration enum; 64 private Enumeration enum2; 65 private HTMLNode node = null; 66 private HTMLLinkTag linkTag = null; 67 private HTMLNode htmlNode = null; 68 private HTMLFormTag formTag = null; 69 private HTMLInputTag inputTag = null; 70 private Hashtable inputParameters = new Hashtable(); 71 private String formName = null; 72 private Vector fullResults = new Vector(); 73 private HtmlGenericTag genericTag = null; 74 private Hashtable parameters = null; 75 private Vector param = new Vector(); 76 private Object[] resultArray; 77 78 private static HtmlParser htmlParser=null; 79 80 private HtmlParser(){ 81 feedback=new DefaultHTMLParserFeedback(DefaultHTMLParserFeedback.QUIET); 82 } 83 84 public static HtmlParser getInstance(){ 85 if(htmlParser==null){ 86 htmlParser=new HtmlParser(); 87 } 88 return htmlParser; 89 } 90 91 99 public Object[] getLinks(String arg) throws Exception { 100 101 reader=new HTMLReader(new StringReader(arg), 50); 102 parser=new HTMLParser(reader, feedback); 103 parser.addScanner(new HTMLLinkScanner("-l")); 104 105 links = new Vector(); 106 107 for (e = parser.elements(); e.hasMoreNodes();) { 108 node = e.nextHTMLNode(); 109 if (node instanceof HTMLLinkTag) { 110 linkTag = (HTMLLinkTag) node; 111 links.add(linkTag.getLink()); 112 } 113 } 114 115 return links.toArray(); 116 } 117 118 126 public Object[] getFields(String arg) throws Exception { 127 128 reader = new HTMLReader(new StringReader(arg), 50); 129 parser = new HTMLParser(reader, feedback); 130 131 parser.registerScanners(); 132 133 formNodes = new Vector(); 134 135 for (e = parser.elements(); e.hasMoreNodes();) { 137 htmlNode = e.nextHTMLNode(); 138 if (htmlNode instanceof HTMLFormTag) { 139 htmlNode.collectInto(formNodes, "-f"); 140 } 141 } 142 143 144 form_inputs = new Vector(); 145 inputParameters = new Hashtable(); 146 fullResults = new Vector(); 147 148 for (enum = formNodes.elements(); enum.hasMoreElements();) { 150 formTag = (HTMLFormTag) enum.nextElement(); 151 form_inputs = formTag.getFormInputs(); 152 formName = formTag.getFormName(); 153 154 for (enum2 = form_inputs.elements(); 156 enum2.hasMoreElements(); 157 ) { 158 inputTag = (HTMLInputTag) enum2.nextElement(); 159 inputParameters = inputTag.parseParameters(); 160 inputParameters.remove("$<TAGNAME>$"); 161 162 if (formName == null) 164 formName = formTag.getFormLocation(); 165 166 inputParameters.put("FORM_NAME", formName); 168 fullResults.add(inputParameters); 170 } 171 } 172 173 return fullResults.toArray(); 175 } 176 177 186 public Object[] getTags(String[] arg) throws Exception { 187 188 reader = new HTMLReader(new StringReader(arg[0]), 50); 189 parser = new HTMLParser(reader, feedback); 190 parser.addScanner(new HtmlGenericScanner("", arg[1])); 191 192 param = new Vector(); 193 194 for (e = parser.elements(); e.hasMoreNodes();) { 195 node = e.nextHTMLNode(); 196 if (node instanceof HtmlGenericTag) { 197 genericTag = (HtmlGenericTag) node; 198 parameters = genericTag.parseParameters(); 199 parameters.remove("$<TAGNAME>$"); 201 param.add(parameters); 202 } 203 204 } 205 206 resultArray = new Object[2]; 208 209 resultArray[0] = param.toArray(); 211 212 resultArray[1] = null; 215 return resultArray; 216 } 217 } 218 | Popular Tags |