Example usage for org.apache.commons.lang StringUtils split

List of usage examples for org.apache.commons.lang StringUtils split

Introduction

In this page you can find the example usage for org.apache.commons.lang StringUtils split.

Prototype

public static String[] split(String str, String separatorChars, int max) 

Source Link

Document

Splits the provided text into an array with a maximum length, separators specified.

Usage

From source file:io.apiman.gateway.vertx.config.RouteMapper.java

/**
 * Simplistic & fast mapping of first element of path, avoiding any regex for now.
 * For instance /gateway/a/b/c => gateway
 */// www . j a v a2  s .  c  om
protected String firstPathElem(String path) {
    return StringUtils.split(path, "/", 2)[0]; //$NON-NLS-1$
}

From source file:com.opengamma.component.factory.web.FreemarkerConfigurationComponentFactory.java

static TemplateLoader[] createLoaders(String[] locations, ServletContext servletContext) {
    Collection<TemplateLoader> templateLoaders = new ArrayList<TemplateLoader>();
    for (String location : locations) {
        String[] prefixAndBase = StringUtils.split(location, ":", 2);
        if (prefixAndBase.length != 2) {
            throw new OpenGammaRuntimeException("Invalid Freemarker template location: " + location);
        }/*from w  w w. j av a  2 s .  c  om*/
        String prefix = prefixAndBase[0].trim();
        String base = prefixAndBase[1].trim();
        if (SERVLET_CONTEXT.equals(prefix)) {
            templateLoaders.add(new WebappTemplateLoader(servletContext, base));
        } else if (FILE.equals(prefix)) {
            try {
                templateLoaders.add(new FileTemplateLoader(new File(base)));
            } catch (IOException e) {
                throw new OpenGammaRuntimeException("Unable to load Freemarker templates from " + base, e);
            }
        } else {
            throw new OpenGammaRuntimeException("Invalid Freemarker template location: " + location);
        }
    }
    return templateLoaders.toArray(new TemplateLoader[templateLoaders.size()]);
}

From source file:edu.cornell.med.icb.ip.IpUtils.java

/**
 * Attempt to validate an IP address or hostname with optional comment.
 * This can be in the formation of "address_or_hostname # comment".
 * Whitespace (space or tab) can padd the valiues anywhere within
 * the value, except within the ip address / hostname iteself. If specifying
 * an ip address must be four values seperated by ".", each value must be 0 - 255...
 * this is not exactly true, see the documentation for java.net.InetAddress
 * for actual parsing rules of ip addresses.
 * @param toValidate the ip address and/or hostname (with optional comment) to try to validate
 * @return an IpAddress object containing the InetAddress and any comment.
 * If the line didn't parse UnknownHostname exception will be thrown. If the line
 * contains ONLY a comment IpAddress.ipAddress will be null but comment will be
 * filled in. If the line is blank (or empty) an IpAddress will be returned
 * with IpAddress.ipAddress as null and an empty String comment.
 * @throws UnknownHostException error parsing the ip address / hostname part of the string
 *//*w  ww. j av a  2s  . c o  m*/
public static IpAddress validateIpAddress(final String toValidate) throws UnknownHostException {
    final String trimmedIpAddress;
    if (StringUtils.isBlank(toValidate)) {
        // No IP, no comment. Blank-ish line
        return new IpAddress(null, "");
    } else {
        trimmedIpAddress = toValidate.trim();
    }

    String comment = "";
    // parse out any comments
    if (trimmedIpAddress.charAt(0) == '#') {
        // Comment only line
        return new IpAddress(null, trimmedIpAddress.substring(1).trim());
    }

    final String[] ipAndCommentParts = StringUtils.split(trimmedIpAddress, "#", 2);
    final String ipNoComment = ipAndCommentParts[0].trim();
    if (ipAndCommentParts.length == 2) {
        // Both IP AND comment
        comment = ipAndCommentParts[1].trim();
    }

    final InetAddress ipAddress = InetAddress.getByName(ipNoComment);
    return new IpAddress(ipAddress, comment);
}

