001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.bpel.debugger.ui.util;
021:
022: import java.awt.Color;
023:
024: /**
025: *
026: * @author Alexander Zgursky
027: */
028: public final class HtmlUtil {
029:
030: /** Creates a new instance of HtmlUtil */
031: private HtmlUtil() {
032: }
033:
034: /**
035: * Converts given text to html with <code>bold=true</code>.
036: *
037: * @param text text to be converted
038: *
039: * @return html-formatted String or <code>null</code>,
040: * if given text is <code>null</code>
041: *
042: * @see #toHtml
043: */
044: public static String toBold(String text) {
045: return toHtml(text, true, false, null);
046: }
047:
048: /**
049: * Converts given text to html using given attributes.
050: *
051: * @param text text to be converted
052: * @param bold bold flag
053: * @param italic italic flag
054: * @param color color to be used or <code>null</code> for default color
055: *
056: * @return html formatted text or <code>null</code> if
057: * given text is <code>null</code>
058: */
059: public static String toHtml(final String text, final boolean bold,
060: final boolean italic, final Color color) {
061:
062: return html(highlight(text, bold, italic, color));
063: }
064:
065: public static String html(final String string) {
066: return "<html>" + string + "</html>"; // NOI18N
067: }
068:
069: public static String highlight(final String text,
070: final boolean bold, final boolean italic, final Color color) {
071:
072: if (text == null) {
073: return null;
074: }
075:
076: final StringBuilder builder = new StringBuilder();
077:
078: if (bold) {
079: builder.append("<b>"); // NOI18N
080: }
081: if (italic) {
082: builder.append("<i>"); // NOI18N
083: }
084: if (color != null) {
085: builder.append("<font color=\"#"); // NOI18N
086: builder.append(String.format("%06x",
087: color.getRGB() & 0xffffff));
088: builder.append("\">"); // NOI18N
089: }
090: builder.append(text.replaceAll("&", "&"). // NOI18N
091: replaceAll("<", "<"). // NOI18N
092: replaceAll(">", ">")); // NOI18N
093:
094: if (color != null) {
095: builder.append("</font>"); // NOI18N
096: }
097: if (italic) {
098: builder.append("</i>"); // NOI18N
099: }
100: if (bold) {
101: builder.append("</b>"); // NOI18N
102: }
103:
104: return builder.toString();
105: }
106: }
|