/*
* The contents of this file are subject to the terms of the Common Development and
* Distribution License (the License). You may not use this file except in compliance with the
* License.
*
* You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
* specific language governing permission and limitations under the License.
*
* When distributing Covered Software, include this CDDL Header Notice in each file and include
* the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
* Header, with the fields enclosed by brackets [] replaced by your own identifying
* information: "Portions Copyright [year] [name of copyright owner]".
*
* Copyright 2006-2009 Sun Microsystems, Inc.
* Portions Copyright 2014-2015 ForgeRock AS.
*/
package org.opends.quicksetup.util;
import static org.opends.messages.QuickSetupMessages.*;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.i18n.LocalizableMessageBuilder;
import org.forgerock.i18n.slf4j.LocalizedLogger;
import org.opends.quicksetup.Constants;
import org.opends.quicksetup.ui.UIFactory;
/**
* This is an implementation of the ProgressMessageFormatter class that
* provides format in HTML.
*/
public class HtmlProgressMessageFormatter implements ProgressMessageFormatter
{
private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
private LocalizableMessage doneHtml;
private LocalizableMessage errorHtml;
/** The constant used to separate parameters in an URL. */
private static final String PARAM_SEPARATOR = "&&&&";
/** The space in HTML. */
private static final LocalizableMessage SPACE = LocalizableMessage.raw(" ");
/**
* The line break.
* The extra char is necessary because of bug:
* http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4988885
*/
private static final LocalizableMessage LINE_BREAK=
LocalizableMessage.raw("
"+Constants.HTML_LINE_BREAK);
private static final LocalizableMessage TAB = new LocalizableMessageBuilder(SPACE)
.append(SPACE)
.append(SPACE)
.append(SPACE)
.append(SPACE)
.toMessage();
/**
* Returns the HTML representation of the text without providing any style.
* @param text the source text from which we want to get the HTML
* representation
* @return the HTML representation for the given text.
*/
@Override
public LocalizableMessage getFormattedText(LocalizableMessage text)
{
return LocalizableMessage.raw(Utils.getHtml(String.valueOf(text)));
}
/**
* Returns the HTML representation of the text that is the summary of the
* installation process (the one that goes in the UI next to the progress
* bar).
* @param text the source text from which we want to get the formatted
* representation
* @return the HTML representation of the summary for the given text.
*/
@Override
public LocalizableMessage getFormattedSummary(LocalizableMessage text)
{
return new LocalizableMessageBuilder("")
.append(UIFactory.applyFontToHtml(
String.valueOf(text), UIFactory.PROGRESS_FONT))
.toMessage();
}
/**
* Returns the HTML representation of an error for a given text.
* @param text the source text from which we want to get the HTML
* representation
* @param applyMargin specifies whether we apply a margin or not to the
* resulting HTML.
* @return the HTML representation of an error for the given text.
*/
@Override
public LocalizableMessage getFormattedError(LocalizableMessage text, boolean applyMargin)
{
String html;
if (!Utils.containsHtml(String.valueOf(text))) {
html = UIFactory.getIconHtml(UIFactory.IconType.ERROR_LARGE)
+ SPACE
+ SPACE
+ UIFactory.applyFontToHtml(Utils.getHtml(String.valueOf(text)),
UIFactory.PROGRESS_ERROR_FONT);
} else {
html =
UIFactory.getIconHtml(UIFactory.IconType.ERROR_LARGE) + SPACE
+ SPACE + UIFactory.applyFontToHtml(
String.valueOf(text), UIFactory.PROGRESS_FONT);
}
String result = UIFactory.applyErrorBackgroundToHtml(html);
if (applyMargin)
{
result =
UIFactory.applyMargin(result,
UIFactory.TOP_INSET_ERROR_MESSAGE, 0, 0, 0);
}
return LocalizableMessage.raw(result);
}
/**
* Returns the HTML representation of a warning for a given text.
* @param text the source text from which we want to get the HTML
* representation
* @param applyMargin specifies whether we apply a margin or not to the
* resulting HTML.
* @return the HTML representation of a warning for the given text.
*/
@Override
public LocalizableMessage getFormattedWarning(LocalizableMessage text, boolean applyMargin)
{
String html;
if (!Utils.containsHtml(String.valueOf(text))) {
html =
UIFactory.getIconHtml(UIFactory.IconType.WARNING_LARGE)
+ SPACE
+ SPACE
+ UIFactory.applyFontToHtml(Utils.getHtml(String.valueOf(text)),
UIFactory.PROGRESS_WARNING_FONT);
} else {
html =
UIFactory.getIconHtml(UIFactory.IconType.WARNING_LARGE) + SPACE
+ SPACE + UIFactory.applyFontToHtml(
String.valueOf(text), UIFactory.PROGRESS_FONT);
}
String result = UIFactory.applyWarningBackgroundToHtml(html);
if (applyMargin)
{
result =
UIFactory.applyMargin(result,
UIFactory.TOP_INSET_ERROR_MESSAGE, 0, 0, 0);
}
return LocalizableMessage.raw(result);
}
/**
* Returns the HTML representation of a success message for a given text.
* @param text the source text from which we want to get the HTML
* representation
* @return the HTML representation of a success message for the given text.
*/
@Override
public LocalizableMessage getFormattedSuccess(LocalizableMessage text)
{
// Note: the text we get already is in HTML form
String html =
UIFactory.getIconHtml(UIFactory.IconType.INFORMATION_LARGE) + SPACE
+ SPACE + UIFactory.applyFontToHtml(String.valueOf(text),
UIFactory.PROGRESS_FONT);
return LocalizableMessage.raw(UIFactory.applySuccessfulBackgroundToHtml(html));
}
/**
* Returns the HTML representation of a log error message for a given
* text.
* @param text the source text from which we want to get the HTML
* representation
* @return the HTML representation of a log error message for the given
* text.
*/
@Override
public LocalizableMessage getFormattedLogError(LocalizableMessage text)
{
String html = Utils.getHtml(String.valueOf(text));
return LocalizableMessage.raw(UIFactory.applyFontToHtml(html,
UIFactory.PROGRESS_LOG_ERROR_FONT));
}
/**
* Returns the HTML representation of a log message for a given text.
* @param text the source text from which we want to get the HTML
* representation
* @return the HTML representation of a log message for the given text.
*/
@Override
public LocalizableMessage getFormattedLog(LocalizableMessage text)
{
String html = Utils.getHtml(String.valueOf(text));
return LocalizableMessage.raw(UIFactory.applyFontToHtml(html,
UIFactory.PROGRESS_LOG_FONT));
}
/**
* Returns the HTML representation of the 'Done' text string.
* @return the HTML representation of the 'Done' text string.
*/
@Override
public LocalizableMessage getFormattedDone()
{
if (doneHtml == null)
{
String html = Utils.getHtml(INFO_PROGRESS_DONE.get().toString());
doneHtml = LocalizableMessage.raw(UIFactory.applyFontToHtml(html,
UIFactory.PROGRESS_DONE_FONT));
}
return LocalizableMessage.raw(doneHtml);
}
/**
* Returns the HTML representation of the 'Error' text string.
* @return the HTML representation of the 'Error' text string.
*/
@Override
public LocalizableMessage getFormattedError() {
if (errorHtml == null)
{
String html = Utils.getHtml(INFO_PROGRESS_ERROR.get().toString());
errorHtml = LocalizableMessage.raw(UIFactory.applyFontToHtml(html,
UIFactory.PROGRESS_ERROR_FONT));
}
return LocalizableMessage.raw(errorHtml);
}
/**
* Returns the HTML representation of the argument text to which we add
* points. For instance if we pass as argument 'Configuring Server' the
* return value will be 'Configuring Server .....'.
* @param text the String to which add points.
* @return the HTML representation of the '.....' text string.
*/
@Override
public LocalizableMessage getFormattedWithPoints(LocalizableMessage text)
{
String html = Utils.getHtml(String.valueOf(text));
String points = SPACE +
Utils.getHtml(INFO_PROGRESS_POINTS.get().toString()) + SPACE;
LocalizableMessageBuilder buf = new LocalizableMessageBuilder();
buf.append(UIFactory.applyFontToHtml(html, UIFactory.PROGRESS_FONT))
.append(
UIFactory.applyFontToHtml(points, UIFactory.PROGRESS_POINTS_FONT));
return buf.toMessage();
}
/**
* Returns the formatted representation of a point.
* @return the formatted representation of the '.' text string.
*/
@Override
public LocalizableMessage getFormattedPoint()
{
return LocalizableMessage.raw(UIFactory.applyFontToHtml(".",
UIFactory.PROGRESS_POINTS_FONT));
}
/**
* Returns the formatted representation of a space.
* @return the formatted representation of the ' ' text string.
*/
@Override
public LocalizableMessage getSpace()
{
return LocalizableMessage.raw(SPACE);
}
/**
* Returns the formatted representation of a progress message for a given
* text.
* @param text the source text from which we want to get the formatted
* representation
* @return the formatted representation of a progress message for the given
* text.
*/
@Override
public LocalizableMessage getFormattedProgress(LocalizableMessage text)
{
return LocalizableMessage.raw(UIFactory.applyFontToHtml(
Utils.getHtml(String.valueOf(text)),
UIFactory.PROGRESS_FONT));
}
/**
* Returns the HTML representation of an error message for a given throwable.
* This method applies a margin if the applyMargin parameter is
* true.
* @param t the throwable.
* @param applyMargin specifies whether we apply a margin or not to the
* resulting HTML.
* @return the HTML representation of an error message for the given
* exception.
*/
@Override
public LocalizableMessage getFormattedError(Throwable t, boolean applyMargin)
{
String openDiv = "
true the resulting HTML will display
* the stack.
* @return the HTML representation of an exception in the progress log for a
* given url.
*/
private String getErrorWithStackHtml(String url, boolean inverse)
{
String p = url.substring("http://".length());
try
{
p = URLDecoder.decode(p, "UTF-8");
} catch (UnsupportedEncodingException uee)
{
// Bug
throw new IllegalStateException("UTF-8 is not supported ", uee);
}
String params[] = p.split(PARAM_SEPARATOR);
int i = 0;
String openDiv = params[i++];
String hideText = params[i++];
String showText = params[i++];
String stackText = params[i++];
String closeDiv = params[i++];
boolean isHide = Boolean.parseBoolean(params[i]);
if (isHide)
{
return getErrorWithStackHtml(openDiv, hideText, showText, stackText,
closeDiv, !inverse);
} else
{
return getErrorWithStackHtml(openDiv, hideText, showText, stackText,
closeDiv, inverse);
}
}
}