Debian Codegraph @

common/src/main/java/com/android/utils/StringHelperWindows.java

raw
1 /*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.android.utils;
18
19 import com.android.annotations.NonNull;
20 import com.google.common.collect.Lists;
21
22 import java.util.List;
23
24 /**
25 * Windows specific StringHelper that applies the following tokenization rules:
26 *
27 * https://msdn.microsoft.com/en-us/library/17w5ykft.aspx
28 * - A string surrounded by double quotation marks ("string") is interpreted as a
29 * single argument, regardless of white space contained within. A quoted string
30 * can be embedded in an argument.
31 * - A double quotation mark preceded by a backslash (\") is interpreted as a
32 * literal double quotation mark character (").
33 * - Backslashes are interpreted literally, unless they immediately precede a
34 * double quotation mark.
35 * - If an even number of backslashes is followed by a double quotation mark, one
36 * backslash is placed in the argv array for every pair of backslashes, and the
37 * double quotation mark is interpreted as a string delimiter.
38 * - If an odd number of backslashes is followed by a double quotation mark, one
39 * backslash is placed in the argv array for every pair of backslashes, and the
40 * double quotation mark is "escaped" by the remaining backslash
41 */
42 public class StringHelperWindows extends StringHelper {
43
44 /**
45 * Split a single command line into individual commands with Windows rules.
46 *
47 * @param commandLine the command line to be split
48 * @return the list of individual commands
49 */
50 @NonNull
51 public static List<String> splitCommandLine(@NonNull String commandLine) {
52
53 final String separators[] = { "&&", "&" };
54
55 List<String> commands = Lists.newArrayList();
56 boolean quoting = false;
57 boolean escapingQuotes = false;
58 boolean escapingOthers = false;
59
60 int commandStart = 0;
61
62 for (int i = 0; i < commandLine.length(); ++i) {
63 final char c = commandLine.charAt(i);
64
65 if (c == '"' && !escapingQuotes) {
66 quoting = !quoting;
67 continue;
68 }
69
70 if (escapingQuotes) {
71 escapingQuotes = false;
72 } else if (c == '\\') {
73 escapingQuotes = true;
74 continue;
75 }
76
77 if (escapingOthers) {
78 escapingOthers = false;
79 continue;
80 } else if (c == '^') {
81 escapingOthers = true;
82 continue;
83 }
84
85 if (!quoting) {
86 for (final String separator : separators) {
87 if (commandLine.substring(i).startsWith(separator)) {
88 commands.add(commandLine.substring(commandStart, i));
89 i += separator.length();
90 commandStart = i;
91 break;
92 }
93 }
94 }
95
96 }
97
98 if (commandStart < commandLine.length())
99 commands.add(commandLine.substring(commandStart));
100
101 return commands;
102 }
103
104 /**
105 * Quote and join a list of tokens with Windows rules.
106 *
107 * @param tokens the token to be quoted and joined
108 * @return the string
109 */
110 @NonNull
111 public static String quoteAndJoinTokens(@NonNull List<String> tokens) {
112 StringBuilder sb = new StringBuilder();
113 for (String token : tokens) {
114 token = token.replaceAll("(\\\\+)(?=\"|$)", "$1$1");
115 token = token.replace("\"", "\\\"");
116 sb.append("\"").append(token).append("\" ");
117 }
118 if (sb.length() > 0)
119 sb.setLength(sb.length() - 1);
120 return sb.toString();
121 }
122
123 /**
124 * Tokenize a string with Windows rules.
125 *
126 * @param string the string to be tokenized
127 * @return the list of tokens
128 */
129 @NonNull
130 public static List<String> tokenizeString(@NonNull String string) {
131 List<String> tokens = Lists.newArrayList();
132 StringBuilder currentToken = new StringBuilder();
133 boolean quoting = false;
134 boolean escapingQuotes = false;
135 boolean escapingOthers = false;
136 boolean skipping = true;
137 for (final char c : string.toCharArray()) {
138 if (skipping) {
139 if (Character.isWhitespace(c))
140 continue;
141 else
142 skipping = false;
143 }
144
145 if (c == '"') {
146 // delete one slash for every pair of preceding slashes
147 for (int i = currentToken.length() - 2;
148 i >= 0 && currentToken.charAt(i) == '\\'
149 && currentToken.charAt(i+1) == '\\';
150 i -= 2) {
151 currentToken.deleteCharAt(i);
152 }
153 if (escapingQuotes) {
154 currentToken.deleteCharAt(currentToken.length() - 1);
155 } else {
156 quoting = !quoting;
157 continue;
158 }
159 }
160
161 if (escapingQuotes) {
162 escapingQuotes = false;
163 } else if (c == '\\') {
164 escapingQuotes = true;
165 }
166
167 if (escapingOthers) {
168 escapingOthers = false;
169 if (c == '\n')
170 continue;
171 } else if (!quoting && c == '^') {
172 escapingOthers = true;
173 continue;
174 }
175
176 if (!quoting && Character.isWhitespace(c)) {
177 skipping = true;
178 if (currentToken.length() > 0)
179 tokens.add(currentToken.toString());
180 currentToken.setLength(0);
181 continue;
182 }
183
184 currentToken.append(c);
185 }
186
187 if (currentToken.length() > 0)
188 tokens.add(currentToken.toString());
189
190 return tokens;
191 }
192 }