Example usage for org.apache.commons.lang3 StringUtils defaultString

List of usage examples for org.apache.commons.lang3 StringUtils defaultString

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils defaultString.

Prototype

public static String defaultString(final String str) 

Source Link

Document

Returns either the passed in String, or if the String is null , an empty String ("").

 StringUtils.defaultString(null)  = "" StringUtils.defaultString("")    = "" StringUtils.defaultString("bat") = "bat" 

Usage

From source file:org.apache.struts2.views.java.Attributes.java

/**
 * Add a key/value pair to the attributes only if the value is not null.
 * @param attrName attribute name//from w w  w . j  av  a 2s  . com
 * @param paramValue value of attribute
 * @param encode html encode the value
 * @return this
 */
public Attributes addIfExists(String attrName, Object paramValue, boolean encode) {
    if (paramValue != null) {
        String val = paramValue.toString();
        if (StringUtils.isNotBlank(val))
            put(attrName, (encode ? StringUtils.defaultString(StringEscapeUtils.escapeHtml4(val)) : val));
    }
    return this;
}

From source file:org.apache.struts2.views.java.Attributes.java

/**
 * Add a key/value pair to the attributes, if the value is null, it will be set as an empty string.
 * @param attrName attribute name/*w  w  w .  j  a  v  a  2  s. com*/
 * @param paramValue value of attribute
 * @param encode html encode the value
 * @return this
 */
public Attributes addDefaultToEmpty(String attrName, Object paramValue, boolean encode) {
    if (paramValue != null) {
        String val = paramValue.toString();
        put(attrName, (encode ? StringUtils.defaultString(StringEscapeUtils.escapeHtml4(val)) : val));
    } else {
        put(attrName, "");
    }
    return this;
}

From source file:org.apache.struts2.views.java.simple.CheckboxHandler.java

public void generate() throws IOException {
    Map<String, Object> params = context.getParameters();
    Attributes attrs = new Attributes();

    String fieldValue = (String) params.get("fieldValue");
    String id = (String) params.get("id");
    String name = (String) params.get("name");
    Object disabled = params.get("disabled");

    attrs.add("type", "checkbox").add("name", name).add("value", fieldValue)
            .addIfTrue("checked", params.get("nameValue")).addIfTrue("readonly", params.get("readonly"))
            .addIfTrue("disabled", disabled).addIfExists("tabindex", params.get("tabindex"))
            .addIfExists("id", id).addIfExists("class", params.get("cssClass"))
            .addIfExists("style", params.get("cssStyle")).addIfExists("title", params.get("title"));
    start("input", attrs);
    end("input");

    //hidden input
    attrs = new Attributes();
    attrs.add("type", "hidden")
            .add("id", "__checkbox_" + StringUtils.defaultString(StringEscapeUtils.escapeHtml4(id)))
            .add("name", "__checkbox_" + StringUtils.defaultString(StringEscapeUtils.escapeHtml4(name)))
            .add("value", "__checkbox_" + StringUtils.defaultString(StringEscapeUtils.escapeHtml4(fieldValue)))
            .addIfTrue("disabled", disabled);
    start("input", attrs);
    end("input");
}

From source file:org.apache.struts2.views.java.simple.CheckboxListHandler.java

