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

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

Introduction

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

Prototype

public static boolean startsWith(String str, String prefix) 

Source Link

Document

Check if a String starts with a specified prefix.

Usage

From source file:net.ymate.platform.core.util.RuntimeUtils.java

/**
 * @param origin //  w w  w .  java 2  s  .c  om
 * @return ?${root}?${user.dir}${user.home}??
 */
public static String replaceEnvVariable(String origin) {
    if ((origin = StringUtils.trimToNull(origin)) != null) {
        String _defaultPath = getRootPath();
        if (StringUtils.startsWith(origin, "${root}")) {
            origin = ExpressionUtils.bind(origin).set("root", _defaultPath).getResult();
        } else if (StringUtils.startsWith(origin, "${user.dir}")) {
            origin = ExpressionUtils.bind(origin).set("user.dir", System.getProperty("user.dir", _defaultPath))
                    .getResult();
        } else if (StringUtils.startsWith(origin, "${user.home}")) {
            origin = ExpressionUtils.bind(origin)
                    .set("user.home", System.getProperty("user.home", _defaultPath)).getResult();
        }
    }
    return origin;
}

From source file:net.ymate.platform.mvc.web.support.HttpRequestExecutor.java

protected IView processMethodResultToView(Object result) throws Exception {
    IView _view = null;/*  w w  w . java  2  s  .  com*/
    if (result == null) {
        _view = new JspView();
    } else if (result instanceof String) {
        String _viewStr = StringUtils.trimToEmpty((String) result).toLowerCase();
        if (StringUtils.startsWith(_viewStr, IWebView.VIEW_REDIRECT)) {
            _view = new RedirectView(StringUtils.substringAfter(_viewStr, IWebView.VIEW_REDIRECT));
        } else if (StringUtils.startsWith(_viewStr, IWebView.VIEW_FORWARD)) {
            _view = new ForwardView(StringUtils.substringAfter(_viewStr, IWebView.VIEW_FORWARD));
        } else if (StringUtils.startsWith(_viewStr, IWebView.VIEW_HTTP_STATUS)) {
            String[] _statusContent = StringUtils
                    .split(StringUtils.substringAfter(_viewStr, IWebView.VIEW_HTTP_STATUS), ",");
            _view = new HttpStatusView(Integer.parseInt(_statusContent[0]),
                    _statusContent.length >= 2 ? _statusContent[1] : null);
        } else if (StringUtils.startsWith(_viewStr, IWebView.VIEW_JSON)) {
            _view = new JsonView(StringUtils.substringAfter(_viewStr, IWebView.VIEW_JSON));
        } else if (StringUtils.startsWith(_viewStr, IWebView.VIEW_JSP)) {
            _view = new JspView(StringUtils.substringAfter(_viewStr, IWebView.VIEW_JSP));
        } else if (StringUtils.startsWith(_viewStr, IWebView.VIEW_FTL)) {
            _view = new FreeMarkerView(StringUtils.substringAfter(_viewStr, IWebView.VIEW_FTL));
        } else if (StringUtils.startsWith(_viewStr, IWebView.VIEW_INLINE_FILE)) {
            _view = BinaryView.loadFromFile(StringUtils.substringAfter(_viewStr, IWebView.VIEW_INLINE_FILE),
                    false);
        } else if (StringUtils.startsWith(_viewStr, IWebView.VIEW_FILE)) {
            _view = BinaryView.loadFromFile(StringUtils.substringAfter(_viewStr, IWebView.VIEW_FILE), true);
        } else {
            _view = new TextView(_viewStr);
        }
    } else if (result instanceof IView) {
        _view = (IView) result;
    }
    return _view;
}

From source file:net.ymate.platform.mvc.web.support.RequestMappingParser.java

/**
 * @param partStr ?/*from w w w . j av  a 2  s. c  o  m*/
 * @return '/'
 */
protected String fixMappingPart(String partStr) {
    partStr = StringUtils.trimToEmpty(partStr);
    if (StringUtils.startsWith(partStr, "/")) {
        partStr = StringUtils.substringAfter(partStr, "/");
    }
    if (StringUtils.endsWith(partStr, "/")) {
        partStr = StringUtils.substringBeforeLast(partStr, "/");
    }
    return partStr;
}

From source file:net.ymate.platform.persistence.jdbc.impl.DefaultModuleCfg.java

/**
 * @param dsName      ????//from   w w  w.ja v a2 s .  c o  m
 * @param _moduleCfgs ???
 * @return ?????
 * @throws Exception ?
 */
