Page MenuHomec4science

XMLParserDecrypt.java
No OneTemporary

File Metadata

Created
Thu, Jan 9, 16:21

XMLParserDecrypt.java

package io.serializer.xml;
import io.serializer.ParserNamespace;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import utils.StringUtils;
import crypto.elgamal.fragment.FragmentPartialEncoded;
import framework.reply.ReplyDecryption;
import framework.test.TestResultPartialEncoded;
public class XMLParserDecrypt extends XMLParser {
private static final Logger LOGGER = Logger.getLogger(XMLParserDecrypt.class.getName());
public String replyToString(ReplyDecryption reply) throws IOException {
Element elemRoot = new Element(ParserNamespace.CNAME_AWR_DECRYPT);
Document doc = new Document(elemRoot);
Element elemPatient = new Element(ParserNamespace.CNAME_PATIENT);
elemPatient.setText(Integer.toString(reply.getPatientId()));
elemRoot.addContent(elemPatient);
Element elemTimestamp = new Element(ParserNamespace.CNAME_TIMESTAMP);
elemTimestamp.setText(Long.toString(reply.getTimestamp()));
elemRoot.addContent(elemTimestamp);
Element elemRequestValue = new Element(ParserNamespace.CNAME_AWR_DECRYPT_VAL);
elemRequestValue.setText(convertReplyToBase64(reply));
elemRoot.addContent(elemRequestValue);
return documentToString(doc);
}
public ReplyDecryption stringToReply(String replyString) {
ReplyDecryption reply = null;
try {
Document doc = stringToDocument(replyString);
Element elemRoot = doc.getRootElement();
Element elemPatient = elemRoot.getChild(ParserNamespace.CNAME_PATIENT);
Element elemTimestamp = elemRoot.getChild(ParserNamespace.CNAME_TIMESTAMP);
Element elemReplyValue = elemRoot.getChild(ParserNamespace.CNAME_AWR_DECRYPT_VAL);
int patientId = Integer.valueOf(elemPatient.getText());
long timestamp = Long.valueOf(elemTimestamp.getText());
reply = convertBase64ToReply(elemReplyValue.getText(), patientId, timestamp);
} catch (JDOMException e) {
LOGGER.log(Level.SEVERE, null, e);
}
return reply;
}
private String convertReplyToBase64(ReplyDecryption reply) {
String results = partialResultsToString(reply.getPartialEncResults());
String hlas = partialHLAsToString(reply.getPartialEncHLAs());
return results + SEP_EXT + hlas;
}
private String partialHLAsToString(
ArrayList<LinkedList<FragmentPartialEncoded>> partialEncHLAs) {
if (partialEncHLAs == null) {
return "";
}
LinkedList<String> partialHLAs = new LinkedList<>();
partialHLAs.add(partialHLAListToString(partialEncHLAs.get(0)));
partialHLAs.add(partialHLAListToString(partialEncHLAs.get(1)));
partialHLAs.add(partialHLAListToString(partialEncHLAs.get(2)));
return StringUtils.join(SEP_EXT, partialHLAs);
}
private String partialHLAListToString(LinkedList<FragmentPartialEncoded> hlaList) {
if (hlaList == null) {
return "";
}
LinkedList<String> partialHLA = new LinkedList<>();
for (FragmentPartialEncoded hla : hlaList) {
partialHLA.add(hla.encodeBase64(SEP_INT));
}
return StringUtils.join(SEP_INT, partialHLA);
}
private String partialResultsToString(
LinkedList<TestResultPartialEncoded> partialEncResults) {
if (partialEncResults == null) {
return "";
}
LinkedList<String> partialResults = new LinkedList<>();
for (TestResultPartialEncoded test : partialEncResults) {
if (test != null) {
FragmentPartialEncoded partialGenomic = test.getGenomic();
FragmentPartialEncoded partialClinical = test.getClinical();
partialResults.add(partialGenomic.encodeBase64(SEP_INT));
if (partialClinical != null) {
partialResults.add(partialClinical.encodeBase64(SEP_INT));
} else {
for (int i = 0; i < 4; i++) {
partialResults.add("");
}
}
} else {
for (int i = 0; i < 8; i++) {
partialResults.add("");
}
}
}
return StringUtils.join(SEP_INT, partialResults);
}
private ReplyDecryption convertBase64ToReply(String awrVal, int patientId, long timestamp) {
String[] parts = awrVal.split(SEP_EXT, -1);
String[] resultsParts = parts[0].split(SEP_INT, -1);
String[] hlaAParts = parts[1].split(SEP_INT, -1);
String[] hlaBParts = parts[2].split(SEP_INT, -1);
String[] hlaCParts = parts[3].split(SEP_INT, -1);
LinkedList<TestResultPartialEncoded> results = parsePartialResultsString(resultsParts);
ArrayList<LinkedList<FragmentPartialEncoded>> hlas = new ArrayList<>(3);
hlas.add(parsePartialHLAString(hlaAParts));
hlas.add(parsePartialHLAString(hlaBParts));
hlas.add(parsePartialHLAString(hlaCParts));
ReplyDecryption reply = new ReplyDecryption(patientId, results, hlas);
reply.setTimestamp(timestamp);
return reply;
}
private LinkedList<FragmentPartialEncoded> parsePartialHLAString(String[] parts) {
LinkedList<FragmentPartialEncoded> hlas = new LinkedList<>();
for (int i = 0; i < parts.length; i+=4) {
String partNumberStr = parts[i];
String R = parts[i+1];
String S = parts[i+2];
String T = parts[i+3];
if (!partNumberStr.equals("") && !R.equals("") && !S.equals("") && !T.equals("")) {
int partNumber= Integer.valueOf(parts[i]);
hlas.add(new FragmentPartialEncoded(R, S, T, partNumber));
}
}
return hlas;
}
private LinkedList<TestResultPartialEncoded> parsePartialResultsString(String[] parts) {
LinkedList<TestResultPartialEncoded> results = new LinkedList<>();
for (int i = 0; i < parts.length; i+=8) {
String genomicPartNumberStr = parts[i];
String genomicR = parts[i+1];
String genomicS = parts[i+2];
String genomicT = parts[i+3];
String clinicalPartNumberStr = parts[i+4];
String clinicalR = parts[i+5];
String clinicalS = parts[i+6];
String clinicalT = parts[i+7];
FragmentPartialEncoded genomic = null;
FragmentPartialEncoded clinical = null;
if (!genomicPartNumberStr.equals("") && !genomicR.equals("") && !genomicS.equals("") && !genomicT.equals("")) {
int genomicPartNumber = Integer.valueOf(parts[i]);
genomic = new FragmentPartialEncoded(genomicR, genomicS, genomicT, genomicPartNumber);
}
if (!clinicalPartNumberStr.equals("") && !clinicalR.equals("") && !clinicalS.equals("") && !clinicalT.equals("")) {
int clinicalPartNumber = Integer.valueOf(parts[i]);
clinical = new FragmentPartialEncoded(clinicalR, clinicalS, clinicalT, clinicalPartNumber);
}
if (genomic != null || clinical != null) {
results.add(new TestResultPartialEncoded(genomic, clinical));
} else {
results.add(null);
}
}
return results;
}
}

Event Timeline