Example usage for org.dom4j Element remove

List of usage examples for org.dom4j Element remove

Introduction

In this page you can find the example usage for org.dom4j Element remove.

Prototype

boolean remove(Text text);

Source Link

Document

Removes the given Text if the node is an immediate child of this element.

Usage

From source file:org.olat.ims.qti.editor.beecom.objects.ChoiceQuestion.java

License:Apache License

/**
 * Build resprocessing for multiple choice question with multiple correct answers. Sets correct score for positive (mastery) and negative (fail) conditions and use
 * mastery feedback when all mastery responses have been selected.
 * //w w  w  . ja v  a2s  . c om
 * @param resprocessingXML
 */
private void buildRespconditionMCMulti_mastery(final Element resprocessingXML) {
    for (final Iterator i = getResponses().iterator(); i.hasNext();) {
        final ChoiceResponse tmpChoice = (ChoiceResponse) i.next();
        final float points = tmpChoice.getPoints();
        if (points == 0) {
            continue;
        }

        final Element respcondition_correct = resprocessingXML.addElement("respcondition");
        respcondition_correct.addAttribute("continue", "Yes");
        if (points > 0) {
            respcondition_correct.addAttribute("title", "Mastery");
        } else {
            respcondition_correct.addAttribute("title", "Fail");
        }

        final Element varequal = respcondition_correct.addElement("conditionvar").addElement("varequal");
        varequal.addAttribute("respident", getIdent());
        varequal.addAttribute("case", "Yes");
        varequal.addText(tmpChoice.getIdent());

        final Element setvar = respcondition_correct.addElement("setvar");
        setvar.addAttribute("varname", "SCORE");
        setvar.addAttribute("action", "Add");
        setvar.addText("" + points);

    } // for loop

    // Resp condition for feedback mastery:
    // all response with points>0 must be selected
    final Element respcondition_correct = resprocessingXML.addElement("respcondition");
    respcondition_correct.addAttribute("title", "Mastery");
    respcondition_correct.addAttribute("continue", "Yes");
    final Element conditionvar = respcondition_correct.addElement("conditionvar");
    final Element and = conditionvar.addElement("and");
    final Element not = conditionvar.addElement("not");
    final Element or = not.addElement("or");

    for (final Iterator i = getResponses().iterator(); i.hasNext();) {
        final ChoiceResponse tmpChoice = (ChoiceResponse) i.next();
        Element varequal;
        if (tmpChoice.getPoints() > 0) {
            varequal = and.addElement("varequal");
        } else { // incorrect answers
            varequal = or.addElement("varequal");
        }
        varequal.addAttribute("respident", getIdent());
        varequal.addAttribute("case", "Yes");
        varequal.addText(tmpChoice.getIdent());
    } // for loop

    // Use mastery feedback
    QTIEditHelper.addFeedbackMastery(respcondition_correct);

    // remove whole respcondition if empty
    if (or.element("varequal") == null && and.element("varequal") == null) {
        resprocessingXML.remove(respcondition_correct);
    } else {
        // remove any unset <and> and <not> nodes
        if (and.element("varequal") == null) {
            conditionvar.remove(and);
        }
        if (or.element("varequal") == null) {
            conditionvar.remove(not);
        }
    }
}

From source file:org.olat.ims.qti.editor.beecom.objects.ChoiceQuestion.java

License:Apache License

/**
 * Build fail resprocessing: Adjust score to 0 (if multiple correct mode) and set hints, solutions and fail feedback
 * //from   w w w. j av a 2 s.  c om
 * @param resprocessingXML
 * @param isSingleCorrect
 */
