|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 using System; |
|
18 using System.Text; |
|
19 using System.IO; |
|
20 using System.Collections.Generic; |
|
21 using CrashAnalyserEngine.Plugins; |
|
22 using CrashItemLib.Engine; |
|
23 using CrashItemLib.Crash; |
|
24 using CrashItemLib.Crash.Container; |
|
25 using CrashItemLib.PluginAPI; |
|
26 using CrashAnalyserEngine.Engine; |
|
27 using CrashAnalyserEngine.Interfaces; |
|
28 using CAPCrashAnalysis.Plugin; |
|
29 using SymbianUtils.FileSystem.Utilities; |
|
30 using SymbianXmlInputLib.Parser; |
|
31 using SymbianXmlInputLib.Parser.Nodes; |
|
32 using SymbianXmlInputLib.Elements; |
|
33 using SymbianXmlInputLib.Elements.Types.Category; |
|
34 using SymbianXmlInputLib.Elements.Types.Extension; |
|
35 using SymbianXmlInputLib.Elements.Types.FileSystem; |
|
36 using SymbianXmlInputLib.Elements.Types.Command; |
|
37 using CrashItemLib.Sink; |
|
38 |
|
39 namespace CAPCrashAnalysis.CommandLine |
|
40 { |
|
41 internal class CACmdLineInputParameters |
|
42 { |
|
43 #region Constructors |
|
44 public CACmdLineInputParameters( CAPluginCrashAnalysis aEngine ) |
|
45 { |
|
46 iEngine = aEngine; |
|
47 // |
|
48 Version version = aEngine.UIManager.UIVersion; |
|
49 string commandLine = aEngine.UIManager.UICommandLineArguments; |
|
50 // |
|
51 iSinkParams = new CISinkSerializationParameters( version, commandLine ); |
|
52 } |
|
53 #endregion |
|
54 |
|
55 #region API |
|
56 public void Read( string aFileName ) |
|
57 { |
|
58 Trace( "[CmdInput] Read() - aFileName: " + aFileName ); |
|
59 try |
|
60 { |
|
61 // First create the tree |
|
62 SXILDocument doc = CreateDocumentTree( aFileName ); |
|
63 |
|
64 // Then convert it to the list of elements that we care about |
|
65 ExtractData( doc ); |
|
66 } |
|
67 catch ( CACmdLineException cmdLineException ) |
|
68 { |
|
69 Trace( "[CmdInput] Read() - CACmdLineException: " + cmdLineException.Message + " " + cmdLineException.StackTrace ); |
|
70 throw cmdLineException; |
|
71 } |
|
72 catch ( Exception generalException ) |
|
73 { |
|
74 Trace( "[CmdInput] Read() - generalException: " + generalException.Message + " " + generalException.StackTrace ); |
|
75 throw new CACmdLineException( "Error reading input xml file", generalException, CAPlugin.KErrCommandLinePluginArgumentsFileInvalid ); |
|
76 } |
|
77 Trace( "[CmdInput] Read() - read OK: " + aFileName ); |
|
78 } |
|
79 #endregion |
|
80 |
|
81 #region Properties |
|
82 public CISinkSerializationParameters SinkParameters |
|
83 { |
|
84 get { return iSinkParams; } |
|
85 } |
|
86 |
|
87 public CACmdLineFSEntityList<CACmdLineFileSource> SourceFiles |
|
88 { |
|
89 get { return iSources; } |
|
90 } |
|
91 |
|
92 public CACmdLineFSEntityList<CACmdLineFSEntity> MetaDataFiles |
|
93 { |
|
94 get { return iMetaData; } |
|
95 } |
|
96 #endregion |
|
97 |
|
98 #region Internal constants |
|
99 private const string KInputFileDocumentRootNode = "crash_analysis"; |
|
100 private const string KInputFileCategorySource = "source"; |
|
101 private const string KInputFileCategoryDebugMetaData = "debug_meta_data"; |
|
102 private const string KInputFileCategoryParameters = "parameters"; |
|
103 private const string KInputFileCategoryOutput = "output"; |
|
104 private const string KInputFileCommandNameAnalysis = "analysis_type"; |
|
105 private const string KInputFileCommandNameAnalysisFull = "FULL"; |
|
106 private const string KInputFileCommandNameAnalysisSummary = "SUMMARY"; |
|
107 #endregion |
|
108 |
|
109 #region Internal methods |
|
110 private SXILDocument CreateDocumentTree( string aFileName ) |
|
111 { |
|
112 SXILDocument doc = new SXILDocument(); |
|
113 |
|
114 // Read input file into document |
|
115 using ( SXILParser parser = new SXILParser( aFileName, KInputFileDocumentRootNode, doc ) ) |
|
116 { |
|
117 parser.CategoryAdd( KInputFileCategorySource, new SXILParserNodeFileSystem() ); |
|
118 parser.CategoryAdd( KInputFileCategoryDebugMetaData, new SXILParserNodeFileSystem() ); |
|
119 parser.CategoryAdd( KInputFileCategoryParameters, |
|
120 new SXILParserNodeCommand(), |
|
121 new SXILParserNodeExtension() |
|
122 ); |
|
123 parser.CategoryAdd( KInputFileCategoryOutput, new SXILParserNodeFileSystem() ); |
|
124 parser.Parse(); |
|
125 } |
|
126 |
|
127 return doc; |
|
128 } |
|
129 |
|
130 private void ExtractData( SXILDocument aDocument ) |
|
131 { |
|
132 foreach ( SXILElement element in aDocument ) |
|
133 { |
|
134 if ( element is SXILElementCategory ) |
|
135 { |
|
136 SXILElementCategory category = (SXILElementCategory) element; |
|
137 string name = category.Name.ToLower(); |
|
138 // |
|
139 switch ( name ) |
|
140 { |
|
141 case KInputFileCategorySource: |
|
142 ExtractFileList<CACmdLineFileSource>( iSources, category ); |
|
143 break; |
|
144 case KInputFileCategoryDebugMetaData: |
|
145 // The debug meta data engine doesn't support directories anymore |
|
146 // so we have to expand all directories to files. |
|
147 ExtractFileList<CACmdLineFSEntity>( iMetaData, category ); |
|
148 break; |
|
149 case KInputFileCategoryParameters: |
|
150 ExtractParameters( category ); |
|
151 break; |
|
152 case KInputFileCategoryOutput: |
|
153 ExtractOutput( category ); |
|
154 break; |
|
155 } |
|
156 } |
|
157 } |
|
158 |
|
159 // We don't require debug meta data if performing a summary operation. Otherwise, we do. |
|
160 if ( iMetaData.Count == 0 && iSinkParams.DetailLevel == CISinkSerializationParameters.TDetailLevel.EFull ) |
|
161 { |
|
162 Trace( "[CmdInput] ExtractData() - WARNING - no debug meta data supplied for full analysis." ); |
|
163 } |
|
164 |
|
165 } |
|
166 |
|
167 private void ExtractFileList<T>( CACmdLineFSEntityList<T> aList, SXILElementCategory aCategory ) where T : CACmdLineFSEntity, new() |
|
168 { |
|
169 foreach ( SXILElement element in aCategory ) |
|
170 { |
|
171 if ( element is SXILElementFile ) |
|
172 { |
|
173 SXILElementFile file = (SXILElementFile) element; |
|
174 Trace( "[CmdInput] ExtractFileList() - file: " + file ); |
|
175 if ( !file.Exists ) |
|
176 { |
|
177 throw new FileNotFoundException( "File not found", file.Name ); |
|
178 } |
|
179 // |
|
180 aList.Add( file ); |
|
181 } |
|
182 else if ( element is SXILElementDirectory ) |
|
183 { |
|
184 SXILElementDirectory dir = (SXILElementDirectory) element; |
|
185 Trace( "[CmdInput] ExtractFileList() - dir: " + dir ); |
|
186 if ( !dir.Exists ) |
|
187 { |
|
188 throw new DirectoryNotFoundException( "Directory not found: " + dir.Name ); |
|
189 } |
|
190 // |
|
191 aList.AddRange( dir.Files ); |
|
192 } |
|
193 } |
|
194 } |
|
195 |
|
196 private void ExtractParameters( SXILElementCategory aCategory ) |
|
197 { |
|
198 foreach ( SXILElement element in aCategory ) |
|
199 { |
|
200 if ( element is SXILElementExtension ) |
|
201 { |
|
202 SXILElementExtension entry = (SXILElementExtension) element; |
|
203 // |
|
204 string extension = entry.Name; |
|
205 if ( !extension.StartsWith( "." ) ) |
|
206 { |
|
207 extension = "." + extension; |
|
208 } |
|
209 // |
|
210 if ( entry.Type == SXILElementExtension.TType.ETypeFailure ) |
|
211 { |
|
212 Trace( "[CmdInput] ExtractFileList() - failed extension: " + extension ); |
|
213 iSinkParams.FileExtensionFailed = extension; |
|
214 } |
|
215 else if ( entry.Type == SXILElementExtension.TType.ETypeSuccess ) |
|
216 { |
|
217 Trace( "[CmdInput] ExtractFileList() - success extension: " + extension ); |
|
218 iSinkParams.FileExtensionSuccess = extension; |
|
219 } |
|
220 } |
|
221 else if ( element is SXILElementCommand ) |
|
222 { |
|
223 SXILElementCommand entry = (SXILElementCommand) element; |
|
224 // |
|
225 if ( entry.Name == KInputFileCommandNameAnalysis ) |
|
226 { |
|
227 string type = entry.Details.Trim().ToUpper(); |
|
228 Trace( "[CmdInput] ExtractFileList() - command: " + type ); |
|
229 switch ( type ) |
|
230 { |
|
231 case KInputFileCommandNameAnalysisFull: |
|
232 iSinkParams.DetailLevel = CISinkSerializationParameters.TDetailLevel.EFull; |
|
233 break; |
|
234 case KInputFileCommandNameAnalysisSummary: |
|
235 iSinkParams.DetailLevel = CISinkSerializationParameters.TDetailLevel.ESummary; |
|
236 break; |
|
237 default: |
|
238 throw new NotSupportedException( "Unsupported analysis type" ); |
|
239 } |
|
240 } |
|
241 else |
|
242 { |
|
243 throw new NotSupportedException( "Unsupported command: " + entry.Name ); |
|
244 } |
|
245 } |
|
246 } |
|
247 } |
|
248 |
|
249 private void ExtractOutput( SXILElementCategory aCategory ) |
|
250 { |
|
251 // We either output to file or directory - if both are present then bail out |
|
252 FileInfo savedFile = null; |
|
253 DirectoryInfo savedDir = null; |
|
254 // |
|
255 foreach ( SXILElement element in aCategory ) |
|
256 { |
|
257 if ( element is SXILElementFile ) |
|
258 { |
|
259 if ( savedFile != null ) |
|
260 { |
|
261 throw new InvalidDataException( "Output file already specified" ); |
|
262 } |
|
263 else |
|
264 { |
|
265 SXILElementFile file = (SXILElementFile) element; |
|
266 savedFile = file; |
|
267 } |
|
268 } |
|
269 else if ( element is SXILElementDirectory ) |
|
270 { |
|
271 if ( savedDir != null ) |
|
272 { |
|
273 throw new InvalidDataException( "Output directory already specified" ); |
|
274 } |
|
275 else |
|
276 { |
|
277 SXILElementDirectory dir = (SXILElementDirectory) element; |
|
278 savedDir = dir; |
|
279 } |
|
280 } |
|
281 } |
|
282 |
|
283 // Ensure we have only one type |
|
284 if ( savedFile != null && savedDir != null ) |
|
285 { |
|
286 throw new InvalidDataException( "Output must be EITHER file or directory" ); |
|
287 } |
|
288 else if ( savedFile != null ) |
|
289 { |
|
290 iSinkParams.OutputFile = savedFile; |
|
291 } |
|
292 else if ( savedDir != null ) |
|
293 { |
|
294 iSinkParams.OutputDirectory = savedDir; |
|
295 } |
|
296 } |
|
297 |
|
298 public void Trace( string aMessage ) |
|
299 { |
|
300 iEngine.UIManager.UITrace( aMessage ); |
|
301 } |
|
302 |
|
303 public void Trace( string aFormat, params object[] aParams ) |
|
304 { |
|
305 iEngine.UIManager.UITrace( aFormat, aParams ); |
|
306 } |
|
307 #endregion |
|
308 |
|
309 #region Data members |
|
310 private readonly CAPluginCrashAnalysis iEngine; |
|
311 private readonly CISinkSerializationParameters iSinkParams; |
|
312 private CACmdLineFSEntityList<CACmdLineFSEntity> iMetaData = new CACmdLineFSEntityList<CACmdLineFSEntity>(); |
|
313 private CACmdLineFSEntityList<CACmdLineFileSource> iSources = new CACmdLineFSEntityList<CACmdLineFileSource>(); |
|
314 #endregion |
|
315 } |
|
316 } |