Debian Codegraph @

common/src/main/java/com/android/utils/StringHelperPOSIX.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 * POSIX specific StringHelper that applies the following tokenization rules:
26 *
27 * http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html
28 * - A backslash that is not quoted shall preserve the literal value of the
29 * following character
30 * - Enclosing characters in single-quotes ( '' ) shall preserve the literal value
31 * of each character within the single-quotes.
32 * - Enclosing characters in double-quotes ( "" ) shall preserve the literal value
33 * of all characters within the double-quotes, with the exception of the
34 * characters dollar sign, backquote, and backslash
35 */
36 public class StringHelperPOSIX extends StringHelper {
37
38 /**
39 * Split a single command line into individual commands with POSIX rules.
40 *
41 * @param commandLine the command line to be split
42 * @return the list of individual commands
43 */
44 @NonNull
45 public static List<String> splitCommandLine(@NonNull String commandLine) {
46
47 final String separators[] = { "&&", ";" };
48
49 List<String> commands = Lists.newArrayList();
50 boolean quoting = false;
51 char quote = '\0';
52 boolean escaping = false;
53
54 int commandStart = 0;
55
56 for (int i = 0; i < commandLine.length(); ++i) {
57 final char c = commandLine.charAt(i);
58
59 if (escaping) {
60 escaping = false;
61 continue;
62 } else if (c == '\\' && (!quoting || quote == '\"')) {
63 escaping = true;
64 continue;
65 } else if (!quoting && (c == '"' || c == '\'')) {
66 quoting = true;
67 quote = c;
68 continue;
69 } else if (quoting && c == quote) {
70 quoting = false;
71 quote = '\0';
72 continue;
73 }
74
75 if (!quoting) {
76 for (final String separator : separators) {
77 if (commandLine.substring(i).startsWith(separator)) {
78 commands.add(commandLine.substring(commandStart, i));
79 i += separator.length();
80 commandStart = i;
81 break;
82 }
83 }
84 }
85 }
86
87 if (commandStart < commandLine.length())
88 commands.add(commandLine.substring(commandStart));
89
90 return commands;
91 }
92
93 /**
94 * Quote and join a list of tokens with POSIX rules.
95 *
96 * @param tokens the token to be quoted and joined
97 * @return the string
98 */
99 @NonNull
100 public static String quoteAndJoinTokens(@NonNull List<String> tokens) {
101 StringBuilder sb = new StringBuilder();
102 for (String token : tokens) {
103 token = token.replace("\\", "\\\\");
104 token = token.replace("\"", "\\\"");
105 sb.append("\"").append(token).append("\" ");
106 }
107 if (sb.length() > 0)
108 sb.setLength(sb.length() - 1);
109 return sb.toString();
110 }
111
112 /**
113 * Tokenize a string with POSIX rules.
114 *
115 * @param string the string to be tokenized
116 * @return the list of tokens
117 */
118 @NonNull
119 public static List<String> tokenizeString(@NonNull String string) {
120 List<String> tokens = Lists.newArrayList();
121 StringBuilder currentToken = new StringBuilder();
122 boolean quoting = false;
123 char quote = '\0';
124 boolean escaping = false;
125 boolean skipping = true;
126 for (final char c : string.toCharArray()) {
127 if (skipping) {
128 if (Character.isWhitespace(c))
129 continue;
130 else
131 skipping = false;
132 }
133
134 if (escaping) {
135 escaping = false;
136 if (c != '\n')
137 currentToken.append(c);
138 continue;
139 } else if (c == '\\' && (!quoting || quote == '\"')) {
140 escaping = true;
141 continue;
142 } else if (!quoting && (c == '"' || c == '\'')) {
143 quoting = true;
144 quote = c;
145 continue;
146 } else if (quoting && c == quote) {
147 quoting = false;
148 quote = '\0';
149 continue;
150 }
151
152 if (!quoting && Character.isWhitespace(c)) {
153 skipping = true;
154 if (currentToken.length() > 0)
155 tokens.add(currentToken.toString());
156 currentToken.setLength(0);
157 continue;
158 }
159
160 currentToken.append(c);
161 }
162
163 if (currentToken.length() > 0)
164 tokens.add(currentToken.toString());
165
166 return tokens;
167 }
168 }