public void generate() throws IOException {

    Map<String, Object> params = context.getParameters();

    //Get parameters
    Object listObj = params.get("list");
    String listKey = (String) params.get("listKey");
    String listValue = (String) params.get("listValue");
    String name = (String) params.get("name");
    Object disabled = params.get("disabled");
    String id = (String) params.get("id");

    int cnt = 1;/*from ww  w  . j a  v  a  2  s . c om*/

    //This will interate through all lists
    ValueStack stack = this.context.getStack();
    if (listObj != null) {
        Iterator itt = MakeIterator.convert(listObj);
        while (itt.hasNext()) {
            Object item = itt.next();
            stack.push(item);

            //key
            Object itemKey = findValue(listKey != null ? listKey : "top");
            String itemKeyStr = StringUtils.defaultString(itemKey == null ? null : itemKey.toString());

            //value
            Object itemValue = findValue(listValue != null ? listValue : "top");
            String itemValueStr = StringUtils.defaultString(itemValue == null ? null : itemValue.toString());

            //Checkbox button section
            Attributes a = new Attributes();
            a.add("type", "checkbox").add("name", name).add("value", itemKeyStr)
                    .addIfTrue("checked", isChecked(params, itemKeyStr))
                    .addIfTrue("readonly", params.get("readonly")).addIfTrue("disabled", disabled)
                    .addIfExists("tabindex", params.get("tabindex"))
                    .addIfExists("id", id + "-" + Integer.toString(cnt));
            start("input", a);
            end("input");

            //Label section
            a = new Attributes();
            a.add("for", id + "-" + Integer.toString(cnt)).addIfExists("class", params.get("cssClass"))
                    .addIfExists("style", params.get("cssStyle"));
            super.start("label", a);
            if (StringUtils.isNotEmpty(itemValueStr))
                characters(itemValueStr);
            super.end("label");

            //Hidden input section
            a = new Attributes();
            a.add("type", "hidden")
                    .add("id", "__multiselect_" + StringUtils.defaultString(StringEscapeUtils.escapeHtml4(id)))
                    .add("name",
                            "__multiselect_" + StringUtils.defaultString(StringEscapeUtils.escapeHtml4(name)))
                    .add("value", "").addIfTrue("disabled", disabled);
            start("input", a);
            end("input");

            stack.pop();
            cnt++;
        }
    }
}

From source file:org.apache.struts2.views.java.simple.RadioHandler.java

public void generate() throws IOException {
    Map<String, Object> params = context.getParameters();

    Object listObj = params.get("list");
    String listKey = (String) params.get("listKey");
    String listValue = (String) params.get("listValue");
    // NameValue is the value that is provided by the name property in the action
    Object nameValue = params.get("nameValue");
    int cnt = 0;/*from  www  . j  av a2  s.  c  om*/

    ValueStack stack = this.context.getStack();
    if (listObj != null) {
        Iterator itt = MakeIterator.convert(listObj);
        while (itt.hasNext()) {
            Object item = itt.next();
            stack.push(item);

            //key
            Object itemKey = findValue(listKey != null ? listKey : "top");
            String itemKeyStr = StringUtils.defaultString(itemKey == null ? null : itemKey.toString());

            //value
            Object itemValue = findValue(listValue != null ? listValue : "top");
            String itemValueStr = StringUtils.defaultString(itemValue == null ? null : itemValue.toString());

            // nameValue needs to cast to a string from object
            String itemNameValueStr = (nameValue == null ? null : nameValue.toString());

            //Checked value.  It's set to true if the nameValue (the value associated with the name which is typically set in 
            //the action is equal to the current key value.
            Boolean checked = itemKeyStr != null && itemNameValueStr != null
                    && itemNameValueStr.equals(itemKeyStr);

            //Radio button section
            String id = params.get("id") + Integer.toString(cnt++);
            Attributes a = new Attributes();
            a.add("type", "radio").addDefaultToEmpty("name", params.get("name")).addIfTrue("checked", checked)
                    .addIfExists("value", itemKeyStr).addIfTrue("disabled", params.get("disabled"))
                    .addIfExists("tabindex", params.get("tabindex")).addIfExists("id", id);
            super.start("input", a);
            super.end("input");

            //Label section
            a = new Attributes();
            a.addIfExists("for", id).addIfExists("class", params.get("cssClass"))
                    .addIfExists("style", params.get("cssStyle")).addIfExists("title", params.get("title"));
            super.start("label", a);
            if (StringUtils.isNotEmpty(itemValueStr)) {
                characters(itemValueStr);
            }
            super.end("label");
            stack.pop();
        }
    }
}

