1/*******************************************************************************
2 * Copyright (c) 2000, 2006 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 *     IBM Corporation - initial API and implementation
10 *******************************************************************************/
11package org.eclipse.releng.generators;
12
13import java.util.ArrayList;
14import java.util.List;
15
16/**
17 * Defines common behaviour for PDE Core applications.
18 */
19public abstract class AbstractApplication {
20
21/**
22 * Starting point for application logic.
23 */
24protected abstract void run() throws Exception;
25
26/*
27 * @see IPlatformRunnable#run(Object)
28 */
29public Object run(Object args) throws Exception {
30	processCommandLine(getArrayList((String[]) args));
31	try {
32		run();
33	} catch (Exception e) {
34		e.printStackTrace(System.out);
35	}
36	return null;
37}
38
39/**
40 * Helper method to ensure an array is converted into an ArrayList.
41 */
42public static ArrayList getArrayList(Object[] args) {
43	// We could be using Arrays.asList() here, but it does not specify
44	// what kind of list it will return. We do need a list that
45	// implements the method List.remove(int) and ArrayList does.
46	ArrayList result = new ArrayList(args.length);
47	for (int i = 0; i < args.length; i++)
48		result.add(args[i]);
49	return result;
50}
51
52
53
54
55/**
56 * Looks for interesting command line arguments.
57 */
58protected void processCommandLine(List commands) {
59}
60
61/**
62 * From a command line list, get the array of arguments of a given parameter.
63 * The parameter and its arguments are removed from the list.
64 * @return null if the parameter is not found or has no arguments
65 */
66protected String[] getArguments(List commands, String param) {
67	int index = commands.indexOf(param);
68	if (index == -1)
69		return null;
70	commands.remove(index);
71	if (index == commands.size()) // if this is the last command
72		return null;
73	List args = new ArrayList(commands.size());
74	while (index < commands.size()) { // while not the last command
75		String command = (String) commands.get(index);
76		if (command.startsWith("-")) // is it a new parameter?
77			break;
78		args.add(command);
79		commands.remove(index);
80	}
81	if (args.isEmpty())
82		return null;
83	return (String[]) args.toArray(new String[args.size()]);
84}
85
86
87
88
89}
90