Subversion Repositories DevTools

Rev

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