List of usage examples for org.jsoup.nodes Document outerHtml
@Override
public String outerHtml()
From source file:com.cognifide.aet.job.common.comparators.source.SourceComparator.java
private String formatCodeAllFormatted(String code) { Document doc = Jsoup.parse(code); return removeEmptyLines(doc.outerHtml()); }
From source file:com.aquest.emailmarketing.web.controllers.CampaignsController.java
/** * Test./*from w w w . j a v a 2 s. com*/ * * @return the string * @throws IOException */ @RequestMapping(value = "/test") public String test() throws IOException { //DONE: Check if bounces process is finished //bouncedEmailService.processAllBounces(); //String url = "http://studenti.unicreditbank.rs/nl4/type2/index.html"; Document doc = Jsoup.connect("http://studenti.unicreditbank.rs/nl9/index.html").get(); String all = doc.outerHtml(); Broadcast broadcast = broadcastService.getBroadcastById(45); Premailer premailer = new Premailer(); PremailerInterface premailerInterface = premailer.getPremailerInstance(); Map<String, Object> options = new HashMap<String, Object>(); options.put("with_html_string", true); //premailerInterface.init(broadcast.getHtmlbody(), options); premailerInterface.init(all, options); System.out.println(premailerInterface.inline_css()); premailer.destroyInstance(); return "test"; }
From source file:com.aquest.emailmarketing.web.controllers.BroadcastTemplateController.java
/** * Define content./*from w w w .j av a 2s . co m*/ * * @param model the model * @param broadcastTemplate1 the broadcast template1 * @param result the result * @param principal the principal * @return the string * @throws IOException */ @RequestMapping(value = "/defineBcastTemplateContent", method = RequestMethod.POST) public String defineContent(Model model, @Valid @ModelAttribute("broadcastTemplate") BroadcastTemplate broadcastTemplate1, @RequestParam(value = "fromUrl", required = false) String fromUrl, @RequestParam(value = "optimize", required = false) boolean optimize, @RequestParam(value = "baseurl", required = false) String baseUrl, @RequestParam(value = "rel2abs", required = false) boolean rel2abs, BindingResult result, Principal principal) throws IOException { String htmlBodyPrep = ""; BroadcastTemplate broadcastTemplate = broadcastTemplateService .getBroadcastTemplateById(broadcastTemplate1.getId()); broadcastTemplate.setB_template_subject(broadcastTemplate1.getB_template_subject()); if (fromUrl != "") { Document doc = Jsoup.connect(fromUrl).get(); htmlBodyPrep = doc.outerHtml(); broadcastTemplate.setHtmlbody(htmlBodyPrep); System.out.println(htmlBodyPrep); } if (broadcastTemplate1.getHtmlbody() != null) { htmlBodyPrep = broadcastTemplate1.getHtmlbody(); broadcastTemplate.setHtmlbody(htmlBodyPrep); } if (rel2abs == true) { if (baseUrl != null) { System.out.println(baseUrl); Document doc = Jsoup.parse(broadcastTemplate.getHtmlbody(), baseUrl); System.out.println(doc.toString()); Elements images = doc.select("img"); for (Element e : images) { e.attr("src", e.absUrl("src")); System.out.println(e.absUrl("src")); } broadcastTemplate.setHtmlbody(doc.outerHtml()); htmlBodyPrep = doc.outerHtml(); } else { // ovde staviti error handling } } if (optimize == true) { // /* PREMAILER API OPTIONS // * line_length - Line length used by to_plain_text. Boolean, default is 65. // warn_level - What level of CSS compatibility warnings to show (see Warnings). // NONE = 0 // SAFE = 1 // POOR = 2 // RISKY = 3 // link_query_string - A string to append to every a href="" link. Do not include the initial ?. // base_url - Used to calculate absolute URLs for local files. // css - Manually specify CSS stylesheets. // css_to_attributes - Copy related CSS attributes into HTML attributes (e.g. background-color to bgcolor) // css_string - Pass CSS as a string // remove_ids - Remove ID attributes whenever possible and convert IDs used as anchors to hashed to avoid collisions in webmail programs. Default is false. // remove_classes - Remove class attributes. Default is false. // remove_comments - Remove html comments. Default is false. // preserve_styles - Whether to preserve any link rel=stylesheet and style elements. Default is false. // preserve_reset - Whether to preserve styles associated with the MailChimp reset code // with_html_string - Whether the html param should be treated as a raw string. // verbose - Whether to print errors and warnings to $stderr. Default is false. // adapter - Which HTML parser to use, either :nokogiri or :hpricot. Default is :hpricot. // */ Premailer premailer = new Premailer(); PremailerInterface premailerInterface = premailer.getPremailerInstance(); Map<String, Object> options = new HashMap<String, Object>(); options.put("with_html_string", true); options.put("base_url", fromUrl); premailerInterface.init(broadcastTemplate.getHtmlbody(), options); //premailerInterface.init(htmlBodyPrep, options); broadcastTemplate.setHtmlbody(premailerInterface.inline_css()); System.out.println(premailerInterface.inline_css()); premailer.destroyInstance(); } broadcastTemplate.setPlaintext(broadcastTemplate1.getPlaintext()); System.out.println(broadcastTemplate.toString()); String bcast_id = broadcastTemplateService.SaveOrUpdate(broadcastTemplate); // Find URLs in html body and add tracking code Urls urls = new Urls(); String html = broadcastTemplate.getHtmlbody(); List<String> urlList = new ArrayList<String>(); Document doc = Jsoup.parse(html); Elements links = doc.select("a[href]"); for (Element link : links) { if (link.attr("abs:href").length() > 5) { urlList.add(link.attr("abs:href")); } } model.addAttribute("urlList", urlList); model.addAttribute("urls", urls); // Google Analytics - utmCampaign List List<String> utmCampaignList = new ArrayList<String>(); utmCampaignList.add("[BROADAST_NAME]"); model.addAttribute("utmCampaignList", utmCampaignList); // Google Analytics - utmSource List List<String> utmSourceList = new ArrayList<String>(); utmSourceList.add("[CAMPAIGN_NAME]"); model.addAttribute("utmSourceList", utmSourceList); // Google Analytics - utmContent List List<String> utmContentList = new ArrayList<String>(); utmContentList.add("[EMAIL]"); // ovde dodati sve varijabilne podatke iz CM_EMAIL_BROADCAST_LIST model.addAttribute("utmContentList", utmContentList); model.addAttribute("broadcastTemplate", broadcastTemplate); return "bcasttemptracking"; }
From source file:mpstyle.mwat.controller.MWAT.java
/** * Scorre tutte le view HTML che devono essere tradotte. Cercando la * propriet//w w w . j a v a2s .co m * <i>segnaposto</i> che indicano quale traduzione utilizzare e prepend del * testo localizzato. * * @param entries Mappa chiave-valore delle traduzioni. * @param jsName Identificativo della traduzione. */ private void parseViews(HashMap<String, String> entries, String jsName) { try { for (HTMLFile htmlInputFile : htmlinputFileList) { File input = new File(htmlInputFile.getAbsoluteFilePath()); Document doc = Jsoup.parse(input, settings.getFileEncode()); doc = HTMLFileBook.translateHtml(doc, entries, settings.getTranslationProperty(), jsName); if (doc == null) { throw new Exception("Unknow error in the translation"); } String outputPath = settings.getHtmlOutputPath() + File.separator + jsName + File.separator + htmlInputFile.getRelativeFolderPath(); boolean result = FileBook.saveFile(doc.outerHtml(), htmlInputFile.getFileName(), outputPath, settings.getFileEncode()); if (!result) { LOGGER.error(String.format("The translated view %s\\%s isn't savad correctly.", outputPath, htmlInputFile.getFileName())); } } } catch (Exception ex) { LOGGER.debug(ex); } }
From source file:com.aquest.emailmarketing.web.controllers.BroadcastController.java
/** * Define content./*from w ww . ja va 2s . c o m*/ * * @param model the model * @param broadcast1 the broadcast1 * @param result the result * @param principal the principal * @return the string * @throws IOException */ @RequestMapping(value = "/defineContent", method = RequestMethod.POST) public String defineContent(Model model, @Valid @ModelAttribute("broadcast") Broadcast broadcast1, @RequestParam(value = "fromUrl", required = false) String fromUrl, @RequestParam(value = "optimize", required = false) boolean optimize, @RequestParam(value = "baseurl", required = false) String baseUrl, @RequestParam(value = "rel2abs", required = false) boolean rel2abs, BindingResult result, Principal principal) throws IOException { String htmlBodyPrep = ""; Broadcast broadcast = broadcastService.getBroadcastById(broadcast1.getId()); broadcast.setSubject(broadcast1.getSubject()); if (fromUrl != "") { Document doc = Jsoup.connect(fromUrl).get(); htmlBodyPrep = doc.outerHtml(); broadcast.setHtmlbody(htmlBodyPrep); System.out.println(htmlBodyPrep); } if (broadcast1.getHtmlbody() != null) { htmlBodyPrep = broadcast1.getHtmlbody(); broadcast.setHtmlbody(htmlBodyPrep); System.out.println("Da vidimo: " + htmlBodyPrep); } if (rel2abs == true) { if (baseUrl != null) { System.out.println(baseUrl); Document doc = Jsoup.parse(broadcast.getHtmlbody(), baseUrl); System.out.println(doc.toString()); Elements images = doc.select("img"); for (Element e : images) { e.attr("src", e.absUrl("src")); System.out.println(e.absUrl("src")); } broadcast.setHtmlbody(doc.outerHtml()); htmlBodyPrep = doc.outerHtml(); } else { // ovde staviti error handling } } if (optimize == true) { // /* PREMAILER API OPTIONS // * line_length - Line length used by to_plain_text. Boolean, default is 65. // warn_level - What level of CSS compatibility warnings to show (see Warnings). // NONE = 0 // SAFE = 1 // POOR = 2 // RISKY = 3 // link_query_string - A string to append to every a href="" link. Do not include the initial ?. // base_url - Used to calculate absolute URLs for local files. // css - Manually specify CSS stylesheets. // css_to_attributes - Copy related CSS attributes into HTML attributes (e.g. background-color to bgcolor) // css_string - Pass CSS as a string // remove_ids - Remove ID attributes whenever possible and convert IDs used as anchors to hashed to avoid collisions in webmail programs. Default is false. // remove_classes - Remove class attributes. Default is false. // remove_comments - Remove html comments. Default is false. // preserve_styles - Whether to preserve any link rel=stylesheet and style elements. Default is false. // preserve_reset - Whether to preserve styles associated with the MailChimp reset code // with_html_string - Whether the html param should be treated as a raw string. // verbose - Whether to print errors and warnings to $stderr. Default is false. // adapter - Which HTML parser to use, either :nokogiri or :hpricot. Default is :hpricot. // */ Premailer premailer = new Premailer(); PremailerInterface premailerInterface = premailer.getPremailerInstance(); Map<String, Object> options = new HashMap<String, Object>(); options.put("with_html_string", true); options.put("base_url", fromUrl); premailerInterface.init(broadcast.getHtmlbody(), options); //premailerInterface.init(htmlBodyPrep, options); broadcast.setHtmlbody(premailerInterface.inline_css()); System.out.println(premailerInterface.inline_css()); premailer.destroyInstance(); } broadcast.setPlaintext(broadcast1.getPlaintext()); broadcastService.SaveOrUpdate(broadcast); // Find URLs in html body and add tracking code Urls urls = new Urls(); String html = broadcast.getHtmlbody(); //HashSet to avoid duplicates Set<String> urlList = new HashSet<String>(); Document doc = Jsoup.parse(html); Elements links = doc.select("a[href]"); for (Element link : links) { if (link.attr("abs:href").length() > 5) { urlList.add(link.attr("abs:href")); } } model.addAttribute("urlList", urlList); model.addAttribute("urls", urls); // Google Analytics - utmCampaign List List<String> utmCampaignList = new ArrayList<String>(); utmCampaignList.add("[BROADAST_NAME]"); model.addAttribute("utmCampaignList", utmCampaignList); // Google Analytics - utmSource List List<String> utmSourceList = new ArrayList<String>(); utmSourceList.add("[CAMPAIGN_NAME]"); model.addAttribute("utmSourceList", utmSourceList); // Google Analytics - utmContent List List<String> utmContentList = new ArrayList<String>(); utmContentList.add("[EMAIL]"); //TODO: add all variables from CM_EMAIL_BROADCAST_LIST model.addAttribute("utmContentList", utmContentList); model.addAttribute("broadcast", broadcast); return "tracking"; }
From source file:com.ibuildapp.romanblack.WebPlugin.WebPlugin.java
/** * Prepare and load data to WebView./*from ww w . jav a 2 s . co m*/ */ private void showHtml() { try { if (isOnline) { if (currentUrl.length() > 0 && !currentUrl.equals("about:blank")) { url = currentUrl; } if (url.length() > 0) html = "<html><body><a href=\"" + url + "\" id=\"link\" /></body></html>"; Document doc = Jsoup.parse(html); Element iframe = doc.select("iframe").first(); boolean isGoogleCalendar = false; boolean isGoogleForms = false; String iframeSrc = ""; try { if (iframe != null) { iframeSrc = iframe.attr("src"); } } catch (Exception e) { } if (iframeSrc.length() > 0) { isGoogleCalendar = iframeSrc.contains("www.google.com/calendar") || iframeSrc.contains("calendar.google.com/calendar"); isGoogleForms = iframeSrc.contains("google.com/forms"); } if (isGoogleCalendar) { webView.loadUrl(iframeSrc); } else if (isGoogleForms) { webView.getSettings().setBuiltInZoomControls(false); DisplayMetrics metrix = getResources().getDisplayMetrics(); int width = metrix.widthPixels; int height = metrix.heightPixels; float density = metrix.density; iframe.attr("width", (int) (width / density) + ""); iframe.attr("height", (int) (height / density - (75 /*+ (hasAdView() ? 50 : 0)*/)) + ""); iframe.attr("style", "margin: 0; padding: 0"); Element body = doc.select("body").first(); body.attr("style", "margin: 0; padding: 0"); html = doc.outerHtml(); webView.loadDataWithBaseURL("http://", html, "text/html", "utf-8", ""); } else { Elements forms = doc.select("form"); Iterator<Element> iterator = forms.iterator(); for (; iterator.hasNext();) { Element form = iterator.next(); String action = form.attr("action"); if (action.contains("paypal.com")) { form.append("<input type=\"hidden\" name=\"bn\" value=\"ibuildapp_SP\">"); } html = doc.html(); } hideProgress = true; if (Build.VERSION.SDK_INT >= 20 && html.contains("ibuildapp") && html.contains("powr")) { int height = getResources().getDisplayMetrics().heightPixels; html = "<iframe width=\"" + 420 + "\" height=\"" + height + "\" frameBorder=\"0\" src=" + url + "></iframe>"; webView.loadData(html, "text/html", "utf-8"); } else webView.loadDataWithBaseURL("http://", html, "text/html", "utf-8", ""); } } else { if (html.length() > 0) { webView.loadDataWithBaseURL("http://", html, "text/html", "utf-8", ""); } } handler.sendEmptyMessageDelayed(HIDE_PROGRESS, 10000); } catch (Exception ex) { // Error Logging } }
From source file:im.ene.lab.attiq.ui.activities.ItemDetailActivity.java
@SuppressWarnings("unused") public void onEventMainThread(ItemDetailEvent event) { Article article = event.article;/*w w w. j a v a 2 s . c om*/ String userName = null; if (article != null) { mArticle = article; mRealm.beginTransaction(); mRealm.copyToRealmOrUpdate(mArticle); mRealm.commitTransaction(); User user = article.getUser(); mArticleName.setText(article.getTitle()); mSpannableTitle = new SpannableString(article.getTitle()); userName = user.getId(); mSpannableSubtitle = new SpannableString(userName); updateTitle(); buildArticleComments(article); buildArticleMenu(article); final String html; try { if (PrefUtil.isMathJaxEnabled()) { html = IOUtil.readAssets("html/article_mathjax.html"); } else { html = IOUtil.readAssets("html/article.html"); } Document doc = Jsoup.parse(html); Element elem = doc.getElementById("content"); elem.append(article.getRenderedBody()); String result = doc.outerHtml(); mContentView.loadDataWithBaseURL(article.getUrl(), result, "text/html", "utf-8", null); } catch (IOException e) { e.printStackTrace(); } } final CharSequence subTitle; if (article != null && !UIUtil.isEmpty(userName)) { subTitle = Html.fromHtml(getString(R.string.item_user_info, userName, userName, TimeUtil.beautify(article.getCreatedAt()))); } else { subTitle = getString(R.string.item_detail_subtitle, userName); } mArticleDescription.setText(subTitle); }
From source file:im.ene.lab.attiq.ui.activities.ItemDetailActivity.java
@SuppressWarnings("unused") public void onEventMainThread(ItemCommentsEvent event) { if (!UIUtil.isEmpty(event.comments)) { mCommentsView.setVisibility(View.VISIBLE); List<Comment> comments = event.comments; mCommentCount.setText(comments.size() + ""); String info = comments.size() == 1 ? getString(R.string.comment_singular) : getString(R.string.comment_plural); // FIXME should use plural strings mCommentInfo.setText(getString(R.string.article_comment, comments.size(), info)); final String html; try {/*from www .j a va2 s . c om*/ html = IOUtil.readAssets("html/comments.html"); Document fullBody = Jsoup.parse(html); Element content = fullBody.getElementById("content"); for (Comment comment : comments) { String commentHtml = IOUtil.readAssets("html/comment.html"); commentHtml = commentHtml.replace("{user_icon_url}", comment.getUser().getProfileImageUrl()) .replace("{user_name}", comment.getUser().getId()) .replace("{comment_time}", TimeUtil.commentTime(comment.getCreatedAt())) .replace("{article_uuid}", mItemUuid).replace("{comment_id}", comment.getId()); Document commentDoc = Jsoup.parse(commentHtml); Element eComment = commentDoc.getElementsByClass("comment-box").first(); eComment.getElementsByClass("message").first().append(comment.getRenderedBody()); // remove comment edit block if it is not from current user if (mMyProfile == null || !mMyProfile.getId().equals(comment.getUser().getId())) { String commentId = "comment_{comment_id}_{user_name}" .replace("{comment_id}", comment.getId()) .replace("{user_name}", comment.getUser().getId()); Element commentEditor = commentDoc.getElementById(commentId); commentEditor.remove(); } content.appendChild(eComment); } String result = fullBody.outerHtml(); mCommentsView.loadDataWithBaseURL("http://qiita.com/", result, null, null, null); } catch (IOException e) { e.printStackTrace(); } } else { mCommentCount.setText("0"); mCommentInfo.setText(getString(R.string.article_comment, 0, getString(R.string.comment_plural))); mCommentsView.setVisibility(View.GONE); } }
From source file:org.apache.archiva.web.docs.RestDocsServlet.java
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { logger.debug("docs request to path: {}", req.getPathInfo()); String path = StringUtils.removeStart(req.getPathInfo(), "/"); InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(path); if (StringUtils.endsWith(path, ".xsd")) { StringEscapeUtils.escapeXml(resp.getWriter(), IOUtils.toString(is)); //IOUtils.copy( is, resp.getOutputStream() ); return;//ww w.j a va 2 s . c o m } String startPath = StringUtils.substringBefore(path, "/"); // replace all links !! Document document = Jsoup.parse(is, "UTF-8", ""); Element body = document.body().child(0); Elements links = body.select("a[href]"); for (Element link : links) { link.attr("href", "#" + startPath + "/" + link.attr("href")); } Elements datalinks = body.select("[data-href]"); for (Element link : datalinks) { link.attr("data-href", "#" + startPath + "/" + link.attr("data-href")); } Elements codes = body.select("code"); for (Element code : codes) { code.attr("class", code.attr("class") + " nice-code"); } //default generated enunciate use h1/h2/h3 which is quite big so transform to h3/h4/h5 Elements headers = body.select("h1"); for (Element header : headers) { header.tagName("h3"); } headers = body.select("h2"); for (Element header : headers) { header.tagName("h4"); } headers = body.select("h3"); for (Element header : headers) { header.tagName("h5"); } Document res = new Document(""); res.appendChild(body.select("div[id=main]").first()); Elements scripts = body.select("script"); for (Element script : scripts) { res.appendChild(script); } resp.getOutputStream().write(res.outerHtml().getBytes()); }
From source file:org.asqatasun.rules.doc.utils.rga33.extractor.Rgaa3Extractor.java
private static void createTestcaseFiles() throws IOException { File srcDir = new File(RGAA3_TESTCASE_PATH); for (File file : srcDir.listFiles()) { String fileName = file.getName().replace("Rgaa30Rule", "").replace(".java", ""); String theme = fileName.substring(0, 2); String crit = fileName.substring(2, 4); String test = fileName.substring(4, 6); String testKey = Integer.valueOf(theme).toString() + "-" + Integer.valueOf(crit).toString() + "-" + Integer.valueOf(test).toString(); String wrongKey = theme + "." + crit + "." + test; for (File testcase : file.listFiles()) { if (testcase.isFile() && testcase.getName().contains("html")) { Document doc = Jsoup.parse(FileUtils.readFileToString(testcase)); Element detail = doc.select(".test-detail").first(); if (detail == null) { System.out.println(doc.outerHtml()); } else { detail.tagName("div"); detail.text(""); for (Element el : detail.children()) { el.remove();//from ww w .j a v a2 s . c o m } if (!detail.hasAttr("lang")) { detail.attr("lang", "fr"); } detail.append("\n" + RGAA3.get(testKey).ruleRawHtml + "\n"); doc.outputSettings().escapeMode(Entities.EscapeMode.xhtml); doc.outputSettings().outline(false); doc.outputSettings().indentAmount(4); String outputHtml = doc.outerHtml(); if (outputHtml.contains(wrongKey)) { outputHtml = outputHtml.replaceAll(wrongKey, RGAA3.get(testKey).getRuleDot()); } FileUtils.writeStringToFile(testcase, outputHtml); } } } } }