<commit_before>﻿using FilenameBuddy;
using System;
using System.Diagnostics;
using System.IO;
using System.Xml;

namespace XmlBuddy
{
	public delegate void XmlNodeFunc(XmlNode xmlNode);

	/// <summary>
	/// Stores all the states, messages, and state transitions
	/// </summary>
	public abstract class XmlFileBuddy
	{
		#region Fields

		/// <summary>
		/// The name this type of content is stored in the xml file
		/// </summary>
		private string ContentName { get; set; }

		/// <summary>
		/// The file this dude was read/write from
		/// </summary>
		public Filename XmlFilename { get; set; }

		#endregion //Fields

		#region Methods

		/// <summary>
		/// constructor
		/// </summary>
		public XmlFileBuddy(string contentName)
		{
			ContentName = contentName;
		}

		/// <summary>
		/// copy constructor
		/// </summary>
		protected XmlFileBuddy(XmlFileBuddy obj)
		{
			ContentName = obj.ContentName;
			XmlFilename = new Filename(obj.XmlFilename);
		}

		/// <summary>
		/// read in serialized xna state machine from XML
		/// </summary>
		/// <param name="strFilename">file to open</param>
		/// <returns>whether or not it was able to open it</returns>
		public virtual void ReadXmlFile(Filename strFilename)
		{
			XmlFilename = strFilename;

			// Open the file.
			FileStream stream = File.Open(strFilename.File, FileMode.Open, FileAccess.Read);
			XmlDocument xmlDoc = new XmlDocument();
			xmlDoc.Load(stream);
			XmlNode rootNode = xmlDoc.DocumentElement;

			if (rootNode.NodeType != XmlNodeType.Element)
			{
				//should be an xml node!!!
				throw new Exception("not an xml node: " + rootNode.NodeType.ToString());
			}

			//eat up the name of that xml node
			if (("XnaContent" != rootNode.Name) || !rootNode.HasChildNodes)
			{
				throw new Exception("invalid XnaContent node: " + rootNode.Name);
			}

			//next node is "<Asset Type="SPFSettings.StateMachineXML">"
			XmlNode assetNode = rootNode.FirstChild;
			if (!assetNode.HasChildNodes)
			{
				throw new Exception("invalid Asset node: no child nodes");
			}
			if ("Asset" != assetNode.Name)
			{
				throw new Exception("invalid Asset node: " + assetNode.Name);
			}

			//should have an attribute Type
			XmlNamedNodeMap mapAttributes = assetNode.Attributes;
			for (int i = 0; i < mapAttributes.Count; i++)
			{
				//will only have the name attribute
				string strName = mapAttributes.Item(i).Name;
				string strValue = mapAttributes.Item(i).Value;
				if ("Type" == strName)
				{
					if (strValue != ContentName)
					{
						throw new Exception("invalid Type node: " + strValue);
					}
				}
				else
				{
					throw new Exception("invalid Type node: " + strName);
				}
			}

			//Read in child nodes
			ReadChildNodes(assetNode, ParseXmlNode);

			// Close the file.
			stream.Close();
		}

