private ScrapingHistory getViadeoConnectionWrapper(Document document) throws ParserConfigurationException, XPathExpressionException, ParseException { ScrapingHistory result = null; Node rootEle = getFirstElementByTagName(document, "ScrappingHistory"); if (rootEle != null) { result = new ScrapingHistory(); result.setDate(getDateFormat().parse(getAttributeValue((Element) rootEle, PROPERTY_DATE))); NodeList searchConnectionNodeList = getNodeList(document, "//ScrappingHistory/ViadeoSearch"); for (int indexSearchCon = 0; indexSearchCon < searchConnectionNodeList.getLength(); indexSearchCon++) { Node searchConnectionNode = searchConnectionNodeList.item(indexSearchCon); UrlConnectionWrapper searchConnection = new UrlConnectionWrapper(); searchConnection.setUrl(getAttributeValue((Element) searchConnectionNode, PROPERTY_URL)); searchConnection.setUserAgent( getAttributeValue((Element) searchConnectionNode, PROPERTY_USERAGENT)); searchConnection.setReferer( getAttributeValue((Element) searchConnectionNode, PROPERTY_REFERER)); searchConnection.setMethod( Method.valueOf(getAttributeValue((Element) searchConnectionNode, PROPERTY_METHOD))); searchConnection.setScrapped( Boolean.valueOf((getAttributeValue((Element) searchConnectionNode, "scrapped")))); int timeOut = getRequestTimeout(); try { timeOut = (int) Float.parseFloat( getAttributeValue((Element) searchConnectionNode, PROPERTY_TIMEOUT)); } catch (NumberFormatException ex) { logger.error(ex.getMessage(), ex); } searchConnection.setTimeout(timeOut); NodeList postParamNodeList = getNodeList(searchConnectionNode, "PostParameterList/PostParameter"); for (int i = 0; i < postParamNodeList.getLength(); i++) { Element element = (Element) postParamNodeList.item(i); searchConnection.putPostParameter( element.getAttribute(PROPERTY_NAME), element.getAttribute(PROPERTY_VALUE)); } NodeList cookieNodeList = getNodeList(searchConnectionNode, "CookieList/Cookie"); for (int i = 0; i < cookieNodeList.getLength(); i++) { Element element = (Element) cookieNodeList.item(i); searchConnection.putCookie( element.getAttribute(PROPERTY_NAME), element.getAttribute(PROPERTY_VALUE)); } result.addSearchConnection(searchConnection); } } return result; }
private Document getXmlDocument(ScrapingHistory scrappingHistory) throws ParserConfigurationException { Document result = null; if (scrappingHistory != null) { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); result = docBuilder.newDocument(); Element rootElement = result.createElement("ScrappingHistory"); addAttribute( result, rootElement, PROPERTY_DATE, getDateFormat().format(scrappingHistory.getDate())); result.appendChild(rootElement); for (UrlConnectionWrapper searchConnection : scrappingHistory.getSearchConnectionList()) { Element viadeoSearchEle = result.createElement("ViadeoSearch"); rootElement.appendChild(viadeoSearchEle); addAttribute( result, viadeoSearchEle, "scrapped", searchConnection.isScrapped() ? "true" : "false"); addAttribute(result, viadeoSearchEle, PROPERTY_URL, searchConnection.getUrl()); addAttribute(result, viadeoSearchEle, PROPERTY_USERAGENT, searchConnection.getUserAgent()); addAttribute(result, viadeoSearchEle, PROPERTY_REFERER, searchConnection.getReferer()); addAttribute(result, viadeoSearchEle, PROPERTY_TIMEOUT, "" + searchConnection.getTimeout()); addAttribute( result, viadeoSearchEle, PROPERTY_METHOD, "" + searchConnection.getMethod().toString()); Element postParamListEle = result.createElement("PostParameterList"); viadeoSearchEle.appendChild(postParamListEle); for (String paramName : searchConnection.getPostParameterMap().keySet()) { String paramValue = searchConnection.getPostParameterMap().get(paramName); Element postParamEle = result.createElement("PostParameter"); addAttribute(result, postParamEle, PROPERTY_NAME, paramName); addAttribute(result, postParamEle, PROPERTY_VALUE, paramValue); postParamListEle.appendChild(postParamEle); } Element cookieListEle = result.createElement("CookieList"); viadeoSearchEle.appendChild(cookieListEle); for (String cookieName : searchConnection.getCookies().keySet()) { String cookieValue = searchConnection.getCookies().get(cookieName); Element cookieEle = result.createElement("Cookie"); addAttribute(result, cookieEle, PROPERTY_NAME, cookieName); addAttribute(result, cookieEle, PROPERTY_VALUE, cookieValue); cookieListEle.appendChild(cookieEle); } } } return result; }