summaryrefslogtreecommitdiff
path: root/dicore3/command/src/main/java/io/dico/dicore/command/parameter/ArgumentMergingPreProcessor.java
blob: 5f7b81dc02dd4cd249d98e40be46ae9b82fad6c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package io.dico.dicore.command.parameter;

public class ArgumentMergingPreProcessor implements IArgumentPreProcessor {
    private final String tokens;
    private final char escapeChar;

    public ArgumentMergingPreProcessor(String tokens, char escapeChar) {
        if ((tokens.length() & 1) != 0 || tokens.isEmpty()) throw new IllegalArgumentException();
        this.tokens = tokens;
        this.escapeChar = escapeChar;
    }

    @Override
    public String[] process(int argStart, String[] args) {
        if (!(0 <= argStart && argStart <= args.length)) {
            throw new IndexOutOfBoundsException();
        }

        Parser parser = new Parser(argStart, args.clone());
        return parser.doProcess();
    }

    private class Parser {
        private final int argStart;
        private final String[] args;

        private int currentIndex;
        private int sectionStart;
        private char closingToken;
        private int sectionEnd;
        private int removeCount;

        Parser(int argStart, String[] args) {
            this.argStart = argStart;
            this.args = args;
        }

        private void reset() {
            removeCount = 0;
            closingToken = 0;
            sectionStart = -1;
            sectionEnd = -1;
            currentIndex = argStart;
        }

        private boolean findNextSectionStart() {
            while (currentIndex < args.length) {
                String arg = args[currentIndex];
                if (arg == null) {
                    throw new IllegalArgumentException();
                }

                if (arg.isEmpty()) {
                    ++currentIndex;
                    continue;
                }

                int openingTokenIndex = tokens.indexOf(arg.charAt(0));
                if (openingTokenIndex == -1 || (openingTokenIndex & 1) != 0) {
                    ++currentIndex;
                    continue;
                }

                // found
                closingToken = tokens.charAt(openingTokenIndex | 1);
                sectionStart = currentIndex;
                return true;
            }

            return false;
        }

        private boolean findNextSectionEnd() {
            while (currentIndex < args.length) {
                String arg = args[currentIndex];
                if (arg == null) {
                    throw new IllegalArgumentException();
                }

                if (arg.isEmpty()
                    || arg.charAt(arg.length() - 1) != closingToken
                    || (sectionStart == currentIndex && arg.length() == 1)) {
                    ++currentIndex;
                    continue;
                }

                if (escapeChar != 0
                    && arg.length() > 1
                    && arg.charAt(arg.length() - 2) == escapeChar) {
                    // escaped
                    ++currentIndex;
                    continue;
                }

                // found
                closingToken = 0;
                sectionEnd = currentIndex;
                ++currentIndex;
                return true;
            }

            return false;
        }

        private void processFoundSection() {
            if (sectionStart == sectionEnd) {
                String arg = args[sectionStart];
                args[sectionStart] = arg.substring(1, arg.length() - 1);
                return;
            }

            removeCount += sectionEnd - sectionStart;

            StringBuilder sb = new StringBuilder();
            sb.append(args[sectionStart].substring(1));

            for (int i = sectionStart + 1; i < sectionEnd; i++) {
                sb.append(' ');
                sb.append(args[i]);
                args[i] = null;
            }
            sb.append(' ');
            sb.append(args[sectionEnd].substring(0, args[sectionEnd].length() - 1));
            args[sectionEnd] = null;

            args[sectionStart] = sb.toString();

            sectionStart = -1;
            sectionEnd = -1;
        }

        public String[] doProcess() {
            reset();

            while (findNextSectionStart()) {
                if (findNextSectionEnd()) {
                    processFoundSection();
                } else {
                    currentIndex = sectionStart + 1;
                }
            }

            if (removeCount == 0) {
                return args;
            }

            String[] result = new String[args.length - removeCount];
            int i = 0;
            for (String arg : args) {
                if (arg != null) {
                    result[i++] = arg;
                }
            }

            return result;
        }

    }

}