private void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
        {
            string      locale = (string)localeBox.SelectedItem;
            ParsingType type   = (ParsingType)parsingControl.SelectedIndex;

            _parser = (Parser)Activator.CreateInstance((Type)parserBox.SelectedItem);
            if (_parser == null)
            {
                throw new ArgumentNullException();
            }

            string address = string.Format("http://{0}{1}", (string.IsNullOrEmpty(locale) ? "www." : locale), _parser.Address);

            switch (type)
            {
            case ParsingType.TypeSingle:
            {
                uint value = (uint)valueBox.Value;
                _worker = new Worker(value, address);
                break;
            }

            case ParsingType.TypeList:
            {
                progressBar.Maximum = _entries.Count;

                _worker = new Worker(_entries, address);
                break;
            }

            case ParsingType.TypeMultiple:
            {
                uint startValue = (uint)rangeStart.Value;
                uint endValue   = (uint)rangeEnd.Value;

                if (startValue > endValue)
                {
                    throw new ArgumentOutOfRangeException(@"Starting value can not be bigger than ending value!");
                }

                if (startValue == endValue)
                {
                    throw new ArgumentOutOfRangeException(@"Starting value can not be equal ending value!");
                }

                progressBar.Maximum = (int)(endValue - startValue);

                _worker = new Worker(startValue, endValue, address);
                break;
            }

            default:
                throw new NotImplementedException(string.Format(@"Unsupported type: {0}", type));
            }

            progressLabel.Text = "Downloading...";

            _startTime = DateTime.Now;
            _worker.Start(backgroundWorker);
        }
예제 #2
0
 public DateParse()
 {
     CubeID        = -1;
     FieldName     = "";
     ParseType     = ParsingType._DATE;
     trancateIndex = -1;
 }
예제 #3
0
 public Worker(uint value, string address)
 {
     _entry      = value;
     _address    = address;
     _threadLock = new object();
     _pages      = new Queue <Block>();
     _type       = ParsingType.TypeSingle;
     _requests   = new List <Requests>();
 }
예제 #4
0
 public Worker(uint value, string address)
 {
     _entry = value;
     _address = address;
     _threadLock = new object();
     _pages = new Queue<Block>();
     _type = ParsingType.TypeSingle;
     _requests = new List<Requests>();
 }
예제 #5
0
 public Worker(List <uint> entries, string address)
 {
     _entries    = entries;
     _address    = address;
     _threadLock = new object();
     _pages      = new Queue <Block>();
     _type       = ParsingType.TypeList;
     _requests   = new List <Requests>();
     _semaphore  = new Semaphore(1, 1);
 }
예제 #6
0
 public Worker(List<uint> entries, string address)
 {
     _entries = entries;
     _address = address;
     _threadLock = new object();
     _pages = new Queue<Block>();
     _type = ParsingType.TypeList;
     _requests = new List<Requests>();
     _semaphore = new Semaphore(1, 1);
 }
예제 #7
0
        public ParsingRequest(FileInfo file, ParsingType type, int maxPages)
        {
            if (maxPages <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(maxPages));
            }

            Type     = type;
            MaxPages = maxPages;
            File     = file ?? throw new ArgumentNullException(nameof(file));
        }
예제 #8
0
 public Worker(uint start, uint end, string address)
 {
     _end = end;
     _start = start;
     _address = address;
     _threadLock = new object();
     _pages = new Queue<Block>();
     _type = ParsingType.TypeMultiple;
     _requests = new List<Requests>();
     _semaphore = new Semaphore(1, 1);
 }
예제 #9
0
 public Worker(int value, string address, BackgroundWorker background)
 {
     _entry = value;
     _address = address;
     _background = background;
     _threadLock = new object();
     _pages = new Queue<Block>();
     _type = ParsingType.TypeSingle;
     _requests = new List<Requests>();
     _background.DoWork += DownloadInitial;
 }
예제 #10
0
 public Worker(uint start, uint end, string address)
 {
     _end        = end;
     _start      = start;
     _address    = address;
     _threadLock = new object();
     _pages      = new Queue <Block>();
     _type       = ParsingType.TypeMultiple;
     _requests   = new List <Requests>();
     _semaphore  = new Semaphore(1, 1);
 }
        public async Task ConstructParsers(string fileName, ParsingType requesting, ParsingType type, int pages, int textLength)
        {
            var file   = new FileInfo(Path.Combine(TestContext.CurrentContext.TestDirectory, $@".\Data\{fileName}"));
            var parser = instance.ConstructParsers(file);

            Assert.IsNotNull(parser);
            var result = await parser.Parse(new ParsingRequest(file, requesting, 10)).ConfigureAwait(false);

            Assert.AreEqual(type, result.ProcessedAs);
            Assert.AreEqual(pages, result.Document.Pages.Length);
            Assert.AreEqual(textLength, result.Document.Pages[0].Build().Length);
        }
