1 //---------------------------------------------------------------------
2 // <copyright file="FixUp.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
11 using System.Data.EntityModel.SchemaObjectModel;
12 using System.Data.Entity.Design;
14 namespace System.Data.EntityModel.Emitters
19 internal sealed class FixUp
21 #region Internal Property
22 internal delegate string FixMethod(string line);
25 #region Instance Fields
27 private string _class = null;
28 private string _method = null;
29 private string _property = null;
33 private static readonly FixMethod[] _CSFixMethods = new FixMethod[]
36 new FixMethod(CSMarkOverrideMethodAsSealed),
37 new FixMethod(CSMarkPropertySetAsInternal),
38 new FixMethod(CSMarkClassAsStatic),
39 new FixMethod(CSMarkPropertyGetAsPrivate),
40 new FixMethod(CSMarkPropertyGetAsInternal),
41 new FixMethod(CSMarkPropertyGetAsPublic),
42 new FixMethod(CSMarkPropertySetAsPrivate),
43 new FixMethod(CSMarkPropertySetAsPublic),
44 new FixMethod(CSMarkMethodAsPartial),
45 new FixMethod(CSMarkPropertyGetAsProtected),
46 new FixMethod(CSMarkPropertySetAsProtected)
49 private static readonly FixMethod[] _VBFixMethods = new FixMethod[]
52 new FixMethod(VBMarkOverrideMethodAsSealed),
53 new FixMethod(VBMarkPropertySetAsInternal),
54 null, // VB doesn't support static classes (during CodeGen we added a private ctor to the class)
55 new FixMethod(VBMarkPropertyGetAsPrivate),
56 new FixMethod(VBMarkPropertyGetAsInternal),
57 new FixMethod(VBMarkPropertyGetAsPublic),
58 new FixMethod(VBMarkPropertySetAsPrivate),
59 new FixMethod(VBMarkPropertySetAsPublic),
60 new FixMethod(VBMarkMethodAsPartial),
61 new FixMethod(VBMarkPropertyGetAsProtected),
62 new FixMethod(VBMarkPropertySetAsProtected)
66 #region Public Methods
70 /// <param name="fqName"></param>
71 /// <param name="type"></param>
72 public FixUp(string fqName,FixUpType type)
75 string[] nameParts = Utils.SplitName(fqName);
76 if ( type == FixUpType.MarkClassAsStatic )
78 Class = nameParts[nameParts.Length-1];
82 Class = nameParts[nameParts.Length-2];
83 string name = nameParts[nameParts.Length-1];
86 case FixUpType.MarkAbstractMethodAsPartial:
87 case FixUpType.MarkOverrideMethodAsSealed:
90 case FixUpType.MarkPropertyGetAsPrivate:
91 case FixUpType.MarkPropertyGetAsInternal:
92 case FixUpType.MarkPropertyGetAsPublic:
93 case FixUpType.MarkPropertyGetAsProtected:
94 case FixUpType.MarkPropertySetAsPrivate:
95 case FixUpType.MarkPropertySetAsInternal:
96 case FixUpType.MarkPropertySetAsPublic:
97 case FixUpType.MarkPropertySetAsProtected:
107 /// <param name="language"></param>
108 /// <param name="line"></param>
109 /// <returns></returns>
110 public string Fix(LanguageOption language, string line)
112 FixMethod method = null;
113 if ( language == LanguageOption.GenerateCSharpCode )
115 method = _CSFixMethods[(int)Type];
117 else if ( language == LanguageOption.GenerateVBCode )
119 method = _VBFixMethods[(int)Type];
122 if ( method != null )
124 line = method( line );
131 #region Public Properties
151 public string Property
181 public FixUpType Type
194 #region Private Methods
198 /// <param name="line"></param>
199 /// <returns></returns>
200 private static string CSMarkMethodAsPartial(string line)
202 line = ReplaceFirst(line, "public abstract", "partial");
209 /// <param name="line"></param>
210 /// <returns></returns>
211 private static string VBMarkMethodAsPartial(string line)
213 line = ReplaceFirst(line, "Public MustOverride", "Partial Private");
214 line += Environment.NewLine + " End Sub";
218 private static string ReplaceFirst(string line, string str1, string str2)
220 int idx = line.IndexOf(str1, StringComparison.Ordinal);
223 line = line.Remove(idx, str1.Length);
224 line = line.Insert(idx, str2);
231 /// <param name="line"></param>
232 /// <returns></returns>
233 private static string CSMarkOverrideMethodAsSealed(string line)
235 return InsertBefore(line,"override","sealed");
241 /// <param name="line"></param>
242 /// <returns></returns>
243 private static string VBMarkOverrideMethodAsSealed(string line)
245 return InsertBefore(line, "Overrides", "NotOverridable");
251 /// <param name="line"></param>
252 /// <returns></returns>
253 private static string CSMarkPropertySetAsInternal(string line)
255 return InsertBefore(line,"set","internal");
261 /// <param name="line"></param>
262 /// <returns></returns>
263 private static string VBMarkPropertySetAsInternal(string line)
265 return InsertBefore(line,"Set","Friend");
269 private static string CSMarkPropertyGetAsPrivate(string line)
271 return InsertBefore(line, "get", "private");
274 private static string VBMarkPropertyGetAsPrivate(string line)
276 return InsertBefore(line, "Get", "Private");
280 private static string CSMarkPropertyGetAsInternal(string line)
282 return InsertBefore(line, "get", "internal");
285 private static string VBMarkPropertyGetAsInternal(string line)
287 return InsertBefore(line, "Get", "Friend");
290 private static string CSMarkPropertySetAsProtected(string line)
292 return InsertBefore(line, "set", "protected");
295 private static string VBMarkPropertySetAsProtected(string line)
297 return InsertBefore(line, "Set", "Protected");
300 private static string CSMarkPropertyGetAsProtected(string line)
302 return InsertBefore(line, "get", "protected");
305 private static string VBMarkPropertyGetAsProtected(string line)
307 return InsertBefore(line, "Get", "Protected");
310 private static string CSMarkPropertyGetAsPublic(string line)
312 return InsertBefore(line, "get", "public");
315 private static string VBMarkPropertyGetAsPublic(string line)
317 return InsertBefore(line, "Get", "Public");
321 private static string CSMarkPropertySetAsPrivate(string line)
323 return InsertBefore(line, "set", "private");
326 private static string VBMarkPropertySetAsPrivate(string line)
328 return InsertBefore(line, "Set", "Private");
332 private static string CSMarkPropertySetAsPublic(string line)
334 return InsertBefore(line, "set", "public");
337 private static string VBMarkPropertySetAsPublic(string line)
339 return InsertBefore(line, "Set", "Public");
346 /// <param name="line"></param>
347 /// <returns></returns>
348 private static string CSMarkClassAsStatic(string line)
350 if ( IndexOfKeyword(line,"static") >= 0 )
353 int insertPoint = IndexOfKeyword(line,"class");
354 if ( insertPoint < 0 )
357 // nothing can be between partial and class
358 int partialIndex = IndexOfKeyword(line,"partial");
359 if ( partialIndex >= 0 )
360 insertPoint = partialIndex;
362 return line.Insert(insertPoint,"static ");
366 /// Inserts one keyword before another one.
367 /// Does nothing if the keyword to be inserted already exists in the line OR if the keyword to insert before doesn't
369 /// <param name="line">line of text to examine</param>
370 /// <param name="searchText">keyword to search for </param>
371 /// <param name="insertText">keyword to be inserted</param>
372 /// <returns>the possibly modified line line</returns>
373 private static string InsertBefore(string line,string searchText,string insertText)
375 if ( IndexOfKeyword(line,insertText) >= 0 )
378 int index = IndexOfKeyword(line,searchText);
382 return line.Insert(index,insertText+" ");
386 /// Finds location of a keyword in a line.
387 /// keyword must be at the beginning of the line or preceeded by whitespace AND at the end of the line or followed by whitespace
389 /// <param name="line">line to seach</param>
390 /// <param name="keyword">keyword to search for</param>
391 /// <returns>location of first character of keyword</returns>
392 private static int IndexOfKeyword(string line,string keyword)
394 int index = line.IndexOf(keyword,StringComparison.Ordinal);
398 int indexAfter = index+keyword.Length;
399 if ( (index == 0 || char.IsWhiteSpace(line,index-1)) && (indexAfter == line.Length || char.IsWhiteSpace(line,indexAfter)) )