//****************************************************************** /// <summary> /// Saves the displayed parses to the indicated parse file /// (sFileName). The Modified property is set to false after the /// file is saved. /// </summary> public void SaveParseFile(string sFileName) { //************************************************************** // Validate the parameters. if ((sFileName == null) || (sFileName == "")) { string sMessage = "Invalid argument: " + "ParseListViewer.SaveParseFile() requires " + "a file name that is not null or blank."; throw new Exception(sMessage); } //************************************************************** // Create a ParseWriter to write to the indicated parse file. StreamWriter oStreamWriter = new StreamWriter(sFileName); ParseWriter oParseWriter = new ParseWriter(oStreamWriter); //************************************************************** // For each item in the ListView, write the parse to the file. foreach (ListViewItem oItem in moListView.Items) { //********************************************************** // Get the parse from the ListView item. Debug.Assert(oItem is ParseListViewerItem); ParseListViewerItem oParseItem = (ParseListViewerItem) oItem; //********************************************************** // Make sure the parse tree is not null. SyntaxNode oParseTreeRoot = oParseItem.ParseTreeRoot; if (oParseTreeRoot == null) { oParseTreeRoot = new SyntaxNode(); } //********************************************************** // Write the parse tree to the file. oParseWriter.ParseTreeRoot = oParseTreeRoot; oParseWriter.Write(); } //************************************************************** // Close the ParseWriter. oParseWriter.Close(); //************************************************************** // Set the Modified property to false. Modified = false; }
//****************************************************************** /// <summary> /// The timer is fired once after the form is loaded, and this event /// handler performs the tree-transfer processing. It uses the /// collection of transfer rules to process the parse trees in the /// input file, and it writes the modified parse trees to the output /// file. /// </summary> private void moTimer_Tick(object oSender,EventArgs oArgs) { ParseReader oParseReader = null; ParseWriter oParseWriter = null; try { //********************************************************** // Stop the timer (so it only fires once after the form is // loaded). moTimer.Stop(); //********************************************************** // Initialize the information displayed on the form. Text = "Processing Parse File..."; moInputFileTextBox.Text = InputFileName; moOutputFileTextBox.Text = OutputFileName; moRuleCountTextBox.Text = TransferRules.Count.ToString(); moParseCountTextBox.Text = "0"; moInputFileTextBox.Select( moInputFileTextBox.Text.Length,0); moOutputFileTextBox.Select( moOutputFileTextBox.Text.Length,0); moRuleCountTextBox.Select( moRuleCountTextBox.Text.Length,0); moParseCountTextBox.Select( moParseCountTextBox.Text.Length,0); moParseCountTextBox.Focus(); moOKButton.Enabled = false; moCancelButton.Enabled = true; //********************************************************** // Make sure the form is visible. Show(); Application.DoEvents(); //********************************************************** // Open the input and output files. StreamReader oStreamReader = new StreamReader(InputFileName); oParseReader = new ParseReader(oStreamReader); StreamWriter oStreamWriter = new StreamWriter(OutputFileName); oParseWriter = new ParseWriter(oStreamWriter); //********************************************************** // Read and process each parse tree from the input file. int iCount = 0; ProcessingWasCanceled = false; while ((oParseReader.Read()) && (! ProcessingWasCanceled)) { //****************************************************** // Create a TreeTransfer object and set its // ParseTreeRoot to the parse tree that was read from // the input file. TreeTransfer oTransfer = new TreeTransfer(); oTransfer.ParseTreeRoot = oParseReader.ParseTreeRoot; //****************************************************** // Apply each rule in the TransferRules collection. foreach (TransferRule oRule in TransferRules) { oTransfer.FindPatternRoot = oRule.FindPatternRoot; oTransfer.ReplacePatternRoot = oRule.ReplacePatternRoot; //************************************************** // Apply the rule to each matching branch. oTransfer.CurrentParseTreeNode = null; while (oTransfer.FindNextMatchingBranch()) { oTransfer.ReplaceCurrentMatchingBranch(); } } //****************************************************** // Write the modified parse tree to the output file. oParseWriter.ParseTreeRoot = oTransfer.ParseTreeRoot; oParseWriter.Write(); //****************************************************** // Update the information displayed on the form. ++iCount; moParseCountTextBox.Text = iCount.ToString(); moParseCountTextBox.Select( moParseCountTextBox.Text.Length,0); //****************************************************** // Call DoEvents() so the form can update the display // and check if the user clicked the Cancel button. Application.DoEvents(); } //********************************************************** // Close the input and output files. oParseReader.Close(); oParseReader = null; oParseWriter.Close(); oParseWriter = null; //********************************************************** // Update the final information displayed on the form. if (ProcessingWasCanceled) { Text = "Processing Parse File... Canceled"; } else { Text = "Processing Parse File... Finished"; } moOKButton.Enabled = true; moCancelButton.Enabled = false; moOKButton.Focus(); //********************************************************** // If the Cancel button was clicked, set DialogResult to // DialogResult.Cancel, which will close the form. // // If the Cancel button was not clicked, but the // CloseWhenFinished property is true, set DialogResult to // DialogResult.OK, which will close the form. if (ProcessingWasCanceled) { DialogResult = DialogResult.Cancel; } else { if (CloseWhenFinished) { DialogResult = DialogResult.OK; } } } catch (Exception oException) { ShowException(oException); ProcessingWasCanceled = true; } try { //********************************************************** // Make sure the output file is closed (even if an exception // was thrown). if (oParseWriter != null) { oParseWriter.Close(); oParseWriter = null; } } catch (Exception oException) { ShowException(oException); ProcessingWasCanceled = true; } try { //********************************************************** // Make sure the OK button is enabled (even if an exception // was thrown). moOKButton.Enabled = true; moCancelButton.Enabled = false; moOKButton.Focus(); } catch (Exception oException) { ShowException(oException); ProcessingWasCanceled = true; } try { //********************************************************** // If the Cancel button was clicked (or an exception was // thrown), make sure DialogResult is set to // DialogResult.Cancel, which will close the form. if (ProcessingWasCanceled) { DialogResult = DialogResult.Cancel; } } catch (Exception oException) { ShowException(oException); } }