/// <summary>
        /// 处理结束标签
        /// </summary>
        /// <param name="endTag">结束标签信息</param>
        /// <returns>相关的元素对象,若不支持则返回null</returns>
        protected virtual IHtmlElement ProcessEndTag(HtmlEndTag endTag)
        {
            var tagName = endTag.TagName;


            if (ContainerStack.OfType <IHtmlElement>().Select(e => e.Name).Contains(tagName, StringComparer.OrdinalIgnoreCase))
            {
                IHtmlElement element;

                while (true)
                {
                    element = ContainerStack.Pop() as IHtmlElement;
                    if (element.Name.EqualsIgnoreCase(tagName))
                    {
                        break;
                    }
                }

                return(element);
            }
            else
            {
                ProcessEndTagMissingBeginTag(endTag);

                return(null);
            }

            //无需退出CData标签,读取器会自动退出
        }
Exemple #2
0
        /// <summary>
        /// 分析 HTML 文本并创建文档
        /// </summary>
        /// <param name="html">HTML 文本</param>
        /// <param name="url">文档的 URL</param>
        /// <returns>分析好的 HTML 文档</returns>
        public virtual IHtmlDocument Parse(string html, Uri url)
        {
            if (html == null)
            {
                throw new ArgumentNullException("html");
            }

            if (url != null && !url.IsAbsoluteUri)
            {
                throw new ArgumentException("必须是绝对URI", "url");
            }

            lock ( SyncRoot )
            {
                Initialize();

                Document = DomProvider.CreateDocument(url);

                if (!string.IsNullOrEmpty(html))
                {
                    ContainerStack.Push(Document);

                    ParseInternal(html);
                }

                if (Document.HtmlSpecification == null)//若始终没能找到 DTD 声明,则设置默认的规范
                {
                    DomProvider.SetHtmlSpecification(Document, null);
                }

                return(CompleteDocument(Document));
            }
        }
        /// <summary>
        /// 分析 HTML 文本并创建文档
        /// </summary>
        /// <param name="html">HTML 文本</param>
        /// <param name="url">文档的 URL</param>
        /// <returns>分析好的 HTML 文档</returns>
        public virtual IHtmlDocument Parse(string html, Uri url)
        {
            if (html == null)
            {
                throw new ArgumentNullException("html");
            }

            if (url != null && !url.IsAbsoluteUri)
            {
                throw new ArgumentException("必须是绝对URI", "url");
            }

            lock ( SyncRoot )
            {
                InitializeStack();

                var document = Provider.CreateDocument(url);

                if (string.IsNullOrEmpty(html))
                {
                    return(document);
                }

                ContainerStack.Push(document);

                ParseInternal(html);

                return(document);
            }
        }
        public void GetHeight_TestStack_5()
        {
            ContainerStack TestStack = new ContainerStack(TestContainers);

            int result = TestStack.GetHeight();

            Assert.AreEqual(5, result);
        }
Exemple #5
0
 protected RawTextReader(TextStream input, IonType parent = IonType.Datagram)
 {
     _state          = GetStateAtContainerStart(parent);
     _valueBuffer    = new StringBuilder();
     _scanner        = new RawTextScanner(input);
     _eof            = false;
     _hasNextCalled  = false;
     _containerStack = new ContainerStack(this, 6);
     _containerStack.PushContainer(IonType.Datagram);
 }
Exemple #6
0
        public void CanContainerBePlacedValuableOnTopTest()
        {
            ContainerStack stack             = new ContainerStack(5, 0, 0);
            Container      valuableContainer = new Container(ContainerType.Valuable, 0);
            Container      normalContainer   = new Container(ContainerType.Normal, 0);

            stack.AddContainer(valuableContainer);
            bool actual = stack.CanContainerBePlaced(normalContainer);

            Assert.AreEqual(false, actual);
        }
Exemple #7
0
        private bool containerProhibitsCommas; // frequently during state transitions actions

        protected RawTextReader(TextStream input)
        {
            this.state          = GetStateAtContainerStart(IonType.Datagram);
            this.valueBuffer    = new StringBuilder();
            this.scanner        = new TextScanner(input);
            this.eof            = false;
            this.valueType      = IonType.None;
            this.hasNextCalled  = false;
            this.containerStack = new ContainerStack(this, 6);
            this.containerStack.PushContainer(IonType.Datagram);
        }
        public void GetWeight_TestStack_26000()
        {
            //arrange
            ContainerStack TestStack = new ContainerStack(TestContainers);

            //act
            int result = TestStack.GetTotalWeight();

            //assert
            Assert.AreEqual(26000, result);
        }
