Skip to content

Commit

Permalink
Modernizer improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-kolb committed Jul 20, 2017
1 parent 78f8779 commit 53589c2
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -67,11 +68,12 @@ public Object getTransferData(DataFlavor someFlavor)

else if (someFlavor.equals(DataFlavor.getTextPlainUnicodeFlavor())) {

String charset = TransferableEntrySelection.FLAVOR_EXTERNAL.getParameter("charset");
if (charset == null) {
charset = "";
String charsetName = TransferableEntrySelection.FLAVOR_EXTERNAL.getParameter("charset");
if (charsetName == null) {
charsetName = "";
}
return new ByteArrayInputStream(s.getBytes(charset.trim()));
Charset charset = Charset.forName(charsetName.trim());
return new ByteArrayInputStream(s.getBytes(charset));
}

//The text/plain DataFormat of javafx uses the String.class directly as representative class and no longer an InputStream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
public final class TitleParser {

private StringBuffer buffer;
private StringBuilder buffer;
private int wordStart;

public List<Word> parse(String title) {
Expand Down Expand Up @@ -55,7 +55,7 @@ private Optional<Word> createWord(boolean[] isProtected) {

private void reset() {
wordStart = -1;
buffer = new StringBuffer();
buffer = new StringBuilder();
}

private static boolean[] determineProtectedChars(String title) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ private BibtexString parseString() throws IOException {

private String parsePreamble() throws IOException {
skipWhitespace();
return parseBracketedText().toString();
return parseBracketedText();

}

Expand Down Expand Up @@ -802,8 +802,8 @@ private String parseKey() throws IOException {

}

private StringBuffer parseBracketedText() throws IOException {
StringBuffer value = new StringBuffer();
private String parseBracketedText() throws IOException {
StringBuilder value = new StringBuilder();

consume('{', '(');

Expand Down Expand Up @@ -842,7 +842,7 @@ private StringBuffer parseBracketedText() throws IOException {

consume('}', ')');

return value;
return value.toString();
}

private boolean isClosingBracketNext() {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/jabref/logic/importer/util/OAI2Handler.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ public class OAI2Handler extends DefaultHandler {

private final BibEntry entry;

private StringBuffer authors;
private StringBuilder authors;

private String keyname;

private String forenames;

private StringBuffer characters;
private StringBuilder characters;


public OAI2Handler(BibEntry be) {
Expand All @@ -35,7 +35,7 @@ public OAI2Handler(BibEntry be) {

@Override
public void startDocument() throws SAXException {
authors = new StringBuffer();
authors = new StringBuilder();
}

@Override
Expand All @@ -47,7 +47,7 @@ public void characters(char[] ch, int start, int length) throws SAXException {
public void startElement(String uri, String localName, String qualifiedName,
Attributes attributes) throws SAXException {

characters = new StringBuffer();
characters = new StringBuilder();
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/logic/xmp/XMPSchemaBibtex.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,13 @@ public void addSequenceDateValue(String field, Calendar date) {
private static String getContents(NodeList seqList) {

Element seqNode = (Element) seqList.item(0);
StringBuffer seq = null;
StringBuilder seq = null;

NodeList items = seqNode.getElementsByTagName("rdf:li");
for (int j = 0; j < items.getLength(); j++) {
Element li = (Element) items.item(j);
if (seq == null) {
seq = new StringBuffer();
seq = new StringBuilder();
} else {
seq.append(" and ");
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/jabref/shared/security/Password.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jabref.shared.security;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
Expand Down Expand Up @@ -41,7 +42,7 @@ public Password(char[] phrase, String key) throws NoSuchAlgorithmException, NoSu
*/
public String encrypt() throws GeneralSecurityException, UnsupportedEncodingException {
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);
return new String(Base64.getEncoder().encode(cipher.doFinal(phrase)), "UTF-8");
return new String(Base64.getEncoder().encode(cipher.doFinal(phrase)), StandardCharsets.UTF_8);
}

/**
Expand All @@ -51,7 +52,7 @@ public String encrypt() throws GeneralSecurityException, UnsupportedEncodingExce
*/
public String decrypt() throws GeneralSecurityException, UnsupportedEncodingException {
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivSpec);
return new String(cipher.doFinal(Base64.getDecoder().decode(phrase)), "UTF-8");
return new String(cipher.doFinal(Base64.getDecoder().decode(phrase)), StandardCharsets.UTF_8);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void writeWithNullPreferencesThrowsException() throws Exception {

@Test
public void writeEncoding() throws Exception {
SavePreferences preferences = new SavePreferences().withEncoding(Charsets.US_ASCII);
SavePreferences preferences = new SavePreferences().withEncoding(StandardCharsets.US_ASCII);

StringSaveSession session = databaseWriter.savePartOfDatabase(bibtexContext, Collections.emptyList(), preferences);

Expand All @@ -98,7 +98,7 @@ public void writePreamble() throws Exception {

@Test
public void writePreambleAndEncoding() throws Exception {
SavePreferences preferences = new SavePreferences().withEncoding(Charsets.US_ASCII);
SavePreferences preferences = new SavePreferences().withEncoding(StandardCharsets.US_ASCII);
database.setPreamble("Test preamble");

StringSaveSession session = databaseWriter.savePartOfDatabase(bibtexContext, Collections.emptyList(), preferences);
Expand All @@ -123,7 +123,7 @@ public void writeEntry() throws Exception {

@Test
public void writeEncodingAndEntry() throws Exception {
SavePreferences preferences = new SavePreferences().withEncoding(Charsets.US_ASCII);
SavePreferences preferences = new SavePreferences().withEncoding(StandardCharsets.US_ASCII);
BibEntry entry = new BibEntry();
entry.setType(BibtexEntryTypes.ARTICLE);
database.insertEntry(entry);
Expand All @@ -148,7 +148,7 @@ public void writeEpilogue() throws Exception {

@Test
public void writeEpilogueAndEncoding() throws Exception {
SavePreferences preferences = new SavePreferences().withEncoding(Charsets.US_ASCII);
SavePreferences preferences = new SavePreferences().withEncoding(StandardCharsets.US_ASCII);
database.setEpilog("Test epilog");

StringSaveSession session = databaseWriter.savePartOfDatabase(bibtexContext, Collections.emptyList(), preferences);
Expand All @@ -171,7 +171,7 @@ public void writeMetadata() throws Exception {

@Test
public void writeMetadataAndEncoding() throws Exception {
SavePreferences preferences = new SavePreferences().withEncoding(Charsets.US_ASCII);
SavePreferences preferences = new SavePreferences().withEncoding(StandardCharsets.US_ASCII);
DatabaseBibtexKeyPattern bibtexKeyPattern = new DatabaseBibtexKeyPattern(mock(GlobalBibtexKeyPattern.class));
bibtexKeyPattern.setDefaultValue("test");
metaData.setCiteKeyPattern(bibtexKeyPattern);
Expand Down Expand Up @@ -202,7 +202,7 @@ public void writeGroups() throws Exception {

@Test
public void writeGroupsAndEncoding() throws Exception {
SavePreferences preferences = new SavePreferences().withEncoding(Charsets.US_ASCII);
SavePreferences preferences = new SavePreferences().withEncoding(StandardCharsets.US_ASCII);

GroupTreeNode groupRoot = GroupTreeNode.fromGroup(new AllEntriesGroup(""));
groupRoot.addChild(GroupTreeNode.fromGroup(new ExplicitGroup("test", GroupHierarchyType.INCLUDING, ',')));
Expand Down Expand Up @@ -232,7 +232,7 @@ public void writeString() throws Exception {

@Test
public void writeStringAndEncoding() throws Exception {
SavePreferences preferences = new SavePreferences().withEncoding(Charsets.US_ASCII);
SavePreferences preferences = new SavePreferences().withEncoding(StandardCharsets.US_ASCII);
database.addString(new BibtexString("name", "content"));

StringSaveSession session = databaseWriter.savePartOfDatabase(bibtexContext, Collections.emptyList(), preferences);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.HashMap;
Expand All @@ -12,7 +13,6 @@
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;

import com.google.common.base.Charsets;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
Expand Down Expand Up @@ -41,7 +41,7 @@ public void setUp() {
exportFormat = ExportFormats.getExportFormat("oocsv");

databaseContext = new BibDatabaseContext();
charset = Charsets.UTF_8;
charset = StandardCharsets.UTF_8;
}

@After
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/jabref/logic/exporter/ExportFormatTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -14,7 +15,6 @@
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;

import com.google.common.base.Charsets;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -46,7 +46,7 @@ public ExportFormatTest(IExportFormat format, String name) {
@Before
public void setUp() {
databaseContext = new BibDatabaseContext();
charset = Charsets.UTF_8;
charset = StandardCharsets.UTF_8;
entries = Collections.emptyList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.HashMap;
Expand All @@ -12,7 +13,6 @@
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;

import com.google.common.base.Charsets;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
Expand Down Expand Up @@ -42,7 +42,7 @@ public void setUp() {
exportFormat = ExportFormats.getExportFormat("html");

databaseContext = new BibDatabaseContext();
charset = Charsets.UTF_8;
charset = StandardCharsets.UTF_8;
BibEntry entry = new BibEntry();
entry.setField("title", "my paper title");
entry.setField("author", "Stefan Kolb");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Collections;
import java.util.List;

import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;

import com.google.common.base.Charsets;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -32,7 +32,7 @@ public class MsBibExportFormatTest {
@Before
public void setUp() throws Exception {
databaseContext = new BibDatabaseContext();
charset = Charsets.UTF_8;
charset = StandardCharsets.UTF_8;
msBibExportFormat = new MSBibExportFormat();
tempFile = testFolder.newFile();
}
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/jabref/logic/remote/RemoteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

import org.jabref.logic.remote.client.RemoteListenerClient;
import org.jabref.logic.remote.server.RemoteListenerServerLifecycle;
Expand Down Expand Up @@ -91,7 +92,7 @@ public void testClientConnectingToWrongServer() throws IOException, InterruptedE
@Override
public void run() {
try (Socket socket2 = socket.accept(); OutputStream os = socket2.getOutputStream()) {
os.write("whatever".getBytes("UTF8"));
os.write("whatever".getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
// Ignored
}
Expand Down

0 comments on commit 53589c2

Please sign in to comment.