summaryrefslogtreecommitdiff
path: root/src/plugins/preflighting/src/com/motorolamobility/preflighting/internal/commandoutput/OutputterFactory.java
blob: c3bbe5bb87b855be84d95e8c4066b9a51cca375a (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
 * 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.
 */
/*
 * 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.commandoutput;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;

import com.motorolamobility.preflighting.core.IParameterProcessor;
import com.motorolamobility.preflighting.core.PreflightingCorePlugin;
import com.motorolamobility.preflighting.core.validation.Parameter;
import com.motorolamobility.preflighting.core.validation.ParameterDescription;
import com.motorolamobility.preflighting.core.validation.ParameterType;
import com.motorolamobility.preflighting.core.validation.ValidationManager.InputParameter;
import com.motorolamobility.preflighting.core.validation.Value;
import com.motorolamobility.preflighting.i18n.PreflightingNLS;
import com.motorolamobility.preflighting.output.AbstractOutputter;

/**
 * Factory class for pre-flighting outputters
 */
public class OutputterFactory implements IParameterProcessor
{

    public static enum OutputType
    {
        CSV, TEXT, XML, DAEMON;

        @Override
        public String toString()
        {
            return super.toString().toLowerCase();
        };

    }

    /**
    * OUTPUT parameter keyword.
    */
    public final static String OUTPUT_PARAMETER = "output"; //$NON-NLS-1$

    /**
     * Maps parameters to possible values
     */
    private final Map<String, List<OutputType>> parameterValueMap =
            new HashMap<String, List<OutputType>>();

    /**
     * Maps parameters to default values
     */
    private final Map<String, Value> parameterDefaultMap = new HashMap<String, Value>();

    /**
     * Maps parameters to their descriptions
     */
    private final Map<String, String> parameterDescriptionMap = new HashMap<String, String>();

    /**
     * Maps possible values to their descriptions
     */
    private final Map<String, String> valueDescriptionMap = new HashMap<String, String>();

    /**
     * Maps possible values to their types
     */
    private final Map<String, ParameterType> parameterTypeMap =
            new HashMap<String, ParameterType>();

    private static OutputterFactory instance;

    private static OutputType defaultOutputter = OutputType.TEXT;

    /**
     * Default constructor.
     */
    private OutputterFactory()
    {
        // Initialize maps

        // List of values
        List<OutputType> valuesList = new ArrayList<OutputType>();
        valuesList.add(OutputType.XML); // XML support not present in this version
        valuesList.add(OutputType.CSV);
        valuesList.add(OutputType.TEXT);

        parameterValueMap.put(OUTPUT_PARAMETER, valuesList);

        Value defaultOutputValue = new Value();
        defaultOutputValue.setDescription(PreflightingNLS.OutputterFactory_TextOutputFormatMessage);
        defaultOutputValue.setValue(OutputType.TEXT.toString());
        parameterDefaultMap.put(OUTPUT_PARAMETER, defaultOutputValue);

        parameterDescriptionMap.put(OUTPUT_PARAMETER,
                PreflightingNLS.OutputterFactory_ValidationOutputModeMessage);

        valueDescriptionMap.put(OutputType.TEXT.toString(),
                PreflightingNLS.OutputterFactory_TextOutputFormatMessage);
        valueDescriptionMap.put(OutputType.CSV.toString(),
                PreflightingNLS.OutputterFactory_XmlOutputFormatNotImplementedMessage);
        valueDescriptionMap.put(OutputType.XML.toString(),
                PreflightingNLS.OutputterFactory_XmlOutputFormatNotImplementedMessage);

        parameterTypeMap.put(OUTPUT_PARAMETER, ParameterType.STRING);
    }

    public static OutputterFactory getInstance()
    {
        if (instance == null)
        {
            instance = new OutputterFactory();
        }
        return instance;
    }

    /**
     * Gets parameters accepted by the outputter created by the factory
     * @return
     */
    public List<ParameterDescription> getParameterDescriptions()
    {
        //TODO Since there is no implemented alternative to TEXT, disable disable parameter
        /*
        List<ParameterDescription> parameterList = new ArrayList<ParameterDescription>();

        for (String p : parameterValueMap.keySet())
        {
            ParameterDescription paramDesc = new ParameterDescription();

            // Set allowed values
            List<Value> valueList = new ArrayList<Value>();
            for (String v : parameterValueMap.get(p))
            {
                Value value = new Value();

                value.setDescription(valueDescriptionMap.get(v));
                value.setValue(v);

                valueList.add(value);
            }
            paramDesc.setType(parameterTypeMap.get(p));
            paramDesc.setAllowedValues(valueList);

            // Set default value
            paramDesc.setDefaultValue(parameterDefaultMap.get(p));

            // Set parameter description
            paramDesc.setDescription(parameterDescriptionMap.get(p));

            // Set parameter name
            paramDesc.setName(p);

            parameterList.add(paramDesc);

        }
        */

        return null;

    }

    /**
     * Create a new outputter passing the application parameter list as parameter
     * @param parameters the application parameters or null to create the default one
     * @return the desired outputter
     */
    public AbstractOutputter createOutputter(List<Parameter> parameters)
    {
        AbstractOutputter outputter = null;
        if (parameters != null)
        {
            for (Parameter param : parameters)
            {
                String parameterType = param.getParameterType();

                if (parameterType.equals(InputParameter.OUTPUT.getAlias()))
                {
                    if (param.getValue() != null)
                    {
                        try
                        {
                            outputter = internalCreateOutputter(param.getValue());
                        }
                        catch (IllegalArgumentException e)
                        {
                            //do nothing
                        }
                    }
                }
            }
        }
        //default outputter
        if (outputter == null)
        {
            outputter = internalCreateOutputter(defaultOutputter.toString());
        }

        return outputter;
    }

    private AbstractOutputter internalCreateOutputter(String type)
    {
        AbstractOutputter outputter = null;
        String typeUpper = type.toUpperCase();
        Map<String, AbstractOutputter> outputters = OutputterExtensionReader.getOutputtersMap();
        outputter = outputters.get(typeUpper);

        // the outputter can be defined through extension point
        if (outputter == null)
        {

            OutputType typeEnum = OutputType.valueOf(typeUpper);

            switch (typeEnum)
            {
                case CSV:
                    outputter = new CSVOutputter();
                    break;

                case XML:
                    outputter = new XmlOutputter();
                    break;

                case DAEMON:
                    outputter = new DaemonXMLOutputter();
                    break;

                case TEXT:
                    outputter = new TextOutputter();
                    break;
                default:

                    break;
            }
        }

        return outputter;
    }

    public IStatus isOutputterAvailable(String value)
    {
        IStatus validationResult = null;

        if (value != null)
        {
            value = value.toUpperCase();
        }

        // if this output is not defined through extension point, see if it is internal
        if (!OutputterExtensionReader.getOutputtersMap().containsKey(value))
        {
            try
            {
                OutputType.valueOf(value);
            }
            catch (IllegalArgumentException e)
            {
                validationResult =
                        new Status(IStatus.ERROR, PreflightingCorePlugin.PLUGIN_ID,
                                PreflightingNLS.OutputterFactory_OutputParametersInvalidMessage);
            }
        }

        // If no problems were found, return a OK status
        if (validationResult == null)
        {
            validationResult =
                    new Status(IStatus.OK, PreflightingCorePlugin.PLUGIN_ID,
                            PreflightingNLS.OutputterFactory_OutputParametersValidMessage);
        }

        return validationResult;
    }

    public IStatus validateInputParams(List<Parameter> parameters)
    {
        return Status.OK_STATUS;
    }

    public OutputType getDefaultOutputter()
    {
        return defaultOutputter;
    }

    public void setDefaultOutputter(OutputType outputter)
    {
        defaultOutputter = outputter;
    }
}