예제 #12
0
 public Worker(List<uint> entries, string address, BackgroundWorker background)
 {
     _entries = entries;
     _address = address;
     _background = background;
     _threadLock = new object();
     _pages = new Queue<Block>();
     _type = ParsingType.TypeList;
     _requests = new List<Requests>();
     _semaphore = new Semaphore(1, 1);
     _background.DoWork += DownloadInitial;
 }
예제 #13
0
 public Worker(int start, int end, string address, BackgroundWorker background)
 {
     _end = end;
     _start = start;
     _address = address;
     _background = background;
     _threadLock = new object();
     _pages = new Queue<Block>();
     _type = ParsingType.TypeMultiple;
     _requests = new List<Requests>();
     _semaphore = new Semaphore(1, 1);
     _background.DoWork += DownloadInitial;
 }
예제 #14
0
        public void Call <T>(Payloader <T> payloader, ParsingType type, object[] arguments) where T : class
        {
            try
            {
                var code = (PayloadCode)arguments[0];

                switch (code)
                {
                case PayloadCode.Error:
                {
                    var error = arguments[1].ToString();
                    payloader.OnError(error);
                }
                break;

                case PayloadCode.Success:
                {
                    var data = default(T);
                    switch (type)
                    {
                    case ParsingType.Json: data = JsonConvert.DeserializeObject <T>(arguments[1].ToString()); break;

                    case ParsingType.Protocol: data = (T)connection.Protocol.ConvertTo(typeof(T), arguments[1]); break;

                    default: data = (T)arguments[1]; break;
                    }
                    //var data = isLocal ? (T)arguments[1] : (T)connection.Protocol.ConvertTo(typeof(T), arguments[1]);
                    payloader.OnSuccess(data);
                    payloader.OnComplete(data);
                }
                break;

                default:
                {
                    payloader.OnFail(code);
                    payloader.OnComplete(null);
                }
                break;
                }
            }
            catch (Exception ex)
            {
                HTTPManager.Logger.Error("BaseSignalR", "GetRealArguments: " + ex);
            }
        }
예제 #15
0
        public Worker(ParsingType type, PageParser parser, EventHandler onPageDownloadingComplete = null)
        {
            _type = type;
            _parser = parser;

            _address = LocaleMgr.GetAddress(parser.Locale);
            ServicePointManager.DefaultConnectionLimit = SemaphoreCount * 10;
            {
                _service = ServicePointManager.FindServicePoint(_address);
                _service.SetTcpKeepAlive(true, KeepAliveTime, KeepAliveTime);
            }
            _address = new Uri(_address, parser.Address);

            _semaphore = new SemaphoreSlim(SemaphoreCount, SemaphoreCount);
            _badIds = new ConcurrentQueue<uint>();

            PageDownloadingComplete += onPageDownloadingComplete;
        }
예제 #16
0
        public Worker(ParsingType type, PageParser parser, EventHandler onPageDownloadingComplete = null)
        {
            _type   = type;
            _parser = parser;

            _address = LocaleMgr.GetAddress(parser.Locale);
            ServicePointManager.DefaultConnectionLimit = SemaphoreCount * 10;
            {
                _service = ServicePointManager.FindServicePoint(_address);
                _service.SetTcpKeepAlive(true, KeepAliveTime, KeepAliveTime);
            }
            _address = new Uri(_address, parser.Address);

            _semaphore = new SemaphoreSlim(SemaphoreCount, SemaphoreCount);
            _badIds    = new ConcurrentQueue <uint>();

            PageDownloadingComplete += onPageDownloadingComplete;
        }
예제 #17
0
        public Worker(ParsingType type, PageParser parser, EventHandler onPageDownloadingComplete = null)
        {
            m_type = type;
            m_parser = parser;

            m_address = new Uri(string.Format("http://{0}.wowhead.com/", parser.Locale.GetLocalePrefix()));
            ServicePointManager.DefaultConnectionLimit = SemaphoreCount * 10;
            {
                m_service = ServicePointManager.FindServicePoint(m_address);
                m_service.SetTcpKeepAlive(true, KeepAliveTime, KeepAliveTime);
            }

            m_semaphore = new SemaphoreSlim(SemaphoreCount, SemaphoreCount);
            m_storeUnprocessedIds = m_type == ParsingType.TypeByList || m_type == ParsingType.TypeByWoWHeadFilter;
            if (m_storeUnprocessedIds)
                m_badIds = new Queue<uint>();

            PageDownloadingComplete += onPageDownloadingComplete;
        }
