Esempio n. 1
0
        /*******************************************************************************
        *  /// Methods
        *******************************************************************************/
        public override void CreateStructure()
        {
            if (_parent == null)
            {
                throw new System.ArgumentException("No Parent");
            }
            foreach (var child in _childIds)
            {
                var b = _parent.GetChildById(child);
                _childMap[child] = b;
                _children.Add(b);
            }

            if (_entityType == EntityType.KEY)
            {
                if (_valueIds.Count >= 1)
                {
                    var child = _parent.GetChildById(_valueIds[0]);
                    if (child.GetBlockType() != BlockType.KEY_VALUE_SET)
                    {
                        throw new System.ArgumentException("Value of Key is not valid type");
                    }
                    _value = (KeyValueSet)child;
                }
            }
        }
        public void ParseJson(JToken token)
        {
            // Start reading
            JToken blocks;

            try
            {
                Console.WriteLine("Starting Try Block");
                if (token["AnalyzeDocumentModelVersion"] != null)
                {
                    this.JobStatus = token["AnalyzeDocumentModelVersion"].ToString() == "1.0" ? "SUCCEEDED" : "FAILED";
                }
                else if (token["JobStatus"] == null)
                {
                    this.JobStatus = token["JobStatus"].ToString();
                }
                if (this.JobStatus != "SUCCEEDED")
                {
                    Console.WriteLine("Textract Failed");
                    return;
                }
                else
                {
                    Console.WriteLine("Textract Succeeded");
                }

                this.DocumentMetadata = new DocumentMetadata
                {
                    Pages = token["DocumentMetadata"]["Pages"].ToObject <int>()
                };

                blocks = token["Blocks"];
            }
            catch (Exception e)
            {
                Console.WriteLine(String.Format("Error: Invalid Textract JSON. {0}", e.Message));
                throw;
            }

            Page currentPage = null;

            foreach (var b in blocks.Children())
            {
                Block block = null;
                switch (b["BlockType"]["Value"].ToString())
                {
                case "PAGE":
                    block = new Page(b);
                    break;

                case "LINE":
                    block = new Line(b);
                    break;

                case "WORD":
                    block = new Word(b);
                    break;

                case "TABLE":
                    block = new Table(b);
                    break;

                case "CELL":
                    block = new Cell(b);
                    break;

                case "KEY_VALUE_SET":
                    block = new KeyValueSet(b);
                    break;

                case "SELECTION_ELEMENT":
                    block = new SelectionElement(b);
                    break;

                default:
                    Console.WriteLine(String.Format("Child Token: {0}", b.ToString()));
                    throw new System.ArgumentException(String.Format("Unknown block type: {0}", b["BlockType"].ToString()));
                }

                switch (block.GetBlockType())
                {
                case Appserver.TextractDocument.BlockType.PAGE:
                    currentPage = (Page)block;
                    Pages.Add(currentPage);
                    break;

                default:
                    currentPage.addBlock(block);
                    break;
                }

                _blockMap.Add(block.GetId(), block);
            }
        }