⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 languagereader.cs

📁 Fireball.CodeEditor is an source code editor control derived from the best compona SyntaxBox Control
💻 CS
📖 第 1 页 / 共 2 页
字号:

//	Copyright (C) 2005  Sebastian Faltoni <sebastian@dotnetfireball.org>
//	
//	Copyright (C) compona.com 
//	
//	This library is free software; you can redistribute it and/or
//	modify it under the terms of the GNU Lesser General Public
//	License as published by the Free Software Foundation; either
//	version 2.1 of the License, or (at your option) any later version.
//	
//	This library is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//	Lesser General Public License for more details.
//	
//	You should have received a copy of the GNU Lesser General Public
//	License along with this library; if not, write to the Free Software
//	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

using System;
using System.Xml;
using System.Collections;
using System.Drawing;
using Fireball.Syntax;
using System.IO;
using System.Collections.Specialized;
using System.Text;

namespace Fireball.Syntax
{
    /// <summary>
    /// 
    /// </summary>
    public class SyntaxLoader
    {
        private Hashtable _Styles = new Hashtable();
        private Hashtable _Blocks = new Hashtable();
        private Language _Language = new Language();
        private static bool _UseUserCustomStyles = false;

        //protected BlockType		mLanguage.MainBlock=null;

        private static string _UserCustomStyles = null;


        /// <summary>
        /// Directory where is saved language font and color configurations
        /// </summary>
        public static string UserCustomStyles
        {
            get { return SyntaxLoader._UserCustomStyles; }
            set { SyntaxLoader._UserCustomStyles = value; }
        }

        /// <summary>
        /// Set if user can user custom user styles for the Language
        /// </summary>
        public static bool UseUserCustomStyles
        {
            get { return _UseUserCustomStyles; }
            set { _UseUserCustomStyles = value; }
        }

        /// <summary>
        /// Load a specific language file
        /// </summary>
        /// <param name="File">File name</param>
        /// <returns>Language object</returns>
        public Language Load(string filename)
        {
            return Load(File.OpenRead(filename));
        }

