Skip to content

Commit

Permalink
(#51) Dicionary and Reference
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriciofx committed Dec 21, 2023
1 parent 2b14e48 commit 2a5477f
Show file tree
Hide file tree
Showing 32 changed files with 546 additions and 331 deletions.
27 changes: 15 additions & 12 deletions src/main/java/com/github/fabriciofx/cactoos/pdf/Catalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
*/
package com.github.fabriciofx.cactoos.pdf;

import com.github.fabriciofx.cactoos.pdf.type.Dictionary;
import com.github.fabriciofx.cactoos.pdf.type.Name;
import com.github.fabriciofx.cactoos.pdf.type.Text;
import java.io.ByteArrayOutputStream;
import org.cactoos.Bytes;
import org.cactoos.text.FormattedText;
import org.cactoos.text.UncheckedText;

/**
* Catalog.
Expand Down Expand Up @@ -73,27 +75,28 @@ public Catalog(final int number, final int generation, final Pages pages) {
}

@Override
public String reference() {
return new UncheckedText(
new FormattedText(
"%d %d R",
this.number,
this.generation
)
).asString();
public Reference reference() {
return new Reference(this.number, this.generation);
}

public Dictionary dictionary() throws Exception {
return new Dictionary()
.add("Type", new Name("Catalog"))
.add("Pages", new Text(this.pages.reference().asString()));
}

@Override
public byte[] asBytes() throws Exception {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(
new FormattedText(
"%d %d obj\n<< /Type /Catalog /Pages %s >>\nendobj\n",
"%d %d obj\n",
this.number,
this.generation,
this.pages.reference()
this.generation
).asString().getBytes()
);
baos.write(this.dictionary().asBytes());
baos.write("\nendobj\n".getBytes());
baos.write(this.pages.asBytes());
return baos.toByteArray();
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/github/fabriciofx/cactoos/pdf/Content.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package com.github.fabriciofx.cactoos.pdf;

import com.github.fabriciofx.cactoos.pdf.type.Dictionary;
import org.cactoos.Bytes;

/**
Expand All @@ -39,4 +40,6 @@ public interface Content extends Object, Bytes {
* @throws Exception if fails
*/
byte[] stream() throws Exception;

Dictionary dictionary() throws Exception;
}
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 @@ -99,9 +99,9 @@ public byte[] asBytes() throws Exception {
baos.write(
new FormattedText(
"trailer << /Root %s /Size %d /Info %s >>\n",
this.catalog.reference(),
this.catalog.reference().asString(),
this.count.value(),
this.information.reference()
this.information.reference().asString()
).asString().getBytes()
);
baos.write(Document.EOF.getBytes());
Expand Down
10 changes: 2 additions & 8 deletions src/main/java/com/github/fabriciofx/cactoos/pdf/Information.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,8 @@ public Information(
}

@Override
public String reference() {
return new UncheckedText(
new FormattedText(
"%d %d R",
this.number,
this.generation
)
).asString();
public Reference reference() {
return new Reference(this.number, this.generation);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ public interface Object {
* Object reference.
* @return The object reference.
*/
String reference();
Reference reference();
}
3 changes: 2 additions & 1 deletion src/main/java/com/github/fabriciofx/cactoos/pdf/Page.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import com.github.fabriciofx.cactoos.pdf.content.Contents;
import com.github.fabriciofx.cactoos.pdf.resource.Resources;
import com.github.fabriciofx.cactoos.pdf.type.Dictionary;

/**
* PageDefault.
Expand All @@ -39,7 +40,7 @@ public interface Page extends Object {
* @param parent Pages parent
* @return The page dictionary
*/
String dictionary(Pages parent);
Dictionary dictionary(Pages parent) throws Exception;

/**
* Page Resources.
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/github/fabriciofx/cactoos/pdf/Pages.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package com.github.fabriciofx.cactoos.pdf;

import com.github.fabriciofx.cactoos.pdf.type.Dictionary;
import org.cactoos.Bytes;

/**
Expand All @@ -38,4 +39,6 @@ public interface Pages extends Object, Bytes {
* @param page An PDF page
*/
void add(Page page);

Dictionary dictionary() throws Exception;
}
54 changes: 54 additions & 0 deletions src/main/java/com/github/fabriciofx/cactoos/pdf/Reference.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* The MIT License (MIT)
*
* Copyright (C) 2023 Fabrício Barros Cabral
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.github.fabriciofx.cactoos.pdf;

import org.cactoos.Text;
import org.cactoos.text.FormattedText;

public final class Reference implements Text {
private final int number;
private final int generation;

public Reference(final int number, final int generation) {
this.number = number;
this.generation = generation;
}

public int number() {
return this.number;
}

public int generation() {
return this.generation;
}

@Override
public String asString() throws Exception {
return new FormattedText(
"%d %d R",
this.number,
this.generation
).asString();
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/github/fabriciofx/cactoos/pdf/Resource.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package com.github.fabriciofx.cactoos.pdf;

import com.github.fabriciofx.cactoos.pdf.type.Dictionary;
import org.cactoos.Bytes;

/**
Expand All @@ -32,4 +33,5 @@
*/
@SuppressWarnings("PMD.ExtendsObject")
public interface Resource extends Bytes {
Dictionary dictionary() throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@

import com.github.fabriciofx.cactoos.pdf.Content;
import com.github.fabriciofx.cactoos.pdf.Object;
import com.github.fabriciofx.cactoos.pdf.Reference;
import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.stream.Collectors;
import org.cactoos.Bytes;
import org.cactoos.list.ListEnvelope;
import org.cactoos.list.ListOf;
Expand Down Expand Up @@ -58,10 +58,13 @@ public Contents(final List<Content> list) {
}

@Override
public String reference() {
return this.stream()
.map(Content::reference)
.collect(Collectors.joining(" "));
public Reference reference() {
if (this.isEmpty()) {
throw new IllegalStateException(
"Contents is empty; there is not any reference"
);
}
return this.get(0).reference();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
package com.github.fabriciofx.cactoos.pdf.content;

import com.github.fabriciofx.cactoos.pdf.Content;
import com.github.fabriciofx.cactoos.pdf.Reference;
import com.github.fabriciofx.cactoos.pdf.type.Dictionary;
import com.github.fabriciofx.cactoos.pdf.type.Int;
import com.github.fabriciofx.cactoos.pdf.type.Name;
import com.github.fabriciofx.cactoos.pdf.type.Stream;
import java.io.ByteArrayOutputStream;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
Expand Down Expand Up @@ -74,23 +79,31 @@ public byte[] stream() throws Exception {
}

@Override
public String reference() {
public Dictionary dictionary() throws Exception {
final byte[] stream = this.stream();
return new Dictionary()
.add("Length", new Int(stream.length))
.add("Filter", new Name("FlateDecode"))
.with(new Stream(stream));
}

@Override
public Reference reference() {
return this.origin.reference();
}

@Override
public byte[] asBytes() throws Exception {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final byte[] stream = this.stream();
baos.write(
new FormattedText(
"%s obj\n<< /Length %d /Filter /FlateDecode >>\nstream\n",
this.reference().replaceAll(" R", ""),
stream.length
"%d %d obj\n",
this.reference().number(),
this.reference().generation()
).asString().getBytes()
);
baos.write(stream);
baos.write("\nendstream\nendobj\n".getBytes());
baos.write(this.dictionary().asBytes());
baos.write("\nendobj\n".getBytes());
return baos.toByteArray();
}
}
32 changes: 19 additions & 13 deletions src/main/java/com/github/fabriciofx/cactoos/pdf/content/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import com.github.fabriciofx.cactoos.pdf.Content;
import com.github.fabriciofx.cactoos.pdf.Count;
import com.github.fabriciofx.cactoos.pdf.Reference;
import com.github.fabriciofx.cactoos.pdf.type.Dictionary;
import com.github.fabriciofx.cactoos.pdf.type.Int;
import com.github.fabriciofx.cactoos.pdf.type.Name;
import com.github.fabriciofx.cactoos.pdf.type.Stream;
import java.io.ByteArrayOutputStream;
import org.cactoos.text.FormattedText;
import org.cactoos.text.Joined;
Expand Down Expand Up @@ -47,29 +52,30 @@ public byte[] stream() throws Exception {
}

@Override
public String reference() {
return new UncheckedText(
new FormattedText(
"%d %d R",
this.number,
this.generation
)
).asString();
public Dictionary dictionary() throws Exception {
final byte[] stream = this.stream();
return new Dictionary()
.add("Length", new Int(stream.length))
.with(new Stream(stream));
}

@Override
public Reference reference() {
return new Reference(this.number, this.generation);
}

@Override
public byte[] asBytes() throws Exception {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(
new FormattedText(
"%d %d obj\n<< /Length %d >>\nstream\n",
"%d %d obj\n",
this.number,
this.generation,
this.stream().length
this.generation
).asString().getBytes()
);
baos.write(this.stream());
baos.write("\nendstream\nendobj\n".getBytes());
baos.write(this.dictionary().asBytes());
baos.write("endobj\n".getBytes());
baos.write(this.png.asBytes());
return baos.toByteArray();
}
Expand Down
Loading

0 comments on commit 2a5477f

Please sign in to comment.