Index: lams_common/src/java/org/lamsfoundation/lams/util/XMLUtil.java =================================================================== diff -u -r45f55e8dd35ae8841dbc8db3f38a41d9d52fd2ca -r6744ca131a85858435878b14bdb6abc0bc9002aa --- lams_common/src/java/org/lamsfoundation/lams/util/XMLUtil.java (.../XMLUtil.java) (revision 45f55e8dd35ae8841dbc8db3f38a41d9d52fd2ca) +++ lams_common/src/java/org/lamsfoundation/lams/util/XMLUtil.java (.../XMLUtil.java) (revision 6744ca131a85858435878b14bdb6abc0bc9002aa) @@ -1,5 +1,8 @@ package org.lamsfoundation.lams.util; +import java.security.InvalidParameterException; + +import org.apache.commons.lang.StringUtils; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; @@ -14,7 +17,14 @@ return defaultValue; } NodeList nodeList = parentElement.getElementsByTagName(childElementName); - return nodeList.getLength() == 0 ? defaultValue : ((Element) nodeList.item(0)).getTextContent(); + if (nodeList.getLength() == 0) { + return defaultValue; + } + if (nodeList.getLength() == 1) { + return nodeList.item(0).getTextContent(); + } + throw new InvalidParameterException( + "There is more than one element with name \"" + childElementName + "\" in its parent"); } /** @@ -23,8 +33,28 @@ */ public static String rewriteTextElement(Element sourceParent, Element targetParent, String sourceElementName, String targetElementName, String defaultValue) { + return XMLUtil.rewriteTextElement(sourceParent, targetParent, sourceElementName, targetElementName, + defaultValue, false); + } + + /** + * Find an element in source, takes its value and puts it under a different name in target. + * If no value is found, default value is used. + * The source source element can be removed, depending on the parameter. + */ + public static String rewriteTextElement(Element sourceParent, Element targetParent, String sourceElementName, + String targetElementName, String defaultValue, boolean removeSource) { String value = XMLUtil.getChildElementValue(sourceParent, sourceElementName, defaultValue); - if (value != null) { + if (removeSource) { + if (sourceParent == null || sourceElementName == null) { + throw new InvalidParameterException("Can not remove an element if its name or parent are null"); + } + NodeList nodeList = sourceParent.getElementsByTagName(sourceElementName); + if (nodeList.getLength() == 1) { + sourceParent.removeChild(nodeList.item(0)); + } + } + if (StringUtils.isNotBlank(value)) { Document document = targetParent.getOwnerDocument(); Element field = document.createElement(targetElementName); field.setTextContent(value); @@ -39,4 +69,20 @@ public static String addTextElement(Element targetParent, String targetElementName, String value) { return XMLUtil.rewriteTextElement(null, targetParent, null, targetElementName, value); } + + public static void removeElement(Element parentElement, String childElementName) { + if (parentElement == null || childElementName == null) { + throw new InvalidParameterException("Neither parent nor child name must not be null"); + } + NodeList nodeList = parentElement.getElementsByTagName(childElementName); + if (nodeList.getLength() == 0) { + return; + } + if (nodeList.getLength() == 1) { + parentElement.removeChild(nodeList.item(0)); + return; + } + throw new InvalidParameterException( + "There is more than one element with name \"" + childElementName + "\" in its parent"); + } } \ No newline at end of file Index: lams_tool_lamc/src/java/org/lamsfoundation/lams/tool/mc/service/McImportContentVersionFilter.java =================================================================== diff -u -r45f55e8dd35ae8841dbc8db3f38a41d9d52fd2ca -r6744ca131a85858435878b14bdb6abc0bc9002aa --- lams_tool_lamc/src/java/org/lamsfoundation/lams/tool/mc/service/McImportContentVersionFilter.java (.../McImportContentVersionFilter.java) (revision 45f55e8dd35ae8841dbc8db3f38a41d9d52fd2ca) +++ lams_tool_lamc/src/java/org/lamsfoundation/lams/tool/mc/service/McImportContentVersionFilter.java (.../McImportContentVersionFilter.java) (revision 6744ca131a85858435878b14bdb6abc0bc9002aa) @@ -103,10 +103,10 @@ // Question ID will be filled later as it requires QbService XMLUtil.addTextElement(qbQuestion, "version", "1"); XMLUtil.addTextElement(qbQuestion, "createDate", createDate); - XMLUtil.rewriteTextElement(mcQuestion, qbQuestion, "mark", "maxMark", "1"); - XMLUtil.rewriteTextElement(mcQuestion, qbQuestion, "feedback", "feedback", null); - String description = XMLUtil.rewriteTextElement(mcQuestion, qbQuestion, "question", "description", - null); + XMLUtil.rewriteTextElement(mcQuestion, qbQuestion, "mark", "maxMark", "1", true); + XMLUtil.rewriteTextElement(mcQuestion, qbQuestion, "feedback", "feedback", null, true); + String description = XMLUtil.rewriteTextElement(mcQuestion, qbQuestion, "question", "description", null, + true); // get name out of description as there were no descriptions in MCQ before if (description != null) { description = description.trim(); @@ -133,12 +133,13 @@ .valueOf(XMLUtil.getChildElementValue(mcOption, "correctOption", "false")); XMLUtil.addTextElement(qbOption, "maxMark", correctOption ? "1" : "0"); - XMLUtil.rewriteTextElement(mcOption, qbOption, "displayOrder", "displayOrder", "1"); - XMLUtil.rewriteTextElement(mcOption, qbOption, "mcQueOptionText", "name", null); + XMLUtil.rewriteTextElement(mcOption, qbOption, "displayOrder", "displayOrder", "1", true); + XMLUtil.rewriteTextElement(mcOption, qbOption, "mcQueOptionText", "name", null, true); } // get rid of junk mcQuestion.removeChild(mcOptions.item(0).getParentNode()); + XMLUtil.removeElement(mcQuestion, "questionHash"); } }); }