Exemple #9
0
        public void CanContainerBePlacedOverHeightLimitTest()
        {
            ContainerStack stack      = new ContainerStack(1, 0, 0);
            Container      container1 = new Container(ContainerType.Normal, 0);
            Container      container2 = new Container(ContainerType.Normal, 0);

            stack.AddContainer(container1);
            bool actual = stack.CanContainerBePlaced(container2);

            Assert.AreEqual(false, actual);
        }
        public MinecraftBedrockStack(IDockerService dockerService, ContainerStack stack)
            : base(dockerService, stack)
        {
            _config = stack.GetConfig <MinecraftBedrockStackConfig>();

            if (!stack.IsForStack(this))
            {
                throw new Exception("Stack mismatch!");
            }

            AddImage(ImageName);
        }
Exemple #11
0
        internal RawBinaryWriter(IWriterBuffer lengthBuffer, IWriterBuffer dataBuffer, List <Memory <byte> > lengthSegments)
        {
            _lengthBuffer   = lengthBuffer;
            _dataBuffer     = dataBuffer;
            _lengthSegments = lengthSegments;
            _containerStack = new ContainerStack(DefaultContainerStackSize);

            //top-level writing also requires a tracker
            var pushedContainer = _containerStack.PushContainer(ContainerType.Datagram);

            _dataBuffer.StartStreak(pushedContainer.Sequence);
        }
        internal RawBinaryWriter(IWriterBuffer lengthBuffer, IWriterBuffer dataBuffer, List <Memory <byte> > lengthSegments, bool forceFloat64)
        {
            this.lengthBuffer   = lengthBuffer;
            this.dataBuffer     = dataBuffer;
            this.lengthSegments = lengthSegments;
            this.containerStack = new ContainerStack(DefaultContainerStackSize);
            this.forceFloat64   = forceFloat64;

            // Top-level writing also requires a tracker
            var pushedContainer = this.containerStack.PushContainer(ContainerType.Datagram);

            this.dataBuffer.StartStreak(pushedContainer.Sequence);
        }
        public void AddContainerToList_OneValuableOneNormal_ShouldReturnTrue()
        {
            // Arrange
            bool           expected       = true;
            ContainerStack containerStack = new ContainerStack();

            containerStack.AddContainerToList(new ContainerCoolableValuable(4000));

            // Act
            bool actual = containerStack.AddContainerToList(new ContainerCoolable(4000));

            // Assert
            Assert.AreEqual(expected, actual);
        }
        public void AddContainerToList_TooHeavy_ShouldReturnFalse()
        {
            // Arrange
            bool           expected       = false;
            ContainerStack containerStack = new ContainerStack();

            containerStack.AddContainerToList(new ContainerCoolableValuable(120001));

            // Act
            bool actual = containerStack.AddContainerToList(new ContainerCoolable(4000));

            // Assert
            Assert.AreEqual(expected, actual);
        }
Exemple #15
0
        public MemoryDb()
        {
            var stackRecord = new ContainerStack
            {
                StackType = typeof(MinecraftBedrockStack)
            };

            stackRecord.SetConfig(new MinecraftBedrockStackConfig());

            Stacks.SaveAsync(stackRecord).Wait();

            Containers.SaveAsync(new Container
            {
                StackId   = stackRecord.Id,
                ImageName = "itzg/minecraft-bedrock-server"
            });
        }
Exemple #16
0
            /// <summary>
            /// 解析 HTML 文本到指定的文档碎片对象
            /// </summary>
            /// <param name="html"></param>
            /// <param name="fragment"></param>
            public virtual void ProcessFragment(string html, DomFragment fragment)
            {
                if (string.IsNullOrEmpty(html))
                {
                    return;
                }

                lock ( SyncRoot )
                {
                    InitializeStack();

                    ContainerStack.Push(fragment);

                    ParseInternal(html);

                    fragment.ContentFragment = new HtmlContentFragment(Reader, 0, Reader.HtmlText.Length);
                }
            }
