From a1a1e6fac9a47ccd9c262043579e05303296c7c2 Mon Sep 17 00:00:00 2001 From: Uladzimir Asipchuk Date: Thu, 15 Jul 2021 21:25:50 +0300 Subject: [PATCH] Implement IElementNode for PageMarginBoxContextNode DEVSIX-5370 --- .../css/page/PageMarginBoxContextNode.java | 86 +++++++++++++++++- .../page/PageMarginBoxContextNodeTest.java | 90 +++++++++++++++++++ 2 files changed, 173 insertions(+), 3 deletions(-) create mode 100644 styled-xml-parser/src/test/java/com/itextpdf/styledxmlparser/css/page/PageMarginBoxContextNodeTest.java diff --git a/styled-xml-parser/src/main/java/com/itextpdf/styledxmlparser/css/page/PageMarginBoxContextNode.java b/styled-xml-parser/src/main/java/com/itextpdf/styledxmlparser/css/page/PageMarginBoxContextNode.java index 54607d6ac7..cbf6b118b8 100644 --- a/styled-xml-parser/src/main/java/com/itextpdf/styledxmlparser/css/page/PageMarginBoxContextNode.java +++ b/styled-xml-parser/src/main/java/com/itextpdf/styledxmlparser/css/page/PageMarginBoxContextNode.java @@ -44,12 +44,20 @@ This file is part of the iText (R) project. import com.itextpdf.kernel.geom.Rectangle; import com.itextpdf.styledxmlparser.css.CssContextNode; +import com.itextpdf.styledxmlparser.node.IAttribute; +import com.itextpdf.styledxmlparser.node.IAttributes; +import com.itextpdf.styledxmlparser.node.ICustomElementNode; import com.itextpdf.styledxmlparser.node.INode; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + /** * {@link CssContextNode} implementation for page margin box contexts. */ -public class PageMarginBoxContextNode extends CssContextNode { +public class PageMarginBoxContextNode extends CssContextNode implements ICustomElementNode { /** The Constant PAGE_MARGIN_BOX_TAG. */ public static final String PAGE_MARGIN_BOX_TAG = "_064ef03_page-margin-box"; @@ -85,7 +93,9 @@ public String getMarginBoxName() { /** * Sets the rectangle in which page margin box contents are shown. - * @param pageMarginBoxRectangle the {@link Rectangle} defining position and dimensions of the margin box content area + * + * @param pageMarginBoxRectangle the {@link Rectangle} defining position and dimensions of the margin box content + * area */ public void setPageMarginBoxRectangle(Rectangle pageMarginBoxRectangle) { this.pageMarginBoxRectangle = pageMarginBoxRectangle; @@ -93,6 +103,7 @@ public void setPageMarginBoxRectangle(Rectangle pageMarginBoxRectangle) { /** * Gets the rectangle in which page margin box contents should be shown. + * * @return the {@link Rectangle} defining position and dimensions of the margin box content area */ public Rectangle getPageMarginBoxRectangle() { @@ -102,6 +113,7 @@ public Rectangle getPageMarginBoxRectangle() { /** * Sets the containing block rectangle for the margin box, which is used for calculating * some of the margin box properties relative values. + * * @param containingBlockForMarginBox the {@link Rectangle} which is used as a reference for some * margin box relative properties calculations. */ @@ -111,9 +123,77 @@ public void setContainingBlockForMarginBox(Rectangle containingBlockForMarginBox /** * @return the {@link Rectangle} which is used as a reference for some - * margin box relative properties calculations. + * margin box relative properties calculations. */ public Rectangle getContainingBlockForMarginBox() { return containingBlockForMarginBox; } + + @Override + public String name() { + return PageMarginBoxContextNode.PAGE_MARGIN_BOX_TAG; + } + + @Override + public IAttributes getAttributes() { + return new AttributesStub(); + } + + @Override + public String getAttribute(String key) { + return null; + } + + @Override + public List> getAdditionalHtmlStyles() { + return null; + } + + @Override + public void addAdditionalHtmlStyles(Map styles) { + throw new UnsupportedOperationException(); + } + + @Override + public String getLang() { + throw new UnsupportedOperationException(); + } + + /** + * A simple {@link IAttributes} implementation. + */ + private static class AttributesStub implements IAttributes { + + /** + * {@inheritDoc} + */ + @Override + public String getAttribute(String key) { + return null; + } + + /** + * {@inheritDoc} + */ + @Override + public void setAttribute(String key, String value) { + throw new UnsupportedOperationException(); + } + + /** + * {@inheritDoc} + */ + @Override + public int size() { + return 0; + } + + /** + * {@inheritDoc} + */ + @Override + public Iterator iterator() { + return Collections.emptyIterator(); + } + } } diff --git a/styled-xml-parser/src/test/java/com/itextpdf/styledxmlparser/css/page/PageMarginBoxContextNodeTest.java b/styled-xml-parser/src/test/java/com/itextpdf/styledxmlparser/css/page/PageMarginBoxContextNodeTest.java new file mode 100644 index 0000000000..b4d14bf480 --- /dev/null +++ b/styled-xml-parser/src/test/java/com/itextpdf/styledxmlparser/css/page/PageMarginBoxContextNodeTest.java @@ -0,0 +1,90 @@ +/* + This file is part of the iText (R) project. + Copyright (c) 1998-2021 iText Group NV + Authors: iText Software. + + This program is offered under a commercial and under the AGPL license. + For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below. + + AGPL licensing: + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program 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 Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + */ +package com.itextpdf.styledxmlparser.css.page; + +import com.itextpdf.kernel.geom.Rectangle; +import com.itextpdf.styledxmlparser.css.pseudo.CssPseudoElementNode; +import com.itextpdf.styledxmlparser.node.IAttributes; +import com.itextpdf.test.ExtendedITextTest; +import com.itextpdf.test.annotations.type.UnitTest; + +import java.util.HashMap; +import org.junit.Assert; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +@Category(UnitTest.class) +public class PageMarginBoxContextNodeTest extends ExtendedITextTest { + + @Test + public void defaultBehaviourTest() { + String marginBoxName = "someName"; + PageMarginBoxContextNode pageMarginBoxContextNode + = new PageMarginBoxContextNode(new PageContextNode(), marginBoxName); + + Assert.assertEquals(marginBoxName, pageMarginBoxContextNode.getMarginBoxName()); + Assert.assertEquals(PageMarginBoxContextNode.PAGE_MARGIN_BOX_TAG, pageMarginBoxContextNode.name()); + + Assert.assertThrows(UnsupportedOperationException.class, () -> pageMarginBoxContextNode.getLang()); + Assert.assertNull(pageMarginBoxContextNode.getAdditionalHtmlStyles()); + Assert.assertThrows(UnsupportedOperationException.class, + () -> pageMarginBoxContextNode.addAdditionalHtmlStyles(new HashMap<>())); + + IAttributes attributes = pageMarginBoxContextNode.getAttributes(); + Assert.assertNotNull(attributes); + Assert.assertEquals(0, attributes.size()); + + String someKey = "someKey"; + String someValue = "someValue"; + Assert.assertThrows(UnsupportedOperationException.class, + () -> attributes.setAttribute(someKey, someValue)); + Assert.assertNull(attributes.getAttribute(someKey)); + Assert.assertNull(pageMarginBoxContextNode.getAttribute(someKey)); + + Assert.assertNull(pageMarginBoxContextNode.getContainingBlockForMarginBox()); + Rectangle someRectangle = new Rectangle(100, 100); + pageMarginBoxContextNode.setContainingBlockForMarginBox(someRectangle); + Assert.assertEquals(someRectangle, pageMarginBoxContextNode.getContainingBlockForMarginBox()); + + Assert.assertNull(pageMarginBoxContextNode.getPageMarginBoxRectangle()); + Rectangle someRectangle2 = new Rectangle(200, 200); + pageMarginBoxContextNode.setPageMarginBoxRectangle(someRectangle2); + Assert.assertEquals(someRectangle2, pageMarginBoxContextNode.getPageMarginBoxRectangle()); + + } + + @Test + public void parentNotPageTest() { + // Create some invalid node + PageContextNode pageContextNode = new PageContextNode(); + CssPseudoElementNode pseudoElementNode = new CssPseudoElementNode(pageContextNode, "test"); + + // Pass this mode to the constructor + Exception e = Assert.assertThrows(IllegalArgumentException.class, + () -> new PageMarginBoxContextNode(pseudoElementNode, "test")); + Assert.assertEquals( + "Page-margin-box context node shall have a page context node as parent.", + e.getMessage()); + + } +}