Page MenuHomec4science

XMLParserMaterial.java
No OneTemporary

File Metadata

Created
Thu, Jan 9, 13:00

XMLParserMaterial.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.FragmentEncoded;
import framework.reply.ReplyMaterial;
public class XMLParserMaterial extends XMLParser {
private static final Logger LOGGER = Logger.getLogger(XMLParserMaterial.class.getName());
public String replyToString(ReplyMaterial reply) throws IOException {
Element elemRoot = new Element(ParserNamespace.CNAME_AWR_MATERIAL);
Document doc = new Document(elemRoot);
Element elemPatient = new Element(ParserNamespace.CNAME_PATIENT);
elemPatient.setText(Integer.toString(reply.getPatientId()));
elemRoot.addContent(elemPatient);
// add markers
Element elemMarkerList = new Element(ParserNamespace.CNAME_MARKER_LIST);
elemMarkerList.setText(concatListEncoded(reply.getEncodedMarkers()));
elemRoot.addContent(elemMarkerList);
// add clinical factors
Element elemCFList = new Element(ParserNamespace.CNAME_CF_LIST);
elemCFList.setText(concatListEncoded(reply.getEncodedCFs()));
elemRoot.addContent(elemCFList);
// add HLAs
ArrayList<LinkedList<FragmentEncoded>> hlas = reply.getEncodedHLAs();
// HLA_A
Element elemHlaA = new Element(ParserNamespace.CNAME_HLA_A);
elemHlaA.setText(concatListEncoded(hlas.get(0)));
elemRoot.addContent(elemHlaA);
// HLA_B
Element elemHlaB = new Element(ParserNamespace.CNAME_HLA_B);
elemHlaB.setText(concatListEncoded(hlas.get(1)));
elemRoot.addContent(elemHlaB);
// HLA_C
Element elemHlaC = new Element(ParserNamespace.CNAME_HLA_C);
elemHlaC.setText(concatListEncoded(hlas.get(2)));
elemRoot.addContent(elemHlaC);
return documentToString(doc);
}
private String concatListEncoded(LinkedList<FragmentEncoded> encodedList) {
if (encodedList == null || encodedList.isEmpty()) {
return "";
}
LinkedList<String> list = new LinkedList<>();
for (FragmentEncoded encoded : encodedList) {
if (encoded == null) {
list.add("" + SEP_INT + "");
} else {
list.add(encoded.encodeBase64(SEP_INT));
}
}
return StringUtils.join(SEP_EXT, list);
}
public ReplyMaterial stringToReply(String replyString) {
ReplyMaterial reply = null;
try {
Document doc = stringToDocument(replyString);
Element elemRoot = doc.getRootElement();
Element elemPatient = elemRoot.getChild(ParserNamespace.CNAME_PATIENT);
Element elemMarkerList = elemRoot.getChild(ParserNamespace.CNAME_MARKER_LIST);
Element elemCFList = elemRoot.getChild(ParserNamespace.CNAME_CF_LIST);
Element elemHlaA = elemRoot.getChild(ParserNamespace.CNAME_HLA_A);
Element elemHlaB = elemRoot.getChild(ParserNamespace.CNAME_HLA_B);
Element elemHlaC = elemRoot.getChild(ParserNamespace.CNAME_HLA_C);
int patientId = Integer.valueOf(elemPatient.getText());
LinkedList<FragmentEncoded> markers = parseConcatList(elemMarkerList.getText());
LinkedList<FragmentEncoded> cfs = parseConcatList(elemCFList.getText());
ArrayList<LinkedList<FragmentEncoded>> hlas = new ArrayList<>();
hlas.add(parseConcatList(elemHlaA.getText()));
hlas.add(parseConcatList(elemHlaB.getText()));
hlas.add(parseConcatList(elemHlaC.getText()));
reply = new ReplyMaterial(patientId, markers, cfs, hlas);
} catch (JDOMException e) {
LOGGER.log(Level.SEVERE, null, e);
}
return reply;
}
private LinkedList<FragmentEncoded> parseConcatList(String val) {
if (val.equals("")) {
return new LinkedList<>();
}
LinkedList<FragmentEncoded> listEncoded = new LinkedList<>();
String[] fragEncodedStrings = val.split(SEP_EXT);
for (int i = 0; i < fragEncodedStrings.length; i++) {
String[] values = fragEncodedStrings[i].split(SEP_INT, -1);
String R = values[0];
String S = values[1];
if (!R.equals("") && !S.equals("")) {
listEncoded.add(new FragmentEncoded(values[0], values[1]));
} else {
listEncoded.add(null);
}
}
return listEncoded;
}
}

Event Timeline