Search in sources :

Example 6 with IConverter

use of org.apache.wicket.util.convert.IConverter in project wicket by apache.

the class Component method getDefaultModelObjectAsString.

/**
 * Gets a model object as a string. Depending on the "escape model strings" flag of the
 * component, the string is either HTML escaped or not. "HTML escaped" meaning that only HTML
 * sensitive chars are escaped but not all none-ascii chars. Proper HTML encoding should be used
 * instead. In case you really need a fully escaped model string you may call
 * {@link Strings#escapeMarkup(CharSequence, boolean, boolean)} on the model string returned.
 *
 * @see Strings#escapeMarkup(CharSequence, boolean, boolean)
 * @see #getEscapeModelStrings()
 *
 * @param modelObject
 *            Model object to convert to string
 * @return The string
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public final String getDefaultModelObjectAsString(final Object modelObject) {
    if (modelObject != null) {
        // Get converter
        final Class<?> objectClass = modelObject.getClass();
        final IConverter converter = getConverter(objectClass);
        // Model string from property
        final String modelString = converter.convertToString(modelObject, getLocale());
        if (modelString != null) {
            // If we should escape the markup
            if (getFlag(FLAG_ESCAPE_MODEL_STRINGS)) {
                // Escape HTML sensitive characters only. Not all none-ascii chars
                return Strings.escapeMarkup(modelString, false, false).toString();
            }
            return modelString;
        }
    }
    return "";
}
Also used : IConverter(org.apache.wicket.util.convert.IConverter)

Aggregations

IConverter (org.apache.wicket.util.convert.IConverter)6 Map (java.util.Map)2 DebugSettings (org.apache.wicket.settings.DebugSettings)2 IValueMap (org.apache.wicket.util.value.IValueMap)2 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 ComponentTag (org.apache.wicket.markup.ComponentTag)1 MarkupStream (org.apache.wicket.markup.MarkupStream)1 Label (org.apache.wicket.markup.html.basic.Label)1