예제 #18
0
        public Worker(ParsingType type, PageParser parser, EventHandler onPageDownloadingComplete = null)
        {
            m_type   = type;
            m_parser = parser;

            m_address = new Uri(string.Format("http://{0}.wowhead.com/", parser.Locale.GetLocalePrefix()));
            ServicePointManager.DefaultConnectionLimit = SemaphoreCount * 10;
            {
                m_service = ServicePointManager.FindServicePoint(m_address);
                m_service.SetTcpKeepAlive(true, KeepAliveTime, KeepAliveTime);
            }

            m_semaphore           = new SemaphoreSlim(SemaphoreCount, SemaphoreCount);
            m_storeUnprocessedIds = m_type == ParsingType.TypeByList || m_type == ParsingType.TypeByWoWHeadFilter;
            if (m_storeUnprocessedIds)
            {
                m_badIds = new Queue <uint>();
            }

            PageDownloadingComplete += onPageDownloadingComplete;
        }
예제 #19
0
        public static Type ParseCsharpTypeName(string typeName)
        {
            if (!typeName.Contains('<'))
            {
                return(ParseTypeFromAssemblyName(typeName));
            }


            var typeStack = new Stack <ParsingType>();

            typeStack.Push(new ParsingType());
            int previousTypeNameStartIndex = 0;

            bool withinArrayDeclaration = false;

            for (int i = 0; i < typeName.Length; i++)
            {
                switch (typeName[i])
                {
                case '<':
                    typeNameFinished();
                    addGenericParameterToStack();
                    break;

                case '>':
                    typeNameFinished();
                    typeStack.Pop();
                    break;

                case ',':
                    if (withinArrayDeclaration)     // For example: List<int[,,]>
                    {
                        break;
                    }

                    typeNameFinished();
                    typeStack.Pop();

                    addGenericParameterToStack();
                    break;

                case '[':
                    withinArrayDeclaration = true;
                    break;

                case ']':
                    withinArrayDeclaration = false;
                    break;
                }

                void typeNameFinished()
                {
                    var subTypeName =
                        typeName.Substring(previousTypeNameStartIndex, i - previousTypeNameStartIndex)
                        .Trim('<', '>', ',', ' ');

                    if (subTypeName.Length > 0)
                    {
                        typeStack.Peek().TypeFullName = subTypeName;
                    }

                    previousTypeNameStartIndex = i;
                }

                void addGenericParameterToStack()
                {
                    var genericParameter = new ParsingType();

                    typeStack.Peek().GenericParameters.Add(genericParameter);

                    typeStack.Push(genericParameter);
                }
            }

            if (typeStack.Count != 1)
            {
                throw new Exception("Oh no, looks like the type name had some non-matching brackets");
            }

            return(typeStack.Pop().TurnIntoType());
        }
예제 #20
0
        public void Start(ParsingType type)
        {
            if (_isWorking)
                throw new InvalidOperationException("_isWorking");

            _isWorking = true;
            _timeStart = DateTime.Now;

            switch (type)
            {
                case ParsingType.TypeBySingleValue:
                    {
                        _semaphore.Wait();

                        Requests request = new Requests(_address, _start);
                        request.Request.BeginGetResponse(RespCallback, request);
                        break;
                    }
                case ParsingType.TypeByMultipleValue:
                    {
                        for (uint entry = _start; entry <= _end; ++entry)
                        {
                            if (!_isWorking)
                                break;

                            _semaphore.Wait();

                            Requests request = new Requests(_address, entry);
                            request.Request.BeginGetResponse(RespCallback, request);
                        }
                        break;
                    }
                case ParsingType.TypeByList:
                    {
                        for (int i = 0; i < _entries.Count; ++i)
                        {
                            if (!_isWorking)
                                break;

                            _semaphore.Wait();

                            Requests request = new Requests(_address, _entries[i]);
                            request.Request.BeginGetResponse(RespCallback, request);
                        }
                        break;
                    }
                case ParsingType.TypeByWoWHeadFilter:
                    {
                        for (uint entry = 0; entry <= _start; ++entry)
                        {
                            if (!_isWorking)
                                break;

                            _semaphore.Wait();

                            Requests request = new Requests(_address, (entry * 200), ((entry + 1) * 200));
                            request.Request.BeginGetResponse(RespCallback, request);
                        }
                        break;
                    }
            }

            while (_semaphore.CurrentCount != SemaphoreCount)
            {
                Application.DoEvents();
            }

            if (type == ParsingType.TypeByList)
            {
                while (!_badIds.IsEmpty)
                {
                    if (!_isWorking)
                        break;

                    uint id;
                    if (!_badIds.TryDequeue(out id))
                        continue;

                    _semaphore.Wait();

                    Requests request = new Requests(_address, id);
                    request.Request.BeginGetResponse(RespCallback, request);
                }
            }

            while (_semaphore.CurrentCount != SemaphoreCount)
            {
                Application.DoEvents();
            }

            _timeEnd = DateTime.Now;
        }
