Subversion Repositories DevTools

Rev

Rev 2106 | Rev 2116 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2088 ghuddy 1
using System;
2
using System.Windows.Forms;
3
using System.IO;
4
using System.Collections;
2094 ghuddy 5
using System.Collections.Specialized;
6
using System.Text;
2088 ghuddy 7
 
8
namespace EA_DocGen
9
{
10
   /// <summary>
11
   /// Summary description for EA_DocGenOptions.
12
   /// </summary>
13
   public class EA_DocGenOptions
14
   {
2106 ghuddy 15
      // Strings used in the notes section of an EA_DocGen element that persists the options.
16
      public static string optstr_GeneralOptions = "[GeneralOptions]";
17
      public static string optstr_ElementTypes   = "[ElementTypes]";
18
      public static string optstr_SuppressElementDescriptionMissingWarnings = "SuppressElementDescriptionMissingWarnings";
19
      public static string optstr_SuppressUnAllocatedRelationshipWarnings = "SuppressUnAllocatedRelationshipWarnings";
20
      public static string optstr_SuppressPrivateAttributes = "SuppressPrivateAttributes";
21
      public static string optstr_SuppressPrivateMethods    = "SuppressPrivateMethods";
22
      public static string optstr_SuppressPrivateClasses    = "SuppressPrivateClasses";
23
      public static string optstr_ConsiderAPIElementsOnly   = "ConsiderAPIClassesOnly";
24
      public static string optstr_ConsiderAPIPackagesOnly   = "ConsiderAPIPackagesOnly";
25
      public static string optstr_ConsiderAPIDiagramsOnly   = "ConsiderAPIDiagramsOnly";
26
      public static string optstr_RestrictForLinkedPackagesOnly = "APIPackageRestrictionForLinkedPackagesOnly";
2094 ghuddy 27
 
28
 
2106 ghuddy 29
      private static string [] options_string = null;
30
      private static bool options_found = false;
2088 ghuddy 31
 
32
      // General options
33
 
2106 ghuddy 34
      public static bool opt_SuppressElementDescriptionMissingWarnings;
35
      public static bool opt_SuppressUnAllocatedRelationshipWarnings;
2094 ghuddy 36
 
2088 ghuddy 37
 
2094 ghuddy 38
      // Visibility options
2106 ghuddy 39
      public static bool opt_SuppressPrivateAttributes;
40
      public static bool opt_SuppressPrivateMethods;
41
      public static bool opt_SuppressPrivateClasses;
2094 ghuddy 42
 
2106 ghuddy 43
      public static bool opt_ConsiderAPIElementsOnly;
44
      public static bool opt_ConsiderAPIPackagesOnly;
45
      public static bool opt_ConsiderAPIDiagramsOnly;
46
      public static bool opt_RestrictForLinkedPackagesOnly;
2094 ghuddy 47
 
48
 
2088 ghuddy 49
      // Element Types list
2106 ghuddy 50
      public static ArrayList opt_ElementTypes = null;
2088 ghuddy 51
 
2108 ghuddy 52
 
2106 ghuddy 53
      public static void initialise()
2088 ghuddy 54
      {
2108 ghuddy 55
         if (opt_ElementTypes == null)
56
            opt_ElementTypes = new ArrayList();
57
         opt_ElementTypes.Clear();
2088 ghuddy 58
         setDefaults();
59
      }
60
 
2106 ghuddy 61
      private static void setDefaults()
2088 ghuddy 62
      {
2094 ghuddy 63
         opt_SuppressElementDescriptionMissingWarnings = false;
2104 ghuddy 64
         opt_SuppressUnAllocatedRelationshipWarnings = false;
65
         opt_SuppressPrivateAttributes = false;
66
         opt_SuppressPrivateMethods    = false;
67
         opt_SuppressPrivateClasses    = false;
68
         opt_ConsiderAPIElementsOnly   = false;
69
         opt_ConsiderAPIDiagramsOnly   = false;
70
         opt_ConsiderAPIPackagesOnly   = false;
71
         opt_RestrictForLinkedPackagesOnly = true;
2088 ghuddy 72
      }
73
 
74
 
75
      /// <summary>
76
      /// Looks for the EA_DocGen element in the selected package. To generate a document
77
      /// on a package, the package must contain the EA_DocGen element, even if that elements
78
      /// notes are empty. It serves to denote a package as a document model that is allowed
79
      /// to be processed by this add-in.
80
      /// </summary>
81
      /// <param name="parentPackage"></param>
82
      /// <returns></returns>
2106 ghuddy 83
      public static bool lookForAndProcess_EA_DocGen_Element( EA.Package parentPackage )
2088 ghuddy 84
      {
85
         options_found  = false;
86
 
87
         // Look for an EA_DocGen element in this package
88
         foreach(EA.Element theElement in parentPackage.Elements)
89
         {
2094 ghuddy 90
            options_found = lookForAndProcess_EA_DocGen_Element(theElement);
91
            if (options_found)
92
               break;
93
         }            
94
         return options_found;
95
      }
2088 ghuddy 96
 
2106 ghuddy 97
      public static bool lookForAndProcess_EA_DocGen_Element( EA.Element theElement )
2094 ghuddy 98
      {
2108 ghuddy 99
         initialise();
100
 
2094 ghuddy 101
         options_string = null;
102
         options_found  = false;
2088 ghuddy 103
 
2094 ghuddy 104
         // Special handling for the EA_DocGen element designed to control this document
105
         // generator
106
         if (0 == theElement.Name.CompareTo(EA_Constants.EA_DocGenBaseName))
107
         {
108
            options_found = true;
2088 ghuddy 109
 
2094 ghuddy 110
            // Extract the content of the EA_DocGen element notes section, into a list of
111
            // strings.
112
            string delimStr = "\n";
113
            char [] delim = delimStr.ToCharArray();
114
            options_string = theElement.Notes.ToString().Split(delim,200);
115
 
116
            int i = 0;
117
            foreach(string s in options_string)
118
            {
119
               options_string[i] = s.Trim();
120
               i++;
2088 ghuddy 121
            }
122
 
2094 ghuddy 123
            // Extract general options from the list of strings
124
            extractGeneralOptions();
125
 
126
            // Extract element types list from the list of strings
127
            extractElementTypes();
128
         }
129
 
2088 ghuddy 130
         return options_found;
131
      }
132
 
133
 
2106 ghuddy 134
      private static string getOptionValueString(string optionLine)
2088 ghuddy 135
      {
136
         string delimStr = "=";
137
         char [] delim = delimStr.ToCharArray();
138
         string [] optionLineParts = optionLine.Split(delim,2);
139
         if (optionLineParts.GetLength(0) == 2)
140
         {
141
            return optionLineParts[1];
142
         }
143
         else
144
         {
145
            MessageBox.Show( "Error in EA_DocGen option\n\n" + optionLine + "\n\n" + "Missing value" );
146
         }
147
         return null;
148
      }
149
 
150
 
151
      // Overloaded "get option value" methods
2106 ghuddy 152
      public static int getOptionValue(string s, int defaultReturnValue)
2088 ghuddy 153
      {
154
         int returnVal = defaultReturnValue;
155
 
156
         string optionValue = getOptionValueString(s);
157
         if (optionValue != null)
158
         {
159
            try
160
            {
161
               returnVal = Convert.ToInt32(optionValue);
162
            }
163
            catch(Exception)
164
            {
165
               MessageBox.Show( "Error in EA_DocGen option\n\n" + s + "\n\n" + "Expected valid integer" );
166
            }
167
         }   
168
         return returnVal;
169
      }
170
 
2106 ghuddy 171
      public static double getOptionValue(string s, double defaultReturnValue)
2100 ghuddy 172
      {
173
         double returnVal = defaultReturnValue;
174
 
175
         string optionValue = getOptionValueString(s);
176
         if (optionValue != null)
177
         {
178
            try
179
            {
180
               returnVal = Convert.ToDouble(optionValue);
181
            }
182
            catch(Exception)
183
            {
184
               MessageBox.Show( "Error in EA_DocGen option\n\n" + s + "\n\n" + "Expected valid double" );
185
            }
186
         }   
187
         return returnVal;
188
      }
189
 
2106 ghuddy 190
      public static bool getOptionValue(string s, bool defaultReturnValue)
2088 ghuddy 191
      {
192
         bool returnVal = defaultReturnValue;
193
 
194
         string optionValue = getOptionValueString(s);
195
         if (optionValue != null)
196
         {
197
            try
198
            {
199
               returnVal = Convert.ToBoolean(optionValue);
200
            }
201
            catch(Exception)
202
            {
203
               MessageBox.Show( "Error in EA_DocGen option\n\n" + s + "\n\n" + "Expected valid boolean" );
204
            }
205
         }   
206
         return returnVal;
207
      }
208
 
209
 
2106 ghuddy 210
      public static string getOptionValue(string s, string defaultReturnValue)
2088 ghuddy 211
      {
212
         string returnVal = defaultReturnValue;
213
 
214
         string optionValue = getOptionValueString(s);
215
         if (optionValue != null)
216
         {
217
            returnVal = optionValue;
218
         }   
219
         return returnVal;
220
      }
221
 
222
 
2106 ghuddy 223
      public static char getOptionValue(string s, char defaultReturnValue)
2088 ghuddy 224
      {
225
         char returnVal = defaultReturnValue;
226
 
227
         string optionValue = getOptionValueString(s);
228
         if (optionValue != null && optionValue.Length > 0)
229
         {
230
            returnVal = optionValue[0];
231
         }   
232
         return returnVal;
233
      }
234
 
235
 
236
 
237
      /// <summary>
238
      /// Method to extract all the general options from the string array obtained from
239
      /// the EA_DocGen element
240
      /// </summary>
2106 ghuddy 241
      private static void extractGeneralOptions()
2088 ghuddy 242
      {
243
         bool parsingGenOpts = false;
244
 
245
         if (options_string != null)
246
         {
247
            // iterate through the string array obtained from the EA_DocGen element
248
            foreach(string s in options_string)
249
            {
2094 ghuddy 250
               if (s.StartsWith(optstr_GeneralOptions))
2088 ghuddy 251
               {
252
                  parsingGenOpts = true;
253
                  continue;
254
               }
255
               else if (   (parsingGenOpts == true) 
256
                        && (s.Length >= 1)
257
                        && (s.Substring(0,1) == "[") )
258
               {
259
                  // we have gone past the general options and reached another section in EA_DocGen
260
                  // so there is no point in continuing. Exit the loop
261
                  break;
262
               }
263
 
2106 ghuddy 264
               // BIG WARNING
265
               // if any option string is a substring of another option string, then the longest of the
266
               // two must appear first in this if-then-else sequence.
267
 
2088 ghuddy 268
               if (parsingGenOpts == true)
269
               {
2104 ghuddy 270
                  if (s.StartsWith(optstr_SuppressElementDescriptionMissingWarnings))
2088 ghuddy 271
                  {
2104 ghuddy 272
                     opt_SuppressElementDescriptionMissingWarnings = getOptionValue(s, opt_SuppressElementDescriptionMissingWarnings);
2088 ghuddy 273
                  }
2104 ghuddy 274
                  else if (s.StartsWith(optstr_SuppressUnAllocatedRelationshipWarnings))
2088 ghuddy 275
                  {
2104 ghuddy 276
                     opt_SuppressUnAllocatedRelationshipWarnings = getOptionValue(s, opt_SuppressUnAllocatedRelationshipWarnings);
2088 ghuddy 277
                  }
2094 ghuddy 278
                  else if (s.StartsWith(optstr_SuppressPrivateClasses))
279
                  {
280
                     opt_SuppressPrivateClasses = getOptionValue(s, opt_SuppressPrivateClasses);
281
                  }
2096 ghuddy 282
                  else if (s.StartsWith(optstr_SuppressPrivateAttributes))
2094 ghuddy 283
                  {
284
                     opt_SuppressPrivateAttributes = getOptionValue(s, opt_SuppressPrivateAttributes);
285
                  }
286
                  else if (s.StartsWith(optstr_SuppressPrivateMethods))
287
                  {
288
                     opt_SuppressPrivateMethods = getOptionValue(s, opt_SuppressPrivateMethods);
289
                  }
2104 ghuddy 290
                  else if (s.StartsWith(optstr_ConsiderAPIElementsOnly))
291
                  {
292
                     opt_ConsiderAPIElementsOnly = getOptionValue(s, opt_ConsiderAPIElementsOnly);
293
                  }
294
                  else if (s.StartsWith(optstr_ConsiderAPIPackagesOnly))
295
                  {
296
                     opt_ConsiderAPIPackagesOnly = getOptionValue(s, opt_ConsiderAPIPackagesOnly);
297
                  }
298
                  else if (s.StartsWith(optstr_RestrictForLinkedPackagesOnly))
299
                  {
300
                     opt_RestrictForLinkedPackagesOnly = getOptionValue(s, opt_RestrictForLinkedPackagesOnly);
301
                  }
302
                  else if (s.StartsWith(optstr_ConsiderAPIDiagramsOnly))
303
                  {
304
                     opt_ConsiderAPIDiagramsOnly = getOptionValue(s, opt_ConsiderAPIDiagramsOnly);
305
                  }
2088 ghuddy 306
                  // add others here
307
 
308
               }
309
            }         
310
         }
311
      }
312
 
313
 
2106 ghuddy 314
      private static void extractElementTypes()
2088 ghuddy 315
      {
316
         bool parsingElementTypes = false;
317
 
318
         opt_ElementTypes.Clear();
319
 
320
         if (options_string != null)
321
         {
322
            // iterate through the string array obtained from the EA_DocGen element
323
            foreach(string s in options_string)
324
            {
2094 ghuddy 325
               if (s.StartsWith(optstr_ElementTypes))
2088 ghuddy 326
               {
327
                  parsingElementTypes = true;
328
                  continue;
329
               }
330
               else if (   (parsingElementTypes == true) 
331
                        && (s.Length >= 1)
332
                        && (s.Substring(0,1) == "[") )
333
               {
334
                  // we have gone past the element types and reached another section in EA_DocGen
335
                  // so there is no point in continuing. Exit the loop
336
                  break;
337
               }
338
 
339
               if (parsingElementTypes == true)
340
               {
341
                  string trimmed_s = s.Trim();
342
                  if (trimmed_s.Length > 0)
343
                  {
344
                     opt_ElementTypes.Add( s );
345
                  }
346
               }
347
            }
348
         }
349
      }
350
 
351
 
352
      /// <summary>
353
      /// This method searches the EA_DocGen string list to find the element types the user
354
      /// has specified that must be included in the document. If the element type given to
355
      /// the function is not in that list then it is excluded from the generated document.
356
      /// The notes of the EA_DocGen element are formed like a .ini file and contain sections 
357
      /// and section content along the lines of:
358
      /// 
359
      /// [sectionName]
360
      /// value
361
      /// value
362
      /// etc
363
      /// [anotherSectionName]
364
      /// value
365
      /// value
366
      /// etc
367
      /// 
368
      /// The [ElementTypes] section is the one this function is interested in
369
      /// 
370
      /// </summary>
371
      /// <param name="elementName"></param>
372
      /// <returns></returns>
2106 ghuddy 373
      public static bool elementTypeFoundInEA_DocGen(string elementType)
2088 ghuddy 374
      {
375
         if (opt_ElementTypes != null && opt_ElementTypes.Count > 0)
376
         {
377
            foreach(string s in opt_ElementTypes)
378
            {
379
               if (s.StartsWith(elementType))
380
               {
381
                  return true;
382
               }
383
            } 
384
            // elementType is not in the opt_ElementTypes list, so element is filtered out
385
            return false;
386
         }
387
         else
388
         {
389
            // opt_ElementTypes is empty so no filtering, return true 
390
            return true;
391
         }
392
      }
393
 
2106 ghuddy 394
      private static void updateEA_DocGen(ref StringBuilder sb, bool bValue, string s)
2094 ghuddy 395
      {
396
         sb.Append( s );
397
         sb.Append( "=" );
398
         sb.Append( bValue.ToString() );
399
         sb.Append( "\r\n" );
400
      }
2088 ghuddy 401
 
2106 ghuddy 402
      private static void updateEA_DocGen(ref StringBuilder sb, int iValue, string s)
2094 ghuddy 403
      {
404
         sb.Append( s );
405
         sb.Append( "=" );
406
         sb.Append( iValue.ToString() );
407
         sb.Append( "\r\n" );
408
      }
409
 
2106 ghuddy 410
      public static void updateEA_DocGen(EA.Element ele)
2094 ghuddy 411
      {
412
         StringBuilder sb = new StringBuilder();
413
 
414
         sb.Append( optstr_GeneralOptions );
415
         sb.Append( "\r\n" );
416
 
417
         // Missing Description handling options
418
         updateEA_DocGen(ref sb, opt_SuppressElementDescriptionMissingWarnings, optstr_SuppressElementDescriptionMissingWarnings);
2104 ghuddy 419
         updateEA_DocGen(ref sb, opt_SuppressUnAllocatedRelationshipWarnings, optstr_SuppressUnAllocatedRelationshipWarnings);
2094 ghuddy 420
 
421
         // visibility options
422
         updateEA_DocGen(ref sb, opt_SuppressPrivateClasses,    optstr_SuppressPrivateClasses);
2096 ghuddy 423
         updateEA_DocGen(ref sb, opt_SuppressPrivateAttributes, optstr_SuppressPrivateAttributes);
2094 ghuddy 424
         updateEA_DocGen(ref sb, opt_SuppressPrivateMethods,    optstr_SuppressPrivateMethods);
2104 ghuddy 425
         updateEA_DocGen(ref sb, opt_ConsiderAPIElementsOnly,   optstr_ConsiderAPIElementsOnly);
426
         updateEA_DocGen(ref sb, opt_ConsiderAPIDiagramsOnly,   optstr_ConsiderAPIDiagramsOnly);
427
         updateEA_DocGen(ref sb, opt_ConsiderAPIPackagesOnly,   optstr_ConsiderAPIPackagesOnly);
428
         updateEA_DocGen(ref sb, opt_RestrictForLinkedPackagesOnly, optstr_RestrictForLinkedPackagesOnly);
2094 ghuddy 429
 
2104 ghuddy 430
 
2094 ghuddy 431
         // Element type filtering options
432
         if (opt_ElementTypes.Count > 0)
433
         {
434
            sb.Append( "\r\n" );
435
            sb.Append( optstr_ElementTypes );
436
            sb.Append( "\r\n" );
437
            foreach (string s in opt_ElementTypes)
438
            {
439
               sb.Append( s );
440
               sb.Append( "\r\n" );
441
            }
442
         }
443
 
444
         ele.Notes = sb.ToString();
445
         ele.Update();
446
 
447
      }
448
 
2088 ghuddy 449
   } // end of class
450
} // end of namespace