// get the variables from the template // its useful to knowing where to save the file or if it applies to all tables.. public static variablesTemplate getVariablesFromTemplate(String textOfTemplate) { variablesTemplate var = new variablesTemplate(); string IPMatchExp = @"<nameFile>(?:(?<t>[^<]*))"; Match theMatch = Regex.Match(textOfTemplate, IPMatchExp); // mirar luego si compilando la expresion regular va mas rapido... if (theMatch.Success) var.namefile = theMatch.Groups[1].Value; // extensionFile theMatch = Regex.Match(textOfTemplate, "<extensionFile>(?:(?<t>[^<]*))"); if (theMatch.Success) var.extensionFile = theMatch.Groups[1].Value; // languageGenerated theMatch = Regex.Match(textOfTemplate, "<languageGenerated>(?:(?<t>[^<]*))"); if (theMatch.Success) var.languageGenerated = theMatch.Groups[1].Value; // description theMatch = Regex.Match(textOfTemplate, "<description>(?:(?<t>[^<]*))"); if (theMatch.Success) var.description = theMatch.Groups[1].Value; // targetDirectory theMatch = Regex.Match(textOfTemplate, "<targetDirectory>(?:(?<t>[^<]*))"); if (theMatch.Success) var.targetDirectory = theMatch.Groups[1].Value; // appliesToAllTables theMatch = Regex.Match(textOfTemplate, "<appliesToAllTables>(?:(?<t>[^<]*))"); if (theMatch.Success) var.appliesToAllTables = theMatch.Groups[1].Value; return var; }
private void butApplyTemplate2_Click(object sender, EventArgs e) { char tabCaracter = '\u0009'; try { String plantilla = util.loadFile(general.templateSelectedFullUri); // clean cmbGotocode cmbGoToCode.Items.Clear(); table tab = new table(); String tableSelected = cmbTablesx.Text; if (tableSelected.Equals("")) { rt1.Text = "Please, select a table"; } foreach (table item in general.actualProject.tables) { if (item.Name.Equals(tableSelected)) { tab = item; if (tab.GetKey == null) { MessageBox.Show("Alert, review data, table doesnt have a key"); AsyncWriteLine("Alert, review data, table doesnt have a key"); } } } string numerocampos = tab.fields.Count.ToString(); try { // si da un error en singleton es que falta la libreria commons o log4net.. Velocity.Init(); //VelocityEngine velocityEngine = new VelocityEngine(); //ExtendedProperties props = new ExtendedProperties(); //props.AddProperty("input.encoding", "UTF-8"); //props.AddProperty("output.encoding", "UTF-8"); //velocityEngine.Init(props); } catch (System.Exception exx) { rt1.Text = "Problem initializing Velocity : " + exx; return; } // lets make a Context and put data into it VelocityContext context = new VelocityContext(); context.Put("project", general.actualProject); context.Put("table", tab); // lets render a template StringWriter writer = new StringWriter(); try { //Velocity.MergeTemplate(plantilla, context, writer); Velocity.Evaluate(context, writer, "prueba", plantilla); // now we got the template , so lets take the variables from the template variablesTemplate var = new variablesTemplate(); var = util.getVariablesFromTemplate(writer.GetStringBuilder().ToString()); // now we delete the variables from the template cause there are no needed... string finalText = util.deleteVariablesFromTemplate(writer.GetStringBuilder().ToString()); // le quitamos saltos de linea extra finalText = finalText.Replace("\r\n\r\n", "\r\n").Replace("\r\n\r\n\r\n", "").Replace("\r\n\r\n\r\n\r\n", ""); // le quitamos los tabuladores extra finalText = finalText.Replace(tabCaracter.ToString(), " "); // rt1.Text = finalText; writeText(finalText); // number of lines written with generator try { long numberOfLinesWrittenBefore = 0; long numberOfLinesWritten = 0; numberOfLinesWrittenBefore = sf.toLong(System.Configuration.ConfigurationManager.AppSettings["numberOfLinesWritten"]); numberOfLinesWritten = numberOfLinesWrittenBefore + util.CountLinesInString(finalText); labNumberOfLinesWritten.Values.Text = sf.cadena(numberOfLinesWritten) + " lines written with myWay"; // save data... System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); config.AppSettings.Settings["numberOfLinesWritten"].Value = sf.cadena(numberOfLinesWritten); config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appSettings"); } catch (Exception) { throw; } // now we got all the functions contained in the code // and populate a combo with this... // Instantiating Regex Object Regex re = new Regex(@"(private|public|protected)\s\w(.)*\((.)*\)", RegexOptions.IgnoreCase); MatchCollection mc = re.Matches(finalText); foreach (Match mt in mc) { string st = ""; st = mt.ToString(); st = st.Replace("public", ""); st = st.Replace("private", ""); st = st.Replace("static", ""); st = st.Replace("void", ""); cmbGoToCode.Items.Add(st.Trim()); // Response.Write(mt.ToString() + "<br />"); } } catch (System.Exception exx) { //util.playSimpleSound(Path.Combine(util.sound_dir, "zasentodalaboca.wav")); SystemSounds.Asterisk.Play(); AsyncWriteLine(exx.Message); rt1.Text = exx.Message; //System.Console.Out.WriteLine("Problem merging template : " + exx); System.Console.Out.WriteLine("Problem evaluating template : " + exx); } SystemSounds.Exclamation.Play(); //util.playSimpleSound(Path.Combine(util.sound_dir, "risapetergriffin.wav")); } catch (Exception ex) { // util.playSimpleSound(Path.Combine(util.sound_dir, "zasentodalaboca.wav")); SystemSounds.Asterisk.Play(); rt1.Text = ex.Message; } }
private void tratarFile(object file) { // ahora tratamos cada archivo en una nueva tarea... try { string archivito = ""; archivito = file.ToString(); AsyncWriteLine("Procesando " + archivito); char tabCaracter = '\u0009'; try { String plantilla = util.loadFile(archivito); // sacamos las variables de la plantilla variablesTemplate var = new variablesTemplate(); var = util.getVariablesFromTemplate(plantilla); // vamos a sacar unas variables que luego nos serviran... string nombreArchivo = ""; string nombreArchivoFinal = ""; string nombreDirectorioAgrabar = ""; string rutaArchivoFinal = ""; nombreArchivo = util.ExtractFilename(archivito); // sacamos de la ruta el directorio del archivo ... nombreDirectorioAgrabar = archivito; nombreDirectorioAgrabar = nombreDirectorioAgrabar.Replace(general.actualProject.projectTemplatesDirectory, ""); nombreDirectorioAgrabar = nombreDirectorioAgrabar.Replace(nombreArchivo, ""); if (nombreDirectorioAgrabar.StartsWith("\\") && !nombreDirectorioAgrabar.Equals("")) nombreDirectorioAgrabar = nombreDirectorioAgrabar.Substring(1, nombreDirectorioAgrabar.Length - 2); rutaArchivoFinal = Path.Combine(general.targetDirectory, nombreDirectorioAgrabar); if (var.namefile != null) { nombreArchivoFinal = Path.Combine(rutaArchivoFinal, var.namefile + "." + var.extensionFile); } else { nombreArchivoFinal = Path.Combine(rutaArchivoFinal, nombreArchivo); } if (!Directory.Exists(Path.Combine(general.targetDirectory, nombreDirectorioAgrabar))) Directory.CreateDirectory(Path.Combine(general.targetDirectory, nombreDirectorioAgrabar)); // si no tiene variables de configuracion es que no es un template... if (var.description == null && var.extensionFile == null) // no es una plantilla... { // simplemente lo grabamos a disco //util.saveTextToFile(nombreArchivoFinal, plantilla); System.IO.File.Copy(archivito, nombreArchivoFinal, true); } else // es una plantilla... { try { // si da un error en singleton es que falta la libreria commons o log4net.. Velocity.Init(); //VelocityEngine velocityEngine = new VelocityEngine(); //ExtendedProperties props = new ExtendedProperties(); //props.AddProperty("input.encoding", "UTF-8"); //props.AddProperty("output.encoding", "UTF-8"); //velocityEngine.Init(props); } catch (System.Exception exx) { AsyncWriteLine("Problem initializing Velocity : " + exx.Message); return; } if (var.appliesToAllTables.Equals("true")) { foreach (table item in general.actualProject.tables) { if (item.excludeFromGeneration) { } else { // tenemos que aplicar esta plantilla una vez por cada tabla... // lets make a Context and put data into it VelocityContext context = new VelocityContext(); context.Put("project", general.actualProject); context.Put("table", item); // lets render a template StringWriter writer = new StringWriter(); try { //Velocity.MergeTemplate(plantilla, context, writer); Velocity.Evaluate(context, writer, "prueba", plantilla); // sacamos las variables del archivo antes de borrarlas variablesTemplate varT = new variablesTemplate(); varT = util.getVariablesFromTemplate(writer.GetStringBuilder().ToString()); if (varT.namefile != null) { if (!Directory.Exists(Path.Combine(rutaArchivoFinal, varT.targetDirectory))) Directory.CreateDirectory(Path.Combine(rutaArchivoFinal, varT.targetDirectory)); nombreArchivoFinal = Path.Combine(Path.Combine(rutaArchivoFinal, varT.targetDirectory), varT.namefile + "." + var.extensionFile); } // now we delete the variables from the template cause there are no needed... string finalText = util.deleteVariablesFromTemplate(writer.GetStringBuilder().ToString()); // todo... // le quitamos saltos de linea extra finalText = finalText.Replace("\r\n\r\n", "\r\n").Replace("\r\n\r\n\r\n", "").Replace("\r\n\r\n\r\n\r\n", ""); // le quitamos los tabuladores extra finalText = finalText.Replace(tabCaracter.ToString(), " "); // grabamos segun el util.saveTextToFile(nombreArchivoFinal, finalText); } catch (System.Exception exx) { // file AsyncCleanRt1(""); AsyncWriteLine("Problem with the template : " + file.ToString()); AsyncWriteLine("Error : " + exx.Message); AsyncWriteLine("file://" + file.ToString()); errores.Add(file.ToString()); //AsyncAddControl(file.ToString()); //rt1.Text = exx.Message; //System.Console.Out.WriteLine("Problem merging template : " + exx); System.Console.Out.WriteLine("Problem evaluating template : " + exx); } }// end de else excludeFromGeneration } // end de for each } else { // es un template que no utiliza las tablas... // lets make a Context and put data into it VelocityContext context = new VelocityContext(); context.Put("project", general.actualProject); //context.Put("table", tab); // lets render a template StringWriter writer = new StringWriter(); try { //Velocity.MergeTemplate(plantilla, context, writer); Velocity.Evaluate(context, writer, "prueba", plantilla); // now we delete the variables from the template cause there are no needed... string finalText = util.deleteVariablesFromTemplate(writer.GetStringBuilder().ToString()); // todo... // le quitamos saltos de linea extra finalText = finalText.Replace("\r\n\r\n", "\r\n").Replace("\r\n\r\n\r\n", "").Replace("\r\n\r\n\r\n\r\n", ""); // le quitamos los tabuladores extra finalText = finalText.Replace(tabCaracter.ToString(), " "); util.saveTextToFile(nombreArchivoFinal, finalText); } catch (System.Exception exx) { rt1.Text = exx.Message; //System.Console.Out.WriteLine("Problem merging template : " + exx); System.Console.Out.WriteLine("Problem evaluating template : " + exx); } } } // //StringTemplate hello = new StringTemplate(plantilla); //hello.SetAttribute("table", tab); //rt1.Text = hello.ToString(); } catch (Exception ex) { AsyncWriteLine(ex.Message); } } catch (Exception ex) { AsyncWriteLine("Error " + ex.Message); //throw; } }
private void tratarFile(object file) { // ahora tratamos cada archivo en una nueva tarea... try { string archivito = ""; archivito = file.ToString(); // AsyncWriteLine("Procesando " + archivito); char tabCaracter = '\u0009'; try { String plantilla = util.loadFile(archivito); // sacamos las variables de la plantilla variablesTemplate var = new variablesTemplate(); var = util.getVariablesFromTemplate(plantilla); // vamos a sacar unas variables que luego nos serviran... string nombreArchivo = ""; nombreArchivo = util.ExtractFilename(archivito); // si no tiene variables de configuracion es que no es un template... if (var.description == null && var.extensionFile == null) // no es una plantilla... { // no es una plantilla... no hacemos nada } else // es una plantilla... { userControlLinktemplate uc = new userControlLinktemplate(); uc.pathTemplate = archivito; uc.nameFile = nombreArchivo; uc.linkLabel1.Text = var.description; uc.parent = this; // panel1.Controls.Add(uc); AsyncAddControl(uc); //uc = null; } } catch (Exception ex) { AsyncWriteLine(ex.Message); } } catch (Exception ex) { AsyncWriteLine("Error " + ex.Message); //throw; } }