From fbc1707b376b09e9a1f064c6fc5138d7b62a32f2 Mon Sep 17 00:00:00 2001 From: VladimirEliTokarev Date: Fri, 28 Oct 2016 20:02:27 +0300 Subject: [PATCH] Added the validation that the answers given are valid strings and pictures --- .../VotingBoothConfiguration.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/voting-booth-gui/src/main/java/meerkat/voting/gui/configuration/VotingBoothConfiguration.java b/voting-booth-gui/src/main/java/meerkat/voting/gui/configuration/VotingBoothConfiguration.java index 2e312e8..fde3d63 100644 --- a/voting-booth-gui/src/main/java/meerkat/voting/gui/configuration/VotingBoothConfiguration.java +++ b/voting-booth-gui/src/main/java/meerkat/voting/gui/configuration/VotingBoothConfiguration.java @@ -1,6 +1,12 @@ package meerkat.voting.gui.configuration; + +import com.google.protobuf.ByteString; +import javafx.embed.swing.SwingFXUtils; import meerkat.protobuf.BallotQuestionUIElementOuterClass; +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; + /** * Created by Vladimir Eliezer Tokarev on 10/1/2016. * This object contains all the questions and configurations needed for the voting-booth-gui work @@ -28,6 +34,24 @@ public class VotingBoothConfiguration { BallotQuestionUIElementOuterClass.ValueType.forNumber(NameSelectionQuestion.getAnswers().getAnswersType()) == null ){ return false; } + // Check that all the given answers are valid bte strings that can be converted into strings + for (ByteString answer: NameSelectionQuestion.getAnswers().getAnswers().getAnswersList()){ + try { + String strAnswer = answer.toStringUtf8(); + } catch (Exception e){ + return false; + } + } + // Check that all given byteStrings that are pictures can be converted into pictures + for (ByteString answer :NameSelectionByPictureQuestion.getAnswers().getAnswers().getAnswersList()){ + try { + BufferedImage bufferedImage = ImageIO.read(answer.newInput()); + SwingFXUtils.toFXImage(bufferedImage, null); + } catch (Exception e) { + return false; + } + } + return true; }