/** * Returns a string representation of the XML document from this element and all it's children (recursively). * The charset used is the current page encoding. * @return the XML string */ public String asXml() { Charset charsetName = null; final HtmlPage htmlPage = getHtmlPageOrNull(); if (htmlPage != null) { charsetName = htmlPage.getCharset(); } final StringWriter stringWriter = new StringWriter(); try (PrintWriter printWriter = new PrintWriter(stringWriter)) { if (charsetName != null && this instanceof HtmlHtml) { printWriter.print("<?xml version=\"1.0\" encoding=\""); printWriter.print(charsetName); printWriter.print("\"?>\r\n"); } printXml("", printWriter); return stringWriter.toString(); } }
/** * Returns a string representation of the XML document from this element and all it's children (recursively). * The charset used is the current page encoding. * @return the XML string */ public String asXml() { Charset charsetName = null; final HtmlPage htmlPage = getHtmlPageOrNull(); if (htmlPage != null) { charsetName = htmlPage.getCharset(); } final StringWriter stringWriter = new StringWriter(); try (PrintWriter printWriter = new PrintWriter(stringWriter)) { if (charsetName != null && this instanceof HtmlHtml) { printWriter.print("<?xml version=\"1.0\" encoding=\""); printWriter.print(charsetName); printWriter.print("\"?>\r\n"); } printXml("", printWriter); return stringWriter.toString(); } }
/** * Saves this content as the specified file. * @param file the file to save to * @throws IOException if an IO error occurs */ public void saveAs(final File file) throws IOException { final HtmlPage page = (HtmlPage) getPage(); final WebClient webclient = page.getWebClient(); final URL url = page.getFullyQualifiedUrl(getAttributeDirect(SRC_ATTRIBUTE)); final WebRequest request = new WebRequest(url); request.setCharset(page.getCharset()); request.setAdditionalHeader(HttpHeader.REFERER, page.getUrl().toExternalForm()); final WebResponse webResponse = webclient.loadWebResponse(request); try (OutputStream fos = Files.newOutputStream(file.toPath()); InputStream content = webResponse.getContentAsStream()) { IOUtils.copy(content, fos); } }
/** * Saves this content as the specified file. * @param file the file to save to * @throws IOException if an IO error occurs */ public void saveAs(final File file) throws IOException { final HtmlPage page = (HtmlPage) getPage(); final WebClient webclient = page.getWebClient(); final URL url = page.getFullyQualifiedUrl(getAttributeDirect("src")); final WebRequest request = new WebRequest(url); request.setCharset(page.getCharset()); request.setAdditionalHeader(HttpHeader.REFERER, page.getUrl().toExternalForm()); final WebResponse webResponse = webclient.loadWebResponse(request); try (FileOutputStream fos = new FileOutputStream(file); InputStream content = webResponse.getContentAsStream()) { IOUtils.copy(content, fos); } }
/** * Returns the request which will allow us to retrieve the content referenced by the {@code href} attribute. * @return the request which will allow us to retrieve the content referenced by the {@code href} attribute * @throws MalformedURLException in case of problem resolving the URL */ public WebRequest getWebRequest() throws MalformedURLException { final HtmlPage page = (HtmlPage) getPage(); final URL url = page.getFullyQualifiedUrl(getHrefAttribute()); final String accept = page.getWebClient().getBrowserVersion().getCssAcceptHeader(); final WebRequest request = new WebRequest(url, accept); // use the page encoding even if this is a GET requests request.setCharset(page.getCharset()); request.setAdditionalHeader(HttpHeader.REFERER, page.getUrl().toExternalForm()); return request; }
/** * <p>Downloads the image contained by this image element.</p> * <p><span style="color:red">POTENTIAL PERFORMANCE KILLER - DOWNLOADS THE IMAGE - USE AT YOUR OWN RISK</span></p> * <p>If the image has not already been downloaded, this method triggers a download and caches the image.</p> * * @throws IOException if an error occurs while downloading the image */ private void downloadImageIfNeeded() throws IOException { if (!downloaded_) { // HTMLIMAGE_BLANK_SRC_AS_EMPTY final String src = getSrcAttribute(); if (!"".equals(src) && !(hasFeature(HTMLIMAGE_BLANK_SRC_AS_EMPTY) && StringUtils.isBlank(src))) { final HtmlPage page = (HtmlPage) getPage(); final WebClient webclient = page.getWebClient(); final URL url = page.getFullyQualifiedUrl(src); final String accept = webclient.getBrowserVersion().getImgAcceptHeader(); final WebRequest request = new WebRequest(url, accept); request.setCharset(page.getCharset()); request.setAdditionalHeader(HttpHeader.REFERER, page.getUrl().toExternalForm()); imageWebResponse_ = webclient.loadWebResponse(request); } if (imageData_ != null) { imageData_.close(); imageData_ = null; } downloaded_ = hasFeature(JS_IMAGE_COMPLETE_RETURNS_TRUE_FOR_NO_REQUEST) || (imageWebResponse_ != null && imageWebResponse_.getContentType().contains("image")); } }
webRequest.setCharset(page.getCharset());
/** * Returns the request which will allow us to retrieve the content referenced by the {@code href} attribute. * @return the request which will allow us to retrieve the content referenced by the {@code href} attribute * @throws MalformedURLException in case of problem resolving the URL */ public WebRequest getWebRequest() throws MalformedURLException { final HtmlPage page = (HtmlPage) getPage(); final URL url = page.getFullyQualifiedUrl(getHrefAttribute()); final WebRequest request = new WebRequest(url); // use the page encoding even if this is a GET requests request.setCharset(page.getCharset()); request.setAdditionalHeader(HttpHeader.REFERER, page.getUrl().toExternalForm()); final String accept = page.getWebClient().getBrowserVersion().getCssAcceptHeader(); request.setAdditionalHeader(HttpHeader.ACCEPT, accept); return request; }
final String accept = webclient.getBrowserVersion().getImgAcceptHeader(); final WebRequest request = new WebRequest(url, accept); request.setCharset(page.getCharset()); request.setAdditionalHeader(HttpHeader.REFERER, page.getUrl().toExternalForm()); imageWebResponse_ = webclient.loadWebResponse(request);
/** * <p>Downloads the image contained by this image element.</p> * <p><span style="color:red">POTENTIAL PERFORMANCE KILLER - DOWNLOADS THE IMAGE - USE AT YOUR OWN RISK</span></p> * <p>If the image has not already been downloaded, this method triggers a download and caches the image.</p> * * @throws IOException if an error occurs while downloading the image */ private void downloadImageIfNeeded() throws IOException { if (!downloaded_) { // HTMLIMAGE_BLANK_SRC_AS_EMPTY final String src = getSrcAttribute(); if (!"".equals(src) && !(hasFeature(HTMLIMAGE_BLANK_SRC_AS_EMPTY) && StringUtils.isBlank(src))) { final HtmlPage page = (HtmlPage) getPage(); final WebClient webclient = page.getWebClient(); final URL url = page.getFullyQualifiedUrl(src); final String accept = webclient.getBrowserVersion().getImgAcceptHeader(); final WebRequest request = new WebRequest(url, accept); request.setCharset(page.getCharset()); request.setAdditionalHeader(HttpHeader.REFERER, page.getUrl().toExternalForm()); imageWebResponse_ = webclient.loadWebResponse(request); } if (imageData_ != null) { imageData_.close(); imageData_ = null; } downloaded_ = hasFeature(JS_IMAGE_COMPLETE_RETURNS_TRUE_FOR_NO_REQUEST) || (imageWebResponse_ != null && imageWebResponse_.getContentType().contains("image")); } }
final String accept = webclient.getBrowserVersion().getImgAcceptHeader(); final WebRequest request = new WebRequest(url, accept); request.setCharset(page.getCharset()); request.setAdditionalHeader(HttpHeader.REFERER, page.getUrl().toExternalForm()); imageWebResponse_ = webclient.loadWebResponse(request);
webRequest.setCharset(page.getCharset());
Charset charset = EncodingSniffer.toCharset(scriptElement.getCharsetAttribute()); if (charset == null) { charset = page.getCharset();
request.setCharset(page.getCharset()); request.setAdditionalHeader(HttpHeader.REFERER, page.getUrl().toExternalForm()); final WebWindow webWindow = enclosingPage.getEnclosingWindow();
request.setCharset(page.getCharset()); request.setAdditionalHeader(HttpHeader.REFERER, page.getUrl().toExternalForm()); final WebWindow webWindow = enclosingPage.getEnclosingWindow();
Charset charset = EncodingSniffer.toCharset(scriptElement.getCharsetAttribute()); if (charset == null) { charset = page.getCharset();
Charset charset = EncodingSniffer.toCharset(getCharsetAttribute()); if (charset == null) { charset = page.getCharset();
Charset charset = EncodingSniffer.toCharset(getCharsetAttribute()); if (charset == null) { charset = page.getCharset();