summaryrefslogtreecommitdiff
path: root/src/plugins/preflighting/src/com/motorolamobility/preflighting/internal/commandinput/CommandLineInputProcessor.java
blob: 293bc49e26bab0c29302791d8f4bb4d31693f2af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.motorolamobility.preflighting.internal.commandinput;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.osgi.util.NLS;

import com.motorolamobility.preflighting.core.validation.ComplexParameter;
import com.motorolamobility.preflighting.core.validation.Parameter;
import com.motorolamobility.preflighting.i18n.PreflightingNLS;
import com.motorolamobility.preflighting.internal.commandinput.exception.ParameterParseException;

/**
 * Process command line commands below:
 */
public class CommandLineInputProcessor
{
    /**
     * White space character
     */
    private static final String WHITE_SPACE = " "; //$NON-NLS-1$

    /**
     * Simple parameter marker character
     */
    private static final String PARAMETER_MARKER = "-"; //$NON-NLS-1$

    /**
     * Simple parameter separator
     */
    private static final String COMPLEX_PARAMETER_SEPARATOR = "="; //$NON-NLS-1$

    /**
     * 
     * @param params
     * @return
     * @throws ParameterParseException
     */
    public List<Parameter> processCommandLine(String params) throws ParameterParseException
    {
        // Parse command line arguments and create parameters
        List<Parameter> commandLineParameters = new ArrayList<Parameter>();

        // if no parameters were passed, assume help will be printed
        if (params.length() == 0)
        {
            Parameter helpParameter =
                    new Parameter(ApplicationParameterInterpreter.PARAM_HELP, null);
            commandLineParameters.add(helpParameter);
        }
        else
        {
            // find the pattern " -", which is expected to separate each parameter
            String regex = "((?!(\\s+" + PARAMETER_MARKER + ")).)*"; //$NON-NLS-1$ //$NON-NLS-2$
            Pattern pat = Pattern.compile(regex);
            Matcher matcher = pat.matcher(params);
            Parameter commandLineparameter = null;

            // for each parameter part found, process it
            while (matcher.find())
            {
                String parameterValues = params.substring(matcher.start(), matcher.end());

                if (parameterValues.length() > 0)
                {
                    commandLineparameter = getCommandLineParameter(parameterValues);
                    commandLineParameters.add(commandLineparameter);
                }
            }
        }
        return commandLineParameters;
    }

    /**
     * Gets a parameter from command line. Command line arguments are passed to the 
     * application like this: -arg foo bar -c check1 name1=val1 name2=val2
     * 
     * The parameter for this example is:
     * name = arg
     * values = foo, bar
     * 
     * @param parameterValues String containing the parameter and its values. Eg: -arg foo bar
     * @return And instance of object Parameter
     * @throws ParameterParseException 
     */
    private Parameter getCommandLineParameter(String parameterValues)
            throws ParameterParseException
    {

        Parameter parameter = null;

        if (parameterValues.indexOf(COMPLEX_PARAMETER_SEPARATOR) == -1)
        {

            // Its a simple parameter, example: -param1 value1 or example: -param2
            parameter = getParameterFromCommandLine(parameterValues);

        }
        else
        {
            // Its a complex parameter, example: -c checker1 file=C:\arg.txt version=2
            parameter = getComplexParameterFromCommandLine(parameterValues);
        }

        return parameter;
    }

    /**
     * Get a complex parameter from command line. Example: -c checker1 param1=val1 param2=var2
     * @param values String[] containing the complex parameter. For example, for command line -c checker1 param1=val1 param2=var2
     * the contents of values are: [[c] [check1] [param1=val1] [param2=val2]]
     * @return Instance of Parameter, containing a ComplexParameter
     * @throws ParameterParseException 
     */
    private Parameter getComplexParameterFromCommandLine(String parameterValues)
            throws ParameterParseException
    {
        // complex parameter example: -c checker param1=val1 param2=val2
        ComplexParameter parameter = new ComplexParameter();

        String[] values = parameterValues.split(WHITE_SPACE);

        if (values.length < 3)
        {
            throw new ParameterParseException(NLS.bind(
                    PreflightingNLS.CommandLineInputProcessor_IncorrectSyntax, parameterValues));
        }
        else
        {

            String parameterType = values[0];

            if (parameterType.startsWith(PARAMETER_MARKER))
            {
                parameterType = parameterType.substring(1);
            }
            parameter.setParameterType(parameterType);
            parameter.setValue(values[1]);

            int lenghtToCut = values[0].length() + values[1].length() + 2;
            String subParameters = parameterValues.substring(lenghtToCut);

            String[] checkSyntax = subParameters.split("\\s*\\w+\\s*" + COMPLEX_PARAMETER_SEPARATOR //$NON-NLS-1$
                    + "\\s*\\w+\\s*"); //$NON-NLS-1$
            if (checkSyntax.length > 0)
            {
                throw new ParameterParseException(NLS.bind(
                        PreflightingNLS.CommandLineInputProcessor_IncorrectSyntax, parameterValues));
            }

            Pattern pat = Pattern.compile("\\w+\\s*" + COMPLEX_PARAMETER_SEPARATOR + "\\s*\\w+"); //$NON-NLS-1$ //$NON-NLS-2$
            Matcher matcher = pat.matcher(subParameters);

            while (matcher.find())
            {
                String part = subParameters.substring(matcher.start(), matcher.end());
                String[] pair = part.split(COMPLEX_PARAMETER_SEPARATOR);
                parameter.addParameter(pair[0].trim(), pair[1].trim());
            }
        }
        return parameter;
    }

    /**
     * Get a parameter from command line (example -param value), passed as a String[].
     * @param parameterValues parameter values
     * @return Instance of Parameter
     * @throws ParameterParseException 
     */
    private Parameter getParameterFromCommandLine(String parameterValues)
            throws ParameterParseException
    {
        Parameter parameter = new Parameter();

        // find first white space to separate tag from value
        int indexOfFirstWhiteSpace = parameterValues.indexOf(WHITE_SPACE);
        String parameterType = null;
        String parameterValue = null;

        // if there is no whitespace, everything is the parameter value necessary
        if (indexOfFirstWhiteSpace >= 0)
        {
            // substring original string from character 1 (skip "-"), to first
            // whitespace character
            parameterType = parameterValues.substring(1, indexOfFirstWhiteSpace);
            // retrieve the rest of the original string (starting from first whitespace)
            // as the value for the parameter
            parameterValue = parameterValues.substring(indexOfFirstWhiteSpace).trim();
        }
        else
        {
            // skip "-"
            parameterType = parameterValues.substring(1);
        }

        // check for unexpected format (both parts of the parameter empty)
        if (((parameterValue == null) || (parameterValue.length() == 0))
                && ((parameterType == null) || (parameterType.length() == 0)))
        {
            throw new ParameterParseException(NLS.bind(
                    PreflightingNLS.CommandLineInputProcessor_IncorrectSyntax, parameterValues));
        }

        // store parameter type and value
        parameter.setParameterType(parameterType);
        if ((parameterValue != null) && (parameterValue.length() > 0))
        {
            parameter.setValue(parameterValue);
        }

        return parameter;
    }
}