View Javadoc

1   /*
2    * This software was designed and created by Jason Carroll.
3    * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4    * The author can be reached at jcarroll@cowsultants.com
5    * ITracker website: http://www.cowsultants.com
6    * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7    *
8    * This program is free software; you can redistribute it and/or modify
9    * it only under the terms of the GNU General Public License as published by
10   * the Free Software Foundation; either version 2 of the License, or
11   * (at your option) any later version.
12   *
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
17   */
18  
19  package org.itracker.services.util;
20  
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.List;
24  import java.util.Locale;
25  import java.util.NoSuchElementException;
26  import java.util.StringTokenizer;
27  
28  import org.itracker.model.Component;
29  import org.itracker.model.CustomFieldValue;
30  import org.itracker.model.NameValuePair;
31  import org.itracker.model.User;
32  import org.itracker.model.Version;
33  
34  public class Convert {
35  	/**
36  	 * Converts an array of CustomFieldValueModels to NameValuePairs
37  	 * 
38  	 * @param options
39  	 *            the array of CustomFieldValueModels to convert
40  	 * @return the new NameValuePair array
41  	 */
42  	public static List<NameValuePair> customFieldOptionsToNameValuePairs(
43  			List<CustomFieldValue> options) {
44  		return customFieldOptionsToNameValuePairs(options, null);
45  		
46  	}
47  	/**
48  	 * Converts an array of CustomFieldValueModels to NameValuePairs
49  	 * 
50  	 * @param options
51  	 *            the array of CustomFieldValueModels to convert
52  	 * @return the new NameValuePair array
53  	 */
54  	public static List<NameValuePair> customFieldOptionsToNameValuePairs(
55  			List<CustomFieldValue> options, Locale locale) {
56  		List<NameValuePair> returnValues = new ArrayList<NameValuePair>();
57  		String name;
58  		if (options != null) {
59  			returnValues = new ArrayList<NameValuePair>();
60  			for (int i = 0; i < options.size(); i++) {
61  				name = CustomFieldUtilities.getCustomFieldOptionName(options.get(i), locale);
62  				returnValues
63  						.add(new NameValuePair(name, options.get(i).getValue()));
64  			}
65  		}
66  
67  		return returnValues;
68  	}
69  	/**
70  	 * Converts an array of UserModels to NameValuePairs
71  	 * 
72  	 * @param options
73  	 *            the array of UserModels to convert
74  	 * @return the new NameValuePair array
75  	 */
76  	public static List<NameValuePair> usersToNameValuePairs(List<User> users) {
77  		List<NameValuePair> returnValues = new ArrayList<NameValuePair>();
78  
79  		if (users != null) {
80  			returnValues = new ArrayList<NameValuePair>();
81  			for (int i = 0; i < users.size(); i++) {
82  				returnValues.add(new NameValuePair(users.get(i).getFirstName()
83  						+ " " + users.get(i).getLastName(), users.get(i)
84  						.getId().toString()));
85  			}
86  		}
87  
88  		return returnValues;
89  	}
90  
91  	/**
92  	 * Converts an array of ComponentModels to NameValuePairs
93  	 * 
94  	 * @param options
95  	 *            the array of ComponentModels to convert
96  	 * @return the new NameValuePair array
97  	 */
98  	public static List<NameValuePair> componentsToNameValuePairs(
99  			List<Component> components) {
100 		NameValuePair[] returnValues = new NameValuePair[0];
101 
102 		if (components != null) {
103 			returnValues = new NameValuePair[components.size()];
104 			for (int i = 0; i < components.size(); i++) {
105 				returnValues[i] = new NameValuePair(
106 						components.get(i).getName(), components.get(i).getId()
107 								.toString());
108 			}
109 		}
110 
111 		return Arrays.asList(returnValues);
112 	}
113 
114 	/**
115 	 * Converts an array of VersionModels to NameValuePairs
116 	 * 
117 	 * @param options
118 	 *            the array of VersionModels to convert
119 	 * @return the new NameValuePair array
120 	 */
121 	public static List<NameValuePair> versionsToNameValuePairs(
122 			List<Version> versions) {
123 		NameValuePair[] returnValues = new NameValuePair[0];
124 
125 		if (versions != null) {
126 			returnValues = new NameValuePair[versions.size()];
127 			for (int i = 0; i < versions.size(); i++) {
128 				returnValues[i] = new NameValuePair(
129 						versions.get(i).getNumber(), versions.get(i).getId()
130 								.toString());
131 			}
132 		}
133 
134 		return Arrays.asList(returnValues);
135 	}
136 
137 	public static String[] stringToArray(String input) {
138 		if (input == null || "".equals(input)) {
139 			return new String[0];
140 		}
141 
142 		List<String> tokenList = new ArrayList<String>();
143 		StringTokenizer tokenizer = new StringTokenizer(input, " ");
144 		while (tokenizer.hasMoreElements()) {
145 			boolean quotedToken = false;
146 			String tokenString = tokenizer.nextToken();
147 			if (tokenString.startsWith("\"")) {
148 				quotedToken = true;
149 				tokenString = tokenString.substring(1);
150 				if (tokenString.endsWith("\"") && !tokenString.endsWith("\\\"")) {
151 					quotedToken = false;
152 					tokenString = tokenString.substring(0, tokenString.length() - 1);
153 				}
154 			}
155 
156 			if (quotedToken) {
157 				boolean getNext = true;
158 
159 				StringBuffer token = new StringBuffer(tokenString);
160 				while (getNext) {
161 					try {
162 						token.append(tokenizer.nextToken("\""));
163 						if (!token.toString().endsWith("\\\"")) {
164 							getNext = false;
165 						}
166 					} catch (NoSuchElementException e) {
167 						break;
168 					}
169 				}
170 				tokenString = token.toString();
171 			}
172 
173 			tokenList.add(tokenString);
174 		}
175 
176 		String[] stringArray = (String[]) tokenList.toArray(new String[] {});
177 
178 		return stringArray;
179 	}
180 }