diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/Catalog.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/Catalog.java index 89966f3..dfc620b 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/Catalog.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/Catalog.java @@ -83,7 +83,7 @@ public String reference() { } @Override - public byte[] asBytes() throws Exception { + public byte[] with(final Object... objects) throws Exception { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); baos.write( new FormattedText( @@ -93,7 +93,7 @@ public byte[] asBytes() throws Exception { this.pages.reference() ).asString().getBytes() ); - baos.write(this.pages.asBytes()); + baos.write(this.pages.with()); return baos.toByteArray(); } } diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/Contents.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/Contents.java index c89620c..aabeab8 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/Contents.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/Contents.java @@ -62,10 +62,10 @@ public String reference() { } @Override - public byte[] asBytes() throws Exception { + public byte[] with(final Object... objects) throws Exception { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); for (final Object obj : this) { - baos.write(obj.asBytes()); + baos.write(obj.with()); } return baos.toByteArray(); } diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/Document.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/Document.java index e84cd24..097a2b8 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/Document.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/Document.java @@ -94,8 +94,8 @@ public byte[] asBytes() throws Exception { ).asString().getBytes() ); baos.write(Document.SIGNATURE); - baos.write(this.metadata.asBytes()); - baos.write(this.catalog.asBytes()); + baos.write(this.metadata.with()); + baos.write(this.catalog.with()); baos.write( new FormattedText( "trailer << /Root %s /Size %d >>\n", diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/Font.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/Font.java index e0ae47c..d8dfbf6 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/Font.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/Font.java @@ -96,7 +96,7 @@ public String reference() { } @Override - public byte[] asBytes() throws Exception { + public byte[] with(final Object... objects) throws Exception { return new FormattedText( "%d %d obj\n<< /Font << /%s %s >> >>\nendobj\n", this.number, diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/Metadata.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/Metadata.java index eb0dfdc..dff1fad 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/Metadata.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/Metadata.java @@ -86,7 +86,7 @@ public String reference() { } @Override - public byte[] asBytes() throws Exception { + public byte[] with(final Object... objects) throws Exception { return new FormattedText( "%d %d obj\n<< /Title (%s) >>\nendobj\n", this.number, diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/MultiText.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/MultiText.java index abf7be6..63ff62f 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/MultiText.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/MultiText.java @@ -158,7 +158,7 @@ public String reference() { } @Override - public byte[] asBytes() throws Exception { + public byte[] with(final Object... objects) throws Exception { final StringBuilder out = new StringBuilder(); final String[] lines = breakLines(this.content.asString(), this.max); for (int idx = 0; idx < lines.length - 1; ++idx) { diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/Object.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/Object.java index 91baa5e..38eef15 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/Object.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/Object.java @@ -23,17 +23,23 @@ */ package com.github.fabriciofx.cactoos.pdf; -import org.cactoos.Bytes; - /** * Represent any PDF object. * * @since 0.0.1 */ -public interface Object extends Bytes { +public interface Object { /** * Object reference. * @return The object reference. */ String reference(); + + /** + * Build part of a PDF document. + * @param objects Other objects + * @return An array of bytes that represents a PDF document + * @throws Exception if fails + */ + byte[] with(Object... objects) throws Exception; } diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/Page.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/Page.java index d5ea2bb..afcef83 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/Page.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/Page.java @@ -29,7 +29,7 @@ import org.cactoos.text.UncheckedText; /** - * Page. + * PageDefault. * * @since 0.0.1 */ @@ -102,23 +102,24 @@ public String reference() { } @Override - public byte[] asBytes() throws Exception { + public byte[] with(final Object... objects) throws Exception { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); baos.write( new FormattedText( new Joined( " ", "%d %d obj\n<< /Type /Page /Resources %s", - "/Contents %s /Parent >>\nendobj\n" + "/Contents %s /Parent %s >>\nendobj\n" ), this.number, this.generation, this.resources.reference(), - this.contents.reference() + this.contents.reference(), + objects[0].reference() ).asString().getBytes() ); - baos.write(this.resources.asBytes()); - baos.write(this.contents.asBytes()); + baos.write(this.resources.with()); + baos.write(this.contents.with()); return baos.toByteArray(); } } diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/Pages.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/Pages.java index de7dc5c..6ac85f2 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/Pages.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/Pages.java @@ -101,7 +101,7 @@ public String reference() { } @Override - public byte[] asBytes() throws Exception { + public byte[] with(final Object... objects) throws Exception { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final String kds = this.kids.stream() .map(Page::reference) @@ -121,14 +121,7 @@ public byte[] asBytes() throws Exception { ).asString().getBytes() ); for (final Page page : this.kids) { - baos.write( - new String( - page.asBytes() - ).replaceAll( - "/Parent", - new FormattedText("/Parent %s", this.reference()).asString() - ).getBytes() - ); + baos.write(page.with(this)); } return baos.toByteArray(); } diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/Resources.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/Resources.java index dc1202c..c599b44 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/Resources.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/Resources.java @@ -62,10 +62,10 @@ public String reference() { } @Override - public byte[] asBytes() throws Exception { + public byte[] with(final Object... objects) throws Exception { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); for (final Object obj : this) { - baos.write(obj.asBytes()); + baos.write(obj.with()); } return baos.toByteArray(); } diff --git a/src/main/java/com/github/fabriciofx/cactoos/pdf/Text.java b/src/main/java/com/github/fabriciofx/cactoos/pdf/Text.java index 43f55d5..e0054d9 100644 --- a/src/main/java/com/github/fabriciofx/cactoos/pdf/Text.java +++ b/src/main/java/com/github/fabriciofx/cactoos/pdf/Text.java @@ -121,7 +121,7 @@ public String reference() { } @Override - public byte[] asBytes() throws Exception { + public byte[] with(final Object... objects) throws Exception { final String stream = new FormattedText( "BT /F1 %d Tf %d %d Td (%s) Tj ET", this.size, diff --git a/src/test/java/com/github/fabriciofx/cactoos/pdf/FontTest.java b/src/test/java/com/github/fabriciofx/cactoos/pdf/FontTest.java index 73864fa..8914a74 100644 --- a/src/test/java/com/github/fabriciofx/cactoos/pdf/FontTest.java +++ b/src/test/java/com/github/fabriciofx/cactoos/pdf/FontTest.java @@ -45,7 +45,7 @@ void build() throws Exception { 0, new FontFamily("Times-Roman", "Type1"), "F0" - ).asBytes() + ).with() ), new IsText( new Joined( diff --git a/src/test/java/com/github/fabriciofx/cactoos/pdf/MetadataTest.java b/src/test/java/com/github/fabriciofx/cactoos/pdf/MetadataTest.java index 4a4f3dd..7323a43 100644 --- a/src/test/java/com/github/fabriciofx/cactoos/pdf/MetadataTest.java +++ b/src/test/java/com/github/fabriciofx/cactoos/pdf/MetadataTest.java @@ -36,11 +36,11 @@ */ final class MetadataTest { @Test - void metadata() { + void metadata() throws Exception { final String title = "Hello World"; new Assertion<>( "Must contain metadata contents", - new TextOf(new Metadata(1, 0, title)), + new TextOf(new Metadata(1, 0, title).with()), new IsText( new FormattedText( "1 0 obj\n<< /Title (Hello World) >>\nendobj\n", diff --git a/src/test/java/com/github/fabriciofx/cactoos/pdf/MultiTextTest.java b/src/test/java/com/github/fabriciofx/cactoos/pdf/MultiTextTest.java index f610241..5f4d937 100644 --- a/src/test/java/com/github/fabriciofx/cactoos/pdf/MultiTextTest.java +++ b/src/test/java/com/github/fabriciofx/cactoos/pdf/MultiTextTest.java @@ -36,7 +36,7 @@ */ final class MultiTextTest { @Test - void multiLines() { + void multiLines() throws Exception { new Assertion<>( "Must break a big text into multiline", new TextOf( @@ -66,7 +66,7 @@ void multiLines() { "deserunt laborum mollit labore", "id amet." ) - ) + ).with() ), new IsText( new Joined(