/// <summary> /// Generate parameterised parcel constructor. /// </summary> public List <string> GenerateParameterisedParcelConstructor(EntityMetaBlock block) { List <string> output = new List <string>(); string line = string.Empty; output.Add(line); line = @" @ParcelConstructor"; output.Add(line); line = @" public " + block.Name + "("; string separator = string.Empty; for (int count = 0; count < block.MemberVariables.Count; count++) { string Type = block.MemberVariables[count].Type; Type = TransformType(Type); string type = block.MemberVariables[count].Type.ToLower(); if (type == "integer") { type = "int"; } string parameter = block.MemberVariables[count].Variable; if (parameter.StartsWith("_")) { parameter = parameter.Substring(1); } line += separator + Type + " " + parameter; separator = ", "; } line += ") {"; output.Add(line); for (int count = 0; count < block.MemberVariables.Count; count++) { string variable = block.MemberVariables[count].Variable; string parameter = block.MemberVariables[count].Variable; if (parameter.StartsWith("_")) { parameter = parameter.Substring(1); } line = " this." + variable + " = " + parameter + ";"; output.Add(line); } line = @" }"; output.Add(line); return(output); }
/// <summary> /// Import member variable declarations from top of API interface class. /// </summary> /// <param name="lineNumber">Enters as starting line number, exits as next starting line number.</param> /// <returns>Entity meta block for home entity.</returns> public EntityMetaBlock ImportMemberVariables() { EntityMetaBlock block = new EntityMetaBlock(); block.Name = _modelClassName; Vector vector = null; string line = string.Empty; int nPos1 = 0; int nPos2 = 0; int nEol = 0; foreach (string inputLine in _inputLines) { line = inputLine.Trim(); if (line.Length > 0) { if (!line.StartsWith("//")) { vector = new Vector(); if (line.Contains(" readonly ")) { vector.ReadOnly = true; line = line.Replace("readonly ", string.Empty); } nPos1 = line.IndexOf(' '); if (nPos1 > -1) { vector.Modifier = line.Substring(0, nPos1).Trim(); nPos2 = line.IndexOf(' ', nPos1 + 1); if (nPos2 > -1) { vector.Type = line.Substring(nPos1, nPos2 - nPos1).Trim(); nEol = line.Length; nPos1 = line.IndexOf(' ', nPos2 + 1); if (nPos1 > -1) { if (nPos1 < nEol) { nEol = nPos1; } } else { nPos1 = line.IndexOf('=', nPos2 + 1); if (nPos1 > -1) { if (nPos1 < nEol) { nEol = nPos1; } } else { nPos1 = line.IndexOf(';', nPos2 + 1); if (nPos1 > -1) { if (nPos1 < nEol) { nEol = nPos1; } } } } if (nPos1 > -1) { vector.Variable = line.Substring(nPos2, nPos1 - nPos2).Trim(); if (vector.Variable.Substring(0, 1) == "_") { vector.Property = vector.Variable.Substring(1, 1).ToUpper() + vector.Variable.Substring(2); } else { vector.Property = vector.Variable.Substring(0, 1).ToUpper() + vector.Variable.Substring(1); } vector.Summary = ProperCase(DescriptionFromCamelCase(vector.Property)) + "."; } } } block.MemberVariables.Add(vector); } } } return(block); }
/// <summary> /// Generate parameterised parcel constructor. /// </summary> public List <string> GenerateApiObjectConstructor(EntityMetaBlock block) { string apiClassName = _modelClassName.Replace("ModelState", string.Empty); string apiObjectName = apiClassName.Substring(0, 1).ToLower() + apiClassName.Substring(1); List <string> output = new List <string>(); string line = string.Empty; output.Add(line); line = @" public " + block.Name + "(" + apiClassName + " " + apiObjectName + ") {"; output.Add(line); line = @" this();"; output.Add(line); line = string.Format(@" if ({0} != null)", apiObjectName) + " {"; output.Add(line); for (int count = 0; count < block.MemberVariables.Count; count++) { string Type = block.MemberVariables[count].Type; string originalType = Type; string originalInnerType = InnerType(originalType); Type = TransformType(Type); string innerType = InnerType(Type); string property = block.MemberVariables[count].Property; string value = string.Format(@"{0}.get{1}()", apiObjectName, property); string type = block.MemberVariables[count].Type.ToLower(); if (type == "boolean") { value = string.Format(@"Boolean.valueOf(true).equals({0})", value); } else if (type == "string") { } else if (type == "integer") { type = "int"; } else if (type == "int") { } else if (type == "long") { } else { value = "new " + innerType + "(" + value + ")"; } string variable = block.MemberVariables[count].Variable; if (innerType == Type) { line = " this." + variable + " = " + value + ";"; output.Add(line); } else { line = " this." + variable + " = new ArrayList<" + innerType + ">();"; output.Add(line); line = string.Format(@" if ({0}.get{1}() != null)", apiObjectName, property) + " {"; output.Add(line); line = string.Format(@" for ({0} item : {1}.get{2}())", originalInnerType, apiObjectName, property) + " {"; output.Add(line); line = string.Format(@" this.{0}.add(new {1}(item));", variable, innerType); output.Add(line); line = " }"; output.Add(line); line = " }"; output.Add(line); } } line = @" }"; output.Add(line); line = @" }"; output.Add(line); return(output); }
/// <summary> /// Edit Models file. /// </summary> /// <remarks> /// Apply global changes to one model file. /// </remarks> protected bool EditModelsFile(string fileSpec) { classFileCount++; _ignoreUntil = true; StringBuilder contents = new StringBuilder(); List <string> memberVariableLines = new List <string>(); bool memberVariableOnNextLine = false; bool memberVariablesOpened = false; bool memberVariablesClosed = false; string line = string.Empty; bool first = true; bool hit = false; bool changed = false; FileInfo fileInfo = null; string record = string.Empty; long count = 0; string currentClassName = System.IO.Path.GetFileNameWithoutExtension(fileSpec); try { if (File.Exists(fileSpec)) { string modelClassName = System.IO.Path.GetFileNameWithoutExtension(fileSpec); fileInfo = R.GetFileInfo(fileSpec); StreamReader sr = new StreamReader(fileSpec); if (sr.Peek() >= 0) { memberVariableOnNextLine = false; memberVariablesOpened = false; memberVariablesClosed = false; do { count++; classFileLineCount++; record = sr.ReadLine(); line = record; if (memberVariableOnNextLine) { memberVariableLines.Add(line); line = line.Replace(" Boolean ", " boolean "); line = line.Replace(" Integer ", " int "); line = line.Replace(" Int ", " int "); line = line.Replace(" Long ", " long "); line = line.Replace(" = null", string.Empty); memberVariableOnNextLine = false; } if (memberVariablesOpened && !memberVariablesClosed) { if (line.Trim().Length == 0) { memberVariablesClosed = true; Generator generator = null; EntityMetaBlock block = null; try { generator = new Generator(modelClassName, memberVariableLines, _apiToModelClassNameMapping); block = generator.ImportMemberVariables(); } catch (Exception e1) { } try { List <string> defaultConstructor = generator.GenerateDefaultConstructor(block); foreach (string ln in defaultConstructor) { contents.AppendLine(ln); } } catch (Exception e2) { } try { List <string> apiObjectConstructor = generator.GenerateApiObjectConstructor(block); foreach (string ln in apiObjectConstructor) { contents.AppendLine(ln); } } catch (Exception e3) { } try { List <string> parcelConstructor = generator.GenerateParameterisedParcelConstructor(block); foreach (string ln in parcelConstructor) { contents.AppendLine(ln); } } catch (Exception e4) { } } } if (line.Contains("@Expose")) { memberVariablesOpened = true; if (!memberVariablesClosed) { memberVariableOnNextLine = true; } continue; } else if (line.Contains("Expose;")) { continue; } else if (line.Contains("@SerializedName")) { continue; } else if (line.Contains("SerializedName;")) { continue; } else if (line.Contains("package com.sample.example")) { continue; } if (_ignoreUntil) { if (line.Trim().Length == 0) { continue; } else if (line.Trim().StartsWith("/*")) { continue; } else if (line.Trim().StartsWith("*")) { continue; } else if (line.Trim().StartsWith("*/")) { continue; } } if (line.Contains("public class")) { // Ignore all lines until the package is found. _ignoreUntil = false; // Insert package line. string packageLine = "package com.example.kotlinmodelsexperiment.common.models.data;"; ConditionalAppendLine(ref contents, packageLine); ConditionalAppendLine(ref contents, string.Empty); // Insert comment block at start of class. string[] parts = line.Split(' '); if (parts.Length > 2) { string className = parts[2]; string comment1 = "/**"; string comment2 = String.Format(" * {0} model state.", CommentFromClassName(className)); string comment3 = " */"; ConditionalAppendLine(ref contents, comment1); ConditionalAppendLine(ref contents, comment2); ConditionalAppendLine(ref contents, comment3); hit = true; changed = true; RecordInsert(hit, ref first, fileSpec, count, record, comment1); RecordInsert(hit, ref first, fileSpec, count, record, comment2); RecordInsert(hit, ref first, fileSpec, count, record, comment3); } ConditionalAppendLine(ref contents, "@Parcel"); //Replace all the API class names with the Model class names. foreach (KeyValuePair <string, string> entry in _apiToModelClassNameMapping) { //ChangeOldClassNameToNewClassName(string.Empty, entry, string.Empty, ref line, ref changed, ref first, fileSpec, count, ref record); ChangeOldClassNameToNewClassName(".", entry, ";", ref line, ref changed, ref first, fileSpec, count, ref record); ChangeOldClassNameToNewClassName("<", entry, ">", ref line, ref changed, ref first, fileSpec, count, ref record); ChangeOldClassNameToNewClassName("<", entry, ",", ref line, ref changed, ref first, fileSpec, count, ref record); ChangeOldClassNameToNewClassName(" ", entry, ",", ref line, ref changed, ref first, fileSpec, count, ref record); ChangeOldClassNameToNewClassName(" ", entry, ">", ref line, ref changed, ref first, fileSpec, count, ref record); ChangeOldClassNameToNewClassName(" ", entry, "<", ref line, ref changed, ref first, fileSpec, count, ref record); ChangeOldClassNameToNewClassName("(", entry, ")", ref line, ref changed, ref first, fileSpec, count, ref record); ChangeOldClassNameToNewClassName("(", entry, " ", ref line, ref changed, ref first, fileSpec, count, ref record); ChangeOldClassNameToNewClassName(" ", entry, "(", ref line, ref changed, ref first, fileSpec, count, ref record); ChangeOldClassNameToNewClassName(" ", entry, ")", ref line, ref changed, ref first, fileSpec, count, ref record); ChangeOldClassNameToNewClassName(" ", entry, " ", ref line, ref changed, ref first, fileSpec, count, ref record); ChangeOldClassNameToNewClassName(string.Empty, entry, ".", ref line, ref changed, ref first, fileSpec, count, ref record); ChangeOldClassNameToNewClassName(string.Empty, entry, " ", ref line, ref changed, ref first, fileSpec, count, ref record); } ConditionalAppendLine(ref contents, line); continue; } //Replace all the API class names with the Model class names. foreach (KeyValuePair <string, string> entry in _apiToModelClassNameMapping) { //ChangeOldClassNameToNewClassName(string.Empty, entry, string.Empty, ref line, ref changed, ref first, fileSpec, count, ref record); ChangeOldClassNameToNewClassName(".", entry, ";", ref line, ref changed, ref first, fileSpec, count, ref record); ChangeOldClassNameToNewClassName("<", entry, ">", ref line, ref changed, ref first, fileSpec, count, ref record); ChangeOldClassNameToNewClassName("<", entry, ",", ref line, ref changed, ref first, fileSpec, count, ref record); ChangeOldClassNameToNewClassName(" ", entry, ",", ref line, ref changed, ref first, fileSpec, count, ref record); ChangeOldClassNameToNewClassName(" ", entry, ">", ref line, ref changed, ref first, fileSpec, count, ref record); ChangeOldClassNameToNewClassName(" ", entry, "<", ref line, ref changed, ref first, fileSpec, count, ref record); ChangeOldClassNameToNewClassName("(", entry, ")", ref line, ref changed, ref first, fileSpec, count, ref record); ChangeOldClassNameToNewClassName("(", entry, " ", ref line, ref changed, ref first, fileSpec, count, ref record); ChangeOldClassNameToNewClassName(" ", entry, "(", ref line, ref changed, ref first, fileSpec, count, ref record); ChangeOldClassNameToNewClassName(" ", entry, ")", ref line, ref changed, ref first, fileSpec, count, ref record); ChangeOldClassNameToNewClassName(" ", entry, " ", ref line, ref changed, ref first, fileSpec, count, ref record); ChangeOldClassNameToNewClassName(string.Empty, entry, ".", ref line, ref changed, ref first, fileSpec, count, ref record); ChangeOldClassNameToNewClassName(string.Empty, entry, " ", ref line, ref changed, ref first, fileSpec, count, ref record); } contents.AppendLine(line); if (_action == "Cancel") { break; } } while (sr.Peek() >= 0); } sr.Close(); if (changed) { FileHelper.WriteFile(fileSpec, contents.ToString()); } } } catch (Exception ex) { } return(hit); }
/// <summary> /// Edit Models file. /// </summary> /// <remarks> /// Apply global changes to one model file. /// </remarks> protected bool EditModelsFile(string fileSpec) { classFileCount++; _ignoreUntil = true; StringBuilder contents = new StringBuilder(); List <string> memberVariableLines = new List <string>(); List <string> memberVarLines = new List <string>(); List <string> importModelClasses = new List <string>(); string line = string.Empty; bool first = true; bool hit = false; bool changed = false; FileInfo fileInfo = null; string record = string.Empty; long count = 0; string currentClassName = System.IO.Path.GetFileNameWithoutExtension(fileSpec); var importsBuffer = new List <string>(); // Read ahead to see which other model classes need import statements. try { if (File.Exists(fileSpec)) { string modelClassName = System.IO.Path.GetFileNameWithoutExtension(fileSpec); string apiModelClassName = modelClassName.Replace("ModelState", string.Empty); importModelClasses.Add(apiModelClassName); fileInfo = R.GetFileInfo(fileSpec); StreamReader sr = new StreamReader(fileSpec); if (sr.Peek() >= 0) { do { record = sr.ReadLine(); line = record; var trimmedLine = line.Trim(); if (trimmedLine.StartsWith("/*") && trimmedLine.EndsWith("*/")) { memberVarLines.Add(line); } else if (trimmedLine.StartsWith("val")) { memberVarLines.Add(line); } else if (trimmedLine.StartsWith(")")) { GeneratorKotlin generator = null; EntityMetaBlock block = null; try { generator = new GeneratorKotlin(modelClassName, memberVarLines, _apiToModelClassNameMapping); block = generator.ImportMemberVariables(); foreach (Vector vector in block.MemberVariables) { string apiType = vector.ApiType; string apiInnerType = InnerType(vector.ApiType); if (_apiToModelClassNameMapping.ContainsKey(apiType)) { if (!importModelClasses.Contains(apiType)) { //importModelClasses.Add(apiType); } } if (_apiToModelClassNameMapping.ContainsKey(apiInnerType)) { if (!importModelClasses.Contains(apiInnerType)) { //importModelClasses.Add(apiInnerType); } } } } catch (Exception e1) { string msg = e1.Message; } } } while (sr.Peek() >= 0); } sr.Close(); } } catch (Exception ex) { } // Now process file normally. try { if (File.Exists(fileSpec)) { string modelClassName = System.IO.Path.GetFileNameWithoutExtension(fileSpec); fileInfo = R.GetFileInfo(fileSpec); StreamReader sr = new StreamReader(fileSpec); if (sr.Peek() >= 0) { do { count++; classFileLineCount++; record = sr.ReadLine(); line = record; var trimmedLine = line.Trim(); if (line.Contains("package com.ubtsupport.streamline3admin")) { continue; } if (line.Contains("import")) { importsBuffer.Add(line); continue; } if (_ignoreUntil) { if (line.Trim().Length == 0) { continue; } else if (line.Trim().StartsWith("/*")) { continue; } else if (line.Trim().StartsWith("*")) { continue; } else if (line.Trim().StartsWith("*/")) { continue; } } if (trimmedLine.StartsWith("/*") && trimmedLine.EndsWith("*/")) { memberVariableLines.Add(line); } if (trimmedLine.StartsWith("val")) { memberVariableLines.Add(line); } if (trimmedLine.StartsWith(")")) { GeneratorKotlin generator = null; EntityMetaBlock block = null; try { generator = new GeneratorKotlin(modelClassName, memberVariableLines, _apiToModelClassNameMapping); block = generator.ImportMemberVariables(); int counter = 0; int maximum = block.MemberVariables.Count; foreach (Vector vector in block.MemberVariables) { counter++; if (vector.Summary.Trim().Length > 0) { string comment = " " + vector.Summary; contents.AppendLine(comment); } StringBuilder output = new StringBuilder(" var "); output.Append(vector.Variable); output.Append(": "); output.Append(vector.Type); output.Append(" = "); output.Append(vector.DefaultValue); if (counter < maximum) { output.Append(","); } contents.AppendLine(output.ToString()); } contents.AppendLine(@") {"); } catch (Exception e1) { } try { List <string> apiObjectConstructor = generator.GenerateApiObjectConstructor(block); foreach (string ln in apiObjectConstructor) { contents.AppendLine(ln); } contents.AppendLine(@"}"); } catch (Exception e3) { } } if (line.Contains("data class")) { // Ignore all lines until the package is found. _ignoreUntil = false; // Insert package line. string packageLine = "package " + FeaturesModelsPackageName; ConditionalAppendLine(ref contents, packageLine); ConditionalAppendLine(ref contents, string.Empty); // Insert constants import line. string importLine = "import com.ubtsupport.streamline3admin.common.constants.Constants"; ConditionalAppendLine(ref contents, importLine); //if (FeaturesModelsPackageName != CommonModelsPackageName) //{ // // Insert API models import line. // importLine = "import " + CommonModelsPackageName; // ConditionalAppendLine(ref contents, importLine); //} // Insert referenced API model class import lines. foreach (var modelClass in importModelClasses) { importLine = "import " + CommonModelsPackageName + "." + modelClass; ConditionalAppendLine(ref contents, importLine); } // Insert referenced modelState class import lines. //foreach (var import in importsBuffer) //{ // var impLine = import; // impLine = impLine.Replace(CommonModelsPackageName, FeaturesModelsPackageName); // //Replace all the API class names with the Model class names. // impLine = EditLine(_apiToModelClassNameEditRules, impLine); // ConditionalAppendLine(ref contents, impLine); //} // Insert parceler import line. importLine = "import org.parceler.Parcel"; ConditionalAppendLine(ref contents, importLine); ConditionalAppendLine(ref contents, string.Empty); // Insert comment block at start of class. string[] parts = line.Split(' '); if (parts.Length > 2) { string className = parts[2]; string comment1 = "/**"; string comment2 = String.Format(" * {0} model state.", CommentFromClassName(className)); string comment3 = " */"; ConditionalAppendLine(ref contents, comment1); ConditionalAppendLine(ref contents, comment2); ConditionalAppendLine(ref contents, comment3); hit = true; changed = true; RecordInsert(hit, ref first, fileSpec, count, record, comment1); RecordInsert(hit, ref first, fileSpec, count, record, comment2); RecordInsert(hit, ref first, fileSpec, count, record, comment3); } ConditionalAppendLine(ref contents, "@Parcel(Parcel.Serialization.BEAN)"); //Replace all the API class names with the Model class names. line = EditLine(_apiToModelClassNameEditRules, line); line = line.Replace(" (", "("); ConditionalAppendLine(ref contents, line); continue; } //Replace all the API class names with the Model class names. line = EditLine(_apiToModelClassNameEditRules, line); //contents.AppendLine(line); if (_action == "Cancel") { break; } } while (sr.Peek() >= 0); } sr.Close(); if (changed) { FileHelper.WriteFile(fileSpec, contents.ToString()); } } } catch (Exception ex) { } return(hit); }
/// <summary> /// Import member variable declarations from top of API interface class. /// </summary> /// <param name="lineNumber">Enters as starting line number, exits as next starting line number.</param> /// <returns>Entity meta block for home entity.</returns> public EntityMetaBlock ImportMemberVariables() { EntityMetaBlock block = new EntityMetaBlock(); block.Name = _modelClassName; Vector vector = null; string line = string.Empty; int nPos1 = 0; int nPos2 = 0; int nEol = 0; string prevLineSummary = string.Empty; foreach (string inputLine in _inputLines) { line = inputLine.Trim(); if (line.Length > 0) { if (!line.StartsWith("//")) { var trimmedLine = line.Trim(); if (trimmedLine.StartsWith("/*") && trimmedLine.EndsWith("*/")) { prevLineSummary = trimmedLine; } else if (line.StartsWith("val")) { vector = new Vector(); nPos1 = line.IndexOf("val"); if (nPos1 > -1) { nPos1 = line.IndexOf(' ', nPos1 + 1); if (nPos1 > -1) { nPos2 = line.IndexOf(':', nPos1 + 1); if (nPos2 > -1) { vector.Modifier = string.Empty; vector.Variable = line.Substring(nPos1, nPos2 - nPos1).Trim(); vector.Property = vector.Variable; nPos2++; nEol = line.Length; nPos1 = line.IndexOf('=', nPos2 + 1); if (nPos1 > -1) { if (nPos1 < nEol) { nEol = nPos1; } } else { nPos1 = line.IndexOf(',', nPos2 + 1); if (nPos1 > -1) { if (nPos1 < nEol) { nEol = nPos1; } } } } } if (nPos1 > -1) { vector.Type = line.Substring(nPos2, nPos1 - nPos2).Trim(); vector.Type = StripType(vector.Type); vector.ApiType = vector.Type; vector.Type = TransformType(vector.Type); vector.Summary = prevLineSummary; prevLineSummary = string.Empty; string Type = vector.Type; //Type = TransformType(Type); string type = vector.Type.ToLower(); string value = Type + "()"; if (Type.StartsWith("Array<")) { vector.Type = vector.Type.Replace("Array", "MutableList"); Type = vector.Type; value = "mutableListOf()"; } if (Type.StartsWith("List<")) { vector.Type = vector.Type.Replace("List", "MutableList"); Type = vector.Type; value = "mutableListOf()"; } if (type == "boolean") { value = "false"; } else if (type == "string") { value = "Constants.EMPTY_STRING"; } else if (type == "integer") { type = "int"; value = "Constants.ZERO"; } else if (type == "int") { value = "Constants.ZERO"; } else if (type == "long") { value = "Constants.ZERO"; } vector.DefaultValue = value; block.MemberVariables.Add(vector); } } } } } } return(block); }
/// <summary> /// Generate parameterised parcel constructor. /// </summary> public List <string> GenerateApiObjectConstructor(EntityMetaBlock block) { string apiClassName = _modelClassName.Replace("ModelState", string.Empty); string apiObjectName = apiClassName.Substring(0, 1).ToLower() + apiClassName.Substring(1); List <string> output = new List <string>(); string line = string.Empty; output.Add(line); line = @" constructor(" + apiObjectName + ": " + apiClassName + "?) : this() {"; output.Add(line); for (int count = 0; count < block.MemberVariables.Count; count++) { string Type = block.MemberVariables[count].Type; string originalType = Type; string originalInnerType = InnerType(originalType); Type = TransformType(Type); string innerType = InnerType(Type); string property = block.MemberVariables[count].Property; string value = string.Format(@"{0}?.{1}", apiObjectName, property); string type = block.MemberVariables[count].Type.ToLower(); string defaultValue = "Constants.EMPTY_STRING"; if (type == "boolean") { defaultValue = "false"; } else if (type == "string") { defaultValue = "Constants.EMPTY_STRING"; } else if (type == "int") { defaultValue = "Constants.ZERO"; } else if (type == "long") { defaultValue = "Constants.ZERO.toLong()"; } else { value = innerType + "(" + value + ")"; } string variable = block.MemberVariables[count].Variable; if (innerType == Type) { line = " " + variable + " = " + value + " ?: " + defaultValue; output.Add(line); } else { line = " " + variable + " = mutableListOf()"; output.Add(line); line = string.Format(@" {0}?.{1}?.forEach", apiObjectName, property) + " {"; output.Add(line); line = " " + variable + ".add(" + innerType + "(it))"; output.Add(line); line = " }"; output.Add(line); } } line = @" }"; output.Add(line); return(output); }