Skip to content

Commit

Permalink
(#36) Add support to new metadata into Information
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriciofx committed Dec 12, 2023
1 parent 4e5b245 commit 211ce17
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 30 deletions.
60 changes: 46 additions & 14 deletions src/main/java/com/github/fabriciofx/cactoos/pdf/Information.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package com.github.fabriciofx.cactoos.pdf;

import java.util.Map;
import org.cactoos.Bytes;
import org.cactoos.text.FormattedText;
import org.cactoos.text.UncheckedText;
Expand All @@ -44,35 +45,37 @@ public final class Information implements Object, Bytes {
private final int generation;

/**
* PDF title.
* Metadata.
*/
private final String title;
private final Map<String, String> metadata;

/**
* Ctor.
*
* @param count Counter
* @param title PDF title
* @param metadata Metadata
*/
public Information(final Count count, final String title) {
this(count.increment(), 0, title);
public Information(
final Count count, final Map<String, String> metadata
) {
this(count.increment(), 0, metadata);
}

/**
* Ctor.
*
* @param number Object number
* @param generation Object generation
* @param title PDF title
* @param metadata Metadata
*/
public Information(
final int number,
final int generation,
final String title
final Map<String, String> metadata
) {
this.number = number;
this.generation = generation;
this.title = title;
this.metadata = metadata;
}

@Override
Expand All @@ -88,11 +91,40 @@ public String reference() {

@Override
public byte[] asBytes() throws Exception {
return new FormattedText(
"%d %d obj\n<< /Title (%s) >>\nendobj\n",
this.number,
this.generation,
this.title
).asString().getBytes();
final StringBuilder params = new StringBuilder(128);
params.append(this.number)
.append(' ')
.append(this.generation)
.append(" obj\n<<");
addIfContains(params, this.metadata, "Title");
addIfContains(params, this.metadata, "Subject");
addIfContains(params, this.metadata, "Author");
addIfContains(params, this.metadata, "Creator");
addIfContains(params, this.metadata, "Producer");
addIfContains(params, this.metadata, "CreationDate");
addIfContains(params, this.metadata, "ModDate");
addIfContains(params, this.metadata, "Keywords");
params.append(" >>\nendobj\n");
return params.toString().getBytes();
}

/**
* Add a metadata to builder if metadata contains data.
*
* @param builder A StringBuilder
* @param metadata The metadata map
* @param data The data
*/
private static void addIfContains(
final StringBuilder builder,
final Map<String, String> metadata,
final String data
) {
if (metadata.containsKey(data)) {
builder.append(" /")
.append(data).append(" (")
.append(metadata.get(data))
.append(')');
}
}
}
124 changes: 112 additions & 12 deletions src/test/java/com/github/fabriciofx/cactoos/pdf/DocumentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
import com.github.fabriciofx.cactoos.pdf.resource.Resources;
import java.io.File;
import java.nio.file.Files;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import org.cactoos.map.MapEntry;
import org.cactoos.map.MapOf;
import org.cactoos.text.Joined;
import org.cactoos.text.TextOf;
import org.junit.jupiter.api.Disabled;
Expand All @@ -45,17 +50,40 @@
*
* @since 0.0.1
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
@SuppressWarnings(
{
"PMD.AvoidDuplicateLiterals",
"PMD.ExcessiveMethodLength"
}
)
final class DocumentTest {
@Test
void buildDocument() {
void buildDocument() throws Exception {
final Count count = new ObjectCount();
final Date date = new Date(
ZonedDateTime.of(
LocalDateTime.of(2023, 12, 11, 20, 11, 32),
ZoneId.of("Etc/GMT-3")
)
);
new Assertion<>(
"Must represent a PDF document",
new TextOf(
new Document(
count,
new Information(count, "Hello World"),
new Information(
count,
new MapOf<>(
new MapEntry<>("Title", "Hello World"),
new MapEntry<>("Subject", "PDF document"),
new MapEntry<>("Author", "Fabrício Cabral"),
new MapEntry<>("Creator", "cactoos-pdf"),
new MapEntry<>("Producer", "cactoos-pdf"),
new MapEntry<>("CreationDate", date.asString()),
new MapEntry<>("ModDate", date.asString()),
new MapEntry<>("Keywords", "cactoos pdf elegant objects")
)
),
new Catalog(
count,
new DefaultPages(
Expand Down Expand Up @@ -90,7 +118,7 @@ void buildDocument() {
new Joined(
"\n",
"%PDF-1.3\n%���������",
"1 0 obj\n<< /Title (Hello World) >>\nendobj",
"1 0 obj\n<< /Title (Hello World) /Subject (PDF document) /Author (Fabrício Cabral) /Creator (cactoos-pdf) /Producer (cactoos-pdf) /CreationDate (D:20231211201132+03'00') /ModDate (D:20231211201132+03'00') /Keywords (cactoos pdf elegant objects) >>\nendobj",
"6 0 obj\n<< /Type /Catalog /Pages 5 0 R >>\nendobj",
"5 0 obj\n<< /Type /Pages /Kids [4 0 R] /Count 1 /MediaBox [0 0 595.28 841.89] >>\nendobj",
"4 0 obj\n<< /Type /Page /Resources 2 0 R /Contents 3 0 R /Parent 5 0 R >>\nendobj",
Expand All @@ -104,14 +132,32 @@ void buildDocument() {
}

@Test
void buildMultiTextDocument() {
void buildMultiTextDocument() throws Exception {
final Count count = new ObjectCount();
final Date date = new Date(
ZonedDateTime.of(
LocalDateTime.of(2023, 12, 11, 20, 11, 32),
ZoneId.of("Etc/GMT-3")
)
);
new Assertion<>(
"Must represent a PDF document",
new TextOf(
new Document(
count,
new Information(count, "Hello World"),
new Information(
count,
new MapOf<>(
new MapEntry<>("Title", "Hello World"),
new MapEntry<>("Subject", "PDF document"),
new MapEntry<>("Author", "Fabrício Cabral"),
new MapEntry<>("Creator", "cactoos-pdf"),
new MapEntry<>("Producer", "cactoos-pdf"),
new MapEntry<>("CreationDate", date.asString()),
new MapEntry<>("ModDate", date.asString()),
new MapEntry<>("Keywords", "cactoos pdf elegant objects")
)
),
new Catalog(
count,
new DefaultPages(
Expand Down Expand Up @@ -164,7 +210,7 @@ void buildMultiTextDocument() {
new Joined(
"\n",
"%PDF-1.3\n%���������",
"1 0 obj\n<< /Title (Hello World) >>\nendobj",
"1 0 obj\n<< /Title (Hello World) /Subject (PDF document) /Author (Fabrício Cabral) /Creator (cactoos-pdf) /Producer (cactoos-pdf) /CreationDate (D:20231211201132+03'00') /ModDate (D:20231211201132+03'00') /Keywords (cactoos pdf elegant objects) >>\nendobj",
"6 0 obj\n<< /Type /Catalog /Pages 5 0 R >>\nendobj",
"5 0 obj\n<< /Type /Pages /Kids [4 0 R] /Count 1 /MediaBox [0 0 595.28 841.89] >>\nendobj",
"4 0 obj\n<< /Type /Page /Resources 2 0 R /Contents 3 0 R /Parent 5 0 R >>\nendobj",
Expand Down Expand Up @@ -194,14 +240,32 @@ void buildMultiTextDocument() {
}

@Test
void buildTwoPagesDocument() {
void buildTwoPagesDocument() throws Exception {
final Count count = new ObjectCount();
final Date date = new Date(
ZonedDateTime.of(
LocalDateTime.of(2023, 12, 11, 20, 11, 32),
ZoneId.of("Etc/GMT-3")
)
);
new Assertion<>(
"Must represent a two pages PDF document",
new TextOf(
new Document(
count,
new Information(count, "Hello World"),
new Information(
count,
new MapOf<>(
new MapEntry<>("Title", "Hello World"),
new MapEntry<>("Subject", "PDF document"),
new MapEntry<>("Author", "Fabrício Cabral"),
new MapEntry<>("Creator", "cactoos-pdf"),
new MapEntry<>("Producer", "cactoos-pdf"),
new MapEntry<>("CreationDate", date.asString()),
new MapEntry<>("ModDate", date.asString()),
new MapEntry<>("Keywords", "cactoos pdf elegant objects")
)
),
new Catalog(
count,
new DefaultPages(
Expand Down Expand Up @@ -253,7 +317,7 @@ void buildTwoPagesDocument() {
new Joined(
"\n",
"%PDF-1.3\n%���������",
"1 0 obj\n<< /Title (Hello World) >>\nendobj",
"1 0 obj\n<< /Title (Hello World) /Subject (PDF document) /Author (Fabrício Cabral) /Creator (cactoos-pdf) /Producer (cactoos-pdf) /CreationDate (D:20231211201132+03'00') /ModDate (D:20231211201132+03'00') /Keywords (cactoos pdf elegant objects) >>\nendobj",
"9 0 obj\n<< /Type /Catalog /Pages 8 0 R >>\nendobj",
"8 0 obj\n<< /Type /Pages /Kids [4 0 R 7 0 R] /Count 2 /MediaBox [0 0 595.28 841.89] >>\nendobj",
"4 0 obj\n<< /Type /Page /Resources 2 0 R /Contents 3 0 R /Parent 8 0 R >>\nendobj",
Expand All @@ -274,11 +338,29 @@ void buildTwoPagesDocument() {
void buildFile() throws Exception {
final File file = new File("HelloWorld.pdf");
final Count count = new ObjectCount();
final Date date = new Date(
ZonedDateTime.of(
LocalDateTime.of(2023, 12, 11, 20, 11, 32),
ZoneId.of("Etc/GMT-3")
)
);
Files.write(
file.toPath(),
new Document(
count,
new Information(count, "Hello World"),
new Information(
count,
new MapOf<>(
new MapEntry<>("Title", "Hello World"),
new MapEntry<>("Subject", "PDF document"),
new MapEntry<>("Author", "Fabrício Cabral"),
new MapEntry<>("Creator", "cactoos-pdf"),
new MapEntry<>("Producer", "cactoos-pdf"),
new MapEntry<>("CreationDate", date.asString()),
new MapEntry<>("ModDate", date.asString()),
new MapEntry<>("Keywords", "cactoos pdf elegant objects")
)
),
new Catalog(
count,
new DefaultPages(
Expand Down Expand Up @@ -333,11 +415,29 @@ void buildFile() throws Exception {
void buildMultiFile() throws Exception {
final File file = new File("HelloWorld.pdf");
final Count count = new ObjectCount();
final Date date = new Date(
ZonedDateTime.of(
LocalDateTime.of(2023, 12, 11, 20, 11, 32),
ZoneId.of("Etc/GMT-3")
)
);
Files.write(
file.toPath(),
new Document(
count,
new Information(count, "Hello World"),
new Information(
count,
new MapOf<>(
new MapEntry<>("Title", "Hello World"),
new MapEntry<>("Subject", "PDF document"),
new MapEntry<>("Author", "Fabrício Cabral"),
new MapEntry<>("Creator", "cactoos-pdf"),
new MapEntry<>("Producer", "cactoos-pdf"),
new MapEntry<>("CreationDate", date.asString()),
new MapEntry<>("ModDate", date.asString()),
new MapEntry<>("Keywords", "cactoos pdf elegant objects")
)
),
new Catalog(
count,
new DefaultPages(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@
*/
package com.github.fabriciofx.cactoos.pdf;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import org.cactoos.map.MapEntry;
import org.cactoos.map.MapOf;
import org.cactoos.text.FormattedText;
import org.cactoos.text.Joined;
import org.cactoos.text.TextOf;
import org.junit.jupiter.api.Test;
import org.llorllale.cactoos.matchers.Assertion;
Expand All @@ -37,14 +43,46 @@
final class InformationTest {
@Test
void info() throws Exception {
final String title = "Hello World";
final Date date = new Date(
ZonedDateTime.of(
LocalDateTime.of(2023, 12, 11, 20, 11, 32),
ZoneId.of("Etc/GMT-3")
)
);
new Assertion<>(
"Must contain metadata contents",
new TextOf(new Information(1, 0, title)),
new TextOf(
new Information(
new ObjectCount(),
new MapOf<>(
new MapEntry<>("Title", "Hello World"),
new MapEntry<>("Subject", "PDF document"),
new MapEntry<>("Author", "Fabrício Cabral"),
new MapEntry<>("Creator", "cactoos-pdf"),
new MapEntry<>("Producer", "cactoos-pdf"),
new MapEntry<>("CreationDate", date.asString()),
new MapEntry<>("ModDate", date.asString()),
new MapEntry<>("Keywords", "cactoos pdf elegant objects")
)
)
),
new IsText(
new FormattedText(
"1 0 obj\n<< /Title (Hello World) >>\nendobj\n",
title
new Joined(
" ",
"1 0 obj\n<<",
"/Title (Hello World)",
"/Subject (PDF document)",
"/Author (Fabrício Cabral)",
"/Creator (cactoos-pdf)",
"/Producer (cactoos-pdf)",
"/CreationDate (%s)",
"/ModDate (%s)",
"/Keywords (cactoos pdf elegant objects)",
">>\nendobj\n"
),
date.asString(),
date.asString()
)
)
).affirm();
Expand Down

0 comments on commit 211ce17

Please sign in to comment.