From source file:org.apache.struts2.views.java.simple.SelectHandler.java

public void generate() throws IOException {
    Map<String, Object> params = context.getParameters();
    Attributes a = new Attributes();

    Object value = params.get("nameValue");

    a.addDefaultToEmpty("name", params.get("name")).addIfExists("size", params.get("size"))
            .addIfExists("value", value).addIfTrue("disabled", params.get("disabled"))
            .addIfTrue("readonly", params.get("readonly")).addIfTrue("multiple", params.get("multiple"))
            .addIfExists("tabindex", params.get("tabindex")).addIfExists("id", params.get("id"))
            .addIfExists("class", params.get("cssClass")).addIfExists("style", params.get("cssStyle"))
            .addIfExists("title", params.get("title"));
    super.start("select", a);

    //options/*from  w  w  w .  j  a  v a  2 s . c  om*/

    //header
    String headerKey = (String) params.get("headerKey");
    String headerValue = (String) params.get("headerValue");
    if (headerKey != null && headerValue != null) {
        boolean selected = ContainUtil.contains(value, params.get("headerKey"));
        writeOption(headerKey, headerValue, selected);
    }

    //emptyoption
    Object emptyOption = params.get("emptyOption");
    if (emptyOption != null && emptyOption.toString().equals(Boolean.toString(true))) {
        boolean selected = ContainUtil.contains(value, "") || ContainUtil.contains(value, null);
        writeOption("", "", selected);
    }

    Object listObj = params.get("list");
    String listKey = (String) params.get("listKey");
    String listValue = (String) params.get("listValue");
    ValueStack stack = this.context.getStack();
    if (listObj != null) {
        Iterator itt = MakeIterator.convert(listObj);
        while (itt.hasNext()) {
            Object item = itt.next();
            stack.push(item);

            //key
            Object itemKey = findValue(listKey != null ? listKey : "top");
            String itemKeyStr = StringUtils.defaultString(itemKey == null ? null : itemKey.toString());
            //value
            Object itemValue = findValue(listValue != null ? listValue : "top");
            String itemValueStr = StringUtils.defaultString(itemValue == null ? null : itemValue.toString());

            boolean selected = ContainUtil.contains(value, itemKey);
            writeOption(itemKeyStr, itemValueStr, selected);

            stack.pop();
        }
    }

    //opt group
    List<ListUIBean> listUIBeans = (List<ListUIBean>) params
            .get(OptGroup.INTERNAL_LIST_UI_BEAN_LIST_PARAMETER_KEY);
    if (listUIBeans != null) {
        for (ListUIBean listUIBean : listUIBeans) {
            writeOptionGroup(listUIBean, value);
        }
    }

    super.end("select");
}

From source file:org.apache.struts2.views.java.simple.SelectHandler.java

private void writeOptionGroup(ListUIBean listUIBean, Object value) throws IOException {
    Map params = listUIBean.getParameters();
    Attributes attrs = new Attributes();
    attrs.addIfExists("label", params.get("label")).addIfTrue("disabled", params.get("disabled"));
    start("optgroup", attrs);

    //options/*from  ww  w.j ava 2 s  .  c o  m*/
    ValueStack stack = context.getStack();
    Object listObj = params.get("list");
    if (listObj != null) {
        Iterator itt = MakeIterator.convert(listObj);
        String listKey = (String) params.get("listKey");
        String listValue = (String) params.get("listValue");
        while (itt.hasNext()) {
            Object optGroupBean = itt.next();
            stack.push(optGroupBean);

            Object tmpKey = stack.findValue(listKey != null ? listKey : "top");
            String tmpKeyStr = StringUtils.defaultString(tmpKey.toString());
            Object tmpValue = stack.findValue(listValue != null ? listValue : "top");
            String tmpValueStr = StringUtils.defaultString(tmpValue.toString());
            boolean selected = ContainUtil.contains(value, tmpKeyStr);
            writeOption(tmpKeyStr, tmpValueStr, selected);

            stack.pop();
        }
    }

    end("optgroup");
}

