Java tutorial
/* Copyright (c) 2011 Danish Maritime Authority * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this library. If not, see <http://www.gnu.org/licenses/>. */ package dk.dma.msiproxy.common.util; import org.apache.commons.lang.StringUtils; import org.jsoup.Jsoup; import org.jsoup.examples.HtmlToPlainText; import org.jsoup.nodes.Document; /** * Text utility methods */ public class TextUtils { /** * Converts the text from html to plain text * @param html the html * @return the plain text version */ public static String html2txt(String html) { try { Document doc = Jsoup.parse(html); return new HtmlToPlainText().getPlainText(doc.body()); } catch (Exception e) { // If any error occurs, return the original html return html; } } /** * Converts the text from plain text to html * @param text the text * @return the html version */ public static String txt2html(String text) { text = StringUtils.replaceEach(text, new String[] { "&", "\"", "<", ">", "\n", "\t" }, new String[] { "&", """, "<", ">", "<br>", " " }); return text; } public static void main(String[] args) { System.out.println(txt2html("test\t this\njdhf")); } }