packagepl.labno.bernard.htmlunified;importcom.gargoylesoftware.htmlunit.WebClient;importcom.gargoylesoftware.htmlunit.html.DomNodeList;importcom.gargoylesoftware.htmlunit.html.HtmlElement;importcom.gargoylesoftware.htmlunit.html.HtmlTableCell;importjava.util.HashMap;importjava.util.Map;publicclassWebClientUtils{privatestaticlongdefaultCheckInterval=500;privatestaticlongdefaultTimeout=10000;publicstaticlonggetDefaultCheckInterval(){returndefaultCheckInterval;}publicstaticvoidsetDefaultCheckInterval(longdefaultCheckInterval){WebClientUtils.defaultCheckInterval=defaultCheckInterval;}publicstaticlonggetDefaultTimeout(){returndefaultTimeout;}publicstaticvoidsetDefaultTimeout(longdefaultTimeout){WebClientUtils.defaultTimeout=defaultTimeout;}publicstaticintwaitForJSJob(WebClientwebClient){returnwaitForJSJob(webClient,webClient.waitForBackgroundJavaScript(10)-1,defaultCheckInterval,defaultTimeout);}publicstaticintwaitForJSJob(WebClientwebClient,intinitialJobCount){returnwaitForJSJob(webClient,initialJobCount,defaultCheckInterval,defaultTimeout);}publicstaticintwaitForJSJob(WebClientwebClient,intinitialJobCount,longtimeout){returnwaitForJSJob(webClient,initialJobCount,defaultCheckInterval,timeout);}publicstaticintwaitForJSJob(WebClientwebClient,intinitialJobCount,longcheckInterval,longtimeout){intjobs;longstartTime=System.currentTimeMillis();do{jobs=webClient.waitForBackgroundJavaScript(checkInterval);if(startTime+timeout<System.currentTimeMillis()){thrownewRuntimeException("Number of JavaScript jobs doesn't drop to initial level for 10000 seconds. It's memory leak in your JavaScript rather then request taking so long!");}}while(jobs>initialJobCount);System.out.println("Waiting took: "+(System.currentTimeMillis()-startTime)+"ms");returnjobs;}/** * Returns list of suggestions from rich:suggestionBox * * @param suggestion suggestionBox element * @param column column of suggestionBox to extract text from * @return list of suggestions */publicstaticMap<String,HtmlTableCell>getSuggestions(HtmlElementsuggestion,intcolumn){finalMap<String,HtmlTableCell>suggestions=newHashMap<String,HtmlTableCell>();finalHtmlElementsuggestElement=suggestion.getElementById(suggestion.getId()+":suggest");@SuppressWarnings("unchecked")finalDomNodeList<HtmlElement>suggestionRows=suggestElement.getElementsByTagName("tr");for(HtmlElementrow:suggestionRows){@SuppressWarnings("unchecked")finalDomNodeList<HtmlElement>cells=row.getElementsByTagName("td");finalHtmlTableCellcell=(HtmlTableCell)cells.get(column+1);suggestions.put(cell.asText(),cell);}returnsuggestions;}}