• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  *             of Java bytecode.
4  *
5  * Copyright (c) 2002-2014 Eric Lafortune (eric@graphics.cornell.edu)
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */
21 package proguard.util;
22 
23 import java.util.*;
24 
25 
26 /**
27  * This class provides some utility methods for working with
28  * <code>java.util.List</code> objects.
29  *
30  * @author Eric Lafortune
31  */
32 public class ListUtil
33 {
34     /**
35      * Creates a comma-separated String from the given List of String objects.
36      */
commaSeparatedString(List list, boolean quoteStrings)37     public static String commaSeparatedString(List list, boolean quoteStrings)
38     {
39         if (list == null)
40         {
41             return null;
42         }
43 
44         StringBuffer buffer = new StringBuffer();
45 
46         for (int index = 0; index < list.size(); index++)
47         {
48             if (index > 0)
49             {
50                 buffer.append(',');
51             }
52 
53             String string = (String)list.get(index);
54 
55             if (quoteStrings)
56             {
57                 string = quotedString(string);
58             }
59 
60             buffer.append(string);
61         }
62 
63         return buffer.toString();
64     }
65 
66 
67     /**
68      * Creates a List of String objects from the given comma-separated String.
69      */
commaSeparatedList(String string)70     public static List commaSeparatedList(String string)
71     {
72         if (string == null)
73         {
74             return null;
75         }
76 
77         List list = new ArrayList();
78         int index = 0;
79         while ((index = skipWhitespace(string, index)) < string.length())
80         {
81             int nextIndex;
82 
83             // Do we have an opening quote?
84             if (string.charAt(index) == '\'')
85             {
86                 // Parse a quoted string.
87                 nextIndex = string.indexOf('\'', index + 1);
88                 if (nextIndex < 0)
89                 {
90                     nextIndex = string.length();
91                 }
92 
93                 list.add(string.substring(index + 1, nextIndex));
94             }
95             else
96             {
97                 // Parse a non-quoted string.
98                 nextIndex = string.indexOf(',', index);
99                 if (nextIndex < 0)
100                 {
101                     nextIndex = string.length();
102                 }
103 
104                 String substring = string.substring(index, nextIndex).trim();
105                 if (substring.length() > 0)
106                 {
107                     list.add(substring);
108                 }
109             }
110 
111             index = nextIndex + 1;
112         }
113 
114         return list;
115     }
116 
117 
118     /**
119      * Skips any whitespace characters.
120      */
skipWhitespace(String string, int index)121     private static int skipWhitespace(String string, int index)
122     {
123         while (index < string.length() &&
124                Character.isWhitespace(string.charAt(index)))
125         {
126             index++;
127         }
128         return index;
129     }
130 
131 
132     /**
133      * Returns a quoted version of the given string, if necessary.
134      */
quotedString(String string)135     private static String quotedString(String string)
136     {
137         return string.length()     == 0 ||
138                string.indexOf(' ') >= 0 ||
139                string.indexOf('@') >= 0 ||
140                string.indexOf('{') >= 0 ||
141                string.indexOf('}') >= 0 ||
142                string.indexOf('(') >= 0 ||
143                string.indexOf(')') >= 0 ||
144                string.indexOf(':') >= 0 ||
145                string.indexOf(';') >= 0 ||
146                string.indexOf(',') >= 0  ? ("'" + string + "'") :
147                                            (      string      );
148     }
149 
150 
main(String[] args)151     public static void main(String[] args)
152     {
153         if (args.length == 1)
154         {
155             System.out.println("Input string: ["+args[0]+"]");
156 
157             List list = commaSeparatedList(args[0]);
158 
159             System.out.println("Resulting list:");
160             for (int index = 0; index < list.size(); index++)
161             {
162                 System.out.println("["+list.get(index)+"]");
163             }
164         }
165         else
166         {
167             List list = Arrays.asList(args);
168 
169             System.out.println("Input list:");
170             for (int index = 0; index < list.size(); index++)
171             {
172                 System.out.println("["+list.get(index)+"]");
173             }
174 
175             String string = commaSeparatedString(list, true);
176 
177             System.out.println("Resulting string: ["+string+"]");
178         }
179     }
180 }
181