예제 #21
0
        public void StartButtonClick(object sender, EventArgs e)
        {
            ConstructorInfo cInfo = _parsers[parserBox.SelectedIndex].Value.GetConstructor(new[] { typeof(Locale), typeof(int) });

            if (cInfo == null)
            {
                return;
            }

            int        flags  = GetSubparsers();
            PageParser parser = (PageParser)cInfo.Invoke(new [] { localeBox.SelectedItem, flags });

            if (parser == null)
            {
                throw new InvalidOperationException("parser");
            }

            ParsingType type = (ParsingType)parsingControl.SelectedIndex;

            _worker = new Worker(type, parser, WorkerPageDownloaded);

            switch (type)
            {
            case ParsingType.TypeBySingleValue:
            {
                uint value = (uint)valueBox.Value;
                _worker.SetValue(value);
                break;
            }

            case ParsingType.TypeByList:
            {
                uint[] entries = GetEntriesList();
                numericUpDown.Maximum = progressBar.Maximum = entries.Length;
                _worker.SetValue(entries);
                break;
            }

            case ParsingType.TypeByMultipleValue:
            {
                uint startValue = (uint)rangeStart.Value;
                uint endValue   = (uint)rangeEnd.Value;

                if (startValue > endValue)
                {
                    ShowMessageBox(MessageType.MultipleTypeBigger);
                    return;
                }

                if (startValue == endValue)
                {
                    ShowMessageBox(MessageType.MultipleTypeEqual);
                    return;
                }

                numericUpDown.Maximum = progressBar.Maximum = (int)(endValue - startValue) + 1;
                _worker.SetValue(startValue, endValue);
                break;
            }

            case ParsingType.TypeByWoWHeadFilter:
            {
                int maxValue = (parser.MaxCount / 200);
                numericUpDown.Maximum = progressBar.Maximum = maxValue + 1;
                _worker.SetValue((uint)maxValue);
                break;
            }

            default:
                return;
            }

            abortButton.Enabled       = true;
            subparsersListBox.Enabled = settingsBox.Enabled = startButton.Enabled = false;
            numericUpDown.Value       = progressBar.Value = 0;
            SetLabelText(Resources.Label_Working);

            Requests.Compress = Settings.Default.DataCompression;

            backgroundWorker.RunWorkerAsync();
        }
