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 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
|
//---------------------------------------------------------------------
// <copyright file="FixUp.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//
// @owner [....]
// @backupOwner [....]
//---------------------------------------------------------------------
using System;
using System.Data.EntityModel.SchemaObjectModel;
using System.Data.Entity.Design;
namespace System.Data.EntityModel.Emitters
{
/// <summary>
///
/// </summary>
internal sealed class FixUp
{
#region Internal Property
internal delegate string FixMethod(string line);
#endregion
#region Instance Fields
FixUpType m_type;
private string _class = null;
private string _method = null;
private string _property = null;
#endregion
#region static
private static readonly FixMethod[] _CSFixMethods = new FixMethod[]
{
null,
new FixMethod(CSMarkOverrideMethodAsSealed),
new FixMethod(CSMarkPropertySetAsInternal),
new FixMethod(CSMarkClassAsStatic),
new FixMethod(CSMarkPropertyGetAsPrivate),
new FixMethod(CSMarkPropertyGetAsInternal),
new FixMethod(CSMarkPropertyGetAsPublic),
new FixMethod(CSMarkPropertySetAsPrivate),
new FixMethod(CSMarkPropertySetAsPublic),
new FixMethod(CSMarkMethodAsPartial),
new FixMethod(CSMarkPropertyGetAsProtected),
new FixMethod(CSMarkPropertySetAsProtected)
};
private static readonly FixMethod[] _VBFixMethods = new FixMethod[]
{
null,
new FixMethod(VBMarkOverrideMethodAsSealed),
new FixMethod(VBMarkPropertySetAsInternal),
null, // VB doesn't support static classes (during CodeGen we added a private ctor to the class)
new FixMethod(VBMarkPropertyGetAsPrivate),
new FixMethod(VBMarkPropertyGetAsInternal),
new FixMethod(VBMarkPropertyGetAsPublic),
new FixMethod(VBMarkPropertySetAsPrivate),
new FixMethod(VBMarkPropertySetAsPublic),
new FixMethod(VBMarkMethodAsPartial),
new FixMethod(VBMarkPropertyGetAsProtected),
new FixMethod(VBMarkPropertySetAsProtected)
};
#endregion
#region Public Methods
/// <summary>
///
/// </summary>
/// <param name="fqName"></param>
/// <param name="type"></param>
public FixUp(string fqName,FixUpType type)
{
Type = type;
string[] nameParts = Utils.SplitName(fqName);
if ( type == FixUpType.MarkClassAsStatic )
{
Class = nameParts[nameParts.Length-1];
}
else
{
Class = nameParts[nameParts.Length-2];
string name = nameParts[nameParts.Length-1];
switch ( type )
{
case FixUpType.MarkAbstractMethodAsPartial:
case FixUpType.MarkOverrideMethodAsSealed:
Method = name;
break;
case FixUpType.MarkPropertyGetAsPrivate:
case FixUpType.MarkPropertyGetAsInternal:
case FixUpType.MarkPropertyGetAsPublic:
case FixUpType.MarkPropertyGetAsProtected:
case FixUpType.MarkPropertySetAsPrivate:
case FixUpType.MarkPropertySetAsInternal:
case FixUpType.MarkPropertySetAsPublic:
case FixUpType.MarkPropertySetAsProtected:
Property = name;
break;
}
}
}
/// <summary>
///
/// </summary>
/// <param name="language"></param>
/// <param name="line"></param>
/// <returns></returns>
public string Fix(LanguageOption language, string line)
{
FixMethod method = null;
if ( language == LanguageOption.GenerateCSharpCode )
{
method = _CSFixMethods[(int)Type];
}
else if ( language == LanguageOption.GenerateVBCode )
{
method = _VBFixMethods[(int)Type];
}
if ( method != null )
{
line = method( line );
}
return line;
}
#endregion
#region Public Properties
/// <summary>
///
/// </summary>
/// <value></value>
public string Class
{
get
{
return _class;
}
private set
{
_class = value;
}
}
/// <summary>
///
/// </summary>
/// <value></value>
public string Property
{
get
{
return _property;
}
private set
{
_property = value;
}
}
/// <summary>
///
/// </summary>
/// <value></value>
public string Method
{
get
{
return _method;
}
private set
{
_method = value;
}
}
/// <summary>
///
/// </summary>
/// <value></value>
public FixUpType Type
{
get
{
return m_type;
}
private set
{
m_type = value;
}
}
#endregion
#region Private Methods
/// <summary>
///
/// </summary>
/// <param name="line"></param>
/// <returns></returns>
private static string CSMarkMethodAsPartial(string line)
{
line = ReplaceFirst(line, "public abstract", "partial");
return line;
}
/// <summary>
///
/// </summary>
/// <param name="line"></param>
/// <returns></returns>
private static string VBMarkMethodAsPartial(string line)
{
line = ReplaceFirst(line, "Public MustOverride", "Partial Private");
line += Environment.NewLine + " End Sub";
return line;
}
private static string ReplaceFirst(string line, string str1, string str2)
{
int idx = line.IndexOf(str1, StringComparison.Ordinal);
if (idx >= 0)
{
line = line.Remove(idx, str1.Length);
line = line.Insert(idx, str2);
}
return line;
}
/// <summary>
///
/// </summary>
/// <param name="line"></param>
/// <returns></returns>
private static string CSMarkOverrideMethodAsSealed(string line)
{
return InsertBefore(line,"override","sealed");
}
/// <summary>
///
/// </summary>
/// <param name="line"></param>
/// <returns></returns>
private static string VBMarkOverrideMethodAsSealed(string line)
{
return InsertBefore(line, "Overrides", "NotOverridable");
}
/// <summary>
///
/// </summary>
/// <param name="line"></param>
/// <returns></returns>
private static string CSMarkPropertySetAsInternal(string line)
{
return InsertBefore(line,"set","internal");
}
/// <summary>
///
/// </summary>
/// <param name="line"></param>
/// <returns></returns>
private static string VBMarkPropertySetAsInternal(string line)
{
return InsertBefore(line,"Set","Friend");
}
private static string CSMarkPropertyGetAsPrivate(string line)
{
return InsertBefore(line, "get", "private");
}
private static string VBMarkPropertyGetAsPrivate(string line)
{
return InsertBefore(line, "Get", "Private");
}
private static string CSMarkPropertyGetAsInternal(string line)
{
return InsertBefore(line, "get", "internal");
}
private static string VBMarkPropertyGetAsInternal(string line)
{
return InsertBefore(line, "Get", "Friend");
}
private static string CSMarkPropertySetAsProtected(string line)
{
return InsertBefore(line, "set", "protected");
}
private static string VBMarkPropertySetAsProtected(string line)
{
return InsertBefore(line, "Set", "Protected");
}
private static string CSMarkPropertyGetAsProtected(string line)
{
return InsertBefore(line, "get", "protected");
}
private static string VBMarkPropertyGetAsProtected(string line)
{
return InsertBefore(line, "Get", "Protected");
}
private static string CSMarkPropertyGetAsPublic(string line)
{
return InsertBefore(line, "get", "public");
}
private static string VBMarkPropertyGetAsPublic(string line)
{
return InsertBefore(line, "Get", "Public");
}
private static string CSMarkPropertySetAsPrivate(string line)
{
return InsertBefore(line, "set", "private");
}
private static string VBMarkPropertySetAsPrivate(string line)
{
return InsertBefore(line, "Set", "Private");
}
private static string CSMarkPropertySetAsPublic(string line)
{
return InsertBefore(line, "set", "public");
}
private static string VBMarkPropertySetAsPublic(string line)
{
return InsertBefore(line, "Set", "Public");
}
/// <summary>
///
/// </summary>
/// <param name="line"></param>
/// <returns></returns>
private static string CSMarkClassAsStatic(string line)
{
if ( IndexOfKeyword(line,"static") >= 0 )
return line;
int insertPoint = IndexOfKeyword(line,"class");
if ( insertPoint < 0 )
return line;
// nothing can be between partial and class
int partialIndex = IndexOfKeyword(line,"partial");
if ( partialIndex >= 0 )
insertPoint = partialIndex;
return line.Insert(insertPoint,"static ");
}
/// <summary>
/// Inserts one keyword before another one.
/// Does nothing if the keyword to be inserted already exists in the line OR if the keyword to insert before doesn't
/// </summary>
/// <param name="line">line of text to examine</param>
/// <param name="searchText">keyword to search for </param>
/// <param name="insertText">keyword to be inserted</param>
/// <returns>the possibly modified line line</returns>
private static string InsertBefore(string line,string searchText,string insertText)
{
if ( IndexOfKeyword(line,insertText) >= 0 )
return line;
int index = IndexOfKeyword(line,searchText);
if ( index < 0 )
return line;
return line.Insert(index,insertText+" ");
}
/// <summary>
/// Finds location of a keyword in a line.
/// keyword must be at the beginning of the line or preceeded by whitespace AND at the end of the line or followed by whitespace
/// </summary>
/// <param name="line">line to seach</param>
/// <param name="keyword">keyword to search for</param>
/// <returns>location of first character of keyword</returns>
private static int IndexOfKeyword(string line,string keyword)
{
int index = line.IndexOf(keyword,StringComparison.Ordinal);
if ( index < 0 )
return index;
int indexAfter = index+keyword.Length;
if ( (index == 0 || char.IsWhiteSpace(line,index-1)) && (indexAfter == line.Length || char.IsWhiteSpace(line,indexAfter)) )
return index;
return -1;
}
#endregion
}
}
|