Exemple #17
0
        /// <summary>
        /// 解析 HTML 文本到指定的文档碎片对象
        /// </summary>
        /// <param name="html">要解析的 HTML 文本</param>
        /// <param name="fragment">要处理的文本碎片</param>
        public virtual void ParseToFragment(string html, DomFragment fragment)
        {
            if (string.IsNullOrEmpty(html))
            {
                return;
            }

            lock ( SyncRoot )
            {
                Initialize();

                SetHtmlSpecification(fragment.Document.HtmlSpecification);

                ContainerStack.Push(fragment);

                ParseInternal(html);

                fragment.ContentFragment = new HtmlContentFragment(Reader, 0, Reader.HtmlText.Length);
            }
        }
 public void Initialize()
 {
     ship = new Ship();
     balanceAlgorithme = new BalanceAlgorithme();
     stack             = new ContainerStack(0);
 }
        /// <summary>
        /// 处理元素开始标签
        /// </summary>
        /// <param name="beginTag">开始标签信息</param>
        /// <returns>处理过程中所创建的元素对象,若不支持则返回 null</returns>
        protected virtual IHtmlElement ProcessBeginTag(HtmlBeginTag beginTag)
        {
            string tagName    = beginTag.TagName;
            bool   selfClosed = beginTag.SelfClosed;

            //检查是否为自结束标签,并作相应处理
            if (IsSelfCloseElement(beginTag))
            {
                selfClosed = true;
            }


            //检查是否为CData标签,并作相应处理
            if (IsCDataElement(beginTag))
            {
                Reader.EnterCDataMode(tagName.ToLowerInvariant());
            }



            //检查父级是否可选结束标记,并作相应处理
            {
                var element = CurrentContainer as IHtmlElement;
                if (element != null && HtmlSpecification.optionalCloseTags.Contains(element.Name, StringComparer.OrdinalIgnoreCase))
                {
                    if (ImmediatelyClose(tagName, element))
                    {
                        ContainerStack.Pop();
                    }
                }
            }



            //处理所有属性
            var attributes = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            foreach (var a in beginTag.Attributes)
            {
                string name  = a.Name;
                string value = a.Value;

                if (value != null)
                {
                    value = HtmlEncoding.HtmlDecode(value);
                }

                if (attributes.ContainsKey(name))//重复的属性名,只取第一个
                {
                    continue;
                }

                attributes.Add(name, value);
            }



            //创建元素
            {
                var element = CreateElement(tagName, attributes);


                //加入容器堆栈
                if (!selfClosed)
                {
                    ContainerStack.Push(element);
                }


                return(element);
            }
        }
Exemple #20
0
 public AbstractStack(IDockerService dockerService, ContainerStack stack)
 {
     _dockerService = dockerService;
     _stack         = stack;
 }
Exemple #21
0
 public MinecraftStack(IDockerService dockerService, ContainerStack stack)
     : base(dockerService, stack)
 {
     AddImage(ImageName);
 }