예제 #22
0
    private void OnInvocation(string method, ParsingType type, object[] arguments)
    {
        switch (method)
        {
        case "Login":
        {
            var payloader = new Payloader <SC_Login>();
            ActionLogin?.Invoke(payloader);
            signalr.Call(payloader, type, arguments);
        }
        break;

        case "EnterMatch":
        {
            var payloader = new Payloader <SC_EnterMatch>();
            ActionEnterMatch?.Invoke(payloader);
            signalr.Call(payloader, type, arguments);
        }
        break;

        case "ExitMatch":
        {
            var payloader = new Payloader <SC_ExitMatch>();
            ActionExitMatch?.Invoke(payloader);
            signalr.Call(payloader, type, arguments);
        }
        break;

        case "SuccessMatch":
        {
            var payloader = new Payloader <SC_SuccessMatch>();
            ActionSuccessMatch?.Invoke(payloader);
            signalr.Call(payloader, type, arguments);
        }
        break;

        case "EnterRoom":
        {
            var payloader = new Payloader <SC_EnterRoom>();
            ActionEnterRoom?.Invoke(payloader);
            signalr.Call(payloader, type, arguments);
        }
        break;

        case "ExitRoom":
        {
            var payloader = new Payloader <SC_ExitRoom>();
            ActionExitRoom?.Invoke(payloader);
            signalr.Call(payloader, type, arguments);
        }
        break;

        case "DeleteRoom":
        {
            var payloader = new Payloader <SC_DeleteRoom>();
            ActionDeleteRoom?.Invoke(payloader);
            signalr.Call(payloader, type, arguments);
        }
        break;

        case "Loading":
        {
            var payloader = new Payloader <SC_Loading>();
            ActionLoading?.Invoke(payloader);
            signalr.Call(payloader, type, arguments);
        }
        break;

        case "CompleteLoading":
        {
            var payloader = new Payloader <SC_CompleteLoading>();
            ActionCompleteLoading?.Invoke(payloader);
            signalr.Call(payloader, type, arguments);
        }
        break;

        case "Play":
        {
            var payloader = new Payloader <SC_Play>();
            ActionPlay?.Invoke(payloader);
            signalr.Call(payloader, type, arguments);
        }
        break;

        case "Wave":
        {
            var payloader = new Payloader <SC_Wave>();
            ActionWave?.Invoke(payloader);
            signalr.Call(payloader, type, arguments);
        }
        break;

        case "Result":
        {
            var payloader = new Payloader <SC_Result>();
            ActionResult?.Invoke(payloader);
            signalr.Call(payloader, type, arguments);
        }
        break;

        case "CreateCube":
        {
            var payloader = new Payloader <SC_CreateCube>();
            ActionCreateCube?.Invoke(payloader);
            signalr.Call(payloader, type, arguments);
        }
        break;

        case "MoveCube":
        {
            var payloader = new Payloader <SC_MoveCube>();
            ActionMoveCube?.Invoke(payloader);
            signalr.Call(payloader, type, arguments);
        }
        break;

        case "CombineCube":
        {
            var payloader = new Payloader <SC_CombineCube>();
            ActionCombineCube?.Invoke(payloader);
            signalr.Call(payloader, type, arguments);
        }
        break;

        case "DeleteCube":
        {
            var payloader = new Payloader <SC_DeleteCube>();
            ActionDeleteCube?.Invoke(payloader);
            signalr.Call(payloader, type, arguments);
        }
        break;

        case "ShotMissile":
        {
            var payloader = new Payloader <SC_ShotMissile>();
            ActionShotMissile?.Invoke(payloader);
            signalr.Call(payloader, type, arguments);
        }
        break;

        case "DieMonster":
        {
            var payloader = new Payloader <SC_DieMonster>();
            ActionDieMonster?.Invoke(payloader);
            signalr.Call(payloader, type, arguments);
        }
        break;

        case "EscapeMonster":
        {
            var payloader = new Payloader <SC_EscapeMonster>();
            ActionEscapeMonster?.Invoke(payloader);
            signalr.Call(payloader, type, arguments);
        }
        break;

        case "UpdateSlot":
        {
            var payloader = new Payloader <SC_UpdateSlot>();
            ActionUpdateSlot?.Invoke(payloader);
            signalr.Call(payloader, type, arguments);
        }
        break;
        }
    }
예제 #23
0
        public void StartButtonClick(object sender, EventArgs e)
        {
            ParserData.Parser data = m_data.Data[parserBox.SelectedIndex];

            PageParser parser = m_parsers[data.ParserType];

            parser.Parser = data;
            parser.Locale = (Locale)localeBox.SelectedItem;
            parser.Flags  = GetSubparsers();

            ParsingType type = (ParsingType)parsingControl.SelectedIndex;

            m_worker = new Worker(type, parser, WorkerPageDownloaded);

            Worker.ParserValue value = default(Worker.ParserValue);
            switch (type)
            {
            case ParsingType.TypeBySingleValue:
            {
                value.Id = (uint)valueBox.Value;
                break;
            }

            case ParsingType.TypeByList:
            {
                value.Array           = GetEntriesList().Data;
                numericUpDown.Maximum = progressBar.Maximum = value.Array.Length;
                break;
            }

            case ParsingType.TypeByMultipleValue:
            {
                value.Start = (uint)rangeStart.Value;
                value.End   = (uint)rangeEnd.Value;

                if (value.Start > value.End)
                {
                    ShowMessageBox(MessageType.MultipleTypeBigger);
                    return;
                }

                if (value.Start == value.End)
                {
                    ShowMessageBox(MessageType.MultipleTypeEqual);
                    return;
                }

                numericUpDown.Maximum = progressBar.Maximum = (int)(value.End - value.Start) + 1;
                break;
            }

            case ParsingType.TypeByWoWHeadFilter:
            {
                value.Maximum         = (data.CountLimit / MaxIdCountPerRequest);
                numericUpDown.Maximum = progressBar.Maximum = (int)value.Maximum + 1;
                break;
            }

            default:
                return;
            }

            m_worker.SetValue(value);

            abortButton.Enabled       = true;
            subparsersListBox.Enabled = settingsBox.Enabled = startButton.Enabled = false;
            numericUpDown.Value       = progressBar.Value = 0;
            SetLabelText(Resources.Label_Working);

            Requests.Compress = Settings.Default.DataCompression;

            backgroundWorker.RunWorkerAsync();
        }
