List of usage examples for org.jsoup.nodes Element attr
public Element attr(String attributeKey, boolean attributeValue)
From source file:models.NotificationMail.java
private static void handleImages(Document doc) { for (Element img : doc.select("img")) { img.attr("style", "max-width:1024px;" + img.attr("style")); img.wrap(String.format("<a href=\"%s\" target=\"_blank\" style=\"border:0;outline:0;\"></a>", img.attr("src"))); }/*from w w w.jav a 2 s . c o m*/ }
From source file:com.zacwolf.commons.email.Email.java
private static void dataurlEncode(final org.jsoup.nodes.Element img, final EmailAttachment attachment) throws IOException, MessagingException { String mime_type = attachment.contenttype; InputStream is = attachment.getDataHandler().getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStream b64os = MimeUtility.encode(baos, "base64"); IOUtils.copy(is, b64os);/*from w w w. ja v a 2 s . c om*/ b64os.close(); img.attr("src", "data:" + mime_type + ";base64," + new String(baos.toByteArray())); }
From source file:com.assignmentone.snippet.ShowCodeSnippet.java
private Element makeShowHtml(String file, String title, String contents) { // create the panel tag Element panel = new Element(Tag.valueOf("div"), ""); panel.addClass("panel"); panel.addClass("panel-default"); Element heading = new Element(Tag.valueOf("div"), ""); heading.addClass("panel-heading"); Element body = new Element(Tag.valueOf("div"), ""); body.addClass("panel-body"); panel.appendChild(heading);/* w w w .j a v a2 s . c o m*/ panel.appendChild(body); // write title and file path String headStr = StringUtils.isEmpty(title) ? "" : title + ":"; headStr += file; heading.appendText(headStr); // create the pre tag Element pre = new Element(Tag.valueOf("pre"), ""); pre.addClass("prettyprint source"); pre.attr("style", "overflow-x:auto"); if (contents != null) { pre.appendChild(new Element(Tag.valueOf("span"), "").appendText(contents)); } body.appendChild(pre); return panel; }
From source file:com.astamuse.asta4d.render.RenderUtil.java
private final static void applySnippetResultToElement(Document doc, String snippetRefId, Element snippetElement, Element renderTarget, Renderer renderer) { apply(renderTarget, renderer);/*from www .jav a2 s . co m*/ if (snippetElement.ownerDocument() == null) { // it means this snippet element is replaced by a // element completely String reSelector = SelectorUtil.attr(ExtNodeConstants.SNIPPET_NODE_TAG_SELECTOR, ExtNodeConstants.ATTR_SNIPPET_REF, snippetRefId); Elements elems = doc.select(reSelector); if (elems.size() > 0) { snippetElement = elems.get(0); } else { snippetElement = null; } } if (snippetElement != null) { snippetElement.attr(ExtNodeConstants.SNIPPET_NODE_ATTR_STATUS, ExtNodeConstants.SNIPPET_NODE_ATTR_STATUS_FINISHED); } }
From source file:com.astamuse.asta4d.web.form.flow.base.AbstractFormFlowSnippet.java
private Renderer setCascadeFormContainerArrayRef(final int cascadeFormArrayIndex) { return Renderer.create(":root", new ElementSetter() { @Override/*from ww w .j a va 2s. co m*/ public void set(Element elem) { elem.attr(cascadeFormContainerArrayRefAttrName(), String.valueOf(cascadeFormArrayIndex)); } }); }
From source file:com.astamuse.asta4d.web.form.flow.base.BasicFormFlowSnippetTrait.java
/** * We only render the form trace map when it exists * //from w w w . java2 s . c o m * @return */ default Renderer renderTraceId(String traceId) { if (StringUtils.isEmpty(traceId)) { return Renderer.create(); } else { return Renderer.create(":root", new ElementSetter() { @Override public void set(Element elem) { Element hide = new Element(Tag.valueOf("input"), ""); hide.attr("name", FormFlowConstants.FORM_FLOW_TRACE_ID_QUERY_PARAM); hide.attr("type", "hidden"); hide.attr("value", traceId); elem.appendChild(hide); } }); } }
From source file:github.srlee309.lessWrongBookCreator.scraper.PostSectionExtractor.java
/** * Saves all images in the given Element to a local newUrl and converts the src for all img tags to the local file * @param postContent - from which to extract the images *///from w w w . j a v a2 s. com protected final void convertImagesToLocal(Element postContent) { Elements imgs = postContent.getElementsByTag("img"); for (Element img : imgs) { String src = img.absUrl("src"); String folder = "htmlOutput"; int indexName = src.lastIndexOf("/"); String name = src; if (indexName != -1) { indexName = src.lastIndexOf("/") + 1; name = src.substring(indexName, src.length()); } img.attr("src", name); saveImage(src, folder, name); } }
From source file:com.astamuse.asta4d.web.form.flow.base.AbstractFormFlowSnippet.java
/** * We only render the form trace map when it exists * /*w w w.j a v a 2 s . c om*/ * @return */ protected Renderer renderTraceMapData() { if (StringUtils.isEmpty(formTraceMapStr)) { return Renderer.create(); } else { return Renderer.create(":root", new ElementSetter() { @Override public void set(Element elem) { Element hide = new Element(Tag.valueOf("input"), ""); hide.attr("name", FormFlowConstants.FORM_STEP_TRACE_MAP_STR); hide.attr("type", "hidden"); hide.attr("value", formTraceMapStr); elem.appendChild(hide); } }); } }
From source file:com.pagecrumb.proxy.util.filter.HtmlProxyTransformParser.java
public HtmlProxyTransformParser(String html, final String targetServer) throws ParserException { log.debug("Creating Html Parser Object."); // TODO Using targetServer directly is dangerous // because there might be URL passed which is not absolute URL. // its either the URL is decoded using some utilities to get // absolute domain this.targetServer = targetServer; log.info(this.getClass().toString() + " " + "Requested URL: " + this.targetServer); NodeVisitor linkvisitor = new NodeVisitor() { @Override//from w w w. j a va 2 s . c o m public void visitTag(Tag tag) { String name = tag.getTagName(); if ("link".equalsIgnoreCase(name)) { String hrefValue = tag.getAttribute("href"); if (hrefValue != null && !hrefValue.startsWith("/proxy")) { if (hrefValue.startsWith("http://") || hrefValue.startsWith("https://")) { // add more protocols later log.info("Rewriting with targetServer: " + hrefValue); hrefValue = hostServlet + hrefValue; } if (!hrefValue.startsWith("http://") || !hrefValue.startsWith("https://") && !hrefValue.startsWith("/proxy")) { // add more protocols later if (!hrefValue.startsWith("/")) { hrefValue = "/" + hrefValue; } if (hrefValue.startsWith("/") && !hrefValue.startsWith("/proxy")) { log.info("Rewriting with targetServer: " + hrefValue); hrefValue = hostServlet + targetServer + hrefValue; } hrefValue = hrefValue.replaceAll("&", "&"); tag.setAttribute("href", hrefValue); log.debug("hrefValue=" + hrefValue); } } } /** * Anchor */ if ("a".equalsIgnoreCase(name)) { String hrefValue = tag.getAttribute("href"); if (hrefValue != null && !hrefValue.startsWith("/proxy")) // Prevent over re-writing the proxy strings { log.debug("hrefValue=" + hrefValue); if (hrefValue.startsWith("//")) { hrefValue = "http:" + hrefValue; } if (hrefValue.startsWith("http://") || hrefValue.startsWith("https://")) { // add more protocols later log.info("Rewriting with targetServer: " + hrefValue); hrefValue = hostServlet + hrefValue; } // TODO Check if the href value is just a filename e.g "home.html" /** * Mail Protocol */ else if (hrefValue.startsWith("mailto:")) { } /** * HTTP Protocol */ else if (!hrefValue.startsWith("http://") || !hrefValue.startsWith("https://") && !hrefValue.startsWith("/proxy")) { // add more protocols later // TODO Must run hrefValue in malformed URL fix, to fix problems with the URL // i.e. "double slash" http://127.0.0.1:8888/proxy?http://localhost:8080/docs//introduction.html // reason could be that target server end with "/" if (!hrefValue.startsWith("/")) { hrefValue = "/" + hrefValue; hrefValue = hostServlet + targetServer + hrefValue; } else if (hrefValue.startsWith("/") && !hrefValue.startsWith("/proxy")) { log.info("Rewriting with targetServer: " + hrefValue); hrefValue = hostServlet + targetServer + hrefValue; } } hrefValue = hrefValue.replaceAll("&", "&"); tag.setAttribute("href", hrefValue); log.debug("hrefValue=" + hrefValue); } } // TODO hostServletNoFilter is intended to be used for // non page documents, like .js or .css // this way it will not run through the filter URL rewriting if ("script".equalsIgnoreCase(name)) { String srcValue = tag.getAttribute("src"); if (srcValue != null && !srcValue.startsWith("/")) { srcValue = "/" + srcValue; srcValue = /*hostServer +*/ hostServlet + targetServer + srcValue; } if (srcValue != null && srcValue.startsWith("//")) { // special case (see YouTube) //srcValue = /*hostServer +*/ hostServletNoFilter + targetServer + srcValue; srcValue = "http:" + srcValue; srcValue = hostServlet + srcValue; } if (srcValue != null) { tag.setAttribute("src", srcValue); } log.debug("srcValue=" + srcValue); } if ("form".equalsIgnoreCase(name)) { String actionValue = tag.getAttribute("action"); if (actionValue != null && !actionValue.startsWith("/")) { actionValue = "/" + actionValue; actionValue = hostServlet + targetServer + actionValue; } if (actionValue != null && actionValue.startsWith("/")) { actionValue = hostServlet + targetServer + actionValue; } if (actionValue != null) { tag.setAttribute("action", actionValue); } log.debug("actionValue=" + actionValue); } /** * Get javascripts */ if ("script".equalsIgnoreCase(name)) { ScriptTag script = (ScriptTag) tag; if (script != null) { //String text = script.getStringText(); //final AstNode astRoot = new org.mozilla.javascript.Parser().parse(text, "", 1); //log.info("Script_from_parser="+astRoot.toSource()); //log.info("script="+text); // Parse the script? based on the documented activities. } } if ("img".equalsIgnoreCase(name)) { String srcValue = tag.getAttribute("src"); if (srcValue != null && !srcValue.startsWith("/proxy")) { if (srcValue.startsWith("http://") || srcValue.startsWith("https://")) { // add more protocols later log.info("Rewriting with targetServer: " + srcValue); srcValue = hostServlet + srcValue; } if (!srcValue.startsWith("http://") || !srcValue.startsWith("https://") && !srcValue.startsWith("/proxy")) { // add more protocols later if (!srcValue.startsWith("/")) { srcValue = "/" + srcValue; srcValue = hostServlet + targetServer + srcValue; } if (srcValue.startsWith("/") && !srcValue.startsWith("/proxy")) { log.info("Rewriting with targetServer: " + srcValue); srcValue = hostServlet + targetServer + srcValue; } srcValue = srcValue.replaceAll("&", "&"); tag.setAttribute("src", srcValue); log.debug("srcValue=" + srcValue); } } } } }; Parser parser = new Parser(html, null); NodeList nl = parser.parse(null); nl.visitAllNodesWith(linkvisitor); this.html = nl.toHtml(); Document doc = Jsoup.parse(this.html); //Element bScriptElement = new Element(org.jsoup.parser.Tag.valueOf("script"), ""); //bScriptElement.attr("src", "/browz.js"); //bScriptElement.attr("type", "text/javascript"); //bScriptElement.attr("language", "javascript"); Element jqEl = new Element(org.jsoup.parser.Tag.valueOf("script"), ""); jqEl.attr("src", "/jquery.min.js"); jqEl.attr("type", "text/javascript"); jqEl.attr("language", "javascript"); Element bzEl = new Element(org.jsoup.parser.Tag.valueOf("script"), ""); bzEl.attr("src", "/browz.js"); bzEl.attr("type", "text/javascript"); bzEl.attr("language", "javascript"); //doc.select("head").first().children().first().before("<script type=\"text/javascript\" language=\"javascript\">" // + readFileAsString("browz.js") + "</script>"); // Important! Removed to satisfy error, must be reviewed // doc.select("head").first().children().first().before(bzEl); // doc.select("head").first().children().first().before(jqEl); /* for (Element el : doc.getElementsByTag("html")) { jqEl = el.appendElement("script"); jqEl.attr("src", "/jquery.min.js"); jqEl.attr("type", "text/javascript"); jqEl.attr("language", "javascript"); bzEl = el.appendElement("script"); bzEl.attr("src", "/browz.js"); bzEl.attr("type", "text/javascript"); bzEl.attr("language", "javascript"); } */ this.html = doc.toString(); }
From source file:com.aquest.emailmarketing.web.controllers.BroadcastTemplateController.java
/** * Define content./* w w w .j av a 2s.c o 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"; }