Java examples for java.lang:String HTML
Removes the HTML, extra whitespace and carriage returns from a string
/**// w w w . j a v a 2 s. c o m * Copyright (c) 2005-2006 Aptana, Inc. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html. If redistributing this code, * this entire header must remain intact. */ import java.text.MessageFormat; public class Main{ /** * LINE_DELIMITER */ public static final String LINE_DELIMITER = System.getProperty( "line.separator", "\r\n"); /** * BULLET */ public static final String BULLET = "\u2022 "; /** * EMPTY */ public static final String EMPTY = ""; /** * Removes the HTML, extra whitespace and carriage returns from a string * * @param text * The text to strip * @return The new text, reformatted */ public static String formatAsPlainText(String text) { String tempText = StringUtils.stripCarriageReturns(text); tempText = StringUtils.stripWhitespace(tempText); tempText = StringUtils .replace(tempText, "</li>", StringUtils.EMPTY); //$NON-NLS-1$ tempText = StringUtils.replace(tempText, "<li>", LINE_DELIMITER + "\t" + BULLET); //$NON-NLS-1$ //$NON-NLS-2$ tempText = StringUtils.replace(tempText, "<p>", LINE_DELIMITER); //$NON-NLS-1$ tempText = StringUtils.stripHTML(tempText); return tempText.trim(); } /** * Removes all carriage returns from a string * * @param text * The string to strip of '\n' * @return The string minus the carriage returns */ public static String stripCarriageReturns(String text) { if (text == null) { return null; } return text.replaceAll("\n", StringUtils.EMPTY); //$NON-NLS-1$ } /** * Removes all extra whitespace (multiple spaces or tabs) from a string * * @param text * The string to strip of '\n' * @return The string minus the whitespace */ public static String stripWhitespace(String text) { if (text == null) { return null; } return text.replaceAll("\\s+", " "); //$NON-NLS-1$ //$NON-NLS-2$ } /** * Replace one string with another * * @param str * @param pattern * @param replace * @return String */ public static String replace(String str, String pattern, String replace) { int s = 0; int e = 0; StringBuffer result = new StringBuffer(); while ((e = str.indexOf(pattern, s)) >= 0) { result.append(str.substring(s, e)); result.append(replace); s = e + pattern.length(); } result.append(str.substring(s)); return result.toString(); } /** * Strips HTML tags from text * * @param text * Text to strip * @return the text, minus any tags */ public static String stripHTML(String text) { if (text == null) { return null; } String tempText = text.replaceAll("<p>", "\n"); //$NON-NLS-1$ //$NON-NLS-2$ return tempText.replaceAll("\\<.*?\\>", StringUtils.EMPTY); //$NON-NLS-1$ } }