private void buildRespcondition_fail(final Element resprocessingXML, final boolean isSingleCorrect) {
    // build
    final Element respcondition_fail = resprocessingXML.addElement("respcondition");
    respcondition_fail.addAttribute("title", "Fail");
    respcondition_fail.addAttribute("continue", "Yes");
    final Element conditionvar = respcondition_fail.addElement("conditionvar");
    final Element or = conditionvar.addElement("or");

    for (final Iterator i = getResponses().iterator(); i.hasNext();) {
        final ChoiceResponse tmpChoice = (ChoiceResponse) i.next();
        Element varequal;
        // Add this response to the fail case
        // if single correct type and not correct
        // or multi correct and points negative or 0
        if ((isSingleCorrect && !tmpChoice.isCorrect()) || (!isSingleCorrect && tmpChoice.getPoints() <= 0)) {
            varequal = or.addElement("varequal");
            varequal.addAttribute("respident", getIdent());
            varequal.addAttribute("case", "Yes");
            varequal.addText(tmpChoice.getIdent());
        }
    } // for loop

    if (isSingleCorrect) {
        final Element setvar = respcondition_fail.addElement("setvar");
        setvar.addAttribute("varname", "SCORE");
        setvar.addAttribute("action", "Set");
        setvar.addText("0");
    }

    // Use fail feedback, hints and solutions
    QTIEditHelper.addFeedbackFail(respcondition_fail);
    QTIEditHelper.addFeedbackHint(respcondition_fail);
    QTIEditHelper.addFeedbackSolution(respcondition_fail);

    // remove whole respcondition if empty
    if (or.element("varequal") == null) {
        resprocessingXML.remove(respcondition_fail);
    }
}

From source file:org.olat.ims.qti.editor.beecom.objects.FIBQuestion.java

License:Apache License

/**
 * Build mastery respcondition for FIB in all-banks-must-be-correct mode. Adds one respcondition in which all blanks must be answered correctly. This respcondition
 * uses the mastery feedback.//from   w w w  .j  av  a2 s  .  c o m
 * 
 * @param resprocessingXML
 */
private void buildRespconditionFIBSingle(final Element resprocessingXML) {
    final Element correct = resprocessingXML.addElement("respcondition");
    correct.addAttribute("title", "Mastery");
    correct.addAttribute("continue", "Yes");

    final Element conditionvar = correct.addElement("conditionvar");
    final Element and = conditionvar.addElement("and");
    for (final Iterator i = getResponses().iterator(); i.hasNext();) {
        final FIBResponse fib = (FIBResponse) i.next();
        if (fib.getType().equals(FIBResponse.TYPE_BLANK)) {
            final String[] correctFIBs = fib.getCorrectBlank().split(";");
            final Element or = and.addElement("or");
            for (int j = 0; j < correctFIBs.length; j++) {
                final Element varequal = or.addElement("varequal");
                varequal.addAttribute("respident", fib.getIdent());
                varequal.addAttribute("case", fib.getCaseSensitive());
                if (correctFIBs[j].length() > 0) {
                    varequal.addCDATA(correctFIBs[j]);
                }
            }
        }
    }

    final Element setvar = correct.addElement("setvar");
    setvar.addAttribute("varname", "SCORE");
    setvar.addAttribute("action", "Set");
    setvar.setText("" + getSingleCorrectScore());

    // Use mastery feedback
    QTIEditHelper.addFeedbackMastery(correct);

    // remove whole respcondition if empty
    if (and.element("or") == null) {
        resprocessingXML.remove(correct);
    }
}

From source file:org.olat.ims.qti.editor.beecom.objects.FIBQuestion.java

License:Apache License

/**
 * Build mastery respconditions for FIB in points-per-blank mode. Adds respconditions for every single blank and a respcondition in case of all blanks answered
 * correctly that uses the mastery feedback.
 * /*from   w  w w . j ava 2 s.com*/
 * @param resprocessingXML
 */