@SuppressWarnings("unchecked")
protected DataSourceCfgMeta __doParserDataSourceCfgMeta(String dsName, Map<String, String> _moduleCfgs)
        throws Exception {
    Map<String, String> _dataSourceCfgs = new HashMap<String, String>();
    for (Map.Entry<String, String> _cfgEntry : _moduleCfgs.entrySet()) {
        String _key = _cfgEntry.getKey();
        String _prefix = "ds." + dsName + ".";
        if (StringUtils.startsWith(_key, _prefix)) {
            String _cfgKey = StringUtils.substring(_key, _prefix.length());
            _dataSourceCfgs.put(_cfgKey, _cfgEntry.getValue());
        }
    }
    if (!_dataSourceCfgs.isEmpty()) {
        //
        DataSourceCfgMeta _meta = new DataSourceCfgMeta();
        _meta.setName(dsName);
        _meta.setConnectionUrl(_dataSourceCfgs.get("connection_url"));
        _meta.setUsername(_dataSourceCfgs.get("username"));
        // ??
        if (StringUtils.isNotBlank(_meta.getConnectionUrl()) && StringUtils.isNotBlank(_meta.getUsername())) {
            // ?
            _meta.setIsShowSQL(new BlurObject(_dataSourceCfgs.get("show_sql")).toBooleanValue());
            _meta.setTablePrefix(_dataSourceCfgs.get("table_prefix"));
            // ???
            String _adapterClassName = JDBC.DS_ADAPTERS
                    .get(StringUtils.defaultIfBlank(_dataSourceCfgs.get("adapter_class"), "default"));
            _meta.setAdapterClass((Class<? extends IDataSourceAdapter>) ClassUtils.loadClass(_adapterClassName,
                    this.getClass()));
            //
            // ?
            try {
                _meta.setType(JDBC.DATABASE
                        .valueOf(StringUtils.defaultIfBlank(_dataSourceCfgs.get("type"), "").toUpperCase()));
            } catch (IllegalArgumentException e) {
                // ??
                String _connUrl = URI.create(_meta.getConnectionUrl()).toString();
                String[] _type = StringUtils.split(_connUrl, ":");
                if (_type != null && _type.length > 0) {
                    if (_type[1].equals("microsoft")) {
                        _type[1] = "sqlserver";
                    }
                    _meta.setType(JDBC.DATABASE.valueOf(_type[1].toUpperCase()));
                }
            }
            //
            _meta.setDialectClass(_dataSourceCfgs.get("dialect_class"));
            _meta.setDriverClass(StringUtils.defaultIfBlank(_dataSourceCfgs.get("driver_class"),
                    JDBC.DB_DRIVERS.get(_meta.getType())));
            _meta.setPassword(_dataSourceCfgs.get("password"));
            _meta.setIsPasswordEncrypted(
                    new BlurObject(_dataSourceCfgs.get("password_encrypted")).toBooleanValue());
            //
            if (_meta.isPasswordEncrypted() && StringUtils.isNotBlank(_meta.getPassword())
                    && StringUtils.isNotBlank(_dataSourceCfgs.get("password_class"))) {
                _meta.setPasswordClass((Class<? extends IPasswordProcessor>) ClassUtils
                        .loadClass(_dataSourceCfgs.get("password_class"), this.getClass()));
            }
            //
            return _meta;
        }
    }
    return null;
}

From source file:net.ymate.platform.persistence.mongodb.impl.MongoModuleCfg.java

@SuppressWarnings("unchecked")
protected MongoDataSourceCfgMeta __doParserDataSourceCfgMeta(String dsName, Map<String, String> _moduleCfgs)
        throws Exception {
    MongoDataSourceCfgMeta _meta = null;
    ////from  w ww .  ja  v a2  s  .c  om
    Map<String, String> _dataSourceCfgs = new HashMap<String, String>();
    for (Map.Entry<String, String> _cfgEntry : _moduleCfgs.entrySet()) {
        String _key = _cfgEntry.getKey();
        String _prefix = "ds." + dsName + ".";
        if (StringUtils.startsWith(_key, _prefix)) {
            String _cfgKey = StringUtils.substring(_key, _prefix.length());
            _dataSourceCfgs.put(_cfgKey, _cfgEntry.getValue());
        }
    }
    //
    if (!_dataSourceCfgs.isEmpty()) {
        String _connectionUrl = StringUtils.trimToNull(_dataSourceCfgs.get("connection_url"));
        if (_connectionUrl != null) {
            _meta = new MongoDataSourceCfgMeta(dsName, _dataSourceCfgs.get("collection_prefix"), _connectionUrl,
                    _dataSourceCfgs.get("database_name"));
        } else {
            List<ServerAddress> _servers = new ArrayList<ServerAddress>();
            String[] _serversArr = StringUtils.split(_dataSourceCfgs.get("servers"), "|");
            if (_serversArr != null) {
                for (String _serverStr : _serversArr) {
                    String[] _server = StringUtils.split(_serverStr, ":");
                    if (_server.length > 1) {
                        _servers.add(new ServerAddress(_server[0], Integer.parseInt(_server[1])));
                    } else {
                        _servers.add(new ServerAddress(_server[0]));
                    }
                }
            }
            //
            boolean _isPwdEncrypted = new BlurObject(_dataSourceCfgs.get("password_encrypted"))
                    .toBooleanValue();
            Class<? extends IPasswordProcessor> _passwordClass = null;
            if (_isPwdEncrypted) {
                _passwordClass = (Class<? extends IPasswordProcessor>) ClassUtils
                        .loadClass(_dataSourceCfgs.get("password_class"), this.getClass());
            }
            _meta = new MongoDataSourceCfgMeta(dsName, _dataSourceCfgs.get("collection_prefix"), _servers,
                    _dataSourceCfgs.get("username"), _dataSourceCfgs.get("password"),
                    _dataSourceCfgs.get("database_name"), _isPwdEncrypted, _passwordClass);
        }
    }
    return _meta;
}

