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

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

Introduction

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

Prototype

public static String substringBefore(String str, String separator) 

Source Link

Document

Gets the substring before the first occurrence of a separator.

Usage

From source file:net.umpay.mailbill.hql.orm.hibernate.HibernateDao.java

/**
 * countHql./*from  w w  w. j a v a2s . c om*/
 * 
 * ???hql?,??hql?count?.
 */
protected long countHqlResult(final String hql, final Map<String, ?> values) {
    String fromHql = hql;
    //select??order by???count,?.
    fromHql = "from " + StringUtils.substringAfter(fromHql, "from");
    fromHql = StringUtils.substringBefore(fromHql, "order by");

    String countHql = "select count(*) " + fromHql.replaceAll("fetch", "");

    try {
        Long count = findUnique(countHql, values);
        return count;
    } catch (Exception e) {
        throw new RuntimeException("hql can't be auto count, hql is:" + countHql, e);
    }
}

From source file:net.ymate.module.webproxy.support.DispatchProxyFilter.java

private boolean __doMatchBlacklist(String requestMapping) {
    if (!__blacklist.isEmpty()) {
        if (__blacklist.contains(requestMapping)) {
            return true;
        } else {/*from  ww  w . j  a  v a2s  . c o m*/
            for (String _mapping : __blacklist) {
                if (StringUtils.endsWith(_mapping, "*")) {
                    if (StringUtils.startsWith(requestMapping, StringUtils.substringBefore(_mapping, "*"))) {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}

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

/**
 * @param safe WEB??WEB-INF//www.  j  a  va2 s .co  m
 * @return 
 */
public static String getRootPath(boolean safe) {
    URL _rootURL = RuntimeUtils.class.getClassLoader().getResource("/");
    boolean _isWeb = false;
    if (_rootURL == null) {
        _rootURL = RuntimeUtils.class.getClassLoader().getResource("");
    } else {
        _isWeb = true;
    }
    String _rootPath = _isWeb ? StringUtils.substringBefore(_rootURL.getPath(), safe ? "classes/" : "WEB-INF/")
            : _rootURL.getPath();
    if (isWindows()) {
        if (_rootPath.startsWith("/")) {
            _rootPath = _rootPath.substring(1);
        }
    }
    return _rootPath;
}

From source file:net.ymate.platform.core.beans.impl.DefaultBeanLoader.java

private boolean __doCheckExculedFile(String targetFileName) {
    if (__excludedFileSet != null && !__excludedFileSet.isEmpty() && StringUtils.isNotBlank(targetFileName)) {
        if (__excludedFileSet.contains(targetFileName)) {
            return true;
        } else {/*from   w  w w. j  a  v  a  2s.  c  o  m*/
            for (String _exculedFile : __excludedFileSet) {
                if (_exculedFile.indexOf('*') > 0)
                    _exculedFile = StringUtils.substringBefore(_exculedFile, "*");
                if (targetFileName.startsWith(_exculedFile)) {
                    return true;
                }
            }
        }
    }
    return false;
}

From source file:net.ymate.platform.core.beans.impl.DefaultBeanLoader.java

private List<Class<?>> __doFindClassByZip(URL zipUrl, IBeanFilter filter) throws Exception {
    List<Class<?>> _returnValue = new ArrayList<Class<?>>();
    ZipInputStream _zipStream = null;
    try {/*from   ww  w .  j a v a2s.co  m*/
        String _zipFilePath = zipUrl.toString();
        if (_zipFilePath.indexOf('!') > 0) {
            _zipFilePath = StringUtils.substringBetween(zipUrl.toString(), "zip:", "!");
        } else {
            _zipFilePath = StringUtils.substringAfter(zipUrl.toString(), "zip:");
        }
        File _zipFile = new File(_zipFilePath);
        if (!__doCheckExculedFile(_zipFile.getName())) {
            _zipStream = new ZipInputStream(new FileInputStream(_zipFile));
            ZipEntry _zipEntry = null;
            while (null != (_zipEntry = _zipStream.getNextEntry())) {
                if (!_zipEntry.isDirectory()) {
                    if (_zipEntry.getName().endsWith(".class") && _zipEntry.getName().indexOf('$') < 0) {
                        String _className = StringUtils.substringBefore(_zipEntry.getName().replace("/", "."),
                                ".class");
                        __doAddClass(_returnValue, __doLoadClass(_className), filter);
                    }
                }
                _zipStream.closeEntry();
            }
        }
    } finally {
        if (_zipStream != null) {
            try {
                _zipStream.close();
            } catch (IOException ignored) {
            }
        }
    }
    return _returnValue;
}

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

/**
 * @param safe WEB??WEB-INF/*from w  w  w  . j  a  v a  2s . co  m*/
 * @return 
 */
public static String getRootPath(boolean safe) {
    //
    String _rootPath = null;
    //
    URL _rootURL = RuntimeUtils.class.getClassLoader().getResource("/");
    if (_rootURL == null) {
        _rootURL = RuntimeUtils.class.getClassLoader().getResource("");
        if (_rootURL != null) {
            _rootPath = _rootURL.getPath();
        }
    } else {
        _rootPath = StringUtils.removeEnd(
                StringUtils.substringBefore(_rootURL.getPath(), safe ? "classes/" : "WEB-INF/"), "/");
    }
    //
    if (_rootPath != null) {
        _rootPath = StringUtils.replace(_rootPath, "%20", " ");
        if (isWindows()) {
            _rootPath = StringUtils.removeStart(_rootPath, "/");
        }
    }
    return StringUtils.trimToEmpty(_rootPath);
}

From source file:net.ymate.platform.module.wechat.WeChat.java

/**
 * ?JSAPI?/* w  w  w. java 2  s . co m*/
 *
 * @param accountId ??ID
 * @param url       JSAPI?URL?
 * @return wx.config
 * @throws Exception
 */
public static JSONObject wxCreateJsApiConfig(String accountId, String url) throws Exception {
    String _jsapiTicket = __dataProvider.getJsApiTicket(accountId);
    String _timestamp = DateTimeUtils.currentTimeMillisUTC() + "";
    String _noncestr = UUIDUtils.uuid();
    //
    StringBuilder _signSB = new StringBuilder().append("jsapi_ticket=").append(_jsapiTicket).append("&")
            .append("noncestr=").append(_noncestr).append("&").append("timestamp=").append(_timestamp)
            .append("&").append("url=").append(StringUtils.substringBefore(url, "#"));
    //
    JSONObject _json = new JSONObject();
    _json.put("jsapi_ticket", _jsapiTicket);
    _json.put("timestamp", _timestamp);
    _json.put("nonceStr", _noncestr);
    _json.put("url", url);
    _json.put("signature", DigestUtils.shaHex(_signSB.toString()));
    _json.put("appId", __dataProvider.getAppId(accountId));
    return _json;
}

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

/**
 * @param context /*from w  ww . j a v a2  s  .  com*/
 * @param mappingSet ???'$'?
 * @return ?????????WebContextPathVariable?
 */
public String doParser(IRequestContext context, Set<String> mappingSet) {
    String _requestMapping = this.fixMappingPart(context.getRequestMapping());
    // ??PairObject<Mapping[??], ??>
    Set<PairObject<String[], Integer>> _filteredMapping = new HashSet<PairObject<String[], Integer>>();
    for (String _key : mappingSet) {
        if (_key.contains("{")) {
            String _mappingKey = StringUtils.substringBefore(_key, "{");
            String _fixedMappingKey = this.fixMappingPart(_mappingKey);
            if (StringUtils.startsWithIgnoreCase(_requestMapping, _fixedMappingKey)) {
                // ?????
                String _paramKey = _key.substring(_key.indexOf("{"));
                _filteredMapping.add(new PairObject<String[], Integer>(
                        new String[] { _key, _mappingKey, _fixedMappingKey, _paramKey },
                        StringUtils.split(_paramKey, "/").length));
            }
        }
    }
    // ?????
    PairObject<String[], Integer> _result = null;
    String _mappingParamPart = null;
    for (PairObject<String[], Integer> _item : _filteredMapping) {
        // ?????
        _mappingParamPart = StringUtils.substringAfter(_requestMapping, _item.getKey()[2]);
        // ???
        int _paramPartCount = StringUtils.split(_mappingParamPart, "/").length;
        // ?????
        if (_paramPartCount == _item.getValue()) {
            _result = _item;
            break;
        }
    }
    if (_result != null) {
        Map<String, String> _params = this.parserMappingParams(_mappingParamPart, _result.getKey()[3]);
        if (WebContext.getContext() != null) {
            // ???WebContextPathVariable?
            for (String _key : _params.keySet()) {
                WebContext.getContext().put(_key, _params.get(_key));
            }
        }
        return _result.getKey()[0];
    }
    return null;
}

From source file:net.ymate.platform.mvc.web.view.impl.BinaryView.java

/**
 * ?Range????/* w  w  w . j  a v a 2  s . co  m*/
 *
 * @param maxLength ??
 * @return ?null
 */
private PairObject<Long, Long> __doParseRange(long maxLength) {
    PairObject<Long, Long> _returnValue = null;
    // Range??
    String _rangeStr = WebContext.getRequest().getHeader("Range");
    if (_rangeStr != null && _rangeStr.startsWith("bytes=") && _rangeStr.length() >= 7) {
        _rangeStr = StringUtils.substringAfter(_rangeStr, "bytes=");
        String[] _ranges = StringUtils.split(_rangeStr, ",");
        // ?Range??...
        for (String _range : _ranges) {
            if (StringUtils.isBlank(_range)) {
                return null;
            }
            try {
                // bytes=-100
                if (_range.startsWith("-")) {
                    long _end = Long.parseLong(_range);
                    long _start = maxLength + _end;
                    if (_start < 0) {
                        return null;
                    }
                    _returnValue = new PairObject<Long, Long>(_start, maxLength);
                    break;
                }
                // bytes=1024-
                if (_range.endsWith("-")) {
                    long _start = Long.parseLong(StringUtils.substringBefore(_range, "-"));
                    if (_start < 0) {
                        return null;
                    }
                    _returnValue = new PairObject<Long, Long>(_start, maxLength);
                    break;
                }
                // bytes=10-1024
                if (_range.contains("-")) {
                    String[] _tmp = _range.split("-");
                    long _start = Long.parseLong(_tmp[0]);
                    long _end = Long.parseLong(_tmp[1]);
                    if (_start > _end) {
                        return null;
                    }
                    _returnValue = new PairObject<Long, Long>(_start, _end + 1);
                }
            } catch (Throwable e) {
                return null;
            }
        }
    }
    return _returnValue;
}

From source file:net.ymate.platform.persistence.jdbc.scaffold.EntityGenerator.java

/**
 * ??//from  w  w  w .  j  av a2  s  .  c om
 */
public void createEntityClassFiles() {
    Map<String, Object> _propMap = buildPropMap();
    //
    boolean _isUseBaseEntity = BlurObject.bind(__owner.getConfig().getParam("jdbc.use_base_entity"))
            .toBooleanValue();
    boolean _isUseClassSuffix = BlurObject.bind(__owner.getConfig().getParam("jdbc.use_class_suffix"))
            .toBooleanValue();
    boolean _isUseChainMode = BlurObject.bind(__owner.getConfig().getParam("jdbc.use_chain_mode"))
            .toBooleanValue();
    boolean _isUseStateSupport = BlurObject.bind(__owner.getConfig().getParam("jdbc.use_state_support"))
            .toBooleanValue();
    _propMap.put("isUseBaseEntity", _isUseBaseEntity);
    _propMap.put("isUseClassSuffix", _isUseClassSuffix);
    _propMap.put("isUseChainMode", _isUseChainMode);
    _propMap.put("isUseStateSupport", _isUseStateSupport);
    if (_isUseBaseEntity) {
        buildTargetFile("/model/BaseEntity.java", "/BaseEntity.ftl", _propMap);
    }
    //
    List<String> _tableList = Arrays.asList(StringUtils
            .split(StringUtils.defaultIfBlank(__owner.getConfig().getParam("jdbc.table_list"), ""), "|"));
    if (_tableList.isEmpty()) {
        _tableList = getTableNames();
    }
    //
    String _dbName = __owner.getConfig().getParam("jdbc.db_name");
    String _dbUser = __owner.getConfig().getParam("jdbc.db_username");
    String[] _prefixs = StringUtils
            .split(StringUtils.defaultIfBlank(__owner.getConfig().getParam("jdbc.table_prefix"), ""), '|');
    boolean _isRemovePrefix = new BlurObject(__owner.getConfig().getParam("jdbc.remove_table_prefix"))
            .toBooleanValue();
    List<String> _tableExcludeList = Arrays.asList(StringUtils.split(StringUtils
            .defaultIfBlank(__owner.getConfig().getParam("jdbc.table_exclude_list"), "").toLowerCase(), "|"));
    for (String _tableName : _tableList) {
        // ???
        if (!_tableExcludeList.isEmpty()) {
            if (_tableExcludeList.contains(_tableName.toLowerCase())) {
                continue;
            } else {
                boolean _flag = false;
                for (String _excludedName : _tableExcludeList) {
                    if (StringUtils.contains(_excludedName, "*") && StringUtils.startsWithIgnoreCase(_tableName,
                            StringUtils.substringBefore(_excludedName, "*"))) {
                        _flag = true;
                        break;
                    }
                }
                if (_flag) {
                    continue;
                }
            }
        }
        TableMeta _tableMeta = getTableMeta(_dbName, _dbUser, _tableName);
        if (_tableMeta != null) {
            String _modelName = null;
            for (String _prefix : _prefixs) {
                if (_tableName.startsWith(_prefix)) {
                    if (_isRemovePrefix) {
                        _tableName = _tableName.substring(_prefix.length());
                    }
                    _modelName = StringUtils.capitalize(EntityMeta.propertyNameToFieldName(_tableName));
                    break;
                }
            }
            if (StringUtils.isBlank(_modelName)) {
                _modelName = StringUtils.capitalize(EntityMeta.propertyNameToFieldName(_tableName));
            }
            //
            _propMap.put("tableName", _tableName);
            _propMap.put("modelName", _modelName);
            List<Attr> _fieldList = new ArrayList<Attr>(); // 
            List<Attr> _fieldListForNotNullable = new ArrayList<Attr>(); // ?
            List<Attr> _allFieldList = new ArrayList<Attr>(); // ????
            if (_tableMeta.getPkSet().size() > 1) {
                _propMap.put("primaryKeyType", _modelName + "PK");
                _propMap.put("primaryKeyName",
                        StringUtils.uncapitalize((String) _propMap.get("primaryKeyType")));
                List<Attr> _primaryKeyList = new ArrayList<Attr>();
                _propMap.put("primaryKeyList", _primaryKeyList);
                Attr _pkAttr = new Attr((String) _propMap.get("primaryKeyType"),
                        (String) _propMap.get("primaryKeyName"), null, false, false, 0, 0, 0, null, null);
                _fieldList.add(_pkAttr);
                _fieldListForNotNullable.add(_pkAttr);
                //
                for (String pkey : _tableMeta.getPkSet()) {
                    ColumnInfo _ci = _tableMeta.getFieldMap().get(pkey);
                    _primaryKeyList.add(_ci.toAttr());
                    _allFieldList.add(new Attr("String", _ci.getColumnName().toUpperCase(), _ci.getColumnName(),
                            _ci.isAutoIncrement(), _ci.isSigned(), _ci.getPrecision(), _ci.getScale(),
                            _ci.getNullable(), _ci.getDefaultValue(), _ci.getRemarks()));
                }
                for (String key : _tableMeta.getFieldMap().keySet()) {
                    if (_tableMeta.getPkSet().contains(key)) {
                        continue;
                    }
                    ColumnInfo _ci = _tableMeta.getFieldMap().get(key);
                    Attr _attr = _ci.toAttr();
                    _fieldList.add(_attr);
                    _fieldListForNotNullable.add(_attr);
                    _allFieldList.add(new Attr("String", _ci.getColumnName().toUpperCase(), _ci.getColumnName(),
                            _ci.isAutoIncrement(), _ci.isSigned(), _ci.getPrecision(), _ci.getScale(),
                            _ci.getNullable(), _ci.getDefaultValue(), _ci.getRemarks()));
                }
            } else {
                _propMap.put("primaryKeyType",
                        _tableMeta.getFieldMap().get(_tableMeta.getPkSet().get(0)).getColumnType());
                _propMap.put("primaryKeyName", StringUtils
                        .uncapitalize(EntityMeta.propertyNameToFieldName(_tableMeta.getPkSet().get(0))));
                for (String key : _tableMeta.getFieldMap().keySet()) {
                    ColumnInfo _ci = _tableMeta.getFieldMap().get(key);
                    Attr _attr = _ci.toAttr();
                    _fieldList.add(_attr);
                    if (_attr.getNullable() == 0) {
                        _fieldListForNotNullable.add(_attr);
                    }
                    _allFieldList.add(new Attr("String", _ci.getColumnName().toUpperCase(), _ci.getColumnName(),
                            _ci.isAutoIncrement(), _ci.isSigned(), _ci.getPrecision(), _ci.getScale(),
                            _ci.getNullable(), _ci.getDefaultValue(), _ci.getRemarks()));
                }
            }
            _propMap.put("fieldList", _fieldList);
            // ??????
            _propMap.put("notNullableFieldList",
                    _fieldList.size() == _fieldListForNotNullable.size() ? Collections.emptyList()
                            : _fieldListForNotNullable);
            _propMap.put("allFieldList", _allFieldList);
            //
            buildTargetFile("/model/" + _modelName + (_isUseClassSuffix ? "Model.java" : ".java"),
                    "/Entity.ftl", _propMap);
            //
            if (_tableMeta.getPkSet().size() > 1) {
                _propMap.put("modelName", _modelName);
                if (_tableMeta.getPkSet().size() > 1) {
                    List<Attr> _primaryKeyList = new ArrayList<Attr>();
                    _propMap.put("primaryKeyList", _primaryKeyList);
                    //
                    for (String pkey : _tableMeta.getPkSet()) {
                        ColumnInfo _ci = _tableMeta.getFieldMap().get(pkey);
                        _primaryKeyList.add(_ci.toAttr());
                    }
                }
                buildTargetFile("/model/" + _modelName + "PK.java", "/EntityPK.ftl", _propMap);
            }
        }
    }
}