private void buildRespconditionFIBMulti(final Element resprocessingXML) {
    for (final Iterator i = getResponses().iterator(); i.hasNext();) {
        final FIBResponse fib = (FIBResponse) i.next();
        if (!fib.getType().equals(FIBResponse.TYPE_BLANK)) {
            continue;
        }
        final float points = fib.getPoints();
        if (points == 0) {
            continue;
        }

        final Element correct = resprocessingXML.addElement("respcondition");
        correct.addAttribute("continue", "Yes");
        if (points > 0) {
            correct.addAttribute("title", "Mastery");
        } else {
            // doesn't make much sense, but maybe the user has some fancy
            // ideas...
            correct.addAttribute("title", "Fail");
        }

        final Element or = correct.addElement("conditionvar").addElement("or");
        final String[] correctFIBs = fib.getCorrectBlank().split(";");
        for (int j = 0; j < correctFIBs.length; j++) {
            final Element varequal = or.addElement("varequal");
            varequal.addAttribute("respident", fib.getIdent());
            varequal.addAttribute("case", fib.getCaseSensitive());
            if (correctFIBs[j].length() > 0) {
                varequal.addCDATA(correctFIBs[j]);
            }
        }
        final Element setvar = correct.addElement("setvar");
        setvar.addAttribute("varname", "SCORE");
        setvar.addAttribute("action", "Add");
        setvar.setText("" + points);
    }

    // Resp condition for feedback mastery:
    // all response with points>0 must be selected
    final Element respcondition_correct = resprocessingXML.addElement("respcondition");
    respcondition_correct.addAttribute("title", "Mastery");
    respcondition_correct.addAttribute("continue", "Yes");
    final Element conditionvar = respcondition_correct.addElement("conditionvar");
    final Element and = conditionvar.addElement("and");

    for (final Iterator i = getResponses().iterator(); i.hasNext();) {
        final FIBResponse tmpResponse = (FIBResponse) i.next();
        if (!tmpResponse.getType().equals(FIBResponse.TYPE_BLANK)) {
            continue;
        }
        final String[] correctFIBs = tmpResponse.getCorrectBlank().split(";");
        final Element or = and.addElement("or");
        for (int j = 0; j < correctFIBs.length; j++) {
            final Element varequal = or.addElement("varequal");
            varequal.addAttribute("respident", tmpResponse.getIdent());
            varequal.addAttribute("case", tmpResponse.getCaseSensitive());
            if (correctFIBs[j].length() > 0) {
                varequal.addCDATA(correctFIBs[j]);
            }
        } // for loop
    } // for loop

    // Use mastery feedback
    QTIEditHelper.addFeedbackMastery(respcondition_correct);

    // remove whole respcondition if empty
    if (and.element("or") == null) {
        resprocessingXML.remove(respcondition_correct);
    }
}

From source file:org.olat.ims.qti.editor.beecom.objects.FIBQuestion.java

License:Apache License

/**
 * Build fail resprocessing: Adjust score to 0 (if single correct mode) and set hints, solutions and fail feedback when any blank is answered wrong
 * //w  ww.  ja  v  a2s . com
 * @param resprocessingXML
 * @param isSingleCorrect
 */
private void buildRespcondition_fail(final Element resprocessingXML, final boolean isSingleCorrect) {
    // build
    final Element respcondition_fail = resprocessingXML.addElement("respcondition");
    respcondition_fail.addAttribute("title", "Fail");
    respcondition_fail.addAttribute("continue", "Yes");
    final Element conditionvar = respcondition_fail.addElement("conditionvar");
    final Element or = conditionvar.addElement("or");

    for (final Iterator i = getResponses().iterator(); i.hasNext();) {
        final FIBResponse tmpResponse = (FIBResponse) i.next();
        if (!tmpResponse.getType().equals(FIBResponse.TYPE_BLANK)) {
            continue;
        }
        final Element not = or.addElement("not");
        final Element varequal = not.addElement("varequal");
        varequal.addAttribute("respident", tmpResponse.getIdent());
        varequal.addAttribute("case", tmpResponse.getCaseSensitive());
        varequal.setText(tmpResponse.getCorrectBlank());
    } // for loop

    if (isSingleCorrect) {
        final Element setvar = respcondition_fail.addElement("setvar");
        setvar.addAttribute("varname", "SCORE");
        setvar.addAttribute("action", "Set");
        setvar.addText("0");
    }

    // Use fail feedback, hints and solutions
    QTIEditHelper.addFeedbackFail(respcondition_fail);
    QTIEditHelper.addFeedbackHint(respcondition_fail);
    QTIEditHelper.addFeedbackSolution(respcondition_fail);

    // remove whole respcondition if empty
    if (or.element("varequal") == null) {
        resprocessingXML.remove(respcondition_fail);
    }
}

From source file:org.olat.ims.qti.qpool.QTIExportProcessor.java

License:Apache License

/**
 * Collect the file and rewrite the //w w  w  .j  a  v a  2  s  .  co  m
 * @param el
 * @param container
 * @param materials
 * @param paths
 */
