/** * Return an email hyperlink using the given email address. If the given value is not a valid * email string it will be rendered as is and not as a hyperlink. * * <p>If the given value is blank then the {@link #getEmptyString()} value will rendered instead. * * <p>The format of the returned email string will be: * * <pre class="codeHtml"> * <a href='mailto:email' attribute>email</a> </pre> * * @param email the email address to hyperlink * @param attribute the anchor tag attribute to render * @return a hyperlinked email */ public String email(String email, String attribute) { if (StringUtils.isNotBlank(email)) { if (email.indexOf('@') != -1 && !email.startsWith("@") && !email.endsWith("@")) { HtmlStringBuffer buffer = new HtmlStringBuffer(128); buffer.elementStart("a"); buffer.appendAttribute("href", "mailto:" + email); if (StringUtils.isNotBlank(attribute)) { buffer.append(" "); buffer.append(attribute); } buffer.closeTag(); buffer.appendEscaped(email); buffer.elementEnd("a"); return buffer.toString(); } else { return email; } } else { return getEmptyString(); } }
/** * Return an hyperlink using the given URL or email address value. If the given value is not a * valid email string or URL it will note be hyperlinked and will be rendered as is. * * <p>If the given value is blank then the {@link #getEmptyString()} value will rendered instead. * * @param value the URL or email address to hyperlink * @param attribute the anchor tag attribute to render * @return a hyperlinked URL or email address */ public String link(String value, String attribute) { if (StringUtils.isNotBlank(value)) { HtmlStringBuffer buffer = new HtmlStringBuffer(128); // If email if (value.indexOf('@') != -1 && !value.startsWith("@") && !value.endsWith("@")) { buffer.elementStart("a"); buffer.appendAttribute("href", "mailto:" + value); if (StringUtils.isNotBlank(attribute)) { buffer.append(" "); buffer.append(attribute); } buffer.closeTag(); buffer.appendEscaped(value); buffer.elementEnd("a"); } else if (value.startsWith("http")) { int index = value.indexOf("//"); if (index != -1) { index += 2; } else { index = 0; } buffer.elementStart("a"); buffer.appendAttribute("href", value); if (StringUtils.isNotBlank(attribute)) { buffer.append(" "); buffer.append(attribute); } buffer.closeTag(); buffer.appendEscaped(value.substring(index)); buffer.elementEnd("a"); } else if (value.startsWith("www")) { buffer.elementStart("a"); buffer.appendAttribute("href", "http://" + value); if (StringUtils.isNotBlank(attribute)) { buffer.append(" "); buffer.append(attribute); } buffer.closeTag(); buffer.appendEscaped(value); buffer.elementEnd("a"); } else { buffer.append(value); } return buffer.toString(); } return getEmptyString(); }