From source file:com.adobe.acs.commons.util.ParameterUtil.java

/**
 * Util for parsing Service properties in the form {@code <value><separator><value>}
 *
 * @param value     must be in the format => {@code x<separator>y}  ... ex. {@code foo:bar}
 * @param separator separator between the values
 * @param isValueOptional if {@code false} returns {@code null} in case there is not at least one separator found (not at the last position) 
 * @return Returns a SimpleEntry representing the key/value pair. The value may be {@null} in case no separator is found and {@code isValueOptional} is {@code true}.
 *//* w w  w.j  a  v a  2  s  .c  om*/
private static AbstractMap.SimpleEntry<String, String> toSimpleEntry(final String value, final String separator,
        boolean isValueOptional) {
    final String[] tmp = StringUtils.split(value, separator, 2);

    if (tmp == null) {
        return null;
    }

    if (tmp.length == 2) {
        return new AbstractMap.SimpleEntry<String, String>(tmp[0], tmp[1]);
    } else {
        if (isValueOptional && tmp.length == 1) {
            return new AbstractMap.SimpleEntry<String, String>(tmp[0], null);
        }
        return null;
    }
}

From source file:com.opengamma.id.UniqueIdSchemeDelegator.java

/**
 * Chooses the delegate for a specific identifier scheme.
 * /*from w  w w .  jav a  2 s .c o m*/
 * @param scheme  the identifier scheme, not null
 * @return the delegate, not null
 */
public T chooseDelegate(final String scheme) {
    ArgumentChecker.notNull(scheme, "scheme");
    String[] schemeParts = StringUtils.split(scheme, "-", 2);
    String schemePrefix = schemeParts[0];
    final T delegate = _schemeToDelegateMap.get(schemePrefix);
    return (delegate != null) ? delegate : _defaultDelegate;
}

From source file:com.flexive.faces.components.input.FxValueInputValidator.java

/**
 * {@inheritDoc}//from w  w w  . j  a  v  a 2 s  .c  o  m
 */
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
    if (!enabled) {
        return;
    }
    AbstractFxValueInput input = (AbstractFxValueInput) component;
    if (input.isReadOnly()) {
        return; // nothing to validate
    }
    FxValue fxValue = (FxValue) value;
    if (!fxValue.isValid()) {
        final String label;
        if (StringUtils.isNotEmpty(fxValue.getXPath())) {
            label = CacheAdmin.getEnvironment().getAssignment(fxValue.getXPath()).getDisplayName();
        } else {
            label = "?";
        }
        FxFacesMsgErr message = new FxFacesMsgValidationErr(fxValue, "FxValueInput.err.invalid", label,
                fxValue.getErrorValue());
        String clientId = input.getExternalId() == -1 ? input.getClientId(context)
                : String.valueOf(input.getExternalId());
        if (!StringUtils.isEmpty(clientId)) {
            if (clientId.indexOf(':') > 0) {
                String[] cid = StringUtils.split(clientId, ":", 2);
                // TODO: this doesn't really work in most JSF2 applications, where every composite component
                // is a naming container
                message.setForm(cid[0]);
                message.setId(cid[1]);
            } else {
                message.setId(clientId);
            }
        }
        message.setLocalizedDetail("FxValueInput.err.invalid.detail", fxValue.getErrorValue());
        throw new ValidatorException(message);
    }
    // check default translation
    if (input.isRequired() && !fxValue.translationExists(fxValue.getDefaultLanguage())) {
        final FxFacesMsgErr message = new FxFacesMsgErr("FxValueInput.err.emptyDefaultLanguage");
        message.setLocalizedDetail("FxValueInput.err.emptyDefaultLanguage");
        throw new ValidatorException(message);
    }
}