private void collectResourcesInMatText(Element el, VFSContainer container, ItemsAndMaterials materials) {
    //mattext
    @SuppressWarnings("unchecked")
    List<Element> mattextList = el.selectNodes(".//mattext");
    for (Element mat : mattextList) {
        Attribute texttypeAttr = mat.attribute("texttype");
        String texttype = texttypeAttr.getValue();
        if ("text/html".equals(texttype)) {
            @SuppressWarnings("unchecked")
            List<Node> childElList = new ArrayList<Node>(mat.content());
            for (Node childEl : childElList) {
                mat.remove(childEl);
            }

            for (Node childEl : childElList) {
                if (Node.CDATA_SECTION_NODE == childEl.getNodeType()) {
                    CDATA data = (CDATA) childEl;
                    boolean changed = false;
                    String text = data.getText();
                    List<String> materialPaths = findMaterialInMatText(text);
                    for (String materialPath : materialPaths) {
                        VFSItem matVfsItem = container.resolve(materialPath);
                        if (matVfsItem instanceof VFSLeaf) {
                            String exportUri = generateExportPath(materials.getPaths(), matVfsItem);
                            materials.addMaterial(new ItemMaterial((VFSLeaf) matVfsItem, exportUri));
                            text = text.replaceAll(materialPath, exportUri);
                            changed = true;
                        }
                    }
                    if (changed) {
                        mat.addCDATA(text);
                    } else {
                        mat.add(childEl);
                    }
                } else {
                    mat.add(childEl);
                }
            }
        }
    }
}

From source file:org.onosproject.yang.serializers.xml.DefaultXmlWalker.java

License:Apache License

/**
 * Determine the type of an element.// w  w w.j  a  va2  s.  co m
 *
 * @param element to be analysed
 * @return type of the element
 */
private XmlNodeType getElementType(Element element) {
    Element newElement = element.createCopy();
    newElement.remove(element.getNamespace());
    return newElement.hasContent() && newElement.isTextOnly() ? TEXT_NODE : OBJECT_NODE;
}

From source file:org.onosproject.yms.app.ych.defaultcodecs.xml.DefaultXmlCodecWalker.java

License:Apache License

@Override
public void walk(XmlListener listener, Element element, Element rootElement) {
    try {//from   ww w.  j a va2s .  c  om
        Element newElement = element.createCopy();
        newElement.remove(element.getNamespace());

        listener.enterXmlElement(element, getElementType(newElement), rootElement);

        if (element.hasContent() && !element.isTextOnly()) {
            for (Iterator i = element.elementIterator(); i.hasNext();) {
                Element childElement = (Element) i.next();
                walk(listener, childElement, rootElement);
            }
        }

        listener.exitXmlElement(element, getElementType(element), rootElement);
    } catch (Exception e) {
        log.error("Exception occurred when walk xml element: {}", element);
    }
}

From source file:org.openadaptor.auxil.convertor.map.DocumentMapFacade.java

License:Open Source License

/**
 * Add child elements to an existing element.
 * <p>//from ww w.ja va  2s.  co  m
 * Forces reparenting if necessary.
 * @param element - element to which the supplied ones are to be attached.
 * @param elements - Array of elements to attach as children.
 */
private void add(Element element, Element[] elements) {
    for (int i = 0; i < elements.length; i++) {
        Element current = elements[i];
        Element parent = current.getParent();
        if (parent != null) {
            log.debug("Reparent node from " + parent.getPath());
            parent.remove(current);
        }
        element.add(current);
    }
}

From source file:org.openadaptor.auxil.convertor.map.Dom4JDocumentMapFacade.java

License:Open Source License

private Object modify(Node node, Attribute attribute) {
    Object old = null;//w  w  w  .  j  a  v  a2  s. c o  m
    if (node instanceof Element) { //Set or replace value of attribute on Element
        Element element = (Element) node;
        String attrName = attribute.getName();
        Attribute existing = element.attribute(attrName);
        if (existing != null) {
            element.remove(existing);
            old = existing;
        }
        element.add(attribute);
    } else if (node instanceof Attribute) { //Modify the value of the existing attribute.
        Attribute existing = (Attribute) node;
        old = DocumentHelper.createAttribute(null, existing.getName(), existing.getValue());
        existing.setValue(attribute.getValue());
    } else {
        throw new RecordException(
                "Target of put(x,Attribute) must be empty or an existing Element or Attribute. Got: " + node);
    }
    return old;
}