Skip to content

Commit

Permalink
fix most of the stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Siedlerchr committed Jan 27, 2025
1 parent c380706 commit 62a9339
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 64 deletions.
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/logic/crawler/StudyYamlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;

import org.jabref.model.study.Study;
Expand All @@ -21,7 +22,7 @@ public class StudyYamlParser {
*/
public Study parseStudyYamlFile(Path studyYamlFile) throws IOException {
ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory());
try (InputStream fileInputStream = new FileInputStream(studyYamlFile.toFile())) {
try (InputStream fileInputStream = Files.newInputStream(studyYamlFile)) {
return yamlMapper.readValue(fileInputStream, Study.class);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public class AtomicFileOutputStream extends FilterOutputStream {
*/
public AtomicFileOutputStream(Path path, boolean keepBackup) throws IOException {
// Files.newOutputStream(getPathOfTemporaryFile(path)) leads to a "sun.nio.ch.ChannelOutputStream", which does not offer "lock"
this(path, getPathOfTemporaryFile(path), new FileOutputStream(getPathOfTemporaryFile(path).toFile()), keepBackup);
this(path, getPathOfTemporaryFile(path), Files.newOutputStream(getPathOfTemporaryFile(path)), keepBackup);
}

/**
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/org/jabref/logic/exporter/CffExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -18,6 +20,7 @@
import org.jabref.model.entry.AuthorList;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.Date;
import org.jabref.model.entry.ParsedEntryLink;
import org.jabref.model.entry.field.BiblatexSoftwareField;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.StandardField;
Expand Down Expand Up @@ -141,7 +144,7 @@ public void export(BibDatabaseContext databaseContext, Path file, List<BibEntry>
if (main.hasField(StandardField.RELATED)) {
main.getEntryLinkList(StandardField.RELATED, databaseContext.getDatabase())
.stream()
.map(link -> link.getLinkedEntry())
.map(ParsedEntryLink::getLinkedEntry)
.filter(Optional::isPresent)
.map(Optional::get)
.forEach(entry -> {
Expand All @@ -158,7 +161,7 @@ public void export(BibDatabaseContext databaseContext, Path file, List<BibEntry>
cffData.put("references", related);
}

try (FileWriter writer = new FileWriter(file.toFile(), StandardCharsets.UTF_8)) {
try (Writer writer = Files.newBufferedWriter(file, StandardCharsets.UTF_8)) {
yaml.dump(cffData, writer);
} catch (IOException ex) {
throw new SaveException(ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand All @@ -33,6 +31,7 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;

public class OpenDocumentSpreadsheetCreator extends Exporter {

Expand Down Expand Up @@ -83,15 +82,15 @@ private static void exportOpenDocumentSpreadsheet(Path file, BibDatabase databas
throws IOException {

// First store the xml formatted content to a temporary file.
File tmpFile = File.createTempFile("opendocument", null);
Path tmpFile = Files.createTempFile("opendocument", null);
OpenDocumentSpreadsheetCreator.exportOpenDocumentSpreadsheetXML(tmpFile, database, entries);

// Then add the content to the zip file:
try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(tmpFile))) {
try (InputStream in = Files.newInputStream(file)) {
OpenDocumentSpreadsheetCreator.storeOpenDocumentSpreadsheetFile(file, in);
}
// Delete the temporary file:
if (!tmpFile.delete()) {
if (!Files.deleteIfExists(tmpFile)) {
LOGGER.info("Cannot delete temporary export file");
}
}
Expand All @@ -106,11 +105,14 @@ public void export(final BibDatabaseContext databaseContext, final Path file,
}
}

private static void exportOpenDocumentSpreadsheetXML(File tmpFile, BibDatabase database, List<BibEntry> entries) {
private static void exportOpenDocumentSpreadsheetXML(Path tmpFile, BibDatabase database, List<BibEntry> entries) {
OpenDocumentRepresentation od = new OpenDocumentRepresentation(database, entries);
writeDomtoFile(tmpFile, od.getDOMrepresentation(), od);
}

try (Writer ps = new OutputStreamWriter(new FileOutputStream(tmpFile), StandardCharsets.UTF_8)) {
DOMSource source = new DOMSource(od.getDOMrepresentation());
static void writeDomtoFile(Path tmpFile, Document domRepresentation, OpenDocumentRepresentation od) {
try (Writer ps = Files.newBufferedWriter(tmpFile, StandardCharsets.UTF_8)) {
DOMSource source = new DOMSource(domRepresentation);
StreamResult result = new StreamResult(ps);
Transformer trans = TransformerFactory.newInstance().newTransformer();
trans.setOutputProperty(OutputKeys.INDENT, "yes");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package org.jabref.logic.exporter;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand Down Expand Up @@ -64,18 +59,16 @@ private static void storeOpenOfficeFile(Path file, InputStream source) throws Ex

private static void exportOpenOfficeCalc(Path file, BibDatabase database, List<BibEntry> entries) throws Exception {
// First store the xml formatted content to a temporary file.
File tmpFile = File.createTempFile("oocalc", null);
Path tmpFile = Files.createTempFile("oocalc", null);
OpenOfficeDocumentCreator.exportOpenOfficeCalcXML(tmpFile, database, entries);

// Then add the content to the zip file:
try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(tmpFile))) {
try (InputStream in = Files.newInputStream(file)) {
OpenOfficeDocumentCreator.storeOpenOfficeFile(file, in);
}

// Delete the temporary file:
if (!tmpFile.delete()) {
LOGGER.info("Cannot delete temporary export file");
}
Files.deleteIfExists(tmpFile);
}

@Override
Expand All @@ -88,10 +81,10 @@ public void export(final BibDatabaseContext databaseContext, final Path file,
}
}

private static void exportOpenOfficeCalcXML(File tmpFile, BibDatabase database, List<BibEntry> entries) {
private static void exportOpenOfficeCalcXML(Path tmpFile, BibDatabase database, List<BibEntry> entries) {
OOCalcDatabase od = new OOCalcDatabase(database, entries);

try (Writer ps = new OutputStreamWriter(new FileOutputStream(tmpFile), StandardCharsets.UTF_8)) {
try (Writer ps = Files.newBufferedWriter(tmpFile, StandardCharsets.UTF_8)) {
DOMSource source = new DOMSource(od.getDOMrepresentation());
StreamResult result = new StreamResult(ps);
Transformer trans = TransformerFactory.newInstance().newTransformer();
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/jabref/logic/net/ssl/SSLCertificate.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
Expand Down Expand Up @@ -87,11 +89,13 @@ public static Optional<SSLCertificate> fromPath(Path certPath) {
Objects.requireNonNull(certPath);
try {
CertificateFactory certificateFactory = CertificateFactory.getInstance("X509");
return fromX509((X509Certificate) certificateFactory.generateCertificate(new FileInputStream(certPath.toFile())));
return fromX509((X509Certificate) certificateFactory.generateCertificate(Files.newInputStream(certPath)));
} catch (CertificateException e) {
LOGGER.warn("Certificate doesn't follow X.509 format", e);
} catch (FileNotFoundException e) {
LOGGER.warn("Bad Certificate path: {}", certPath, e);
} catch (IOException e) {
LOGGER.warn("Error while reading certificate", e);
}
return Optional.empty();
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/logic/net/ssl/TrustStoreManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public TrustStoreManager(Path storePath) {
createTruststoreFileIfNotExist(storePath);
try {
store = KeyStore.getInstance(KeyStore.getDefaultType());
store.load(new FileInputStream(storePath.toFile()), STORE_PASSWORD.toCharArray());
store.load(Files.newInputStream(storePath), STORE_PASSWORD.toCharArray());
} catch (CertificateException | IOException | NoSuchAlgorithmException | KeyStoreException e) {
LOGGER.warn("Error while loading trust store from: {}", storePath.toAbsolutePath(), e);
}
Expand All @@ -56,7 +56,7 @@ public void addCertificate(String alias, Path certPath) {

try {
CertificateFactory certificateFactory = CertificateFactory.getInstance("X509");
store.setCertificateEntry(alias, certificateFactory.generateCertificate(new FileInputStream(certPath.toFile())));
store.setCertificateEntry(alias, certificateFactory.generateCertificate(Files.newInputStream(certPath)));
} catch (KeyStoreException | CertificateException | IOException e) {
LOGGER.warn("Error while adding a new certificate to the truststore: {}", alias, e);
}
Expand Down
Loading

0 comments on commit 62a9339

Please sign in to comment.