public static String quoteValueIfRequired()

in src/main/java/org/apache/sling/commons/metrics/internal/JmxUtil.java [41:71]


    public static String quoteValueIfRequired(String unquotedValue) {
        String result;
        String quotedValue = ObjectName.quote(unquotedValue);

        //Check if some chars are escaped or not. In that case
        //length of quoted string (excluding quotes) would differ
        if (quotedValue.substring(1, quotedValue.length() - 1).equals(unquotedValue)) {
            ObjectName on = null;
            try {
                //Quoting logic in ObjectName does not escape ',', '='
                //etc. So try now by constructing ObjectName. If that
                //passes then value can be used as safely

                //Also we cannot just rely on ObjectName as it treats
                //*, ? as pattern chars and which should ideally be escaped
                on = new ObjectName("dummy", "dummy", unquotedValue);
            } catch (MalformedObjectNameException ignore) {
                //ignore
            }

            if (on != null){
                result = unquotedValue;
            } else {
                result = quotedValue;
            }
        } else {
            //Some escaping done. So do quote
            result = quotedValue;
        }
        return result;
    }