View | Details | Raw Unified | Return to bug 208967 | Differences between
and this patch

Collapse All | Expand All

(-)text/org/eclipse/pde/internal/core/text/bundle/ExportPackageObject.java (-27 / +132 lines)
Lines 13-18 Link Here
13
import java.io.PrintWriter;
13
import java.io.PrintWriter;
14
import java.util.ArrayList;
14
import java.util.ArrayList;
15
import java.util.Iterator;
15
import java.util.Iterator;
16
import java.util.List;
16
import java.util.StringTokenizer;
17
import java.util.StringTokenizer;
17
import java.util.TreeMap;
18
import java.util.TreeMap;
18
19
Lines 27-32 Link Here
27
    private static final String INTERNAL = "x-internal"; //$NON-NLS-1$
28
    private static final String INTERNAL = "x-internal"; //$NON-NLS-1$
28
    private static final String FRIENDS = "x-friends"; //$NON-NLS-1$
29
    private static final String FRIENDS = "x-friends"; //$NON-NLS-1$
29
    
30
    
31
	private static final int NEWLINE_LIMIT = 3;
32
	private static final int NEWLINE_LIMIT_BOTH = 1;
33
34
    
30
    private static final long serialVersionUID = 1L;
35
    private static final long serialVersionUID = 1L;
31
    
36
    
32
    private TreeMap fFriends = new TreeMap();
37
    private TreeMap fFriends = new TreeMap();
Lines 137-192 Link Here
137
    }
142
    }
138
    
143
    
139
    protected void appendValuesToBuffer(StringBuffer sb, TreeMap table) {
144
    protected void appendValuesToBuffer(StringBuffer sb, TreeMap table) {
140
    	if (table == null)
145
146
    	if (table == null) {
141
    		return;
147
    		return;
148
    	}
149
    	
142
    	Object usesValue = null;
150
    	Object usesValue = null;
143
    	// remove the Uses directive, we will make sure to put it at the end
151
    	// remove the Uses directive, we will make sure to put it at the end
144
    	if (table.containsKey(Constants.USES_DIRECTIVE))
152
    	if (table.containsKey(Constants.USES_DIRECTIVE)) {
145
    		usesValue = table.remove(Constants.USES_DIRECTIVE);
153
    		usesValue = table.remove(Constants.USES_DIRECTIVE);
154
    	}
155
    	
156
    	Object friendsValue = null;
157
    	// remove the friends directive, ensure it's appropriately formatted
158
    	if (table.containsKey(FRIENDS)) {
159
    		friendsValue = table.remove(FRIENDS);
160
    	}
161
    	
146
    	super.appendValuesToBuffer(sb, table);
162
    	super.appendValuesToBuffer(sb, table);
163
    	
164
    	// If only one of uses and x-friends is specified, then the directives
165
		// have new lines at commas if there are more than 3 of them; if they're
166
		// both specified then they insert new lines for more than 1.
167
		int newLineLimit = NEWLINE_LIMIT;
168
		if (friendsValue != null && usesValue != null) {
169
			newLineLimit = NEWLINE_LIMIT_BOTH;
170
		}
171
    	
172
    	if( friendsValue != null ) {
173
    		table.put(FRIENDS, friendsValue);
174
    		formatDirective(FRIENDS, sb, friendsValue, newLineLimit);
175
    	}
176
    	
177
    	// uses goes last
147
    	if (usesValue != null) {
178
    	if (usesValue != null) {
148
    		table.put(Constants.USES_DIRECTIVE, usesValue);
179
    		table.put(Constants.USES_DIRECTIVE, usesValue);
149
    		formatUsesDirective(sb, usesValue);
180
    		formatDirective(Constants.USES_DIRECTIVE, sb, usesValue, newLineLimit);
150
    	}
181
    	}
151
    }
182
    }
152
    
183
    
