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); }
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; } }
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]) != 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; } }
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); } }
public static Object getPropertyGraph(String path, Object bean) { String[] tokens = StringUtil.splitOnFirstSeparator(path, '.'); Object tmp = BeanUtil.getPropertyValue(bean, tokens[0]); if (tokens[1] != null && tmp != null) return getPropertyGraph(tokens[1], tmp); else return tmp; }
public static Script parseScriptText(String text, boolean removeBrackets) { if (text == null) return null; if (removeBrackets && text.startsWith("{") && text.endsWith("}")) text = text.substring(1, text.length() - 1); String[] tokens = StringUtil.splitOnFirstSeparator(text, ':'); String engineId = tokens[0]; if (getFactory(engineId, false) != null) { String scriptText = tokens[1]; return parseScriptText(scriptText, engineId); } else return parseScriptText(text, ScriptUtil.getDefaultScriptEngine()); }
public static Script parseScriptText(String text, boolean removeBrackets) { if (text == null) return null; if (removeBrackets && text.startsWith("{") && text.endsWith("}")) text = text.substring(1, text.length() - 1); String[] tokens = StringUtil.splitOnFirstSeparator(text, ':'); String engineId = tokens[0]; if (getFactory(engineId) != null) { String scriptText = tokens[1]; return parseScriptText(scriptText, engineId); } else return parseScriptText(text, ScriptUtil.getDefaultScriptEngine()); }
@SuppressWarnings("null") public static void saveAsProperties(Properties properties, File file, String encoding) throws FileNotFoundException { if (properties.size() == 0) throw new IllegalArgumentException("Cannot save empty Properties"); Document document = null; for (Map.Entry<Object, Object> entry : properties.entrySet()) { String key = (String) entry.getKey(); String value = (String) entry.getValue(); String[] prefixAndRemainingPath = StringUtil.splitOnFirstSeparator(key, '.'); if (document == null) document = createDocument(prefixAndRemainingPath[0]); String rootElementName = document.getDocumentElement().getNodeName(); if (!key.startsWith(rootElementName + '.')) throw new SyntaxError("Required prefix '" + rootElementName + "' not present in key", key); setProperty(prefixAndRemainingPath[1], value, document.getDocumentElement(), document); } document.setXmlStandalone(true); // needed to omit standalone="yes/no" in the XML header saveDocument(document, file, encoding); }
@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; }
private Object haveTargetObject(Object bean, String featurePath) { if (featurePath == null) { return bean; } else if (featurePath.contains(".")) { String[] pathParts = StringUtil.splitOnFirstSeparator(featurePath, '.'); Object child = haveTargetObject(bean, pathParts[0]); return haveTargetObject(child, pathParts[1]); } Object value = FeatureAccessor.getValue(bean, featurePath, false); if (value != null) return value; else return AnyMutator.setFeatureDefault(bean, featurePath); }