From 2318c606a66465142b76727b40588f5f69e9ac47 Mon Sep 17 00:00:00 2001 From: paweldomas <pawel.domas@jitsi.org> Date: Tue, 20 Jan 2015 12:06:30 +0100 Subject: [PATCH] Moves ConfigUtils and StringCompiler from videobridge to libjitsi util package, so that they can be reused in other Jitsi projects. --- src/org/jitsi/util/ConfigUtils.java | 70 ++++++++++++ src/org/jitsi/util/StringCompiler.java | 144 +++++++++++++++++++++++++ 2 files changed, 214 insertions(+) create mode 100644 src/org/jitsi/util/ConfigUtils.java create mode 100644 src/org/jitsi/util/StringCompiler.java diff --git a/src/org/jitsi/util/ConfigUtils.java b/src/org/jitsi/util/ConfigUtils.java new file mode 100644 index 00000000..c002a5c2 --- /dev/null +++ b/src/org/jitsi/util/ConfigUtils.java @@ -0,0 +1,70 @@ +/* + * Jitsi, the OpenSource Java VoIP and Instant Messaging client. + * + * Distributable under LGPL license. + * See terms of license at gnu.org. + */ +package org.jitsi.util; + +import org.jitsi.service.configuration.*; + +import java.io.*; + +/** + * @author George Politis + */ +public class ConfigUtils +{ + /** + * Gets an absolute path in the form of <tt>File</tt> from an absolute or + * relative <tt>path</tt> specified in the form of a <tt>String</tt>. If + * <tt>path</tt> is relative, it is resolved against + * <tt>ConfigurationService.PNAME_SC_HOME_DIR_LOCATION</tt> and + * <tt>ConfigurationService.PNAME_SC_HOME_DIR_NAME</tt>, <tt>user.home</tt>, + * or the current working directory. + * + * @param path the absolute or relative path in the form of <tt>String</tt> + * for/from which an absolute path in the form of <tt>File</tt> is to be + * returned + * @param cfg the <tt>ConfigurationService</tt> to be employed by the method + * (invocation) if necessary + * @return an absolute path in the form of <tt>File</tt> for/from the + * specified <tt>path</tt> + */ + public static File getAbsoluteFile(String path, ConfigurationService cfg) + { + File file = new File(path); + + if (!file.isAbsolute()) + { + String scHomeDirLocation, scHomeDirName; + + if (cfg == null) + { + scHomeDirLocation + = System.getProperty( + ConfigurationService.PNAME_SC_HOME_DIR_LOCATION); + scHomeDirName + = System.getProperty( + ConfigurationService.PNAME_SC_HOME_DIR_NAME); + } + else + { + scHomeDirLocation = cfg.getScHomeDirLocation(); + scHomeDirName = cfg.getScHomeDirName(); + } + if (scHomeDirLocation == null) + { + scHomeDirLocation = System.getProperty("user.home"); + if (scHomeDirLocation == null) + scHomeDirLocation = "."; + } + if (scHomeDirName == null) + scHomeDirName = "."; + file + = new File(new File(scHomeDirLocation, scHomeDirName), path) + .getAbsoluteFile(); + } + return file; + } +} diff --git a/src/org/jitsi/util/StringCompiler.java b/src/org/jitsi/util/StringCompiler.java new file mode 100644 index 00000000..8b4d5fb7 --- /dev/null +++ b/src/org/jitsi/util/StringCompiler.java @@ -0,0 +1,144 @@ +/* + * Jitsi, the OpenSource Java VoIP and Instant Messaging client. + * + * Distributable under LGPL license. + * See terms of license at gnu.org. + */ +package org.jitsi.util; + +import java.lang.reflect.*; +import java.util.*; +import java.util.regex.*; + +/** + * @author George Politis + * + */ +public class StringCompiler +{ + private final Map<String, Object> bindings; + + public StringCompiler(Map<String, Object> bindings) + { + this.bindings = bindings; + } + + public StringCompiler() + { + this.bindings = new HashMap<String, Object>(); + } + + public void bind(String key, Object value) + { + this.bindings.put(key, value); + } + + private static final Pattern p = Pattern.compile("\\{([^\\}]+)\\}"); + + private StringBuffer sb; + + public StringCompiler c(String s) + { + if (StringUtils.isNullOrEmpty(s)) + { + return this; + } + + Matcher m = p.matcher(s); + sb = new StringBuffer(); + + while (m.find()) { + String key = m.group(1); + String value = getValue(key); + m.appendReplacement(sb, value); + } + + m.appendTail(sb); + + return this; + } + + @Override + public String toString() + { + return (sb == null) ? "" : sb.toString(); + } + + private String getValue(String key) + { + if (StringUtils.isNullOrEmpty(key)) + throw new IllegalArgumentException("key"); + + String value = ""; + + String[] path = key.split("\\."); + + // object graph frontier. + Object obj = null; + + for (int i = 0; i < path.length; i++) + { + String identifier = path[i]; + + if (i == 0) + { + // Init. + if (bindings.containsKey(identifier)) + { + obj = bindings.get(identifier); + } + else + { + break; + } + } + else + { + if (obj != null) + { + // Decent the object graph. + Class<?> c = obj.getClass(); + Field f; + + try + { + f = c.getDeclaredField(identifier); + } + catch (NoSuchFieldException e) + { + break; + } + + if (!f.isAccessible()) + { + f.setAccessible(true); + } + + try + { + obj = f.get(obj); + } + catch (IllegalAccessException e) + { + break; + } + } + else + { + break; + } + } + + if (i == path.length - 1) + { + // Stop the decent. + if (obj != null) + { + value = obj.toString(); + } + } + } + + return value; + } +} -- GitLab