|
1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 package com.symbian.smt.gui; |
|
16 |
|
17 import java.io.File; |
|
18 import java.io.IOException; |
|
19 import java.util.ArrayList; |
|
20 import java.util.List; |
|
21 |
|
22 /** |
|
23 * A helper class for general usage. |
|
24 * |
|
25 * @author barbararosi-schwartz |
|
26 * |
|
27 */ |
|
28 public final class Helper { |
|
29 |
|
30 /** |
|
31 * Concatenates the strings in the provided array using the provided |
|
32 * separator and returns the concatenated string. |
|
33 * |
|
34 * @param separator |
|
35 * the separator to be used for the concatenation |
|
36 * @param items |
|
37 * the array of strings to be concatenated |
|
38 * @return the concatenated String or an empty String if the provided array |
|
39 * is null or empty. |
|
40 */ |
|
41 public static String concatenateString(String separator, String[] items) { |
|
42 StringBuilder joinedString = new StringBuilder(); |
|
43 |
|
44 if (items != null) { |
|
45 for (String item : items) { |
|
46 joinedString.append(item); |
|
47 joinedString.append(separator); |
|
48 } |
|
49 |
|
50 if (joinedString.length() > 0) { |
|
51 joinedString.deleteCharAt(joinedString.length() - 1); |
|
52 } |
|
53 } |
|
54 |
|
55 return joinedString.toString(); |
|
56 } |
|
57 |
|
58 /** |
|
59 * Adds the absolute location represented by the provided rootLocation to |
|
60 * the filenames |
|
61 * |
|
62 * @param filenames |
|
63 * the String representing the relative filenames, separated by |
|
64 * the provided separator |
|
65 * @param rootLocation |
|
66 * the String representing the location of the root of the |
|
67 * filesystem that will turn the relative paths into absolute |
|
68 * ones |
|
69 * @param separator |
|
70 * the String representing the separator that separates the file |
|
71 * names |
|
72 * @return the String representing the separator separated absolute |
|
73 * filenames |
|
74 */ |
|
75 public static String relative2AbsolutePaths(String filenames, |
|
76 String rootLocation, String separator) { |
|
77 String[] relativeNames = Helper.splitString(filenames, separator); |
|
78 String[] absoluteNames = new String[relativeNames.length]; |
|
79 int i = 0; |
|
80 |
|
81 for (String name : relativeNames) { |
|
82 if (name.startsWith(".")) { |
|
83 try { |
|
84 name = new File(rootLocation + name).getCanonicalPath(); |
|
85 } catch (IOException e) { |
|
86 Logger.log(e.getMessage(), e); |
|
87 } |
|
88 } |
|
89 |
|
90 absoluteNames[i] = name; |
|
91 i++; |
|
92 } |
|
93 |
|
94 return concatenateString(separator, absoluteNames); |
|
95 } |
|
96 |
|
97 /** |
|
98 * Splits the provided concatenated string into its constituent parts based |
|
99 * upon the provided separator and returns an array of the constituents. If |
|
100 * the provided input is null or empty, returns an empty array. |
|
101 * |
|
102 * @param concatenatedString |
|
103 * a String containing components separated by the given |
|
104 * separator |
|
105 * @param separator |
|
106 * the separator used |
|
107 * @return an array of String with the separate components or an empty array |
|
108 * if <code>concatenatedString</code> is null or empty |
|
109 */ |
|
110 public static String[] splitString(String concatenatedString, |
|
111 String separator) { |
|
112 if (separator.equals("|")) { |
|
113 separator = "\\|"; |
|
114 } |
|
115 |
|
116 if (concatenatedString != null && concatenatedString.length() > 0) { |
|
117 return concatenatedString.split(separator); |
|
118 } else { |
|
119 String[] empty = {}; |
|
120 return empty; |
|
121 } |
|
122 } |
|
123 |
|
124 /** |
|
125 * Converts the List of String objects into a String array. The opposite |
|
126 * conversion is also provided by this class with method |
|
127 * toListofStrings(String []). |
|
128 * |
|
129 * @param list |
|
130 * the java.util.List of String objects to be converted (may be |
|
131 * empty but cannot be null) |
|
132 * @return the corresponding String array or an empty array if list is |
|
133 * empty. |
|
134 * @see #toListofStrings(String []) |
|
135 */ |
|
136 public static final String[] toArrayOfStrings(List<String> list) { |
|
137 if (list == null) { |
|
138 throw new IllegalArgumentException("Parameter list cannot be null."); |
|
139 } |
|
140 |
|
141 String[] array = new String[list.size()]; |
|
142 |
|
143 return list.toArray(array); |
|
144 } |
|
145 |
|
146 /** |
|
147 * Converts the provided ResourcesEnums array into a java.util.List |
|
148 * containing the args of the provided ResourceEnums. |
|
149 * |
|
150 * @param array |
|
151 * the array of ResourcesEnums objects to be converted (may be |
|
152 * empty but cannot be null) |
|
153 * @return the corresponding List of String args or an empty List if array |
|
154 * is empty |
|
155 */ |
|
156 public static final List<String> toListOfStrings(ResourcesEnums[] array) { |
|
157 if (array == null) { |
|
158 throw new IllegalArgumentException( |
|
159 "Parameter array cannot be null."); |
|
160 } |
|
161 |
|
162 ArrayList<String> arrayList = new ArrayList<String>(); |
|
163 |
|
164 for (ResourcesEnums re : array) { |
|
165 arrayList.add(re.arg()); |
|
166 } |
|
167 |
|
168 return arrayList; |
|
169 } |
|
170 |
|
171 /** |
|
172 * Converts the provided String array into a java.util.List. The opposite |
|
173 * conversion is also provided by this class with method |
|
174 * toArrayOfStrings(List). |
|
175 * |
|
176 * @param array |
|
177 * the array of String objects to be converted (may be empty but |
|
178 * cannot be null) |
|
179 * @return the corresponding List of String objects or an empty List if |
|
180 * array is empty |
|
181 * @see #toArrayOfStrings(List<String>) |
|
182 */ |
|
183 public static final List<String> toListOfStrings(String[] array) { |
|
184 if (array == null) { |
|
185 throw new IllegalArgumentException( |
|
186 "Parameter array cannot be null."); |
|
187 } |
|
188 |
|
189 ArrayList<String> arrayList = new ArrayList<String>(); |
|
190 |
|
191 for (String s : array) { |
|
192 arrayList.add(s); |
|
193 } |
|
194 |
|
195 return arrayList; |
|
196 } |
|
197 |
|
198 } |