Skip to content

Commit

Permalink
Merge pull request #171 from SynBioDex/ArrayListDecodeFix
Browse files Browse the repository at this point in the history
Fix ArrayList decode error
  • Loading branch information
cjmyers authored Jun 4, 2021
2 parents 4f52aa3 + a2a7f8b commit 8af602e
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions SBOLCanvasBackend/src/utils/MxToSBOL.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,23 +119,17 @@ public void toSBOL1(InputStream graphStream, OutputStream outputStream) throws I
SBOLWriter.setKeepGoing(true);
SBOLWriter.write(document, outputStream, SBOLDocument.RDFV1);
}

@SuppressWarnings("unchecked")
public SBOLDocument setupDocument(InputStream graphStream) throws IOException, URISyntaxException, SBOLValidationException, TransformerFactoryConfigurationError, TransformerException, SynBioHubException {
// read in the mxGraph
mxGraph graph = parseGraph(graphStream);
mxGraphModel model = (mxGraphModel) graph.getModel();
mxCell cell0 = (mxCell) model.getCell("0");
ArrayList<Object> dataContainer = (ArrayList<Object>)cell0.getValue();
infoDict = (Hashtable<String, Info>) dataContainer.get(INFO_DICT_INDEX);
if(dataContainer.get(COMBINATORIAL_DICT_INDEX) instanceof ArrayList) {
for(CombinatorialInfo combInfo : (ArrayList<CombinatorialInfo>) dataContainer.get(COMBINATORIAL_DICT_INDEX)) {
combinatorialDict.put(combInfo.getFullURI(), combInfo);
}
}else {
combinatorialDict = (Hashtable<String, CombinatorialInfo>) dataContainer.get(COMBINATORIAL_DICT_INDEX);
}
interactionDict = (Hashtable<String, InteractionInfo>) dataContainer.get(INTERACTION_DICT_INDEX);
infoDict = loadDictionary(dataContainer, INFO_DICT_INDEX);
combinatorialDict = loadDictionary(dataContainer, COMBINATORIAL_DICT_INDEX);
interactionDict = loadDictionary(dataContainer, INTERACTION_DICT_INDEX);

// cells may show up in the child array not based on their x location
enforceChildOrdering(model, graph);
Expand Down Expand Up @@ -931,5 +925,35 @@ private mxGraph parseGraph(InputStream graphStream) throws IOException {
codec.decode(document.getDocumentElement(), graph.getModel());
return graph;
}

/**
* Dictionaries from the front end sometimes get decoded as array lists.
* This method ensures that we load them as hash tables.
* @param <T>
* @param dataContainer
* @param dictionaryIndex
*/
@SuppressWarnings("unchecked")
private <T extends Info> Hashtable<String, T> loadDictionary(ArrayList<Object> dataContainer, int dictionaryIndex) {
if(dataContainer.get(dictionaryIndex) instanceof ArrayList) {
// 90% sure it only happens when it's empty meaning that we could just return a empty hash table.
Hashtable<String, T> dict = new Hashtable<String, T>();
for(T item : (ArrayList<T>) dataContainer.get(dictionaryIndex)) {
// nasty instanceof as I couldn't convince the compiler that the abstract method is guaranteed to be implemented
if(item instanceof GlyphInfo) {
dict.put(((GlyphInfo) item).getFullURI(), item);
}else if(item instanceof ModuleInfo) {
dict.put(((ModuleInfo) item).getFullURI(), item);
}else if(item instanceof CombinatorialInfo) {
dict.put(((CombinatorialInfo) item).getFullURI(), item);
}else if(item instanceof InteractionInfo) {
dict.put(((InteractionInfo) item).getFullURI(), item);
}
}
return dict;
}else {
return (Hashtable<String, T>) dataContainer.get(dictionaryIndex);
}
}

}

0 comments on commit 8af602e

Please sign in to comment.