153
    private void formatUsesDirective(StringBuffer sb, Object usesValue) {
184
    /**
154
    	StringTokenizer tokenizer = null;
185
	 * Format the specified directive of the Export-Package manifest header.
155
		if (usesValue instanceof String) 
186
	 * 
156
			tokenizer = new StringTokenizer((String)usesValue, ","); //$NON-NLS-1$
187
	 * @param directiveName
157
		boolean newLine = (tokenizer != null) ? tokenizer.countTokens() > 3 :
188
	 *            The name of the directive, e.g. x-friends or uses
158
			((ArrayList)usesValue).size() > 3;
189
	 * @param sb
159
		String eol = getHeader().getLineLimiter();
190
	 *            buffer to append the directives
191
	 * @param usesValue
192
	 *            The value of the uses directive, expected to be a String or a
193
	 *            List.
194
	 * @param newLineLimit
195
	 *            The number of items, above which, a new line would be needed
196
	 *            between all values.
197
	 */
198
	private void formatDirective(String directiveName, StringBuffer sb,
199
			Object usesValue, final int newLineLimit) {
200
201
		final String INDENT2 = "  "; //$NON-NLS-1$
202
		final String INDENT3 = "   "; //$NON-NLS-1$
203
204
		StringTokenizer tokenizer = null;
205
206
		boolean newLine = false;
207
208
		if (usesValue instanceof String) {
209
210
			// break the string down at commas
211
212
			tokenizer = new StringTokenizer((String) usesValue, ","); //$NON-NLS-1$
213
214
			if (tokenizer.countTokens() > newLineLimit) {
215
				newLine = true;
216
			}
217
218
		} else if (usesValue instanceof List) {
219
220
			List usesList = (List) usesValue;
221
222
			if (usesList.size() > newLineLimit) {
223
				newLine = true;
224
			}
225
226
		} else {
227
			// wrong type for usesValue! - in this situation the old
228
			// formatUsesDirective() would throw a ClassCastException.
229
			// So for consistency! :-(
230
			Object foo = (ArrayList) usesValue;
231
			// To remove 'non-usage' error :-(
232
			foo.getClass();
233
234
			// return should be unreachable
235
			return;
236
		}
237
238
		final String EOL = getHeader().getLineLimiter();
239
160
		sb.append(';');
240
		sb.append(';');
161
		if (newLine)
241
162
			sb.append(eol).append("  "); //$NON-NLS-1$
242
		if (newLine) {
163
		sb.append(Constants.USES_DIRECTIVE);
243
			sb.append(EOL).append(INDENT2);
244
		}
245
246
		sb.append(directiveName);
164
		sb.append(":=\""); //$NON-NLS-1$
247
		sb.append(":=\""); //$NON-NLS-1$
165
		if (tokenizer != null) 
248
249
		if (tokenizer != null) {
250
251
			// For a String based value, output each value (comma separated),
252
			// potentially adding a new line between them
253
166
			while (tokenizer.hasMoreTokens()) {
254
			while (tokenizer.hasMoreTokens()) {
167
				sb.append(tokenizer.nextToken());
255
				sb.append(tokenizer.nextToken());
168
				if (tokenizer.hasMoreTokens()) {
256
				if (tokenizer.hasMoreTokens()) {
169
					sb.append(',');
257
					sb.append(',');
170
					if (newLine) 
258
					if (newLine) {
171
						sb.append(eol).append("   "); //$NON-NLS-1$
259
						sb.append(EOL).append(INDENT3);
260
					}
172
				}
261
				}
173
			}
262
			}
174
		else {
263
		} else {
175
			ArrayList list = ((ArrayList)usesValue);
264
			List usesList = (List) usesValue;
176
			for (int i = 0; i < list.size(); i++) {
265
177
				if (i != 0) {
266
			// For each item in the collection, output each value (comma
267
			// separated), potentially adding a new line between them
268
269
			boolean firstTime = true;
270
			for (Iterator iterator = usesList.iterator(); iterator.hasNext();) {
271
272
				if (!firstTime) {
273
178
					sb.append(',');
274
					sb.append(',');
179
					if (newLine) 
275
					if (newLine) {
180
						sb.append(eol).append("   "); //$NON-NLS-1$
276
						sb.append(EOL).append(INDENT3);
277
					}
278
279
				} else {
280
					// No comma first time through
281
					firstTime = false;
181
				}
282
				}
182
				sb.append(list.get(i));
283
284
				sb.append(iterator.next());
183
			}
285
			}
184
		}
286
		}
185
		sb.append("\""); //$NON-NLS-1$
287
		sb.append("\""); //$NON-NLS-1$
186
    }
288
	}
187
289
    
188
	/* (non-Javadoc)
290
	/*
189
	 * @see org.eclipse.pde.internal.core.bundle.BundleObject#write(java.lang.String, java.io.PrintWriter)
291
	 * (non-Javadoc)
292
	 * 
293
	 * @see org.eclipse.pde.internal.core.bundle.BundleObject#write(java.lang.String,
294
	 *      java.io.PrintWriter)
190
	 */
295
	 */
191
	public void write(String indent, PrintWriter writer) {
296
	public void write(String indent, PrintWriter writer) {
192
		// Used for text transfers for copy, cut, paste operations
297
		// Used for text transfers for copy, cut, paste operations

Return to bug 208967