@Override public String toString() { return Strings.concat(cause, ": ", message); } }
@Override public String toString() { return Strings.toString(name, value); }
/** * Escape XML reserved chars and write as element text content. Used by text and numbering operators to actually write the * element text content. At the moment this method is invoked templates engine was already wrote start tag, including * closing tag mark, e.g. <em><div attrs... ></em>. * * @param text element text content. * @throws IOException if underlying writer fails to write. */ void writeTextContent(String text) throws IOException { writer.write((String) Strings.escapeXML(text)); }
@Override public boolean hasCssClass(String classToMatch) { String classes = node.getAttribute(ATTR_CLASS); if (classes.isEmpty()) { return false; } Pattern pattern = Pattern.compile(Strings.concat(LEADING_SPACE_REX, Strings.escapeRegExp(classToMatch), TRAILING_SPACE_REX)); Matcher matcher = pattern.matcher(classes); return matcher.find(); }
/** * Create new illegal argument exception with joined list of arguments. * * @param args arguments are joined and used as exception message. */ public IllegalArgumentException(Object[] args) { super(Strings.join(args)); } }
/** * Store the name for currently working field. Given <code>fieldName</code> is not checked for existence but just stored * into {@link #fieldName}. * * @param fieldName current working field name. */ public final void setFieldName(String fieldName) { this.fieldName = Strings.toMemberName(fieldName); }
protected CssClass(Element element) { super(ATTR_CLASS); classNames = Strings.split(element.getAttr(ATTR_CLASS)); if(this.classNames == null) { this.classNames = new ArrayList<String>(); } }
/** * Variant for {@link #invokeSetter(Object, String, String)} but no exception if setter not found. * * @param object object instance, * @param name setter name, * @param value value to set. * @throws Exception if invocation fail for whatever reason including method logic. */ public static void invokeOptionalSetter(Object object, String name, String value) throws Exception { String setterName = Strings.getMethodAccessor("set", name); Class<?> clazz = object.getClass(); Method method = null; try { method = findMethod(clazz, setterName); } catch(NoSuchMethodException e) { log.debug("Setter |%s| not found in class |%s| or its super hierarchy.", setterName, clazz); return; } Class<?>[] parameterTypes = method.getParameterTypes(); if(parameterTypes.length != 1) { log.debug("Setter |%s#%s(%s)| with invalid parameters number.", method.getDeclaringClass(), method.getName(), Strings.join(parameterTypes, ',')); return; } invoke(object, method, ConverterRegistry.getConverter().asObject((String)value, parameterTypes[0])); }
/** * Convert words separated by dash, underscore, space, slash and backslash to title case. Title case follows English * convention: uses space for separator and every word begin with upper case. Returns null or empty string if given * <code>string</code> parameter is null, respective empty. * * @param string string containing words to convert. * @return <code>string</code> converted to title case, null or empty. */ public static String toTitleCase(String string) { if(string == null) { return null; } if(string.isEmpty()) { return ""; } List<String> words = Strings.split(string, '-', '_', ' ', '/', '\\'); StringBuilder title = new StringBuilder(toCamelCase(words.get(0))); for(int i = 1; i < words.size(); ++i) { title.append(' '); title.append(toCamelCase(words.get(i))); } return title.toString(); }
/** * Load string from UTF-8 file content. * * @param file source file, * @param maxCount optional maximum character count to load, default to entire file. * @return loaded string, possible empty but never null. * @throws IOException if file not found or file read operation fails. */ public static String load(File file, Integer... maxCount) throws IOException { return load(new FileReader(file), maxCount); }
/** * Test if parameter is of requested type and throw exception if not. * * @param parameter invocation parameter, * @param typeToMatch type to match, * @param name the name of invocation parameter. * @throws IllegalArgumentException if parameter is not of requested type. */ public static void isKindOf(Type parameter, Type typeToMatch, String name) { if (!Types.isKindOf(parameter, typeToMatch)) { throw new IllegalArgumentException(Strings.format("%s is not %s.", name, typeToMatch)); } } }
/** * Return object property value using content getter. By convention accessor name is <code>get</code> concatenated with * title case property name. If content has not such method throws content exception. * * @param object content scope object, * @param property the name of property to retrieve. * @return property value. * @throws TemplateException if property getter not found. */ private Object getContentObject(Object object, String property) throws TemplateException { String getterName = Strings.getMethodAccessor("get", property); try { Method getter = this.getClass().getDeclaredMethod(getterName, object.getClass()); getter.setAccessible(true); return getter.invoke(this, object); } catch (NoSuchMethodException expectedMissingGetter) { throw new TemplateException("Missing property |%s| from object |%s|.", property, object.getClass()); } catch (Exception unexpected) { throw new BugError(unexpected); } }
/** * Join array of objects, converted to string, using space as separator. Returns null if given objects array is null * and empty if empty. Null objects or empty strings from given <code>objects</code> parameter are ignored. * * @param objects array of objects to join. * @return joined string. */ public static String join(Object[] objects) { return join(objects, " "); }
/** * Return named form field or null if there is none with requested name. Given name supports both dashed case or Java member * name formats. * * @param name form field name. * @return named form field or null if not found. */ private FormField getFormField(String name) { // key of the form fields map is normalized to Java member name return fields.get(Strings.toMemberName(name)); } }
@Override public Element removeCssClass(String classToRemove) { String classes = node.getAttribute(ATTR_CLASS); if (classes.isEmpty()) { return this; } Pattern pattern = Pattern.compile(Strings.concat(LEADING_SPACE_REX, Strings.escapeRegExp(classToRemove), TRAILING_SPACE_REX)); Matcher matcher = pattern.matcher(classes); node.setAttribute(ATTR_CLASS, matcher.replaceFirst(" ").trim()); return this; }
/** * Set value to object field identified by object property path. Form this class perspective an object is a graph of value * types. A value type is a primitive value or a related boxing class. Also any class that can be converted to a primitive * are included; for example {@link File} or {@link URL} are value types since can be converted to/from strings. Opposite to * value types are compound entities, that is, objects, arrays and collections that aggregates value types or other compound * entities. * <p> * To sum up, an object is a graph with compound entities and value types as nodes where value types are leafs. The * <code>propertyPath</code> is the path through graph nodes till reach the value type and basically is a dot separated * field names list. * * @param propertyPath object property path, * @param value value to set. * @throws ConverterException if value is primitive and fail to convert to field type. * @throws IllegalAccessException this exception is required by reflective field signature but never thrown. */ private void setValue(String propertyPath, Object value) throws ConverterException, IllegalAccessException { List<String> nodeIDs = Strings.split(propertyPath, '.'); int lastNodeIndex = nodeIDs.size() - 1; Node node = new ObjectNode(object); for (int index = 0; index < lastNodeIndex; index++) { node = node.getChild(nodeIDs.get(index)); if (node == null) { return; } } node.setValue(nodeIDs.get(lastNodeIndex), value); }
/** * Load string from UTF-8 bytes stream then closes it. * * @param inputStream source input stream, * @param maxCount optional maximum number of characters to read, default to MAX_VALUE. * @return string from input stream. * @throws NullPointerException if input stream is null. * @throws IOException if input stream read operation fails. */ public static String load(InputStream inputStream, Integer... maxCount) throws IOException { return load(new InputStreamReader(inputStream, "UTF-8"), maxCount); }
/** * Delete file if exists throwing exception if delete fails. Note that this method does nothing if file does not * exist; anyway, null parameter sanity check is still performed. * * @param file file path. * @throws IOException if delete operation fails. * @throws IllegalArgumentException if <code>file</code> parameter is null. */ public static void delete(File file) throws IOException { Params.notNull(file, "File"); if(file.exists() && !file.delete()) { throw new IOException(Strings.format("Fail to delete file |%s|.", file)); } }
/** * Invoke setter method on given object instance. Setter name has format as accepted by * {@link Strings#getMethodAccessor(String, String)} and value string is converted to method parameter type using * {@link Converter} facility. For this reason set parameter type should have a converter registered. * <p> * This method has no means to determine method using {@link Class#getMethod(String, Class...)} because parameter * value is always string and setter parameter type is unknown. For this reason this method uses * {@link #findMethod(Class, String)}. Note that find method searches for named method on object super hierarchy too. * * @param object object instance, * @param name setter name, method name only without <code>set</code> prefix, dashed case accepted, * @param value value to set, string value that is converted to setter method parameter type. * @throws NoSuchMethodException if setter not found. * @throws Exception if invocation fail for whatever reason including method logic. */ public static void invokeSetter(Object object, String name, String value) throws NoSuchMethodException, Exception { String setterName = Strings.getMethodAccessor("set", name); Class<?> clazz = object.getClass(); Method method = findMethod(clazz, setterName); Class<?>[] parameterTypes = method.getParameterTypes(); if(parameterTypes.length != 1) { throw new NoSuchMethodException(String.format("%s#%s", clazz.getName(), setterName)); } invoke(object, method, ConverterRegistry.getConverter().asObject((String)value, parameterTypes[0])); }
/** * Set the host address for client connected to this event stream. * * @param remoteHost client remote host address. */ protected void setRemoteHost(String remoteHost) { if (string == null) { string = Strings.concat('#', STREAM_ID++, ':', remoteHost); } }