Skip to content

Commit

Permalink
(#28) Change Object to has byte[] with(Objects... objects) method ins…
Browse files Browse the repository at this point in the history
…tead of extends Bytes
  • Loading branch information
fabriciofx committed Dec 11, 2023
1 parent 4a97121 commit 31dc531
Show file tree
Hide file tree
Showing 14 changed files with 35 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/github/fabriciofx/cactoos/pdf/Catalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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();
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/github/fabriciofx/cactoos/pdf/Contents.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/github/fabriciofx/cactoos/pdf/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/github/fabriciofx/cactoos/pdf/Font.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/com/github/fabriciofx/cactoos/pdf/Object.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
13 changes: 7 additions & 6 deletions src/main/java/com/github/fabriciofx/cactoos/pdf/Page.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.cactoos.text.UncheckedText;

/**
* Page.
* PageDefault.
*
* @since 0.0.1
*/
Expand Down Expand Up @@ -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();
}
}
11 changes: 2 additions & 9 deletions src/main/java/com/github/fabriciofx/cactoos/pdf/Pages.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/github/fabriciofx/cactoos/pdf/Text.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void build() throws Exception {
0,
new FontFamily("Times-Roman", "Type1"),
"F0"
).asBytes()
).with()
),
new IsText(
new Joined(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -66,7 +66,7 @@ void multiLines() {
"deserunt laborum mollit labore",
"id amet."
)
)
).with()
),
new IsText(
new Joined(
Expand Down

0 comments on commit 31dc531

Please sign in to comment.