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

stream io error safety #530

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -56,29 +57,29 @@ public class EasyWorshipParser implements SongParser {
*/
@Override
public List<SongDisplayable> getSongs(File file, StatusPanel statusPanel) throws IOException {
List<SongDisplayable> ret = new ArrayList<>();
try {
Class.forName("com.googlecode.paradox.Driver");
LOGGER.log(Level.INFO, "Easyworship importer from {0}", file.getParent());
Connection conn = DriverManager.getConnection("jdbc:paradox:/" + file.getParent());
ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM \"Songs.DB\"");
while(rs.next()) {
String title = rs.getString("Title");
String author = rs.getString("Author");
if(author == null) {
author = "";
try (Connection conn = DriverManager.getConnection("jdbc:paradox:/" + file.getParent());
ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM \"Songs.DB\"")) {
List<SongDisplayable> ret = new ArrayList<>();
while (rs.next()) {
String title = rs.getString("Title");
String author = rs.getString("Author");
if (author == null) {
author = "";
}
String lyrics = rs.getString("Words");
String copyright = rs.getString("Copyright");
String num = rs.getString("Song Number");
ret.add(getSong(title, author, lyrics, copyright, num));
}
String lyrics = rs.getString("Words");
String copyright = rs.getString("Copyright");
String num = rs.getString("Song Number");
ret.add(getSong(title, author, lyrics, copyright, num));
return ret;
}

}
catch(ClassNotFoundException | SQLException ex) {
LOGGER.log(Level.INFO, "Couldn't import using SQL from " + file.getParent(), ex);
LOGGER.log(Level.INFO, ex, () -> "Couldn't import using SQL from " + file.getParent());
return Collections.emptyList();
}
return ret;
}

private SongDisplayable getSong(String title, String author, String songContent, String copyright, String ccli) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,55 +44,56 @@ public class EpicWorshipParser implements SongParser {

@Override
public List<SongDisplayable> getSongs(File location, StatusPanel statusPanel) throws IOException {
BufferedReader bfr = new BufferedReader(new InputStreamReader(new FileInputStream(location), Utils.getEncoding(location)));
ArrayList<SongDisplayable> ret = new ArrayList<>();
String fullContents = bfr.readLine();
String songsWhole = fullContents.substring(10, fullContents.length() - 2);
String[] songInfoList = songsWhole.split("\\},\\{");
for (String songInfo : songInfoList) {
songInfo = songInfo.replace("{", "").replace("{", "");
Matcher m1 = NAME.matcher(songInfo);
String name = "";
String author = "";
String lyrics = "";
if (m1.find()) {
name = m1.group(0);
name = format(name);
}
try(BufferedReader bfr = new BufferedReader(new InputStreamReader(new FileInputStream(location), Utils.getEncoding(location)))) {
ArrayList<SongDisplayable> ret = new ArrayList<>();
String fullContents = bfr.readLine();
String songsWhole = fullContents.substring(10, fullContents.length() - 2);
String[] songInfoList = songsWhole.split("\\},\\{");
for (String songInfo : songInfoList) {
songInfo = songInfo.replace("{", "").replace("{", "");
Matcher m1 = NAME.matcher(songInfo);
String name = "";
String author = "";
String lyrics = "";
if (m1.find()) {
name = m1.group(0);
name = format(name);
}

Matcher m2 = AUTHOR.matcher(songInfo);
if (m2.find()) {
author = m2.group(0);
author = format(author);
}
Matcher m2 = AUTHOR.matcher(songInfo);
if (m2.find()) {
author = m2.group(0);
author = format(author);
}

Matcher m3 = LYRICS.matcher(songInfo);
if (m3.find()) {
lyrics = m3.group(0);
lyrics = format(lyrics);
}
Matcher m3 = LYRICS.matcher(songInfo);
if (m3.find()) {
lyrics = m3.group(0);
lyrics = format(lyrics);
}

SongDisplayable s = new SongDisplayable(name, author);
SongDisplayable s = new SongDisplayable(name, author);

ArrayList<String> section = new ArrayList<>();
int sectionCount = 0;
String[] lines = lyrics.split("\\\\n");
for (String line : lines) {
if (!line.isEmpty()) {
section.add(line);
} else {
String[] sectionsArray = new String[section.size()];
s.addSection(sectionCount, new TextSection("", section.toArray(sectionsArray), section.toArray(sectionsArray), true));
sectionCount++;
section.clear();
ArrayList<String> section = new ArrayList<>();
int sectionCount = 0;
String[] lines = lyrics.split("\\\\n");
for (String line : lines) {
if (!line.isEmpty()) {
section.add(line);
} else {
String[] sectionsArray = new String[section.size()];
s.addSection(sectionCount, new TextSection("", section.toArray(sectionsArray), section.toArray(sectionsArray), true));
sectionCount++;
section.clear();
}
}
}
String[] sectionsArray = new String[section.size()];
s.addSection(sectionCount, new TextSection("", section.toArray(sectionsArray), section.toArray(sectionsArray), true));
String[] sectionsArray = new String[section.size()];
s.addSection(sectionCount, new TextSection("", section.toArray(sectionsArray), section.toArray(sectionsArray), true));

ret.add(s);
ret.add(s);
}
return ret;
}
return ret;
}

private String format(String s) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,14 @@ public class SongBeamerParser implements SongParser {
@Override
public List<SongDisplayable> getSongs(File file, StatusPanel statusPanel) throws IOException {
List<SongDisplayable> ret = new ArrayList<>();
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName("Cp1250")));
String fileLine;
StringBuilder rawLinesBuilder = new StringBuilder();
while ((fileLine = reader.readLine()) != null) {
rawLinesBuilder.append(fileLine).append("\n");
StringBuilder rawLinesBuilder;
try(BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName(
"Cp1250")))) {
String fileLine;
rawLinesBuilder = new StringBuilder();
while ((fileLine = reader.readLine()) != null) {
rawLinesBuilder.append(fileLine).append("\n");
}
}
String rawLines = rawLinesBuilder.toString();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import com.healthmarketscience.jackcess.Table;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
Expand Down Expand Up @@ -53,45 +53,46 @@ public class SongProParser implements SongParser {
@Override
public List<SongDisplayable> getSongs(File file, StatusPanel statusPanel) throws IOException {
List<SongDisplayable> ret = new ArrayList<>();
Database db = new DatabaseBuilder(file).setCharset(Charset.forName("ISO-8859-1")).setReadOnly(true).open();
Table table = db.getTable("Chorus");
int total = table.getRowCount();
int iter = 0;
for (Row r1 : table) {
statusPanel.setProgress((double) iter / total);
String title = r1.getString("Title");
String author = r1.getString("Author");
String copyright = r1.getString("Copyright");
String key = r1.getString("Key");
String comments = r1.getString("Comments");
try(Database db = new DatabaseBuilder(file).setCharset(StandardCharsets.ISO_8859_1).setReadOnly(true).open()) {
Table table = db.getTable("Chorus");
int total = table.getRowCount();
int iter = 0;
for (Row r1 : table) {
statusPanel.setProgress((double) iter / total);
String title = r1.getString("Title");
String author = r1.getString("Author");
String copyright = r1.getString("Copyright");
String key = r1.getString("Key");
String comments = r1.getString("Comments");
// String sequence = r1.getString("Sequence"); //TODO: For future use when we implement song sequences?

StringBuilder lyrics = new StringBuilder();
for (int i = 1; i <= 8; i++) {
String versesec = getSection("Verse " + i, r1.getString("Verse" + i));
if (versesec != null && !versesec.trim().isEmpty()) {
lyrics.append(versesec);
StringBuilder lyrics = new StringBuilder();
for (int i = 1; i <= 8; i++) {
String versesec = getSection("Verse " + i, r1.getString("Verse" + i));
if (versesec != null && !versesec.trim().isEmpty()) {
lyrics.append(versesec);
}
}
String chorussec = getSection("Chorus", r1.getString("Chorus"));
if (chorussec != null && !chorussec.trim().isEmpty()) {
lyrics.append(chorussec);
}
String bridgesec = getSection("Bridge", r1.getString("Bridge"));
if (bridgesec != null && !bridgesec.trim().isEmpty()) {
lyrics.append(bridgesec);
}
}
String chorussec = getSection("Chorus", r1.getString("Chorus"));
if (chorussec != null && !chorussec.trim().isEmpty()) {
lyrics.append(chorussec);
}
String bridgesec = getSection("Bridge", r1.getString("Bridge"));
if (bridgesec != null && !bridgesec.trim().isEmpty()) {
lyrics.append(bridgesec);
}

SongDisplayable song = new SongDisplayable(title, author);
song.setLyrics(lyrics.toString().trim());
song.setKey(key);
song.setInfo(comments);
song.setCopyright(copyright);
ret.add(song);
iter++;
SongDisplayable song = new SongDisplayable(title, author);
song.setLyrics(lyrics.toString().trim());
song.setKey(key);
song.setInfo(comments);
song.setCopyright(copyright);
ret.add(song);
iter++;
}
statusPanel.done();
return ret;
}
statusPanel.done();
return ret;
}

private String getSection(String secName, String sec) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,10 @@ public class WorshipHimParser implements SongParser {
@Override
public List<SongDisplayable> getSongs(File location, StatusPanel statusPanel) {
List<SongDisplayable> ret = new ArrayList<>();
try {
Database d = new DatabaseBuilder()
.setReadOnly(true)
.setFile(location)
.open();
try (Database d = new DatabaseBuilder()
.setReadOnly(true)
.setFile(location)
.open()){

Table table = d.getTable("Songs");
for (Row row : table) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
*/
package org.quelea.services.languages;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.*;
import java.util.Properties;
import java.util.regex.Pattern;

Expand All @@ -34,22 +32,30 @@
public class USEnglishConverter {

public static void main(String[] args) {
try {
System.out.println("Converting language file to American English...");
Properties gb = new Properties();
gb.load(new FileReader("languages/gb.lang"));
Properties usa = new Properties();
for(Object k : gb.keySet()) {
String key = (String) k;
usa.setProperty(key, translateToUS(gb.getProperty(key)));
}
usa.setProperty("LANGUAGENAME", "English (US)");
usa.store(new FileWriter("languages/us.lang"), "THIS IS AN AUTO GENERATED FILE, AND WILL BE OVERWRITTEN EACH TIME QUELEA IS REBUILT. DO NOT EDIT MANUALLY!");
System.out.println("Successfully converted language file to American English.");
}
catch(IOException ex) {
System.out.println("Converting language file to American English...");
Properties gb = new Properties();
try (Reader reader = new FileReader("languages/gb.lang")) {
gb.load(reader);
} catch (IOException ex) {
ex.printStackTrace();
return;
}


Properties usa = new Properties();
for (Object k : gb.keySet()) {
String key = (String) k;
usa.setProperty(key, translateToUS(gb.getProperty(key)));
}
usa.setProperty("LANGUAGENAME", "English (US)");
try (Writer writer = new FileWriter("languages/us.lang")) {
usa.store(writer, "THIS IS AN AUTO GENERATED FILE, AND WILL BE OVERWRITTEN EACH TIME QUELEA IS REBUILT. " +
"DO NOT EDIT MANUALLY!");
} catch (IOException e) {
e.printStackTrace();
return;
}
System.out.println("Successfully converted language file to American English.");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -74,8 +75,7 @@ public final void setDictionary(Dictionary dict) {
else {
words = new HashSet<>();
dictionaries.put(dict.getDictFile(), words);
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(dict.getDictFile()), "UTF-8"));
try(BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(dict.getDictFile()), StandardCharsets.UTF_8))) {
String line;
while((line = reader.readLine()) != null) {
line = sanitiseWord(line);
Expand Down Expand Up @@ -198,15 +198,14 @@ public boolean checkText(String text, boolean checkLastWord) {
*/
public void addWord(String word) {
word = sanitiseWord(word);
try {
if(!words.contains(word)) {
words.add(word);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dict.getDictFile(), true), "UTF-8"));
out.append(System.getProperty("line.separator") + word).close();
if(!words.contains(word)) {
words.add(word);
try(BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dict.getDictFile(), true), StandardCharsets.UTF_8))) {
out.append(System.getProperty("line.separator")).append(word).close();
}
catch(IOException ex) {
throw new RuntimeException("Couldn't add word to dictionary file", ex);
}
}
catch(IOException ex) {
throw new RuntimeException("Couldn't add word to dictionary file", ex);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.nio.file.Paths;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;

/**
* Registers the Quelea bundled fonts with the JVM.
Expand All @@ -43,8 +44,8 @@ public class FontInstaller {
*/
public void setupBundledFonts() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
try {
Files.list(Paths.get("icons", "bundledfonts")).forEach((Path path) -> {
try (Stream<Path> fontFiles = Files.list(Paths.get("icons", "bundledfonts"))){
fontFiles.forEach((Path path) -> {
File file = path.toFile();
if (file.getName().toLowerCase().endsWith("otf") || file.getName().toLowerCase().endsWith("ttf")) {
try {
Expand Down
Loading