public static AttributesImpl addAttribute(String name, String value, AttributesImpl atts) { if (!StringUtil.isEmpty(value)) atts.addAttribute("", "", name, "CDATA", value); return atts; }
protected static String getOptionalAttribute(String name, Element element) { return StringUtil.emptyToNull(element.getAttribute(name)); }
@Override public String convert(String sourceValue) throws ConversionException { return StringUtil.escape(sourceValue); }
@Override public String convert(String sourceValue) { return StringUtil.normalizeName(StringUtil.normalizeSpace(sourceValue)); } }
private static String normalizeSQL(String sql) { sql = StringUtil.normalizeSpace(sql.trim().toLowerCase()); while (sql.contains("/*")) sql = StringUtil.removeSection(sql, "/*", "*/").trim(); return sql; }
/** * Maps the locale code to a locale, e.g. de_DE to Locale.GERMANY. * @param code the locale colde to map * @return a locale instance the represents the code */ public static Locale getLocale(String code) { if (StringUtil.isEmpty(code)) throw new IllegalArgumentException("code is empty"); String[] path = StringUtil.tokenize(code, '_'); switch (path.length) { case 1 : return new Locale(path[0]); case 2 : return new Locale(path[0], path[1]); case 3 : return new Locale(path[0], path[1], path[2]); default : return new Locale(path[0], path[1], ArrayFormat.formatPart("_", 2, path.length - 2, path)); } }
char padChar = getPadChar(); switch (getAlignment()) { case LEFT : tmp = StringUtil.trimRight(tmp, padChar); break; case RIGHT : boolean neg = (padChar == '0' && tmp.length() > 0 && tmp.charAt(0) == '-'); if (neg) tmp = '-' + StringUtil.trimLeft(tmp.substring(1), padChar); else tmp = StringUtil.trimLeft(tmp, padChar); break; case CENTER : tmp = StringUtil.trim(tmp, padChar); break; default : throw new IllegalArgumentException("Illegal Alignement: " + getAlignment()); } else { result = tmp; if (StringUtil.isEmpty(source)) pos.setIndex(1); else
@Override public Timestamp convert(String sourceValue) throws ConversionException { if (StringUtil.isEmpty(sourceValue)) return null; // separate the String into Date and nano parts sourceValue = sourceValue.trim(); String datePart; String nanoPart; if (sourceValue.contains(".")) { String[] parts = StringUtil.splitOnFirstSeparator(sourceValue, '.'); datePart = parts[0]; nanoPart = parts[1]; } else { datePart = sourceValue; nanoPart = null; } // calculate date part Date date = realConverter.convert(datePart); Timestamp result = new Timestamp(date.getTime()); // calculate nano part if (!StringUtil.isEmpty(nanoPart)) { nanoPart = StringUtil.padRight(nanoPart, 9, '0'); int nanos = Integer.parseInt(nanoPart); result.setNanos(nanos); } return result; }
public static void setProperty(String key, String value, Element element, Document document) { if (!StringUtil.isEmpty(key)) { String[] prefixAndRemainingPath = StringUtil.splitOnFirstSeparator(key, '.'); String childName = prefixAndRemainingPath[0]; Element child = getChildElement(element, false, false, childName); if (child == null) { child = document.createElement(childName); element.appendChild(child); } setProperty(prefixAndRemainingPath[1], value, child, document); } else { element.setTextContent(value); } }
private static void checkForSyntaxErrors(String text, String type, org.databene.script.antlr.DatabeneScriptParser parser, ParserRuleReturnScope r) { if (parser.getNumberOfSyntaxErrors() > 0) throw new SyntaxError("Illegal " + type, text, -1, -1); CommonToken stop = (CommonToken) r.stop; if (stop.getStopIndex() < StringUtil.trimRight(text).length() - 1) { if (stop.getStopIndex() == 0) throw new SyntaxError("Syntax error after " + stop.getText(), text); else throw new SyntaxError("Unspecific syntax error", text); } }
public static void setProperty(String key, String value, Document document) { String[] prefixAndRemainingPath = StringUtil.splitOnFirstSeparator(key, '.'); Element rootElement = document.getDocumentElement(); if (rootElement == null) { rootElement = document.createElement(prefixAndRemainingPath[0]); document.appendChild(rootElement); } else if (!key.equals(rootElement.getNodeName())) throw new IllegalArgumentException("Cannot set a property '" + key + "' on a document with root <" + rootElement.getNodeName() + ">"); setProperty(prefixAndRemainingPath[1], value, rootElement, document); }
@Override public String[] convert(String sourceValue) throws ConversionException { return StringUtil.tokenize(sourceValue, separator); } }
/** * Trims all String in the array. * @param array an array of the Strings to trim * @return the same array but with its elements trimmed * @since 0.2.05 */ public static String[] trimAll(String[] array) { for (int i = 0; i < array.length; i++) array[i] = trim(array[i]); return array; }
@Override public String convert(String sourceValue) throws ConversionException { return StringUtil.normalizeSpace(sourceValue); }
public static String normalizeSpace(String s) { if (s == null || s.length() == 0) return s; s = trim(s); if (s.length() == 0) return s; char lastChar = s.charAt(0); StringBuilder result = new StringBuilder().append(lastChar); for (int i = 1; i < s.length(); i++) { char c = s.charAt(i); if (!isWhitespace(c)) result.append(c); else if (!(isWhitespace(lastChar))) result.append(' '); lastChar = c; } return result.toString(); }
private static String postProcess(String base, boolean upperCase, String pattern, int length) { if (upperCase) base = base.toUpperCase(); if (length > 0) base = StringUtil.padLeft(base, length, '0'); if (pattern != null) base = MessageFormat.format(pattern, base); return base; }
public static String[] splitAndTrim(String list, char separator) { return StringUtil.trimAll(list.split(String.valueOf(separator))); }
/** * Determines a locale's parent, e.g. for a locale 'de_DE' it returns 'de'. * @param locale the locale of which to determine the parent * @return the locale's parent, or null if the locale has no parent. */ public static Locale parent(Locale locale) { String variant = locale.getVariant(); if (!StringUtil.isEmpty(variant)){ if (variant.contains("_")) { String[] variantPath = StringUtil.tokenize(variant, '_'); variant = ArrayFormat.formatPart("_", 0, variantPath.length - 1, variantPath); return new Locale(locale.getLanguage(), locale.getCountry(), variant); } else return new Locale(locale.getLanguage(), locale.getCountry()); } else if (!StringUtil.isEmpty(locale.getCountry())) return new Locale(locale.getLanguage()); else return null; }
private static void checkForSyntaxErrors(String text, String type, SQLParser parser, ParserRuleReturnScope r) { if (parser.getNumberOfSyntaxErrors() > 0) throw new SyntaxError("Illegal " + type, text, -1, -1); CommonToken stop = (CommonToken) r.stop; if (text != null && stop.getStopIndex() < StringUtil.trimRight(text).length() - 1) { if (stop.getStopIndex() == 0) throw new SyntaxError("Syntax error after " + stop.getText(), text); else throw new SyntaxError("Syntax error at the beginning ", text); } }
public ScriptDescriptor(String text) { if (text != null && text.startsWith("{") && text.endsWith("}")) { text = text.substring(1, text.length() - 1); String[] tokens = StringUtil.splitOnFirstSeparator(text, ':'); if (tokens.length > 1 && ScriptUtil.getFactory(tokens[0], false) != null) { this.scriptEngine = tokens[0]; this.text = tokens[1]; } else { this.scriptEngine = ScriptUtil.getDefaultScriptEngine(); this.text = text; } this.level = ScriptLevel.SCRIPT; } else { this.scriptEngine = null; this.level = ScriptLevel.NONE; this.text = text; } }