        public Language Load(Stream stream)
        {
            _Styles = new Hashtable();
            _Blocks = new Hashtable();
            _Language = new Language();

            XmlDocument langDocument = new XmlDocument();
            langDocument.Load(stream);


            if (_UseUserCustomStyles && Directory.Exists(_UserCustomStyles))
            {
                string langName = langDocument.SelectSingleNode("Language/@Name").InnerText;

                string path = Path.Combine(SyntaxLoader.UserCustomStyles, langName + ".conf");

                if (File.Exists(path))
                {
                    XmlDocument userConf = new XmlDocument();

                    userConf.Load(path);

                    XmlNodeList xlist = langDocument.SelectNodes("Language/Style");

                    foreach (XmlNode current in xlist)
                    {
                        XmlNode userStyleNode = userConf.SelectSingleNode("styles/Style[@Name='" +
                            current.Attributes["Name"].InnerText + "']");

                        if (userStyleNode == null)
                            continue;

                        foreach (XmlAttribute userAtt in userStyleNode.Attributes)
                        {
                            current.Attributes[userAtt.LocalName].InnerText = userAtt.InnerText;
                        }
                    }
                }
            }

            ReadLanguageDef(langDocument);

            return _Language;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="File"></param>
        /// <returns></returns>
        public Language Load(string File, string Separators)
        {
            _Styles = new Hashtable();
            _Blocks = new Hashtable();
            _Language = new Language();
            _Language.Separators = Separators;

            XmlDocument myXmlDocument = new XmlDocument();
            myXmlDocument.Load(File);
            ReadLanguageDef(myXmlDocument);

            return _Language;
        }

        /// <summary>
        /// Load a specific language from an xml string
        /// </summary>
        /// <param name="XML"></param>
        /// <returns></returns>
        public Language LoadXML(string xml)
        {
            _Styles = new Hashtable();
            _Blocks = new Hashtable();
            _Language = new Language();

            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(xml);
            ReadLanguageDef(xDoc);

            return _Language;
        }

        private void ReadLanguageDef(XmlDocument xml)
        {
            ParseLanguage(xml["Language"]);
        }

        private void ParseLanguage(XmlNode node)
        {

            //get language name and startblock
            string name = "";
            string startBlock = "";

            foreach (XmlAttribute att in node.Attributes)
            {
                if (att.Name.ToLower() == "name")
                    name = att.Value;

                if (att.Name.ToLower() == "startblock")
                    startBlock = att.Value;
            }

            _Language.Name = name;
            _Language.MainBlock = GetBlock(startBlock);

            foreach (XmlNode n in node.ChildNodes)
            {
                if (n.NodeType == XmlNodeType.Element)
                {
                    if (n.Name.ToLower() == "filetypes")
                        ParseFileTypes(n);
                    if (n.Name.ToLower() == "block")
                        ParseBlock(n);
                    if (n.Name.ToLower() == "style")
                        ParseStyle(n);
                }
            }
        }

        private void ParseFileTypes(XmlNode node)
        {

            foreach (XmlNode current in node.ChildNodes)
            {
                if (current.NodeType == XmlNodeType.Element)
                {
                    if (current.Name.ToLower() == "filetype")
                    {
                        //add filetype
                        string Extension = "";
                        string Name = "";
                        foreach (XmlAttribute a in current.Attributes)
                        {
                            if (a.Name.ToLower() == "name")
                                Name = a.Value;
                            if (a.Name.ToLower() == "extension")
                                Extension = a.Value;
                        }
                        FileType ft = new FileType();
                        ft.Extension = Extension;
                        ft.Name = Name;
                        _Language.FileTypes.Add(ft);
                    }
                }
            }
        }

        private void ParseBlock(XmlNode node)
        {
            string Name = "", Style = "", PatternStyle = "";
            bool IsMultiline = false;
            bool TerminateChildren = false;
            Color BackColor = Color.Transparent;
            foreach (XmlAttribute att in node.Attributes)
            {
                if (att.Name.ToLower() == "name")
                    Name = att.Value;
                if (att.Name.ToLower() == "style")
                    Style = att.Value;
                if (att.Name.ToLower() == "patternstyle")
                    PatternStyle = att.Value;
                if (att.Name.ToLower() == "ismultiline")
                    IsMultiline = bool.Parse(att.Value);
                if (att.Name.ToLower() == "terminatechildren")
                    TerminateChildren = bool.Parse(att.Value);
                if (att.Name.ToLower() == "backcolor")
                {
                    BackColor = Color.FromName(att.Value);
                    //Transparent =false;
                }
            }

            //create block object here
            BlockType bl = GetBlock(Name);
            bl.BackColor = BackColor;
            bl.Name = Name;
            bl.MultiLine = IsMultiline;
            bl.Style = GetStyle(Style);
            bl.TerminateChildren = TerminateChildren;
            //		if (PatternStyle!="")
            //			bl.PatternStyle = GetStyle(PatternStyle);
            //		else
            //			bl.PatternStyle = bl.Style;			

            foreach (XmlNode n in node.ChildNodes)
            {
                if (n.NodeType == XmlNodeType.Element)
                {
                    if (n.Name.ToLower() == "scope")
                    {
                        //bool IsComplex=false;
                        //bool IsSeparator=false;
                        string Start = "";
                        string End = "";
                        string style = "";
                        string text = "";
                        string EndIsSeparator = "";
                        string StartIsSeparator = "";
                        string StartIsComplex = "false";
                        string EndIsComplex = "false";
                        string StartIsKeyword = "false";
                        string EndIsKeyword = "false";
                        string spawnstart = "";
                        string spawnend = "";
                        string EscapeChar = "";
                        string CauseIndent = "false";

                        bool expanded = true;

                        foreach (XmlAttribute att in n.Attributes)
                        {
                            if (att.Name.ToLower() == "start")
                                Start = att.Value;
                            if (att.Name.ToLower() == "escapechar")
                                EscapeChar = att.Value;
                            if (att.Name.ToLower() == "end")
                                End = att.Value;
                            if (att.Name.ToLower() == "style")
                                style = att.Value;
                            if (att.Name.ToLower() == "text")
                                text = att.Value;
                            if (att.Name.ToLower() == "defaultexpanded")
                                expanded = bool.Parse(att.Value);
                            if (att.Name.ToLower() == "endisseparator")
                                EndIsSeparator = att.Value;
                            if (att.Name.ToLower() == "startisseparator")
                                StartIsSeparator = att.Value;
                            if (att.Name.ToLower() == "startiskeyword")
                                StartIsKeyword = att.Value;
                            if (att.Name.ToLower() == "startiscomplex")
                                StartIsComplex = att.Value;
                            if (att.Name.ToLower() == "endiscomplex")
                                EndIsComplex = att.Value;
                            if (att.Name.ToLower() == "endiskeyword")
                                EndIsKeyword = att.Value;
                            if (att.Name.ToLower() == "spawnblockonstart")
                                spawnstart = att.Value;
                            if (att.Name.ToLower() == "spawnblockonend")
                                spawnend = att.Value;
                            if (att.Name.ToLower() == "causeindent")
                                CauseIndent = att.Value;
                        }
                        if (Start != "")
                        {
                            //bl.StartPattern =new Pattern (Pattern,IsComplex,false,IsSeparator);
                            //bl.StartPatterns.Add (new Pattern (Pattern,IsComplex,IsSeparator,true));
                            Scope scop = new Scope();
                            scop.Style = GetStyle(style);
                            scop.ExpansionText = text;
                            scop.DefaultExpanded = expanded;
                            bool blnStartIsComplex = bool.Parse(StartIsComplex);
                            bool blnEndIsComplex = bool.Parse(EndIsComplex);
                            bool blnCauseIndent = bool.Parse(CauseIndent);
                            scop.CauseIndent = blnCauseIndent;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -