コード例 #1
0
        public static XElement Load(string filename, BxlParserOptions options = BxlParserOptions.None)
        {
            var f = filename;

            if (!File.Exists(f))
            {
                f = filename + ".xml";
            }
            if (!File.Exists(f))
            {
                f = filename + ".bxl";
            }
            if (!File.Exists(f))
            {
                return(null);
            }
            var ext = Path.GetExtension(f);

            if (null != ext)
            {
                if (ext.StartsWith(".bxl"))
                {
                    return(new BxlParser().Parse(File.ReadAllText(f), f, options));
                }
            }
            return(XElement.Load(f));
        }
コード例 #2
0
ファイル: FileSet.cs プロジェクト: Qorpent/qorpent.sys
 public IEnumerable<XElement> CollectBxl(BxlParserOptions options = BxlParserOptions.None) {
     var bxl = new BxlParser();
     return 
         from file in Collect()
         let text = File.ReadAllText(file)
         select bxl.Parse(text, file, options);
 }
コード例 #3
0
        static int Main(string[] args)
        {
            var argsdict = new ConsoleArgumentHelper().ParseDictionary(args);

            if (argsdict.ContainsKey("arg1"))
            {
                filename = argsdict["arg1"];
            }
            if (argsdict.ContainsKey("nolex"))
            {
                opts |= BxlParserOptions.NoLexData;
            }
            if (argsdict.ContainsKey("tofile"))
            {
                useoutfile = true;
            }
            if (string.IsNullOrWhiteSpace(filename))
            {
                useoutfile = true;
            }
            string[] files;
            if (!string.IsNullOrWhiteSpace(filename))
            {
                files = new[] { filename };
            }
            else
            {
                files = Directory.GetFiles(Environment.CurrentDirectory, "*.bxl", SearchOption.AllDirectories);
            }
            foreach (var file in files)
            {
                Execute(file);
            }
            return(0);
        }
コード例 #4
0
        public IEnumerable <XElement> CollectBxl(BxlParserOptions options = BxlParserOptions.None)
        {
            var bxl = new BxlParser();

            return
                (from file in Collect()
                 let text = File.ReadAllText(file)
                            select bxl.Parse(text, file, options));
        }
コード例 #5
0
        private void init(string filename, BxlParserOptions options)
        {
            _options = options;
            _info    = new LexInfo(filename, 1);
            _expStack.Clear();
            DEFAULT_NS_PREFIX = NAMESPACE + filename + "_";
            string __ = "";

            if (_options.HasFlag(BxlParserOptions.SafeAttributeNames))
            {
                __ = "__";
            }
            ANON_CODE = __ + CODE;
            ANON_ID   = __ + ID;
            ANON_NAME = __ + NAME;
            _skip     = 0;
            _level    = -1;
            _anon     = new List <Stats>()
            {
                new Stats()
            };
            _symbolCount  = 0;
            _tabIgnore    = 0;
            _tabs         = 0;
            _defNsCount   = 0;
            _value        = "";
            _prefix       = "";
            _isString     = false;
            _isExpression = false;
            _buf.Clear();
            _stack.Clear();
            _next = '\0';

            _root    = new XElement(ROOT_NAME);
            _current = _root;
            _mode    = ReadMode.Start;
        }
コード例 #6
0
ファイル: BxlService.cs プロジェクト: Qorpent/qorpent.sys
 /// <summary>
 ///     Perform Bxl parsing of given code
 /// </summary>
 /// <param name="code"> source code </param>
 /// <param name="filename"> filename for lexinfo </param>
 /// <param name="options"> BxlParsing options </param>
 /// <returns> XElement with xml equivalent </returns>
 public XElement Parse(string code, string filename = "code.bxl", BxlParserOptions options = BxlParserOptions.None)
 {
     return(new BxlParser().Parse(code, filename, options));
 }
