private StructureType assignStructureType(Element structure) throws VisualizerLoadException { String name = structure.getName().toLowerCase(); if (name.compareTo("graph") == 0) { if (structure.getAttributeValue("weighted").compareTo("true") == 0) return new Graph_Network("NETWORK"); else return new Graph_Network("GRAPH"); } else if (name.equals("array")) return new MD_Array(); else if (name.equals("no3darray")) return new No3darray(); else if (name.equals("linkedlist")) return new VisLinkedList(); else if (name.equals("no3dlinkedlist")) return new No3dLinkedList(); else if (name.equals("linkedlistnonull")) return new VisLinkedListNoNull(); else if (name.equals("bargraph")) return new BarScat("BAR"); else if (name.equals("scattergraph")) return new BarScat( "SCAT"); // not implemented in xml dtd, and does not have a load-from-xml method defined else if (name.equals("stack")) return new Stack(); else if (name.equals("queue")) return new Queue(); else if (name.equals("tree")) { if (structure.getChild("binary_node") != null) return new BinaryTree(); else return new GeneralTree(); } else if (name.equals("text")) return new TextStructure(); // if the XML element name is different from your structure's name, you can do something like // this: // else if( name.equalsIgnoreCase("node")) // return new Node(); else if (name.equals("legend")) return new LegendofColors(); else { // try dynamic typing try { return assignStructureType(name); } catch (Exception e) { throw new VisualizerLoadException( "Unable to instantiate class \"" + name + "\":\n" + e.getMessage()); } } }
private void load_snap_from_xml(Element snap, StructureCollection structs, LinkedList tempList) throws VisualizerLoadException { Iterator iterator = snap.getChildren().iterator(); if (!iterator.hasNext()) throw new VisualizerLoadException("Ran out of elements"); structs.loadTitle((Element) (iterator.next()), tempList, this); structs.calcDimsAndStartPts(tempList, this); if (!iterator.hasNext()) return; Element child = (Element) iterator.next(); if (child.getName().compareTo("doc_url") == 0) { // load the doc_url add_a_documentation_URL(child.getText().trim()); if (!iterator.hasNext()) return; child = (Element) iterator.next(); } if (child.getName().compareTo("pseudocode_url") == 0) { // load the psuedocode_url add_a_pseudocode_URL(child.getText().trim()); if (!iterator.hasNext()) return; child = (Element) iterator.next(); } if (child.getName().compareTo("audio_text") == 0) { // load the psuedocode_url add_audio_text(child.getText().trim()); if (!iterator.hasNext()) return; child = (Element) iterator.next(); } // create & load the individual structures, hooking them into the StructureCollection. // linespernode : calculate it at end of loadStructure call while (child.getName().compareTo("question_ref") != 0) { StructureType child_struct = assignStructureType(child); child_struct.loadStructure(child, tempList, this); structs.addChild(child_struct); if (!iterator.hasNext()) return; child = (Element) iterator.next(); } if (child.getName().compareTo("question_ref") == 0) { // set up question ref add_a_question_xml(child.getAttributeValue("ref")); } else // should never happen throw new VisualizerLoadException( "Expected question_ref element, but found " + child.getName()); if (iterator.hasNext()) { child = (Element) iterator.next(); throw new VisualizerLoadException( "Found " + child.getName() + " when expecting end of snap."); } } // load_snap_from_xml