Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix LGTM Errors #278

Merged
merged 1 commit into from
Oct 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion openpdf/src/main/java/com/lowagie/text/pdf/PdfNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@

import com.lowagie.text.error_messages.MessageLocalization;

import java.util.Objects;

/**
* <CODE>PdfNumber</CODE> provides two types of numbers, integer and real.
* <P>
Expand All @@ -62,7 +64,7 @@
* @see PdfObject
* @see BadPdfFormatException
*/
public class PdfNumber extends PdfObject {
public class PdfNumber extends PdfObject implements Comparable<PdfNumber> {

// CLASS VARIABLES

Expand Down Expand Up @@ -170,4 +172,28 @@ public void increment() {
value += 1.0;
setContent(ByteBuffer.formatDouble(value));
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof PdfNumber)) {
return false;
}
PdfNumber pdfNumber = (PdfNumber) o;
return Double.compare(pdfNumber.value, value) == 0;
}

@Override
public int hashCode() {
return Objects.hash(value);
}

@Override
public int compareTo(PdfNumber o) {
Objects.requireNonNull(o, "PdfNumber is null, can't be compared to current instance.");
if (this == o) return 0;
return Double.compare(o.value, value);
}
}
8 changes: 4 additions & 4 deletions openpdf/src/main/java/com/lowagie/text/pdf/PdfNumberTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ public static PdfDictionary writeTree(Map<Integer, ? extends PdfObject> items, P
}
}

private static void iterateItems(PdfDictionary dic, Map<PdfObject, PdfObject> items) {
private static void iterateItems(PdfDictionary dic, Map<Integer, PdfObject> items) {
PdfArray nn = (PdfArray)PdfReader.getPdfObjectRelease(dic.get(PdfName.NUMS));
if (nn != null) {
for (int k = 0; k < nn.size(); ++k) {
PdfNumber s = (PdfNumber)PdfReader.getPdfObjectRelease(nn.getPdfObject(k++));
items.put(s, nn.getPdfObject(k));
items.put(s.intValue(), nn.getPdfObject(k));
}
}
else if ((nn = (PdfArray)PdfReader.getPdfObjectRelease(dic.get(PdfName.KIDS))) != null) {
Expand All @@ -148,8 +148,8 @@ else if ((nn = (PdfArray)PdfReader.getPdfObjectRelease(dic.get(PdfName.KIDS))) !
}
}

public static HashMap<PdfObject, PdfObject> readTree(PdfDictionary dic) {
HashMap<PdfObject, PdfObject> items = new HashMap<>();
public static HashMap<Integer, PdfObject> readTree(PdfDictionary dic) {
HashMap<Integer, PdfObject> items = new HashMap<>();
if (dic != null)
iterateItems(dic, items);
return items;
Expand Down
17 changes: 13 additions & 4 deletions openpdf/src/main/java/com/lowagie/text/pdf/PdfPageLabels.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public static String[] getPageLabels(PdfReader reader) {

String[] labelstrings = new String[n];

Map<PdfObject, PdfObject> numberTree = PdfNumberTree.readTree(labels);
Map<Integer, PdfObject> numberTree = PdfNumberTree.readTree(labels);

int pagecount = 1;
Integer current;
Expand All @@ -227,8 +227,7 @@ public static String[] getPageLabels(PdfReader reader) {
prefix = "";
}
if (d.contains(PdfName.S)) {
type = d.get(PdfName.S).toString()
.charAt(1);
type = d.get(PdfName.S).toString().charAt(1);
}
}
switch (type) {
Expand Down Expand Up @@ -267,7 +266,7 @@ public static PdfPageLabelFormat[] getPageLabelFormats(PdfReader reader) {
if (labels == null) {
return null;
}
Map<PdfObject, PdfObject> numberTree = PdfNumberTree.readTree(labels);
Map<Integer, PdfObject> numberTree = PdfNumberTree.readTree(labels);
Integer[] numbers = new Integer[numberTree.size()];
numbers = numberTree.keySet().toArray(numbers);
Arrays.sort(numbers);
Expand Down Expand Up @@ -346,5 +345,15 @@ public PdfPageLabelFormat( int physicalPage,
this.prefix = prefix;
this.logicalPage = logicalPage;
}

@Override
public String toString() {
return "PdfPageLabelFormat{" +
"physicalPage=" + physicalPage +
", numberStyle=" + numberStyle +
", prefix='" + prefix + '\'' +
", logicalPage=" + logicalPage +
'}';
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPageLabels;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Demonstrates how pagelabels work.
*
Expand All @@ -36,34 +40,39 @@ public class PageLabels {
*
* @param args no arguments needed here
*/
public static void main(String[] args) {
public static void main(String[] args) throws IOException {

System.out.println("pagelabels");

// step 1: creation of a document-object
Document document = new Document();
try {
// step 2:
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("PageLabels.pdf"));
// step 3:
writer.setViewerPreferences(PdfWriter.PageModeUseThumbs);
document.open();
// step 4:
// we add some content
for (int k = 1; k <= 10; ++k) {
document.add(new Paragraph("This document has the logical page numbers: i,ii,iii,iv,1,2,3,A-8,A-9,A-10\nReal page " + k));
document.newPage();
}
PdfPageLabels pageLabels = new PdfPageLabels();
pageLabels.addPageLabel(1, PdfPageLabels.LOWERCASE_ROMAN_NUMERALS);
pageLabels.addPageLabel(5, PdfPageLabels.DECIMAL_ARABIC_NUMERALS);
pageLabels.addPageLabel(8, PdfPageLabels.DECIMAL_ARABIC_NUMERALS, "A-", 8);
writer.setPageLabels(pageLabels);
} catch (DocumentException | IOException de) {
System.err.println(de.getMessage());
// step 2:
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("PageLabels.pdf"));
// step 3:
writer.setViewerPreferences(PdfWriter.PageModeUseThumbs);
document.open();
// step 4:
// we add some content
for (int k = 1; k <= 10; ++k) {
document.add(new Paragraph("This document has the logical page numbers: i,ii,iii,iv,1,2,3,A-8,A-9,A-10\nReal page " + k));
document.newPage();
}
PdfPageLabels pdfPageLabels = new PdfPageLabels();
pdfPageLabels.addPageLabel(1, PdfPageLabels.LOWERCASE_ROMAN_NUMERALS);
pdfPageLabels.addPageLabel(5, PdfPageLabels.DECIMAL_ARABIC_NUMERALS);
pdfPageLabels.addPageLabel(8, PdfPageLabels.DECIMAL_ARABIC_NUMERALS, "A-", 8);
writer.setPageLabels(pdfPageLabels);

// step 5: we close the document
document.close();

// step 6: check if the labels are correct.
PdfReader reader = new PdfReader("PageLabels.pdf");
final String[] pageLabels = PdfPageLabels.getPageLabels(reader);
assertThat(pageLabels).isNotNull();
assertThat(Arrays.asList(pageLabels)).containsExactly("i", "ii", "iii", "iv", "1", "2", "3", "A-8", "A-9", "A-10");
final PdfPageLabels.PdfPageLabelFormat[] pageLabelFormats = PdfPageLabels.getPageLabelFormats(reader);
assertThat(pageLabelFormats).isNotNull();
assertThat(pageLabelFormats).hasSize(3);
}
}