Exemple #22
0
        // Assumptions: Containers in the front and back can still be reached from one side
        public void Sort(List <Container> containers)
        {
            int widthToCheckLeft       = 0;
            int widthToCheckFrontLeft  = 0;
            int widthToCheckBackLeft   = 0;
            int widthToCheckRight      = 0;
            int widthToCheckFrontRight = 0;
            int widthToCheckBackRight  = 0;

            for (int i = 0; i < containers.Count; i++)
            {
                // Switch case depending on where the container has to be placed
                switch (CheckPlacement(containers[i], Weight.CurrentWeight,
                                       Weight.WeightLeft))
                {
                case ShipPosition.Left:
                    // If there's no definition for dictionary at specific key (or width)
                    if (!ContainerStack.CheckIfStackExists(ContainersLeft, widthToCheckLeft))
                    {
                        // Create new dictionary and add container immediately
                        ContainersLeft.Add(widthToCheckLeft, new ContainerStack());
                        Crane.AddContainerLeft(this, containers[i], widthToCheckLeft);
                        break;
                    }
                    // If there's still space left
                    if (ContainersLeft[widthToCheckLeft].CanStack)
                    {
                        // If adding the container wouldn't go over the limit
                        if (ContainersLeft[widthToCheckLeft]
                            .CheckIfContainerGoesOnTop(ContainersLeft[widthToCheckLeft].StackedContainers,
                                                       containers[i].Weight))
                        {
                            // Then add the container
                            Crane.AddContainerLeft(this, containers[i], widthToCheckLeft);
                        }
                        else
                        {
                            // If the width is at maximum value
                            if (widthToCheckLeft == Width)
                            {
                                // Cannot stack on middle left anymore
                                ContainersLeft[widthToCheckLeft].CanStack = false;
                            }
                            else
                            {
                                // Otherwise increase width to check
                                widthToCheckLeft++;
                            }
                        }
                    }
                    break;

                // The following cases repeat the same pattern
                case ShipPosition.Right:
                    if (!ContainerStack.CheckIfStackExists(ContainersRight, widthToCheckRight))
                    {
                        ContainersRight.Add(widthToCheckRight, new ContainerStack());
                        Crane.AddContainerRight(this, containers[i], widthToCheckRight);
                        break;
                    }
                    if (ContainersRight[widthToCheckRight].CanStack)
                    {
                        if (ContainersRight[widthToCheckRight]
                            .CheckIfContainerGoesOnTop(ContainersRight[widthToCheckRight].StackedContainers,
                                                       containers[i].Weight))
                        {
                            Crane.AddContainerRight(this, containers[i], widthToCheckRight);
                        }
                        else
                        {
                            if (widthToCheckRight == Width)
                            {
                                ContainersRight[widthToCheckRight].CanStack = false;
                            }
                            else
                            {
                                widthToCheckRight++;
                            }
                        }
                    }
                    break;

                case ShipPosition.FrontRight:
                    if (!ContainerStack.CheckIfStackExists(ContainersInFrontRight, widthToCheckFrontRight))
                    {
                        ContainersInFrontRight.Add(widthToCheckFrontRight, new ContainerStack());
                        Crane.AddContainerToFrontRight(this, containers[i], widthToCheckLeft);
                        break;
                    }
                    if (ContainersInFrontRight[widthToCheckFrontRight].CanStack)
                    {
                        if (ContainersInFrontRight[widthToCheckFrontRight]
                            .CheckIfContainerGoesOnTop(ContainersInFrontRight[widthToCheckFrontRight].StackedContainers,
                                                       containers[i].Weight))
                        {
                            Crane.AddContainerToFrontRight(this, containers[i], widthToCheckFrontRight);
                        }
                        else
                        {
                            if (widthToCheckFrontRight == Width)
                            {
                                ContainersLeft[widthToCheckFrontRight].CanStack = false;
                            }
                            else
                            {
                                widthToCheckFrontRight++;
                            }
                        }
                    }
                    break;

                case ShipPosition.FrontLeft:
                    if (!ContainerStack.CheckIfStackExists(ContainersInFrontLeft, widthToCheckFrontLeft))
                    {
                        ContainersInFrontLeft.Add(widthToCheckFrontLeft, new ContainerStack());
                        Crane.AddContainerToFrontLeft(this, containers[i], widthToCheckFrontLeft);
                        break;
                    }
                    if (ContainersInFrontLeft[widthToCheckFrontLeft].CanStack)
                    {
                        if (ContainersInFrontLeft[widthToCheckFrontLeft]
                            .CheckIfContainerGoesOnTop(ContainersInFrontLeft[widthToCheckFrontLeft].StackedContainers,
                                                       containers[i].Weight))
                        {
                            Crane.AddContainerToFrontLeft(this, containers[i], widthToCheckFrontLeft);
                        }
                        else
                        {
                            if (widthToCheckFrontLeft == Width)
                            {
                                ContainersInFrontLeft[widthToCheckFrontLeft].CanStack = false;
                            }
                            else
                            {
                                widthToCheckFrontLeft++;
                            }
                        }
                    }
                    break;

                case ShipPosition.BackRight:
                    if (!ContainerStack.CheckIfStackExists(ContainersInBackRight, widthToCheckBackRight))
                    {
                        ContainersInBackRight.Add(widthToCheckLeft, new ContainerStack());
                        Crane.AddContainerToBackRight(this, containers[i], widthToCheckLeft);
                        break;
                    }
                    if (ContainersInBackRight[widthToCheckBackRight].CanStack)
                    {
                        if (ContainersInBackRight[widthToCheckBackRight]
                            .CheckIfContainerGoesOnTop(ContainersInBackRight[widthToCheckBackRight].StackedContainers,
                                                       containers[i].Weight))
                        {
                            Crane.AddContainerToBackRight(this, containers[i], widthToCheckBackRight);
                        }
                        else
                        {
                            if (widthToCheckBackRight == Width)
                            {
                                ContainersInBackRight[widthToCheckBackRight].CanStack = false;
                            }
                            else
                            {
                                widthToCheckBackRight++;
                            }
                        }
                    }
                    break;

                case ShipPosition.BackLeft:
                    if (!ContainerStack.CheckIfStackExists(ContainersInBackLeft, widthToCheckBackLeft))
                    {
                        ContainersLeft.Add(widthToCheckBackLeft, new ContainerStack());
                        Crane.AddContainerToBackLeft(this, containers[i], widthToCheckBackLeft);
                        break;
                    }
                    if (ContainersInBackLeft[widthToCheckFrontRight].CanStack)
                    {
                        if (ContainersLeft[widthToCheckBackLeft]
                            .CheckIfContainerGoesOnTop(ContainersLeft[widthToCheckBackLeft].StackedContainers,
                                                       containers[i].Weight))
                        {
                            Crane.AddContainerToBackLeft(this, containers[i], widthToCheckBackLeft);
                        }
                        else
                        {
                            if (widthToCheckBackLeft == Width)
                            {
                                ContainersInBackLeft[widthToCheckBackLeft].CanStack = false;
                            }
                            else
                            {
                                widthToCheckBackLeft++;
                            }
                        }
                    }
                    break;
                }
            }
        }