//SHARED #region Convert Functions /// ----------------------------------------------------------------------------- /// <summary> /// Convert ASP.NET code using language-autodetection. /// </summary> /// <param name="aspxSource">The ASPX source code.</param> /// <returns>New instance of Class ASPXConverter</returns> /// <remarks> /// Supported is only ASP.NET code with inline serverside code of the languages Visual Basic .NET or C#. /// </remarks> /// ----------------------------------------------------------------------------- public static ASPXConverter Convert(string aspxSource, ConverterLanguagesAutodetectable converterLanguage = ConverterLanguagesAutodetectable.Autodetect) { ASPXConverter RetConv = new ASPXConverter(); Int32 TmpStart; Int32 TmpEnd; Int32 TmpLastEnd; string TmpDirective; string TmpComment = ""; string TmpCodeLines; string TmpConvertCode = ""; ArrayList TmpHtmlBlocks = new ArrayList(); string TmpS; try { //set language if (converterLanguage == ConverterLanguagesAutodetectable.Autodetect) { RetConv.FConverterLanguage = SearchLanguage(aspxSource); } else { RetConv.FConverterLanguage = converterLanguage; } //get Before-Convert-Comment-Sign switch (RetConv.FConverterLanguage) { case ConverterLanguagesAutodetectable.CSharpToVBNet: TmpComment = "//"; case ConverterLanguagesAutodetectable.VBNetToCSharp: TmpComment = "'"; } //for each <%...%> block TmpStart = -1; TmpLastEnd = 0; do { //search "<%" TmpStart = aspxSource.IndexOf("<%", TmpStart + 1); if (TmpStart > -1) { //search "%>" TmpEnd = aspxSource.IndexOf("%>", TmpStart + 2); if (TmpEnd > -1) { if (aspxSource.Substring(TmpStart + 2, 1) == "@") { //if @: replace language-attribute and codebehind file extension TmpDirective = aspxSource.Substring(TmpStart, TmpEnd - TmpStart + 2); switch (RetConv.FConverterLanguage) { case ConverterLanguagesAutodetectable.CSharpToVBNet: TmpDirective = TmpDirective.Replace("\"c#\"", "\"vb\"").Replace(".cs\"", ".vb\"").Replace("=cs ", "=vb ").Replace("=c# ", "=vb ").Replace("\"cs\"", "\"vb\""); case ConverterLanguagesAutodetectable.VBNetToCSharp: TmpDirective = TmpDirective.Replace("\"vb\"", "\"c#\"").Replace(".vb\"", ".cs\"").Replace("=vb ", "=c# "); } aspxSource = aspxSource.Substring(0, TmpStart) + TmpDirective + aspxSource.Substring(TmpEnd + 2, aspxSource.Length - TmpEnd - 2); } else { //else: convert statements TmpHtmlBlocks.Add(aspxSource.Substring(TmpLastEnd, TmpStart - TmpLastEnd + 2)); TmpConvertCode += TmpComment + "HTMLBLOCK-" + TmpHtmlBlocks.Count.ToString + "." + vbCrLf; TmpCodeLines = aspxSource.Substring(TmpStart + 2, TmpEnd - TmpStart - 2); TmpS = TmpCodeLines.TrimStart(char.Parse(" "), Chr(9), Chr(10), Chr(13)); if ((TmpS != "") && (TmpS.Substring(0, 1) == "=")) { //Add Temporary Out-Variable-Prefix TmpCodeLines = "NETVERTASPNETOUT " + TmpS; } try { } catch (Exception ex) { } //if StrTrimmLeft(StrTrimmLeft(StrTrimmLeft(tmpcodelines," "), chr(9).ToString) TmpConvertCode += TmpCodeLines + vbCrLf; TmpLastEnd = TmpEnd; } } } } while (TmpStart > -1); //Append last HTML-Block if (aspxSource.Length > TmpLastEnd) { TmpHtmlBlocks.Add(aspxSource.Substring(TmpLastEnd, aspxSource.Length - TmpLastEnd)); TmpConvertCode += TmpComment + "HTMLBLOCK-" + TmpHtmlBlocks.Count.ToString + "." + vbCrLf; } //Convert Code switch (RetConv.FConverterLanguage) { case ConverterLanguagesAutodetectable.CSharpToVBNet: // ERROR: Not supported in C#: WithStatement //get After-Convert-Comment-Sign TmpComment = "'"; case ConverterLanguagesAutodetectable.VBNetToCSharp: // ERROR: Not supported in C#: WithStatement //get After-Convert-Comment-Sign TmpComment = "//"; } //Remove Temporary Out-Variable-Prefix TmpConvertCode = TmpConvertCode.Replace("NETVERTASPNETOUT ", ""); //Insert HTML-Blocks for (Int32 iBlock = 0; iBlock <= TmpHtmlBlocks.Count - 1; iBlock++) { TmpConvertCode = TmpConvertCode.Replace(TmpComment + "HTMLBLOCK-" + (iBlock + 1).ToString + ".", TmpHtmlBlocks(iBlock)); } //set result RetConv.FResultSource = TmpConvertCode; #if (DEBUG) //catch only internal exceptions } catch (NetVertException nEx) { RetConv.FHasError = true; RetConv.FErrorText = nEx.Message; #Else //catch all exceptions } catch (Exception ex) { RetConv.FHasError = true; RetConv.FErrorText = ex.Message; #endif } return(RetConv); }
/// ----------------------------------------------------------------------------- /// <summary> /// Convert single ASPX file. /// </summary> /// <param name="sourceFile">The full path and filename to the source ASPX file.</param> /// <param name="targetFile">The full path and filename to the destination ASPX file.</param> /// <param name="overwriteExistingFile">True if existing file should be overwriten ([default] is false).</param> /// ----------------------------------------------------------------------------- public void ConvertFile(string sourceFile, string targetFile, bool overwriteExistingFile = false) { ASPXConverter TmpConv; string TmpS; ProcessedFileEventArgs TmpE; try { // ERROR: Not supported in C#: WithStatement FTotalCount += 1; if (File.Exists(targetFile) && !overwriteExistingFile) { //Skipped, file exists if (AfterFileProcessed != null) { AfterFileProcessed(this, new ProcessedFileEventArgs(ProcessedFileEventArgs.ConverterFileTypes.ASPXFile, sourceFile, "", targetFile, "", ProcessedFileEventArgs.ConverterOperations.Skipped_Exists, FLanguage, "")); } } else { // ERROR: Not supported in C#: WithStatement //create EventArgs TmpE = new ProcessedFileEventArgs(ProcessedFileEventArgs.ConverterFileTypes.ASPXFile, sourceFile, TmpS, targetFile, "", ProcessedFileEventArgs.ConverterOperations.Before_Conversion, FLanguage, ""); if (BeforeFileProcessed != null) { BeforeFileProcessed(this, TmpE); } //convert now TmpConv = ASPXConverter.Convert(TmpS, FLanguage); if (TmpConv.HasError) { //has an error FFailedCount += 1; TmpE.ModifyStatus(ProcessedFileEventArgs.ConverterOperations.Skipped_Error, TmpConv.ErrorText, TmpConv.ConverterLanguage); if (AfterFileProcessed != null) { AfterFileProcessed(this, TmpE); } } else { //no error FConvertedCount += 1; if (File.Exists(targetFile)) { TmpE.ModifyStatus(ProcessedFileEventArgs.ConverterOperations.Success_Overwriting, "", TmpConv.ConverterLanguage); File.Delete(targetFile); } else { TmpE.ModifyStatus(ProcessedFileEventArgs.ConverterOperations.Success_Creating, "", TmpConv.ConverterLanguage); } TmpE.OutputSource = TmpConv.ASPXOutputSource; if (AfterFileProcessed != null) { AfterFileProcessed(this, TmpE); } //save new stream to file try { // ERROR: Not supported in C#: WithStatement } catch (Exception ex) { TmpE.ModifyStatus(ProcessedFileEventArgs.ConverterOperations.Skipped_Error, ex.Message); if (AfterFileProcessed != null) { AfterFileProcessed(this, TmpE); } } } } #if (DEBUG) } catch (NetVertException ex) { //common error if (AfterFileProcessed != null) { AfterFileProcessed(this, new ProcessedFileEventArgs(ProcessedFileEventArgs.ConverterFileTypes.ASPXFile, sourceFile, "", targetFile, "", ProcessedFileEventArgs.ConverterOperations.Skipped_Error, FLanguage, ex.Message)); } #Else } catch (Exception ex) { //common error if (AfterFileProcessed != null) { AfterFileProcessed(this, new ProcessedFileEventArgs(ProcessedFileEventArgs.ConverterFileTypes.ASPXFile, sourceFile, "", targetFile, "", ProcessedFileEventArgs.ConverterOperations.Skipped_Error, FLanguage, ex.Message)); } #endif } //With Directory.GetParent(targetFile) // If Not .Exists Then // OutputText("Creating directory " & .FullName) // .Create() // End If //End With //FTotalCount += 1 //If File.Exists(targetFile) AndAlso _ // Not overwriteExistingFile Then // OutputText("Skipped, file exists " & targetFile) //Else // TmpReader = File.OpenText(sourceFile) // TmpS = TmpReader.ReadToEnd // TmpReader.Close() // 'TmpError = "" // TmpConv = ASPXConverter.Convert(TmpS, FLanguage) // If TmpConv.HasError Then // 'has an error // FFailedCount += 1 // OutputText("Error in " & sourceFile & vbCrLf & TmpConv.ErrorText) // Else // 'no error // If File.Exists(targetFile) Then // OutputText("Overwriting " & targetFile) // Else // OutputText("Creating " & targetFile) // End If // FConvertedCount += 1 // TmpWriter = File.CreateText(targetFile) // TmpWriter.Write(TmpConv.ASPXOutputSource) // TmpWriter.Close() // End If //End If }