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 exception for unlinked files #6567

Merged
merged 5 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,15 @@ private List<Path> getFileListFromNode(CheckBoxTreeItem<FileNodeWrapper> node) {
List<Path> filesList = new ArrayList<>();
for (TreeItem<FileNodeWrapper> childNode : node.getChildren()) {
CheckBoxTreeItem<FileNodeWrapper> child = (CheckBoxTreeItem<FileNodeWrapper>) childNode;
if (child.isLeaf() && child.isSelected()) {
Path nodeFile = child.getValue().path;
if ((nodeFile != null) && Files.isRegularFile(nodeFile)) {
filesList.add(nodeFile);
if (child.isLeaf()) {
if (child.isSelected()) {
Path nodeFile = child.getValue().path;
if ((nodeFile != null) && Files.isRegularFile(nodeFile)) {
filesList.add(nodeFile);
}
}
} else {
filesList.addAll(getFileListFromNode(child));
}
}
return filesList;
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/jabref/logic/xmp/XmpUtilReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static List<BibEntry> readXmp(Path path, XmpPreferences xmpPreferences)
* <p/>
*
*
* @return empty Optional if no metadata has been found
* @return empty List if no metadata has been found, or cannot properly find start or end tag in metadata
*/
private static List<XMPMetadata> getXmpMetadata(PDDocument document) throws IOException {
PDDocumentCatalog catalog = document.getDocumentCatalog();
Expand All @@ -120,6 +120,10 @@ private static List<XMPMetadata> getXmpMetadata(PDDocument document) throws IOEx
int startDescriptionSection = xmp.indexOf(START_TAG);
int endDescriptionSection = xmp.lastIndexOf(END_TAG) + END_TAG.length();

if (startDescriptionSection < 0 || startDescriptionSection > endDescriptionSection || endDescriptionSection == END_TAG.length() - 1) {
return metaList;
}

// XML header for the xmpDomParser
String start = xmp.substring(0, startDescriptionSection);
// descriptionArray - mid part of the textual metadata
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/jabref/logic/xmp/XmpUtilReaderTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.jabref.logic.xmp;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
Expand All @@ -17,6 +19,7 @@
import org.jabref.model.util.DummyFileUpdateMonitor;

import com.google.common.io.Resources;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.xmpbox.XMPMetadata;
import org.apache.xmpbox.schema.DublinCoreSchema;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -109,4 +112,16 @@ void testReadPDMetadata() throws IOException, URISyntaxException, ParseException

assertEquals(entryFromBibFile.get(), entries.get(0));
}

/**
* Tests an pdf file with metadata which has no description section.
*/
@Test
void testGetXmpMetadataWithNoDescription() throws IOException, URISyntaxException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Method method = XmpUtilReader.class.getDeclaredMethod("getXmpMetadata", PDDocument.class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need reflection, the method getMetadata is implicitly caleld, you you can simple do this two lines:

   List<BibEntry> entries = XmpUtilReader.readXmp(Path.of(XmpUtilShared.class.getResource("no_description_metadata.pdf").toURI()), xmpPreferences);
        assertEquals(Collections.emptyList(), entries);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Thanks.

method.setAccessible(true);
List<XMPMetadata> entries = (List<XMPMetadata>) method.invoke(XmpUtilReader.class, PDDocument.load(XmpUtilShared.class.getResource("no_description_metadata.pdf").openStream()));
method.setAccessible(false);
assertEquals(Collections.emptyList(), entries);
}
}
Binary file not shown.