/** * Decodes locale code in string array that can be used for <code>Locale</code> constructor. */ public static String[] decodeLocaleCode(String localeCode) { String[] result = new String[3]; String[] data = StringUtil.splitc(localeCode, '_'); result[0] = data[0]; result[1] = result[2] = StringPool.EMPTY; if (data.length >= 2) { result[1] = data[1]; if (data.length >= 3) { result[2] = data[2]; } } return result; }
/** * Resolves active profiles from special property. * This property can be only a base property! * If default active property is not defined, nothing happens. * Otherwise, it will replace currently active profiles. */ protected void resolveActiveProfiles() { if (activeProfilesProp == null) { activeProfiles = null; return; } final PropsEntry pv = data.getBaseProperty(activeProfilesProp); if (pv == null) { // no active profile set as the property, exit return; } final String value = pv.getValue(); if (StringUtil.isBlank(value)) { activeProfiles = null; return; } activeProfiles = StringUtil.splitc(value, ','); StringUtil.trimAll(activeProfiles); }
/** * Determines if string array contains empty strings. * @see #isEmpty(CharSequence) */ public static boolean isAllEmpty(String... strings) { for (String string : strings) { if (!isEmpty(string)) { return false; } } return true; }
/** * Removes starting and ending single or double quotes. */ public static String removeQuotes(String string) { if ( (startsWithChar(string, '\'') && endsWithChar(string, '\'')) || (startsWithChar(string, '"') && endsWithChar(string, '"')) ) { return substring(string, 1, -1); } return string; }
public DbEntityColumnDescriptor( final DbEntityDescriptor ded, final String columnName, final String fieldName, final Class fieldType, final boolean isId, final Class<? extends SqlType> sqlTypeClass) { this.dbEntityDescriptor = ded; this.propertyName = fieldName; this.propertyType = fieldType; this.isId = isId; this.sqlTypeClass = sqlTypeClass; this.columnNameForQuery = columnName; if (StringUtil.detectQuoteChar(columnNameForQuery) != 0) { this.columnName = StringUtil.substring(columnNameForQuery, 1, -1); } else { this.columnName = columnNameForQuery; } }
/** * Prepares classname for loading, respecting the arrays. * Returns <code>null</code> if class name is not an array. */ public static String prepareArrayClassnameForLoading(String className) { int bracketCount = StringUtil.count(className, '['); if (bracketCount == 0) { // not an array return null; } String brackets = StringUtil.repeat('[', bracketCount); int bracketIndex = className.indexOf('['); className = className.substring(0, bracketIndex); int primitiveNdx = getPrimitiveClassNameIndex(className); if (primitiveNdx >= 0) { className = String.valueOf(PRIMITIVE_BYTECODE_NAME[primitiveNdx]); return brackets + className; } else { return brackets + 'L' + className + ';'; } }
@Test void testEmpty() { assertFalse(StringUtil.isBlank("foo")); assertTrue(StringUtil.isNotBlank("foo")); assertTrue(StringUtil.isBlank("")); assertFalse(StringUtil.isNotBlank("")); assertTrue(StringUtil.isBlank(" ")); assertFalse(StringUtil.isNotBlank(" ")); assertTrue(StringUtil.isBlank(" \t \t")); assertFalse(StringUtil.isNotBlank(" \t \t")); assertTrue(StringUtil.isBlank(null)); assertFalse(StringUtil.isNotBlank(null)); assertFalse(StringUtil.isEmpty("foo")); assertTrue(StringUtil.isNotEmpty("foo")); assertTrue(StringUtil.isEmpty("")); assertFalse(StringUtil.isNotEmpty("")); assertFalse(StringUtil.isEmpty(" ")); assertTrue(StringUtil.isNotEmpty(" ")); assertFalse(StringUtil.isEmpty(" \t \t")); assertTrue(StringUtil.isNotEmpty(" \t \t")); assertTrue(StringUtil.isEmpty(null)); assertFalse(StringUtil.isNotEmpty(null)); assertTrue(StringUtil.isAllEmpty("", null)); assertFalse(StringUtil.isAllEmpty("", null, "a")); assertTrue(StringUtil.isAllBlank("", " ", "\t", "\r", null)); assertFalse(StringUtil.isAllBlank("", " ", "\t", "\ra", null)); }
/** * Determines if string array contains just blank strings. */ public static boolean isAllBlank(String... strings) { for (String string : strings) { if (!isBlank(string)) { return false; } } return true; }
public static HttpRequest readFrom(final InputStream in, final String encoding) { BufferedReader reader; try { reader = new BufferedReader(new InputStreamReader(in, encoding)); } catch (UnsupportedEncodingException uneex) { return null; } final HttpRequest httpRequest = new HttpRequest(); httpRequest.headers.clear(); final String line; try { line = reader.readLine(); } catch (IOException ioex) { throw new HttpException(ioex); } if (!StringUtil.isBlank(line)) { String[] s = StringUtil.splitc(line, ' '); httpRequest.method(s[0]); httpRequest.path(s[1]); httpRequest.httpVersion(s[2]); httpRequest.readHeaders(reader); httpRequest.readBody(reader); } return httpRequest; }
/** * Resolves tx scope from scope pattern. */ public String resolveScope(final Class type, final String methodName) { if (scopePattern == null) { return null; } String ctx = scopePattern; ctx = StringUtil.replace(ctx, JTXCTX_PATTERN_CLASS, type.getName()); ctx = StringUtil.replace(ctx, JTXCTX_PATTERN_METHOD, methodName); return ctx; }
@Test void testStartWith() { assertTrue(StringUtil.startsWithChar("asd", 'a')); assertFalse(StringUtil.startsWithChar("asd", 's')); assertFalse(StringUtil.startsWithChar("", 'a')); assertTrue(StringUtil.endsWithChar("asd", 'd')); assertFalse(StringUtil.endsWithChar("asd", 's')); assertFalse(StringUtil.endsWithChar("", 'd')); assertEquals(3, StringUtil.startsWithOne("qwe123", "Qwe", null, ".", "qwe")); assertEquals(-1, StringUtil.startsWithOne("qwe123", "Qwe", null, ".", "we")); assertEquals(0, StringUtil.startsWithOneIgnoreCase("qwe123", "Qwe", null, ".", "qwe")); assertEquals(-1, StringUtil.startsWithOneIgnoreCase("qwe123", "we", null, ".", "we")); assertEquals(3, StringUtil.endsWithOne("qwezxc", "Zxc", null, ".", "zxc")); assertEquals(-1, StringUtil.endsWithOne("qwezxc", "Zxc", null, ".", "zx")); assertEquals(0, StringUtil.endsWithOneIgnoreCase("qweZXC", "Zxc", null, ".", "zxc")); assertEquals(-1, StringUtil.endsWithOneIgnoreCase("qweZXC", "zx", null, ".", "zx")); }
/** * Resolves subpath in safer way. For some reason, if child starts with * a separator it gets resolved as a full path, ignoring the base. * This method acts different. */ public static Path resolve(Path base, String child) { if (StringUtil.startsWithChar(child, File.separatorChar)) { child = child.substring(1); } return base.resolve(child); }
public static boolean validate(final Object value) { if (value == null) { return true; } return StringUtil.isNotBlank(value.toString()); } }
@Override public void setDynamicAttribute(final String uri, final String localName, final Object value) { params.add(new String[] {localName, StringUtil.toSafeString(value)}); }
/** * Appends method name to existing path. */ protected void append(final String methodName) { if (path.length() != 0) { path += StringPool.DOT; } if (methodName.startsWith(StringPool.LEFT_SQ_BRACKET)) { path = StringUtil.substring(path, 0, -1); } path += methodName; }
/** * Reads action path from class. If the class is annotated with {@link MadvocAction} annotation, * class action path will be read from annotation value. Otherwise, action class path will be built from the * class name. This is done by removing the package name and the last contained word * (if there is more then one) from the class name. Such name is finally uncapitalized. */ protected String[] readClassActionPath(final Class actionClass) { // read class annotation MadvocAction madvocActionAnnotation = ((Class<?>)actionClass).getAnnotation(MadvocAction.class); String classActionPath = madvocActionAnnotation != null ? madvocActionAnnotation.value().trim() : null; if (StringUtil.isEmpty(classActionPath)) { classActionPath = null; } String actionClassName = actionClass.getSimpleName(); actionClassName = StringUtil.uncapitalize(actionClassName); actionClassName = MadvocUtil.stripLastCamelWord(actionClassName); // removes 'Action' from the class name if (classActionPath == null) { classActionPath = actionClassName; } return ArraysUtil.array(actionClassName, classActionPath); }
if (!StringUtil.endsWithChar(path, '/')) { path += '/'; boolean noRelativePath = StringUtil.isEmpty(path);
protected String strip(String string) { string = StringUtil.removeChars(string, " \r\n\t"); string = StringUtil.replace(string, ">", ">\n"); return string; }
/** * Converts all tabs on a line to spaces according to the provided tab width. * This is not a simple tab to spaces replacement, since the resulting * indentation remains the same. */ public static String convertTabsToSpaces(String line, int tabWidth) { int tab_index, tab_size; int last_tab_index = 0; int added_chars = 0; if (tabWidth == 0) { return remove(line, '\t'); } StringBuilder result = new StringBuilder(); while ((tab_index = line.indexOf('\t', last_tab_index)) != -1) { tab_size = tabWidth - ((tab_index + added_chars) % tabWidth); if (tab_size == 0) { tab_size = tabWidth; } added_chars += tab_size - 1; result.append(line.substring(last_tab_index, tab_index)); result.append(repeat(' ', tab_size)); last_tab_index = tab_index+1; } if (last_tab_index == 0) { return line; } result.append(line.substring(last_tab_index)); return result.toString(); }