private Object doExpand()

in gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Expander.java [668:818]


    private Object doExpand() throws Exception
    {
        final String special = "%$\\\"'";
        int i = text.length();
        while ((--i >= 0) && (special.indexOf(text.charAt(i)) == -1));
        // shortcut if word doesn't contain any special characters
        if (i < 0)
            return text;

        StringBuilder buf = new StringBuilder();
        Token value;

        while (ch != EOT)
        {
            int start = index;

            switch (ch)
            {
                case '%':
                    Object exp = expandExp();
                    if (EOT == ch && buf.length() == 0)
                    {
                        return exp;
                    }
                    if (null != exp)
                    {
                        buf.append(exp);
                    }
                    break;

                case '$':
                    // Posix quote
                    if (peek() == '\'')
                    {
                        getch();
                        skipQuote();
                        value = text.subSequence(start + 1, index - 1);
                        getch();
                        buf.append("\'");
                        buf.append(ansiEscape(value));
                        buf.append("\'");
                    }
                    // Parameter expansion
                    else
                    {
                        Object val = expandVar(true);
                        if (EOT == ch && buf.length() == 0)
                        {
                            return val;
                        }
                        rawVariable = false;
                        if (null != val)
                        {
                            buf.append(val);
                        }
                    }
                    break;

                case '\\':
                    buf.append(ch);
                    if (peek() != EOT)
                    {
                        getch();
                        buf.append(ch);
                    }
                    getch();
                    break;

                case '"':
                    skipQuote();
                    value = text.subSequence(start, index - 1);
                    getch();
                    Object expand = expand(value, evaluate, true);
                    if (eot() && buf.length() == 0)
                    {
                        if (expand instanceof ArgList)
                        {
                            List<String> l = new ArrayList<>();
                            for (Object o : (ArgList) expand) {
                                l.add("\"" + String.valueOf(o) + "\"");
                            }
                            return l;
                        }
                        else if (expand instanceof Collection)
                        {
                            StringBuilder sb = new StringBuilder();
                            sb.append("\"");
                            boolean first = true;
                            for (Object o : asCollection(expand)) {
                                if (first) {
                                    first = false;
                                } else {
                                    sb.append(" ");
                                }
                                sb.append(String.valueOf(o));
                            }
                            return sb.append("\"").toString();
                        }
                        else if (expand != null)
                        {
                            return "\"" + expand.toString() + "\"";
                        }
                        else
                        {
                            return "";
                        }
                    }
                    if (expand instanceof Collection)
                    {
                        boolean first = true;
                        buf.append("\"");
                        for (Object o : ((Collection<?>) expand))
                        {
                            if (!first)
                            {
                                buf.append(" ");
                            }
                            first = false;
                            buf.append(o);
                        }
                        buf.append("\"");
                    }
                    else if (expand != null)
                    {
                        buf.append("\"");
                        buf.append(expand.toString());
                        buf.append("\"");
                    }
                    break;

                case '\'':
                    skipQuote();
                    value = text.subSequence(start - 1, index);
                    getch();
                    if (eot() && buf.length() == 0)
                    {
                        return value;
                    }
                    buf.append(value);
                    break;

                default:
                    buf.append(ch);
                    getch();
                    break;
            }

        }

        return buf.toString();
    }