Java Code Examples for org.apache.commons.lang.StringUtils.defaultString()

The following are Jave code examples for showing how to use defaultString() of the org.apache.commons.lang.StringUtils class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: gitea-plugin   File: GiteaServers.java   Source Code and License Vote up 5 votes
/**
 * Fix a serverUrl.
 *
 * @param serverUrl the server URL.
 * @return the normalized server URL.
 */
@NonNull
public static String normalizeServerUrl(@CheckForNull String serverUrl) {
    serverUrl = StringUtils.defaultString(serverUrl);
    try {
        URI uri = new URI(serverUrl).normalize();
        String scheme = uri.getScheme();
        if ("http".equals(scheme) || "https".equals(scheme)) {
            // we only expect http / https, but also these are the only ones where we know the authority
            // is server based, i.e. [[email protected]]server[:port]
            // DNS names must be US-ASCII and are case insensitive, so we force all to lowercase

            String host = uri.getHost() == null ? null : uri.getHost().toLowerCase(Locale.ENGLISH);
            int port = uri.getPort();
            if ("http".equals(scheme) && port == 80) {
                port = -1;
            } else if ("https".equals(scheme) && port == 443) {
                port = -1;
            }
            serverUrl = new URI(
                    scheme,
                    uri.getUserInfo(),
                    host,
                    port,
                    uri.getPath(),
                    uri.getQuery(),
                    uri.getFragment()
            ).toASCIIString();
        }
    } catch (URISyntaxException e) {
        // ignore, this was a best effort tidy-up
    }
    return serverUrl.replaceAll("/$", "");
}
 
Example 2
Project: morf   File: XmlDataSetProducer.java   Source Code and License Vote up 4 votes
/**
 * Creates the column and buffers its meta data.
 */
public PullProcessorColumn() {
  super();
  columnName = xmlStreamReader.getAttributeValue(XmlDataSetNode.URI, XmlDataSetNode.NAME_ATTRIBUTE);
  dataType = DataType.valueOf(xmlStreamReader.getAttributeValue(XmlDataSetNode.URI, XmlDataSetNode.TYPE_ATTRIBUTE));
  defaultValue = StringUtils.defaultString(xmlStreamReader.getAttributeValue(XmlDataSetNode.URI, XmlDataSetNode.DEFAULT_ATTRIBUTE));

  try {
    // not all datatypes need a width
    if (dataType.hasWidth()) {
      // The use of null indicates that although a scale should exist none
      // was provided.
      String widthString = xmlStreamReader.getAttributeValue(XmlDataSetNode.URI, XmlDataSetNode.WIDTH_ATTRIBUTE);
      width = StringUtils.isEmpty(widthString) ? null : Integer.valueOf(widthString);
    } else {
      width = 0;
    }

    // not all datatypes need a scale
    if (dataType.hasScale()) {
      // The use of null indicates that although a scale should exist none
      // was provided.
      String scaleString = xmlStreamReader.getAttributeValue(XmlDataSetNode.URI, XmlDataSetNode.SCALE_ATTRIBUTE);
      scale = StringUtils.isEmpty(scaleString) ? null : Integer.valueOf(scaleString);
    } else {
      scale = 0;
    }

    String nullableString = xmlStreamReader.getAttributeValue(XmlDataSetNode.URI, XmlDataSetNode.NULLABLE_ATTRIBUTE);
    nullable = StringUtils.isEmpty(nullableString) ? null : Boolean.valueOf(nullableString);

    String primaryKeyString = xmlStreamReader.getAttributeValue(XmlDataSetNode.URI, XmlDataSetNode.PRIMARYKEY_ATTRIBUTE);
    primaryKey = StringUtils.isEmpty(primaryKeyString) ? null : Boolean.valueOf(primaryKeyString);

    String autoNumString = xmlStreamReader.getAttributeValue(XmlDataSetNode.URI, XmlDataSetNode.AUTONUMBER_ATTRIBUTE);
    autonumbered = StringUtils.isNotEmpty(autoNumString);
    autonumberStart = autonumbered ? Integer.valueOf(autoNumString) : null;

  } catch (NumberFormatException nfe) {
    throw new RuntimeException("Error parsing metadata for column [" + columnName + "]", nfe);
  }

}
 
Example 3
Project: morf   File: ColumnBean.java   Source Code and License Vote up 4 votes
/**
 * @see org.alfasoftware.morf.metadata.Column#getDefaultValue()
 */
@Override
public String getDefaultValue() {
  return StringUtils.defaultString(defaultValue);
}
 
Example 4
Project: lams   File: ExceptionUtils.java   Source Code and License Vote up 3 votes
/**
 * Gets a short message summarising the exception.
 * <p>
 * The message returned is of the form
 * {ClassNameWithoutPackage}: {ThrowableMessage}
 *
 * @param th  the throwable to get a message for, null returns empty string
 * @return the message, non-null
 * @since Commons Lang 2.2
 */
public static String getMessage(Throwable th) {
    if (th == null) {
        return "";
    }
    String clsName = ClassUtils.getShortClassName(th, null);
    String msg = th.getMessage();
    return clsName + ": " + StringUtils.defaultString(msg);
}