/// <summary>
 /// Stores the content of the Colorizer object (language definitions and styles) in an XML file
 /// </summary>
 /// <param name="filePath">The complete file path where the file will be located at</param>
 /// <param name="colorizer">The Colorizer object to be serialized</param>
 public static void Serialize(string filePath, Colorizer colorizer)
 {
     XmlTextWriter writer = null;
     FileStream file = null;
     try
     {
         /* TODO: reference loops can still cause problems, like when a LanguageElement has a Sublanguage that is also stored by the Colorizer
            the object appears multiple times in the XML file, yet after deserialization .NET seems to recognize that they are the same */
         DataContractSerializer serializer = new DataContractSerializer(typeof(Colorizer), null, int.MaxValue, true, false, null);
         file = File.Create(filePath);
         writer = new XmlTextWriter(file, Encoding.UTF8);
         writer.Formatting = Formatting.Indented;
         serializer.WriteObject(writer, colorizer);
     }
     catch (Exception exception)
     {
         MessageBox.Show("Couldn't save the file: \n" + filePath);
     }
     finally
     {
         if (writer != null)
             writer.Close();
         if (filePath != null)
             file.Close();
     }
 }
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            if (Directory.Exists(directoryPath))
            {
                colorizer = ColorizerSerializer.Deserialize(directoryPath + @"\default.xml");
                formatter = WordFormatterSerializer.Deserialize(directoryPath + @"\basicformat.xml");
                formatter.Initialize(colorizer, this.Application);
            }
            else // if it's the first starting of this version of the Add-In with this user
            {
                Directory.CreateDirectory(directoryPath);

                colorizer = new Colorizer();
                colorizer.LoadPredefinedLanguages();
                colorizer.Initialize();

                formatter = new WordFormatter();
                formatter.Initialize(colorizer, this.Application);
                formatter.SetToDefault();
            }

            indentFixer = new WordIndentFixer(this.Application);
            codecleaner = new CodeCleaner(this.Application);

            Globals.Ribbons.WordCodeEditorToolsRibbon.InitializeAddIn(this);
        }
        /// <summary>
        /// Loads the content of Colorizer object (language definitions and styles) from an XML file.
        /// If the deserializatoin is unsuccessful, then a Colorizer object with the default languages and styles is returned.
        /// </summary>
        /// <param name="filePath">The complete file path where the file will be located at</param>
        /// <returns>The Colorizer object filled with the contents of the file</returns>
        public static Colorizer Deserialize(string filePath)
        {
            Colorizer colorizer;
            FileStream fs = null;
            XmlDictionaryReader reader = null;
            try
            {
                fs = new FileStream(filePath, FileMode.Open);
                reader = XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas());
                DataContractSerializer ser = new DataContractSerializer(typeof(Colorizer));
                colorizer = (Colorizer)ser.ReadObject(reader, true);

                colorizer.Initialize();
            }
            catch (Exception exception)
            {
                MessageBox.Show("Couldn't load the language database file at: \n"
                    + filePath
                    + "\nonly the predefined styles and languages will be avaliable.");
                colorizer = new Colorizer();
                colorizer.LoadPredefinedLanguages();
                colorizer.Initialize();
            }
            finally
            {
                if (reader != null)
                    reader.Close();
                if (fs != null)
                    fs.Close();
            }
            return colorizer;
        }
        /// <summary>
        /// Stores the content of the Colorizer object (language definitions and styles) in an XML file
        /// </summary>
        /// <param name="filePath">The complete file path where the file will be located at</param>
        /// <param name="colorizer">The Colorizer object to be serialized</param>
        public static void Serialize(string filePath, Colorizer colorizer)
        {
            XmlTextWriter writer = null;
            FileStream    file   = null;

            try
            {
                /* TODO: reference loops can still cause problems, like when a LanguageElement has a Sublanguage that is also stored by the Colorizer
                 * the object appears multiple times in the XML file, yet after deserialization .NET seems to recognize that they are the same */
                DataContractSerializer serializer = new DataContractSerializer(typeof(Colorizer), null, int.MaxValue, true, false, null);
                file              = File.Create(filePath);
                writer            = new XmlTextWriter(file, Encoding.UTF8);
                writer.Formatting = Formatting.Indented;
                serializer.WriteObject(writer, colorizer);
            }
            catch (Exception exception)
            {
                MessageBox.Show("Couldn't save the file: \n" + filePath);
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }
                if (filePath != null)
                {
                    file.Close();
                }
            }
        }
        public LanguageStyleForm(Colorizer colorizer_)
        {
            InitializeComponent();

            colorizer = colorizer_;
            InitializeLanguageList();
        }
        /// <summary>
        /// Loads the content of Colorizer object (language definitions and styles) from an XML file.
        /// If the deserializatoin is unsuccessful, then a Colorizer object with the default languages and styles is returned.
        /// </summary>
        /// <param name="filePath">The complete file path where the file will be located at</param>
        /// <returns>The Colorizer object filled with the contents of the file</returns>
        public static Colorizer Deserialize(string filePath)
        {
            Colorizer           colorizer;
            FileStream          fs     = null;
            XmlDictionaryReader reader = null;

            try
            {
                fs     = new FileStream(filePath, FileMode.Open);
                reader = XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas());
                DataContractSerializer ser = new DataContractSerializer(typeof(Colorizer));
                colorizer = (Colorizer)ser.ReadObject(reader, true);

                colorizer.Initialize();
            }
            catch (Exception exception)
            {
                MessageBox.Show("Couldn't load the language database file at: \n"
                                + filePath
                                + "\nonly the predefined styles and languages will be avaliable.");
                colorizer = new Colorizer();
                colorizer.LoadPredefinedLanguages();
                colorizer.Initialize();
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
                if (fs != null)
                {
                    fs.Close();
                }
            }
            return(colorizer);
        }
Exemplo n.º 7
0
 /// <summary>
 /// The formatter needs an application (or more precisely its active document) to format, and a colorizer, to determine what colors to use for the formatting. These references must be initialized before any real work can be done.
 /// </summary>
 /// <param name="colorizer_"></param>
 /// <param name="application_"></param>
 public void Initialize(Colorizer colorizer_, Word.Application application_)
 {
     colorizer = colorizer_;
     wordApp = application_;
 }
        // Change whole language database
        private void button_LoadFile_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Do you want to save the current language database, before you load the new one?", "Syntax Highlighter", MessageBoxButtons.YesNoCancel);
            if (result == DialogResult.Yes)
            {
                button_SaveFile_Click(sender, EventArgs.Empty);
            }
            else if (result != DialogResult.Cancel)
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.InitialDirectory = @"c:\";
                openFileDialog.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*";
                openFileDialog.FilterIndex = 2;
                openFileDialog.RestoreDirectory = true;

                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    colorizer = ColorizerSerializer.Deserialize(openFileDialog.FileName);
                    this.InitializeLanguageList();
                    languageListChanged = true;
                    colorizerReassigned = true;
                }
            }
        }