From 1bce2e1482fd7998af628255d251b953d3f52738 Mon Sep 17 00:00:00 2001 From: Jo Rabin Date: Fri, 25 Aug 2023 18:01:40 +0100 Subject: [PATCH] Tidy --- .../pwdb/kdbx/jackson/JacksonDatabase.java | 21 ++++----- .../pwdb/kdbx/jackson/JacksonGroup.java | 38 ++++++---------- .../jackson/JacksonSerializableDatabase.java | 43 ++++++++----------- 3 files changed, 41 insertions(+), 61 deletions(-) diff --git a/jackson/src/main/java/org/linguafranca/pwdb/kdbx/jackson/JacksonDatabase.java b/jackson/src/main/java/org/linguafranca/pwdb/kdbx/jackson/JacksonDatabase.java index 1c15344c..ef99e038 100644 --- a/jackson/src/main/java/org/linguafranca/pwdb/kdbx/jackson/JacksonDatabase.java +++ b/jackson/src/main/java/org/linguafranca/pwdb/kdbx/jackson/JacksonDatabase.java @@ -16,14 +16,6 @@ package org.linguafranca.pwdb.kdbx.jackson; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.Date; -import java.util.List; -import java.util.Objects; -import java.util.UUID; - import org.linguafranca.pwdb.Credentials; import org.linguafranca.pwdb.StreamConfiguration; import org.linguafranca.pwdb.StreamFormat; @@ -32,17 +24,22 @@ import org.linguafranca.pwdb.kdbx.KdbxStreamFormat; import org.linguafranca.pwdb.kdbx.jackson.model.KeePassFile; -import com.fasterxml.jackson.core.exc.StreamReadException; -import com.fasterxml.jackson.databind.DatabindException; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Date; +import java.util.List; +import java.util.Objects; +import java.util.UUID; -import static org.linguafranca.pwdb.kdbx.jackson.JacksonSerializableDatabase.createEmptyDatabase;; +import static org.linguafranca.pwdb.kdbx.jackson.JacksonSerializableDatabase.createEmptyDatabase; public class JacksonDatabase extends AbstractDatabase { KeePassFile keePassFile; StreamFormat streamFormat; - public JacksonDatabase() throws StreamReadException, DatabindException, IOException { + public JacksonDatabase() throws IOException { this(createEmptyDatabase(), null); } diff --git a/jackson/src/main/java/org/linguafranca/pwdb/kdbx/jackson/JacksonGroup.java b/jackson/src/main/java/org/linguafranca/pwdb/kdbx/jackson/JacksonGroup.java index 436d66d5..f71ffb3d 100644 --- a/jackson/src/main/java/org/linguafranca/pwdb/kdbx/jackson/JacksonGroup.java +++ b/jackson/src/main/java/org/linguafranca/pwdb/kdbx/jackson/JacksonGroup.java @@ -16,20 +16,6 @@ package org.linguafranca.pwdb.kdbx.jackson; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.UUID; - -import com.fasterxml.jackson.annotation.JsonAutoDetect; -import org.jetbrains.annotations.NotNull; -import org.linguafranca.pwdb.base.AbstractGroup; -import org.linguafranca.pwdb.kdbx.jackson.converter.StringToBooleanConverter; -import org.linguafranca.pwdb.kdbx.jackson.converter.UUIDToBase64Converter; -import org.linguafranca.pwdb.kdbx.jackson.converter.Base64ToUUIDConverter; -import org.linguafranca.pwdb.kdbx.jackson.converter.BooleanToStringConverter; -import org.linguafranca.pwdb.kdbx.jackson.model.Times; - import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonPropertyOrder; @@ -37,6 +23,18 @@ import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; +import org.jetbrains.annotations.NotNull; +import org.linguafranca.pwdb.base.AbstractGroup; +import org.linguafranca.pwdb.kdbx.jackson.converter.Base64ToUUIDConverter; +import org.linguafranca.pwdb.kdbx.jackson.converter.BooleanToStringConverter; +import org.linguafranca.pwdb.kdbx.jackson.converter.StringToBooleanConverter; +import org.linguafranca.pwdb.kdbx.jackson.converter.UUIDToBase64Converter; +import org.linguafranca.pwdb.kdbx.jackson.model.Times; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.UUID; @JsonPropertyOrder({ "uuid", @@ -165,11 +163,7 @@ public void setParent(JacksonGroup group) { @Override public List getGroups() { - List result = new ArrayList<>(); - for (JacksonGroup aGroup : groups) { - result.add(aGroup); - } - return result; + return new ArrayList<>(groups); } @Override @@ -208,11 +202,7 @@ public JacksonGroup removeGroup(JacksonGroup group) { @Override public List getEntries() { - List result = new ArrayList<>(); - for (JacksonEntry entry : this.entries) { - result.add(entry); - } - return result; + return new ArrayList<>(this.entries); } @Override diff --git a/jackson/src/main/java/org/linguafranca/pwdb/kdbx/jackson/JacksonSerializableDatabase.java b/jackson/src/main/java/org/linguafranca/pwdb/kdbx/jackson/JacksonSerializableDatabase.java index 2ecf4f58..5aedec57 100644 --- a/jackson/src/main/java/org/linguafranca/pwdb/kdbx/jackson/JacksonSerializableDatabase.java +++ b/jackson/src/main/java/org/linguafranca/pwdb/kdbx/jackson/JacksonSerializableDatabase.java @@ -15,18 +15,13 @@ */ package org.linguafranca.pwdb.kdbx.jackson; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; - -import javax.xml.stream.XMLOutputFactory; -import javax.xml.stream.XMLStreamWriter; - +import com.ctc.wstx.api.WstxInputProperties; +import com.ctc.wstx.api.WstxOutputProperties; import com.fasterxml.jackson.databind.MapperFeature; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.module.SimpleModule; +import com.fasterxml.jackson.dataformat.xml.XmlMapper; +import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator; import org.jetbrains.annotations.NotNull; import org.linguafranca.pwdb.Entry; import org.linguafranca.pwdb.SerializableDatabase; @@ -37,29 +32,27 @@ import org.linguafranca.pwdb.kdbx.jackson.model.KeePassFile; import org.linguafranca.pwdb.security.StreamEncryptor; -import com.ctc.wstx.api.WstxInputProperties; -import com.ctc.wstx.api.WstxOutputProperties; -import com.fasterxml.jackson.core.exc.StreamReadException; -import com.fasterxml.jackson.databind.DatabindException; -import com.fasterxml.jackson.databind.SerializationFeature; -import com.fasterxml.jackson.databind.module.SimpleModule; -import com.fasterxml.jackson.dataformat.xml.XmlMapper; -import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator; - -import static org.linguafranca.pwdb.Entry.STANDARD_PROPERTY_NAME_TITLE; +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; public class JacksonSerializableDatabase implements SerializableDatabase { public KeePassFile keePassFile; private StreamEncryptor encryptor; - public static KeePassFile createEmptyDatabase() throws StreamReadException, DatabindException, IOException { + public static KeePassFile createEmptyDatabase() throws IOException { InputStream inputStream = JacksonSerializableDatabase.class.getClassLoader() .getResourceAsStream("base.kdbx.xml"); XmlMapper mapper = new XmlMapper(); - KeePassFile res = mapper.readValue(inputStream, KeePassFile.class); - return res; + return mapper.readValue(inputStream, KeePassFile.class); } @@ -82,7 +75,7 @@ public JacksonSerializableDatabase load(InputStream inputStream) throws IOExcept @Override - public void save(OutputStream outputStream) throws IOException { + public void save(OutputStream outputStream) { prepareForSave(keePassFile.root.group); try {