package org.wookie.qti; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.TreeMap; import java.util.Vector; import org.apache.log4j.Logger; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.wookie.qti.util.DocumentBuilderHelper; import org.wookie.qti.util.PropertiesHelper; import org.wookie.qti.util.StatusHandler; import ee.tlu.htk.waramu.Waramu_Interface; public class XMLEngine { private HashMap assessmentData = new HashMap(); private Logger logger = Logger.getLogger(XMLEngine.class.getName()); public String generateAssessmentXML(ArrayList items, String assessmentTitle) { StringBuilder xmlString = new StringBuilder(); boolean itemsFound = false; xmlString.append(""); xmlString.append(""); Iterator itemIt = items.iterator(); while(itemIt.hasNext()) { Item curItem = itemIt.next(); xmlString.append(""); xmlString.append("", "").toString()+"]]>"); Iterator respIds = curItem.getResponseIdentifiers().iterator(); while(respIds.hasNext()) { String respId = respIds.next(); xmlString.append(""); } xmlString.append(""); itemsFound = true; } if(!itemsFound) { //if no items found, return statusCode 1 return new StatusHandler(1).getStatusXML(); } xmlString.append(""); xmlString.append(""); return xmlString.toString(); } public String generateAssessmentList(TreeMap testItems) { Collection testC = testItems.values(); Iterator testIt = testC.iterator(); String testList = "\n"; testList += ""; while(testIt.hasNext()) { Test tmpTestItem = testIt.next(); String attachmentId = tmpTestItem.getAttachmentId(); String resourceId = tmpTestItem.getResourceId(); String title = tmpTestItem.getTitle(); String desc = tmpTestItem.getDescription(); if(!attachmentId.trim().isEmpty() && !resourceId.trim().isEmpty()) { testList += "\n"; } } testList += ""; testList += "\n"; return testList; } /* parse widget response */ public ArrayList parseWidgetResponse(String xmlIn) { logger.debug("WidgetResponse XML: \n"+xmlIn); Document doc = new DocumentBuilderHelper().createDocument(xmlIn); // parse test node NodeList testNodes = doc.getElementsByTagName("test"); Element testNode = (Element) testNodes.item(0); this.assessmentData.put("attachmentId", testNode.getAttribute("id")); this.assessmentData.put("resourceKey", testNode.getAttribute("resourceKey")); //parse item nodes NodeList xmlItemNodes = doc.getElementsByTagName("item"); ArrayList itemsList = new ArrayList(); for(int i=0; i> responseVars = new HashMap>(); Element itemNode = (Element) xmlItemNodes.item(i); String guid = itemNode.getAttribute("guid"); String quesFilename = itemNode.getAttribute("identifier"); //parse response nodes NodeList responseNodes = itemNode.getElementsByTagName("response"); for(int j=0; j valuesVector = new Vector(); //parse value nodes inside response node NodeList values = responseNode.getElementsByTagName("value"); for(int l = 0; l(), quesFilename); newItem.setResponseVars(responseVars); //add created item to ArrayList itemsList.add(newItem); } return itemsList; } /** Create server response against widget response * @throws IOException */ public String generateServerResponse(ArrayList items) throws IOException { R2Q2_Interface R2Q2_Intf = new R2Q2_Interface(); String response = "\n"; float finalScore = 0; for(int i=0; i responseOut = new HashMap(); responseOut = R2Q2_Intf.doNextStage(tempItem.getGuid(), tempItem.getResponseVars()); if(!responseOut.containsKey("axisFault") && responseOut.containsKey("score") && responseOut.containsKey("feedback")) { response += "\n"; response += "\n"; response += "\n"; } if(responseOut.containsKey("axisFault") && responseOut.get("axisFault").toString() == "noGuid") { byte[] zip = null; String resourceKey = this.assessmentData.get("resourceKey"); String attachmentId = this.assessmentData.get("attachmentId"); if(!resourceKey.equalsIgnoreCase("zip")) { PropertiesHelper p = new PropertiesHelper(); Waramu_Interface waramuIntf = new Waramu_Interface(); waramuIntf.newSession(p.getWaramuUsername(), p.getWaramuPassword()); zip = waramuIntf.getAttachment(resourceKey, attachmentId); waramuIntf.closeSession(); } else { zip = R2Q2_Intf.getZipViaUrl(attachmentId); } //TODO reinit ZIP check, statusCode return String tempGuid = R2Q2_Intf.newSession(zip, tempItem.getId()+".xml"); R2Q2_Intf.doFirstStage(tempGuid); responseOut = R2Q2_Intf.doNextStage(tempGuid, tempItem.getResponseVars()); response += "\n"; response += "\n"; response += "\n"; } try { if(responseOut.containsKey("score")) { finalScore += Float.parseFloat(responseOut.get("score")); } } catch(NumberFormatException e) { //TODO something } } response += "\n"; response += ""; return response; } }