From source file:net.ymate.platform.persistence.redis.impl.RedisModuleCfg.java

private Map<String, String> __doGetCfgs(Map<String, String> _moduleCfgs, String keyPrefix) {
    Map<String, String> _returnValues = new HashMap<String, String>();
    for (Map.Entry<String, String> _cfgEntry : _moduleCfgs.entrySet()) {
        String _key = _cfgEntry.getKey();
        if (StringUtils.startsWith(_key, keyPrefix)) {
            String _cfgKey = StringUtils.substring(_key, keyPrefix.length());
            _returnValues.put(_cfgKey, _cfgEntry.getValue());
        }//from  w  ww. j  av a 2 s  .c o m
    }
    return _returnValues;
}

From source file:net.ymate.platform.webmvc.support.RequestMappingParser.java

/**
 * @param partStr ?/*from ww  w  .  ja va 2s  . c  om*/
 * @return '/'
 */
private String __doFixMappingPart(String partStr) {
    partStr = StringUtils.trimToEmpty(partStr);
    if (StringUtils.startsWith(partStr, "/")) {
        partStr = StringUtils.substringAfter(partStr, "/");
    }
    if (StringUtils.endsWith(partStr, "/")) {
        partStr = StringUtils.substringBeforeLast(partStr, "/");
    }
    return partStr;
}

From source file:nl.edia.sakai.tool.skinmanager.impl.SkinServiceImpl.java

@Override
public List<String> fetchAvailableSkinNames() throws SkinException, IOException {
    // Maybe a bit fanatic, but at least check if the user is supposed to be here
    checkAction(org.sakaiproject.site.api.SiteService.SITE_VISIT);
    List<SkinDirectory> installedSkins = skinFileSystemService.fetchInstalledSkins();
    List<String> rv = new ArrayList<String>(installedSkins.size());
    String skinPrefix = portalService.getSkinPrefix();
    for (SkinDirectory dir : installedSkins) {
        String name = dir.getName();
        if (StringUtils.isEmpty(skinPrefix) || StringUtils.startsWith(name, skinPrefix)) {
            rv.add(name);/*from   ww w  .j  av a  2 s.c  o m*/
        }
    }
    return rv;
}

From source file:nl.knaw.huygens.timbuctoo.search.FullTextSearchFieldFinder.java

@Override
protected void addField(Set<String> fields, IndexAnnotation indexAnnotation) {
    String fieldName = indexAnnotation.fieldName();
    if (StringUtils.startsWith(fieldName, FULL_TEXT_SEARCH_PREFIX)) {
        fields.add(fieldName);//from  w  ww  . j a  v  a2  s.  c  om
    }
}

From source file:nl.knaw.huygens.timbuctoo.storage.mongo.MongoStorage.java

@Override
public <T extends DomainEntity> void deleteVariation(Class<T> type, String id, Change change)
        throws StorageException {
    String variationToDelete = TypeNames.getInternalName(type);
    if (TypeRegistry.isPrimitiveDomainEntity(type)) {
        throwTypeIsAPrimitiveException(type);
    }// w  ww. j a va2s .co  m

    DBObject query = queries.selectById(id);
    JsonNode tree = getExisting(type, query);

    if (tree.isObject()) {
        ObjectNode objectTree = (ObjectNode) tree;

        if (!doesVariationExist(variationToDelete, objectTree)) {
            throw new NoSuchEntityException(type, id);
        }

        List<String> fieldsToDelete = Lists.newArrayList();
        for (Iterator<String> fieldNames = objectTree.fieldNames(); fieldNames.hasNext();) {
            String fieldName = fieldNames.next();
            if (StringUtils.startsWith(fieldName, variationToDelete)) {
                fieldsToDelete.add(fieldName);
            }
        }
        objectTree.remove(fieldsToDelete);

    }

    DomainEntity entity = reducer.reduceVariation(toBaseDomainEntity(type), tree);
    int revision = entity.getRev();

    entity.setRev(revision + 1);
    entity.setModified(change);
    entity.setPid(null);
    entity.getVariations().remove(variationToDelete);

    inducer.adminDomainEntity(entity, (ObjectNode) tree);

    mongoDB.update(getDBCollection(type), query, toDBObject(tree));
}