Skip to main content
summaryrefslogtreecommitdiffstats
blob: 879aa2e9a11a137de4788779c43b24e92aaba50e (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2002-2005 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *   IBM - Initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.wsi.internal.core.analyzer;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
import java.util.Vector;

import org.eclipse.wst.wsi.internal.core.ToolInfo;
import org.eclipse.wst.wsi.internal.core.WSIConstants;
import org.eclipse.wst.wsi.internal.core.WSIException;
import org.eclipse.wst.wsi.internal.core.analyzer.config.AnalyzerConfig;
import org.eclipse.wst.wsi.internal.core.document.DocumentFactory;
import org.eclipse.wst.wsi.internal.core.log.Log;
import org.eclipse.wst.wsi.internal.core.profile.ProfileAssertions;
import org.eclipse.wst.wsi.internal.core.profile.validator.ProfileValidatorFactory;
import org.eclipse.wst.wsi.internal.core.report.Reporter;
import org.eclipse.wst.wsi.internal.core.util.MessageList;
import org.eclipse.wst.wsi.internal.core.util.TestUtils;

/**
 * The Analyzer will process all Profile conformance functions.
 * This class should be sub-classed for each profile.
 *
 * @version 1.0.1
 * @author Jim Clune
 * @author Peter Brittenham
 */
public abstract class Analyzer
{
  /**
   * Message list.
   */
  protected MessageList messageList = null;

  protected final static String RESOURCE_BUNDLE_NAME =
    "org.eclipse.wst.wsi.internal.core.analyzer.Analyzer";

  public final static String USAGE_MESSAGE =
    "Usage: Analyzer -config <configFilename>";

  protected Reporter reporter = null;
  protected AnalyzerContext analyzerContext = null;
  protected CandidateInfo candidateInfo = null;

  /**
   * Analyzer tool information.
   */
  protected ToolInfo toolInfo = null;

  /**
   * Profile validator factory.
   */
  protected ProfileValidatorFactory factory = null;

  /**
   * Profile validator factory.
   */
  protected DocumentFactory documentFactory = null;

  /**
   * Analyzer config file list.
   */
  protected List analyzerConfigList = new Vector();
  protected int analyzerConfigIndex = 0;

  /**
   * Profile assertions.
   */
  protected ProfileAssertions profileAssertions = null;

  /**
   * Log file.
   */
  protected Log log = null;

  /**
   * Create an instance of the analyzer tool.
   * @param args      command line arguments.
   * @param toolInfo  analyzer tool information.
   * @throws WSIException if problems creating the instance of the analyzer tool.
   */
  public Analyzer(String[] args, ToolInfo toolInfo) throws WSIException
  {
    init(toolInfo);

    // Get new config object   
    this.analyzerConfigList.add(
      analyzerConfigIndex,
      documentFactory.newAnalyzerConfig());
    getAnalyzerConfig().init(messageList);

    // Parse command line arguments
    getAnalyzerConfig().parseArgs(args, true);

    // Display copyright and options that were specified
    TestUtils.printToolInfo(toolInfo);
    System.out.println(getAnalyzerConfig().toString());

    // Display message
    printMessage(
      "progress01",
      null,
      "Please wait while the specified artifacts are analyzed...");
  }

  /**
   * Create an instance of the analyzer tool.
   * @param args      command line arguments.
   * @param toolInfo  analyzer tool information.
   * @param validate  flag for config options validation. 
   * @throws WSIException if problems creating the instance of the analyzer tool.
   */
  public Analyzer(String[] args, ToolInfo toolInfo, boolean validate) throws WSIException
  {
    init(toolInfo);

    // Get new config object   
    this.analyzerConfigList.add(
        analyzerConfigIndex,
        documentFactory.newAnalyzerConfig());
    getAnalyzerConfig().init(messageList);

    // Parse command line arguments
    getAnalyzerConfig().parseArgs(args, validate);

    // Display copyright and options that were specified
    TestUtils.printToolInfo(toolInfo);
    System.out.println(getAnalyzerConfig().toString());

    // Display message
    printMessage(
        "progress01",
        null,
    "Please wait while the specified artifacts are analyzed...");
  }

  /**
   * Create an instance of the analyzer tool.
   * @param analyzerConfigList  analyzer config file list.
   * @param toolInfo            analyzer tool information.
   * @throws WSIException if problems creating the instance of the analyzer tool.
   */
  public Analyzer(List analyzerConfigList, ToolInfo toolInfo)
    throws WSIException
  {
    init(toolInfo);

    this.analyzerConfigList = analyzerConfigList;
  }

  /**
   * Common initialization.
   * @param toolInfo  analyzer tool information.
   * @throws WSIException if problems occur during common initialization.
   */
  protected void init(ToolInfo info) throws WSIException
  {
    this.toolInfo = info;

    // Create message list
    messageList = new MessageList(RESOURCE_BUNDLE_NAME);

    // Create profile validator factory
    factory = ProfileValidatorFactory.newInstance();

    // Create document factory
    documentFactory = DocumentFactory.newInstance();
  }

  /**
   * Process all conformance validation functions.
   * @return status code.
   * @throws WSIException if problems occur during validation.
   */
  public int validateAll() throws WSIException
  {
    int statusCode = 0;
    int tempStatusCode;

    for (int index = 0; index < analyzerConfigList.size(); index++)
    {
      // DEBUG:
      //System.out.println("index: " + index);

      // Set current analyzer config index 
      setAnalyzerConfig(index);

      if ((tempStatusCode = validateConformance()) > statusCode)
        statusCode = tempStatusCode;
    }

    return statusCode;
  }

  /**
   * Process all conformance validation functions.
   * @return status code.
   * @throws WSIException if problems occur during conformance validation.
   */
  public abstract int validateConformance() throws WSIException;

  /**
   * Get tool information.
   * @return the tool information.
   */
  public ToolInfo getToolInfo()
  
  {
    return this.toolInfo;
  }

  /**
   * Set current analyzer configuration.
   * @param index  urrent analyzer configuration.
   * @see #getAnalyzerConfig
   */
  protected void setAnalyzerConfig(int index)
  {
    this.analyzerConfigIndex = index;
  }

  protected int getAnalyzerConfigIndex()
  {
    return this.analyzerConfigIndex;
  }

  /**
   * Get analyzer configuration information.
   * @return analyzer configuration information.
   * @see #setAnalyzerConfig
   */
  public AnalyzerConfig getAnalyzerConfig()
  {
    return (AnalyzerConfig) this.analyzerConfigList.get(analyzerConfigIndex);
  }

  /**
   * Print a message from the resource bundle.
   * @param key             a key.
   * @param messageData     message data.
   * @param defaultMessage  a default message.
   */
  public void printMessage(
    String key,
    String messageData,
    String defaultMessage)
  {
    messageList.printMessage(key, messageData, defaultMessage);
  }

  /**
   * Print message.
   * @param key             a key.
   * @param messageData     message data.
   * @param defaultMessage  a default message.
   */
  public static void staticPrintMessage(
    String key,
    String messageData,
    String defaultMessage)
  {
    MessageList.printMessage(
      RESOURCE_BUNDLE_NAME,
      key,
      messageData,
      defaultMessage);
  }

  /**
   * Return XML string representation of this object.
   * @param namespaceName  a namespace prefix.
   * @return XML string representation of this object.
   */
  public String toXMLString(String namespaceName)
  {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);

    // Start     
    pw.print(toolInfo.getStartXMLString(namespaceName));

    // Config
    pw.print(
      getAnalyzerConfig().toXMLString(
        WSIConstants.NS_NAME_WSI_ANALYZER_CONFIG));

    // End
    pw.println(toolInfo.getEndXMLString(namespaceName));

    return sw.toString();
  }

  /**
   * Print a message.
   * @param message a message.
   */
  public void printMessage(String message)
  {
    if (getAnalyzerConfig().getVerboseOption())
    {
      System.err.println(message);
    }
  }
}

Back to the top