예제 #24
0
        public static PDFStyleSelector ParseSingleSelector(string selector)
        {
            string appliedType = null;
            PDFStyleClassSelector appliedClass = null;
            string appliedId = null;

            ParsingType pt = ParsingType.Type; // no selector

            StringBuilder sb = new StringBuilder();

            foreach (var c in selector)
            {
                if (c == '#')
                {
                    if (sb.Length > 0)
                    {
                        switch (pt)
                        {
                        case ParsingType.Type:
                            appliedType = sb.ToString();
                            break;

                        case ParsingType.Class:
                            appliedClass = new PDFStyleClassSelector(sb.ToString(), appliedClass);
                            break;

                        case ParsingType.Id:
                            appliedId = sb.ToString();
                            break;

                        default:

                            break;
                        }
                        sb.Clear();
                    }
                    pt = ParsingType.Id;
                }
                else if (c == '.')
                {
                    if (sb.Length > 0)
                    {
                        switch (pt)
                        {
                        case ParsingType.Type:
                            appliedType = sb.ToString();
                            break;

                        case ParsingType.Class:
                            appliedClass = new PDFStyleClassSelector(sb.ToString(), appliedClass);
                            break;

                        case ParsingType.Id:
                            appliedId = sb.ToString();
                            break;

                        default:

                            break;
                        }
                        sb.Clear();
                    }
                    pt = ParsingType.Class;
                }
                else
                {
                    sb.Append(c);
                }
            }
            if (sb.Length > 0)
            {
                switch (pt)
                {
                case ParsingType.Type:
                    appliedType = sb.ToString();
                    break;

                case ParsingType.Class:
                    appliedClass = new PDFStyleClassSelector(sb.ToString(), appliedClass);;
                    break;

                case ParsingType.Id:
                    appliedId = sb.ToString();
                    break;

                default:

                    break;
                }
                sb.Clear();
            }
            return(new PDFStyleSelector()
            {
                AppliedClass = appliedClass, AppliedID = appliedId, AppliedElement = appliedType
            });
        }