		/// <summary>
		/// write out this object to an xml file
		/// </summary>
		/// <param name="strFilename">teh file to write out to</param>
		public void WriteXml(Filename strFilename)
		{
			XmlFilename = strFilename;

			//open the file, create it if it doesnt exist yet
			XmlTextWriter xmlFile = new XmlTextWriter(strFilename.File, null);
			xmlFile.Formatting = Formatting.Indented;
			xmlFile.Indentation = 1;
			xmlFile.IndentChar = '\t';

			xmlFile.WriteStartDocument();

			//add the xml node
			xmlFile.WriteStartElement("XnaContent");
			xmlFile.WriteStartElement("Asset");
			xmlFile.WriteAttributeString("Type", ContentName);

			WriteXmlNodes(xmlFile);

			xmlFile.WriteEndElement();
			xmlFile.WriteEndElement();

			xmlFile.WriteEndDocument();

			// Close the file.<commit_msg>readings tuf fin<commit_after>﻿using FilenameBuddy;
using System;
using System.Diagnostics;
using System.IO;
using System.Xml;

namespace XmlBuddy
{
	public delegate void XmlNodeFunc(XmlNode xmlNode);

	/// <summary>
	/// Stores all the states, messages, and state transitions
	/// </summary>
	public abstract class XmlFileBuddy
	{
		#region Fields

		/// <summary>
		/// The name this type of content is stored in the xml file
		/// </summary>
		private string ContentName { get; set; }

		/// <summary>
		/// The file this dude was read/write from
		/// </summary>
		public Filename XmlFilename { get; set; }

		#endregion //Fields

		#region Methods

		/// <summary>
		/// constructor
		/// </summary>
		public XmlFileBuddy(string contentName)
		{
			ContentName = contentName;
			XmlFilename = new Filename();
		}

		/// <summary>
		/// constructor
		/// </summary>
		public XmlFileBuddy(string contentName, Filename file)
		{
			ContentName = contentName;
			XmlFilename = new Filename(file);
		}

		/// <summary>
		/// copy constructor
		/// </summary>
		protected XmlFileBuddy(XmlFileBuddy obj)
			: this(obj.ContentName, obj.XmlFilename)
		{
		}

		/// <summary>
		/// read in serialized xna state machine from XML
		/// </summary>
		/// <param name="strFilename">file to open</param>
		/// <returns>whether or not it was able to open it</returns>
		public virtual void ReadXmlFile()
		{
			// Open the file.
			FileStream stream = File.Open(XmlFilename.File, FileMode.Open, FileAccess.Read);
			XmlDocument xmlDoc = new XmlDocument();
			xmlDoc.Load(stream);
			XmlNode rootNode = xmlDoc.DocumentElement;

			if (rootNode.NodeType != XmlNodeType.Element)
			{
				//should be an xml node!!!
				throw new Exception("not an xml node: " + rootNode.NodeType.ToString());
			}

			//eat up the name of that xml node
			if (("XnaContent" != rootNode.Name) || !rootNode.HasChildNodes)
			{
				throw new Exception("invalid XnaContent node: " + rootNode.Name);
			}

			//next node is "<Asset Type="SPFSettings.StateMachineXML">"
			XmlNode assetNode = rootNode.FirstChild;
			if (!assetNode.HasChildNodes)
			{
				throw new Exception("invalid Asset node: no child nodes");
			}
			if ("Asset" != assetNode.Name)
			{
				throw new Exception("invalid Asset node: " + assetNode.Name);
			}

			//should have an attribute Type
			XmlNamedNodeMap mapAttributes = assetNode.Attributes;
			for (int i = 0; i < mapAttributes.Count; i++)
			{
				//will only have the name attribute
				string strName = mapAttributes.Item(i).Name;
				string strValue = mapAttributes.Item(i).Value;
				if ("Type" == strName)
				{
					if (strValue != ContentName)
					{
						throw new Exception("invalid Type node: " + strValue);
					}
				}
				else
				{
					throw new Exception("invalid Type node: " + strName);
				}
			}

			//Read in child nodes
			ReadChildNodes(assetNode, ParseXmlNode);

			// Close the file.
			stream.Close();
		}

		/// <summary>
		/// write out this object to an xml file
		/// </summary>
		/// <param name="strFilename">teh file to write out to</param>
		public void WriteXml()
		{
			//open the file, create it if it doesnt exist yet
			XmlTextWriter xmlFile = new XmlTextWriter(XmlFilename.File, null);
			xmlFile.Formatting = Formatting.Indented;
			xmlFile.Indentation = 1;
			xmlFile.IndentChar = '\t';

			xmlFile.WriteStartDocument();

			//add the xml node
			xmlFile.WriteStartElement("XnaContent");
			xmlFile.WriteStartElement("Asset");
			xmlFile.WriteAttributeString("Type", ContentName);

			WriteXmlNodes(xmlFile);

			xmlFile.WriteEndElement();
			xmlFile.WriteEndElement();

			xmlFile.WriteEndDocument();

			// Close the file.
{'fraction_non_alphanumeric': 0.12548315847598013, 'fraction_numerical': 0.0005521811154058532, 'mean_word_length': 3.0027624309392267, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 44}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3576525', 'n_tokens_mistral': 2682, 'n_tokens_neox': 2184, 'n_words': 632}