From source file:org.apache.struts2.views.java.XHTMLTagSerializer.java

public void characters(String text, boolean encode) throws IOException {
    writer.write(encode ? StringUtils.defaultString(StringEscapeUtils.escapeHtml4(text)) : text);
}

From source file:org.biokoframework.system.command.authentication.RequestPasswordResetCommand.java

@Override
public Fields execute(Fields input) throws CommandException {
    logInput(input);//  w  w w  .ja  v  a2  s . co m

    Repository<Login> loginRepo = getRepository(Login.class);

    String userEmail = input.get(Login.USER_EMAIL);
    ensureLoginExists(loginRepo, userEmail);

    Template mailTemplate = retrieveTemplateOrFailTrying();

    Map<String, Object> contentMap = new HashMap<>();
    contentMap.put("url", StringUtils.defaultString(fLandingPageUrl));

    fPasswordResetService.requestPasswordReset(userEmail, mailTemplate, contentMap);

    logOutput();
    return new Fields(GenericFieldNames.RESPONSE, new ArrayList<DomainEntity>());
}

From source file:org.blocks4j.reconf.client.constructors.SimpleConstructor.java

public Object construct(MethodData data) throws Throwable {
    Class<?> returnClass = (Class<?>) data.getReturnType();

    String trimmed = StringUtils.defaultString(StringUtils.trim(data.getValue()));
    if (!trimmed.startsWith("'") || !trimmed.endsWith("'")) {
        throw new RuntimeException(msg.format("error.invalid.string", data.getValue(), data.getMethod()));
    }//from   w  w w.  jav  a  2  s .c  o m

    String wholeValue = StringUtils.substring(trimmed, 1, trimmed.length() - 1);

    if (String.class.equals(returnClass)) {
        return wholeValue;
    }

    if (null == data.getValue()) {
        return null;
    }

    if (Object.class.equals(returnClass)) {
        returnClass = String.class;
    }

    if (char.class.equals(data.getReturnType()) || Character.class.equals(data.getReturnType())) {
        if (StringUtils.length(wholeValue) == 1) {
            return CharUtils.toChar(wholeValue);
        }
        return CharUtils.toChar(StringUtils.replace(wholeValue, " ", ""));
    }

    if (primitiveBoxing.containsKey(returnClass)) {
        if (StringUtils.isBlank(wholeValue)) {
            return null;
        }
        Method parser = primitiveBoxing.get(returnClass).getMethod(
                "parse" + StringUtils.capitalize(returnClass.getSimpleName()), new Class<?>[] { String.class });
        return parser.invoke(primitiveBoxing.get(returnClass), new Object[] { StringUtils.trim(wholeValue) });
    }

    Method valueOf = null;
    try {
        valueOf = returnClass.getMethod("valueOf", new Class<?>[] { String.class });
    } catch (NoSuchMethodException ignored) {
    }

    if (valueOf == null) {
        try {

            valueOf = returnClass.getMethod("valueOf", new Class<?>[] { Object.class });
        } catch (NoSuchMethodException ignored) {
        }
    }

    if (null != valueOf) {
        if (StringUtils.isEmpty(wholeValue)) {
            return null;
        }
        return valueOf.invoke(data.getReturnType(), new Object[] { StringUtils.trim(wholeValue) });
    }

    Constructor<?> constructor = null;

    try {
        constructor = returnClass.getConstructor(String.class);
        constructor.setAccessible(true);

    } catch (NoSuchMethodException ignored) {
        throw new IllegalStateException(
                msg.format("error.string.constructor", returnClass.getSimpleName(), data.getMethod()));
    }

    try {
        return constructor.newInstance(wholeValue);

    } catch (Exception e) {
        if (e.getCause() != null && e.getCause() instanceof NumberFormatException) {
            return constructor.newInstance(StringUtils.trim(wholeValue));
        }
        throw e;
    }
}