Skip to content

Commit

Permalink
Implement IElementNode for PageMarginBoxContextNode
Browse files Browse the repository at this point in the history
DEVSIX-5370
  • Loading branch information
ars18wrw committed Aug 4, 2021
1 parent 04976e6 commit a1a1e6f
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -85,14 +93,17 @@ 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;
}

/**
* 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() {
Expand All @@ -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.
*/
Expand All @@ -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<Map<String, String>> getAdditionalHtmlStyles() {
return null;
}

@Override
public void addAdditionalHtmlStyles(Map<String, String> 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<IAttribute> iterator() {
return Collections.<IAttribute>emptyIterator();
}
}
}
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/
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());

}
}

0 comments on commit a1a1e6f

Please sign in to comment.