コード例 #7
0
        /// <summary>
        ///     Parses source code into Xml
        /// </summary>
        /// <param name="code"></param>
        /// <param name="filename"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public XElement Parse(string code = null, string filename = "code.bxl", BxlParserOptions options = BxlParserOptions.None)
        {
            if (string.IsNullOrWhiteSpace(code))
            {
                if (string.IsNullOrWhiteSpace(filename))
                {
                    return(new XElement("root"));
                }
                code = File.ReadAllText(filename);
            }

            if (string.IsNullOrWhiteSpace(filename))
            {
                filename = "code.bxl";
            }
            init(filename, options);
            int l = code.Length;

            for (var i = 0; i < l; i++)
            {
                if (_skip > 0)
                {
                    _skip--;
                    continue;
                }
                var c = code[i];
                if (i < code.Length - 1)
                {
                    _next = code[i + 1];
                }
                else
                {
                    _next = '\0';
                }
                _info.CharIndex++;
                _info.Column++;
                if (c == 160)
                {
                    c = ' ';
                }
                if (c == '\t')
                {
                    _info.Column += 3;
                }
                if (c == '\r')
                {
                    continue;
                }
                map[(int)_mode](c);

                if (c == '\n')
                {
                    _info.Line++;
                    _info.Column = 0;
                }
            }

            if (code.Last() != '\n')
            {
                map[(int)_mode]('\n');
            }

            if (_options.HasFlag(BxlParserOptions.ExtractSingle) && _root.Elements().Count() == 1)
            {
                _current = _root.Elements().First();
                _current.Remove();
                // explicit copy namespaces from _root to _current ?
                _root = _current;
            }

            if (_stack.IsNotEmpty())
            {
                throw new BxlException("invalid quotes or braces", _info.Clone());
            }
            if (!options.HasFlag(BxlParserOptions.NoLexData))
            {
                _root.SetAttr("_file", filename);
            }

            if (options.HasFlag(BxlParserOptions.PerformInterpolation))
            {
                _root = _root.Interpolate();
            }

            return(_root);
        }
コード例 #8
0
        /// <summary>
        ///     converts given object to xml due it's type - XElement, XmlReader, TextReader, Stream, string are supported
        /// </summary>
        /// <param name="xmlsource"> The xmlsource. </param>
        /// <param name="options"> </param>
        /// <returns> </returns>
        /// <remarks>
        /// </remarks>
        public static XElement GetXmlFromAny(object xmlsource, BxlParserOptions options = BxlParserOptions.None)
        {
            if (null == xmlsource)
            {
                throw new ArgumentNullException("xmlsource");
            }
            var xelement = xmlsource as XElement;

            if (null != xelement)
            {
                return(xelement);
            }
            var str = xmlsource as string;

            if (null != str)
            {
                if (str.Contains("<") && str.Contains(">"))
                {
                    //it's xml string
                    return(XElement.Parse(str));
                }
                if (str.Contains("\r") || str.Contains("\n"))             //bxl like mark)
                {
                    return(new BxlParser().Parse(str, "main", options));
                }
                //it's filename/url
                if (IsBxlFileName(str))
                {
                    return(new BxlParser().Parse(File.ReadAllText(str), str));
                }
                return(XElement.Load(str));
            }
            var xmlreader = xmlsource as XmlReader;

            if (null != xmlreader)
            {
                return(XElement.Load(xmlreader));
            }
            var textreader = xmlsource as TextReader;

            if (null != textreader)
            {
                return(XElement.Load(textreader));
            }
            var stream = xmlsource as Stream;

#if SQL2008
            if (null != stream)
            {
                using (var sr = new StreamReader(stream)) {
                    return(XElement.Load(sr));
                }
            }
#else
            if (null != stream)
            {
                return(XElement.Load(stream));
            }
#endif
            var uri = xmlsource as Uri;
            if (null != uri)
            {
                return(XElement.Load(uri.ToString()));
            }
            var olddoc = xmlsource as XmlDocument;
            if (null != olddoc)
            {
                var sw = new StringWriter();
                var xw = XmlWriter.Create(sw);
                olddoc.ChildNodes[0].WriteTo(xw);
                xw.Flush();
                return(XElement.Parse(sw.ToString()));
            }
            var nav = xmlsource as XPathNavigator;
            if (null != nav)
            {
                var sw = new StringWriter();
                var xw = XmlWriter.Create(sw);
                nav.WriteSubtree(xw);
                xw.Flush();
                return(XElement.Parse(sw.ToString()));
            }
            throw new QorpentException("xmlsource type " + xmlsource.GetType().FullName +
                                       " is not supported to convert to XElement");
        }