예제 #25
0
    private static ParsedJSONValue decodeObject(string input, int flags = 0)
    {
        // Decodes a string object into a ParsedJSONValue containing an array, a string, a number, of a literal
        int  i;
        char c;

        if ((flags & FLAG_REMOVE_COMMENTS) == FLAG_REMOVE_COMMENTS)
        {
            // Remove the comment and reset the flag
            flags -= FLAG_REMOVE_COMMENTS;
            input  = Regex.Replace(input, "/\\*[\\w\\W]*?\\*/", "");
        }

        ParsedJSONValue returnObject = new ParsedJSONValue();

        ParsingType parsingType         = ParsingType.Unknown;
        object      parsingObject       = null;
        string      parsingObjectString = "";
        string      parsingName         = "";

        ParsedJSONValue parsedObject;

        i = 0;

        // Debug.Log("Decoding Object ------ [" + input + "]");

        bool mustEnd = false;

        while (i < input.Length && !mustEnd)
        {
            c = input[i];

            switch (parsingType)
            {
            case ParsingType.Unknown:
                //Debug.Log("STATE: UNKNOWN");

                parsingObject       = null;
                parsingObjectString = "";

                if (c == STRUCTURE_BEGIN_OBJECT)
                {
                    // Starting object
                    //Debug.Log("-> starting object @ " + i);
                    parsingType   = ParsingType.ValueObjectPreItem;
                    parsingObject = new Dictionary <string, object>();
                }
                else if (c == STRUCTURE_BEGIN_ARRAY)
                {
                    // Starting array
                    //Debug.Log("-> starting array @ " + i);
                    parsingType   = ParsingType.ValueArrayPreItem;
                    parsingObject = new List <object>();
                }
                else if (c == STRUCTURE_STRING_DELIMITER)
                {
                    // Starting string
                    //Debug.Log("-> starting string @ " + i);
                    parsingType         = ParsingType.ValueString;
                    parsingObjectString = "";
                }
                else if (CHARS_NUMBER.Contains(c))
                {
                    // Starting number
                    //Debug.Log("-> starting number @ " + i);
                    parsingType         = ParsingType.ValueNumber;
                    parsingObjectString = c.ToString();
                }
                else if (c == VALUE_LITERAL_NULL[0] && compareStringValue(VALUE_LITERAL_NULL, input, i))
                {
                    // Starting "null"
                    //Debug.Log("-> starting null @ " + i);
                    mustEnd             = true;
                    returnObject.value  = null;
                    returnObject.length = i + VALUE_LITERAL_NULL.Length;
                }
                else if (c == VALUE_LITERAL_TRUE[0] && compareStringValue(VALUE_LITERAL_TRUE, input, i))
                {
                    // Starting "true"
                    //Debug.Log("-> starting boolean true @ " + i);
                    mustEnd             = true;
                    returnObject.value  = true;
                    returnObject.length = i + VALUE_LITERAL_TRUE.Length;
                }
                else if (c == VALUE_LITERAL_FALSE[0] && compareStringValue(VALUE_LITERAL_FALSE, input, i))
                {
                    // Starting "false"
                    //Debug.Log("-> starting boolean false @ " + i);
                    mustEnd             = true;
                    returnObject.value  = false;
                    returnObject.length = i + VALUE_LITERAL_FALSE.Length;
                }
                break;

            case ParsingType.ValueString:
                //Debug.Log("STATE: VALUE STRING");
                if (c == STRUCTURE_STRING_DELIMITER)
                {
                    // Ended string
                    //Debug.Log("-> ending string [" + parsingObjectString + "] @ " + i);
                    mustEnd             = true;
                    returnObject.value  = parsingObjectString;
                    returnObject.length = i + 1;
                }
                else if (compareStringValue(STRING_ESCAPE_UNICODE, input, i))
                {
                    //Debug.Log("-> continuing string with unicode char @ " + i);
                    // Unicode encoded character
                    i++;
                    if (i < input.Length - 5)
                    {
                        parsingObjectString += Char.ConvertFromUtf32(Convert.ToInt32(input.Substring(i + 1, 4), 16));
                        i += 4;
                    }
                }
                else if (c == CHAR_ESCAPE)
                {
                    // Some special character
                    //Debug.Log("-> continuing string @ " + i);
                    if (i < input.Length - 1)
                    {
                        string nc      = c.ToString() + input[i + 1];
                        int    charPos = CHARS_STRING_ESCAPED.IndexOf(nc);
                        if (charPos >= 0)
                        {
                            parsingObjectString += CHARS_STRING_NEED_ESCAPE[charPos];
                            i++;
                        }
                        else
                        {
                            //Debug.LogError("Error! Escape string without equivalent char!");
                        }
                    }
                }
                else
                {
                    // Continued string
                    parsingObjectString += c;
                }
                break;

            case ParsingType.ValueNumber:
                //Debug.Log("STATE: VALUE NUMBER");
                if (CHARS_NUMBER.Contains(c))
                {
                    // Continued number
                    //Debug.Log("-> continuing number @ " + i);
                    parsingObjectString += c;
                    break;
                }
                else
                {
                    // Ended number
                    //Debug.Log("-> ending number [" + parsingObjectString + "] @ " + i);
                    mustEnd             = true;
                    returnObject.value  = Convert.ToDouble(parsingObjectString);
                    returnObject.length = i;
                }
                break;

            case ParsingType.ValueObjectPreItem:
                //Debug.Log("STATE: VALUE OBJECT PRE ITEM");
                if (c == STRUCTURE_END_OBJECT)
                {
                    // Empty object?
                    parsingType = ParsingType.ValueObjectPostItem;
                    i--;
                }
                else if (c == STRUCTURE_STRING_DELIMITER)
                {
                    // Starting a key name
                    //Debug.Log("-> starting a key name");
                    parsingName = "";
                    parsingType = ParsingType.KeyName;
                }
                break;

            case ParsingType.ValueObjectPostItem:
                //Debug.Log("STATE: VALUE OBJECT POST ITEM");
                if (c == STRUCTURE_VALUE_SEPARATOR)
                {
                    // Starting a new item
                    //Debug.Log("--> --> new object item");
                    parsingType = ParsingType.ValueObjectPreItem;
                }
                else if (c == STRUCTURE_END_OBJECT)
                {
                    // Ending object
                    //Debug.Log("--> --> ending object of len " + (i+1));
                    mustEnd             = true;
                    returnObject.value  = parsingObject;
                    returnObject.length = i + 1;
                }
                break;

            case ParsingType.ValueArrayPreItem:
                //Debug.Log("STATE: VALUE ARRAY PRE ITEM");
                if (c == STRUCTURE_END_ARRAY)
                {
                    // Empty array?
                    //Debug.Log("-> ending empty array @ " + i);
                    parsingType = ParsingType.ValueArrayPostItem;
                    i--;
                }
                else if (CHARS_WHITESPACE.Contains(c))
                {
                    // Whitespace elements
                }
                else
                {
                    //Debug.Log("-> getting array item @ " + i);

                    // Value that must be added to this object
                    // Everything that comes after is a new value that must be added to this object
                    parsedObject = decodeObject(input.Substring(i), flags);

                    i += parsedObject.length - 1;
                    (parsingObject as List <object>).Add(parsedObject.value);

                    parsingType = ParsingType.ValueArrayPostItem;
                }
                break;

            case ParsingType.ValueArrayPostItem:
                //Debug.Log("STATE: VALUE ARRAY POST ITEM");
                if (c == STRUCTURE_VALUE_SEPARATOR)
                {
                    // Starting a new object
                    //Debug.Log("-> starting array @ " + i);
                    parsingType = ParsingType.ValueArrayPreItem;
                }
                else if (c == STRUCTURE_END_ARRAY)
                {
                    // Ending array
                    //Debug.Log("-> ending array @ " + i);
                    mustEnd = true;

                    returnObject.value  = parsingObject;
                    returnObject.length = i + 1;
                }
                break;

            case ParsingType.KeyName:
                //Debug.Log("STATE: KEY NAME");
                if (c == STRUCTURE_STRING_DELIMITER)
                {
                    // Ending a key name
                    //Debug.Log("-> ending key name [" + parsingName + "] @ " + i);
                    parsingType = ParsingType.PostKeyName;
                }
                else
                {
                    // Continuing the key name
                    //Debug.Log("-> continuing key name @ " + i + " = [" + parsingName + "]");
                    parsingName += c;
                }
                break;

            case ParsingType.PostKeyName:
                //Debug.Log("STATE: POST KEY NAME");
                if (c == STRUCTURE_NAME_SEPARATOR)
                {
                    //Debug.Log("-> found key name separator (:) @ " + i);

                    // Find value that must be added to this object
                    parsedObject = decodeObject(input.Substring(i + 1), flags);

                    i += parsedObject.length;
                    ((Dictionary <string, object>)parsingObject)[parsingName] = parsedObject.value;

                    //Debug.Log("-> object created has length " + parsedObject.length);

                    parsingType = ParsingType.ValueObjectPostItem;
                    parsingName = null;
                }
                break;
            }

            i++;
        }

        //Debug.Log("Returning object ------ [" + returnObject.value + "] with length [" + returnObject.length + "]");

        return(returnObject);
    }
예제 #26
0
        public static PDFStyleSelector ParseSingleSelector(string selector, StringBuilder buffer)
        {
            string appliedType = null;
            PDFStyleClassSelector appliedClass = null;
            string appliedId = null;

            int         stateIndex        = -1;
            ParsingType statePreviousType = ParsingType.Type;

            ComponentState appliedState = ComponentState.Normal;

            ParsingType pt = ParsingType.Type; // no selector

            StringBuilder sb = buffer;

            sb.Clear();

            for (var currIndex = 0; currIndex < selector.Length; currIndex++)
            {
                char c = selector[currIndex];

                if (c == '#')
                {
                    if (sb.Length > 0)
                    {
                        switch (pt)
                        {
                        case ParsingType.Type:
                            appliedType = sb.ToString();
                            break;

                        case ParsingType.Class:
                            appliedClass = new PDFStyleClassSelector(sb.ToString(), appliedClass);
                            break;

                        case ParsingType.Id:
                            appliedId = sb.ToString();
                            break;

                        default:

                            break;
                        }
                        sb.Clear();
                    }
                    pt = ParsingType.Id;
                }
                else if (c == '.')
                {
                    if (sb.Length > 0)
                    {
                        switch (pt)
                        {
                        case ParsingType.Type:
                            appliedType = sb.ToString();
                            break;

                        case ParsingType.Class:
                            appliedClass = new PDFStyleClassSelector(sb.ToString(), appliedClass);
                            break;

                        case ParsingType.Id:
                            appliedId = sb.ToString();
                            break;

                        default:

                            break;
                        }
                        sb.Clear();
                    }
                    pt = ParsingType.Class;
                }
                else
                {
                    if (c == ':')
                    {
                        stateIndex        = currIndex;
                        statePreviousType = pt;
                    }
                    sb.Append(c);
                }
            }
            //At the end of the string
            if (sb.Length > 0)
            {
                switch (pt)
                {
                case ParsingType.Type:
                    appliedType = sb.ToString();
                    break;

                case ParsingType.Class:
                    appliedClass = new PDFStyleClassSelector(sb.ToString(), appliedClass);;
                    break;

                case ParsingType.Id:
                    appliedId = sb.ToString();
                    break;

                default:

                    break;
                }
                sb.Clear();
            }

            if (stateIndex >= 0)
            {
                //TODO: Check for state
                if (stateIndex == 0 && appliedType == ":root")
                {
                }
            }

            return(new PDFStyleSelector()
            {
                AppliedClass = appliedClass, AppliedID = appliedId, AppliedElement = appliedType, AppliedState = appliedState
            });
        }