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.jivesoftware.xmpp.workgroup.WorkgroupIQHandler.java

License:Open Source License

private void getIQPrivate(IQ packet) {
    WorkgroupSettings settings = new WorkgroupSettings();

    IQ replyPacket = null;//www .j av  a  2 s  . co  m
    Element child = packet.getChildElement();
    Element dataElement = (Element) child.elementIterator().next();

    if (dataElement != null) {
        if (IQ.Type.get.equals(packet.getType())) {
            replyPacket = IQ.createResultIQ(packet);
            Element dataStored = settings.get(workgroup.getJID().toBareJID(), dataElement);
            dataStored.setParent(null);

            child.remove(dataElement);
            child.setParent(null);
            replyPacket.setChildElement(child);
            child.add(dataStored);
        }
    } else {
        replyPacket = IQ.createResultIQ(packet);
        replyPacket.setChildElement("query", "jabber:iq:private");
    }

    workgroup.send(replyPacket);
}

From source file:org.jogre.server.ServerProperties.java

License:Open Source License

/**
 * Delete the ELO element from the game.
 *
 * @param gameId/*from   www.  j  a v a 2s  .  c  o m*/
 */
public void deleteELOElm(String gameId) {
    Element gameElm = getGameElm(gameId);
    Element gameEloElm = getELOElm(gameId);

    if (gameElm != null && gameEloElm != null)
        gameElm.remove(gameEloElm);
}

From source file:org.jogre.server.ServerProperties.java

License:Open Source License

/**
 * Delet an existing custom elm using a gameID and a custom elm type.
 *
 * @param gameID//from www.  java 2  s . c  om
 * @param type
 */
public void deleteCustomElm(String gameID, String type) {
    Element gameElm = getGameElm(gameID);
    Object customElm = doc.selectSingleNode(
            "/server_properties/supported_games/game[@id='" + gameID + "']/custom[@type='" + type + "']");

    if (gameElm != null && customElm != null)
        gameElm.remove((Element) customElm);
}

From source file:org.nuxeo.automation.scripting.blockly.converter.Chains2Blockly.java

License:Open Source License

protected void convertChain(Element root, OperationChainContribution chain, ChainsGroup chainsd) {

    OpBlocks state = new OpBlocks(root);

    for (Operation op : chain.getOps()) {
        if (!skipOperation(op.getId())) {

            OperationBlock block = createOperationBlock(op, state);

            if (state.lastProcessed != null) {
                if (state.lastResult != null) {
                    if (block.getWrapper().hasInput()) {
                        // plug output of last block on the current one
                        Element input = getInput(block);
                        Element previousParent = state.lastProcessed.getBlock().getParent();
                        // unplung if needed !?
                        if (previousParent != null) {
                            previousParent.remove(state.lastProcessed.getBlock());
                            input.add(state.lastProcessed.getBlock());
                            previousParent.add(block.getBlock());
                        } else {
                            input.add(state.lastProcessed.getBlock());
                        }// w  w  w  .ja  v a2s.  c  om
                        state.lastProcessed = block;
                        if (block.getWrapper().hasOutput()) {
                            state.lastResult = block;
                        } else {
                            state.lastResult = null;
                            stackBlock(state, block);
                        }
                    } else {
                        // we have a result but no-one to read it
                        // add swallow block
                        OperationBlock swallowBlock = swallow(state.lastResult);
                        // stack it
                        stackBlock(state, swallowBlock);

                        state.lastProcessed = swallowBlock;
                        state.lastResult = null;
                        storeOrstackBlock(state, block);
                        state.lastProcessed = block;
                    }
                } else {
                    // no pending result
                    storeOrstackBlock(state, block);
                    state.lastProcessed = block;
                }
            } else {
                // init round
                storeOrstackBlock(state, block);
                state.lastProcessed = block;
            }
        }
    }

    if (state.lastResult != null) {
        OperationBlock swallowBlock = swallow(state.lastResult);
        stackBlock(state, swallowBlock);
    }
}

From source file:org.nuxeo.ecm.jsf2.migration.parser.AttributeAjaxSingleParser.java

License:Open Source License

@Override
public void migrate(Document input) throws Exception {
    // Migrate the elements matching the rule
    if (rule.isMigrationAuto()) {
        for (Node node : listElementsToMigrate) {
            Element element = (Element) node;
            // Remove the "ajaxSingle" attribute
            Attribute attrAS = element.attribute("ajaxSingle");
            element.remove(attrAS);
            // Add the "execute" attribute
            Attribute attribute = new DefaultAttribute("execute", "@this");
            element.add(attribute);//ww w. j  a v a2 s .c  om
        }
    }
}

From source file:org.nuxeo.ecm.jsf2.migration.parser.NamespaceParser.java

License:Open Source License

@Override
public void migrate(Document input) throws Exception {
    Element root = input.getRootElement();
    for (String prefix : listPrefixToMigrate) {
        Namespace newNamespace = new Namespace(prefix, EnumPrefixes.getPrefix(prefix).getNamespace());
        Namespace oldNamespace = root.getNamespaceForPrefix(prefix);
        if (oldNamespace != null) {
            root.remove(oldNamespace);
        }//from   w ww  .  jav  a  2 s  . co m
        root.add(newNamespace);

        // Change the name of every elements with the prefix
        StringBuilder prefixXpath = new StringBuilder("//");
        prefixXpath.append(prefix);
        prefixXpath.append(":*");
        // Create a new XPath expression, with the old namespace in order
        // to
        // get the elements matching the expression
        XPath xpath = new Dom4jXPath(prefixXpath.toString());
        SimpleNamespaceContext nc = new SimpleNamespaceContext();
        nc.addNamespace(prefix, oldNamespace.getURI());
        xpath.setNamespaceContext(nc);

        @SuppressWarnings("unchecked")
        List<Element> elementsToMigrate = xpath.selectNodes(input);
        for (Element element : elementsToMigrate) {
            // The namespace to change is not hold by the element but the
            // QName
            QName qname = element.getQName();
            QName newQName = new QName(qname.getName(), newNamespace, qname.getQualifiedName());
            element.setQName(newQName);
        }
    }
}

From source file:org.nuxeo.ecm.jsf2.migration.parser.ReRenderParser.java

License:Open Source License

@Override
public void migrate(Document input) throws Exception {
    // Migrate the elements matching the rule
    if (rule.isMigrationAuto()) {
        for (Node node : listElementsToMigrate) {
            // Change the name of attribute "reRender"
            Attribute attribute = createNewAttribute((Attribute) node);
            Element parentElement = node.getParent();
            parentElement.remove(node);
            parentElement.add(attribute);
        }/* w  w  w  .j av  a  2  s .c  om*/
    }
}

From source file:org.olat.ims.qti.container.SectionContext.java

License:Apache License

/**
 * @param assessInstance//w  w  w  .j  ava  2  s.  c  o m
 * @param el_section
 * @param sw
 */
public void setUp(final AssessmentInstance assessInstance, final Element el_section, Switches sw) {
    this.assessInstance = assessInstance;
    this.el_section = el_section;
    this.ident = el_section.attributeValue("ident");
    init();

    final Element dur = (Element) el_section.selectSingleNode("duration");
    if (dur == null) {
        durationLimit = -1; // no limit
    } else {
        final String sdur = dur.getText();
        durationLimit = QTIHelper.parseISODuration(sdur);
        if (durationLimit == 0) {
            durationLimit = -1; // Assesst Designer fix
        }
    }

    // get objectives
    final Element el_objectives = (Element) el_section.selectSingleNode("objectives");
    if (el_objectives != null) {
        objectives = new Objectives(el_objectives);
    }

    // feedback switches
    // ---------------------------------------------------------
    if (sw == null) { // no switches from the assessment context dominate
        // retrieve section switches
        final Element el_control = (Element) el_section.selectSingleNode("sectioncontrol");
        if (el_control != null) {
            final String feedbackswitch = el_control.attributeValue("feedbackswitch");
            final String hintswitch = el_control.attributeValue("hintswitch");
            final String solutionswitch = el_control.attributeValue("solutionswitch");
            feedbackswitchedon = (feedbackswitch == null) ? true : feedbackswitch.equals("Yes");
            final boolean hints = (hintswitch == null) ? true : hintswitch.equals("Yes");
            final boolean solutions = (solutionswitch == null) ? true : solutionswitch.equals("Yes");
            sw = new Switches(feedbackswitchedon, hints, solutions);
        }
    }

    // ----------------------- selection
    List el_items = new ArrayList();
    // determine which items (sections not implemented) will be chosen/selected
    // for this section
    // --- 1. take all items and resolved itemrefs which are in the section
    final List items = el_section.selectNodes("item|itemref");
    for (final Iterator iter = items.iterator(); iter.hasNext();) {
        Element el_item = (Element) iter.next();
        // <!ELEMENT itemref (#PCDATA)> <!ATTLIST itemref %I_LinkRefId; > <!ENTITY
        // % I_LinkRefId " linkrefid CDATA #REQUIRED">
        if (el_item.getName().equals("itemref")) {
            // resolve the entity first
            final String linkRefId = el_item.attributeValue("linkrefid");
            el_item = (Element) el_section.selectSingleNode("//item[@ident='" + linkRefId + "']");
            // if item == null -> TODO Error
        }
        el_items.add(el_item);
    }
    // --- 2. select all items from the objectbank which fulfill the selection
    // criteria

    final Element el_selordering = (Element) el_section.selectSingleNode("selection_ordering");
    if (el_selordering != null) {
        // do some selection and ordering
        // here comes the selection....
        // xpath =
        // "//item[itemmetadata/qtimetadata/qtimetadatafield[fieldlabel[text()='qmd_dificulty']
        // and fieldentry[text()='4']] or
        // itemmetadata/qtimetadata/qtimetadatafield[fieldlabel[text()='qmd_author']
        // and fieldentry[text()='felix']]]"
        // <!ELEMENT selection_ordering (qticomment? , sequence_parameter* ,
        // selection* , order?)>
        // <!ATTLIST selection_ordering sequence_type CDATA #IMPLIED >
        // <!ELEMENT selection (sourcebank_ref? , selection_number? ,
        // selection_metadata? ,
        // (and_selection | or_selection | not_selection | selection_extension)?)>
        // <!ELEMENT sourcebank_ref (#PCDATA)>
        // not <!ELEMENT order (order_extension?)>
        // <!ATTLIST order order_type CDATA #REQUIRED >
        // <!ELEMENT selection_number (#PCDATA)>
        // not <!ELEMENT sequence_parameter (#PCDATA)>
        // not <!ATTLIST sequence_parameter %I_Pname; >
        final List el_selections = el_selordering.selectNodes("selection");

        // iterate over all selection elements : after each we have some items to
        // add to the run-time-section
        for (final Iterator it_selection = el_selections.iterator(); it_selection.hasNext();) {
            List selectedItems;
            final Element el_selection = (Element) it_selection.next();
            final Element el_sourcebankref = (Element) el_selection.selectSingleNode("sourcebank_ref");
            if (el_sourcebankref == null) {
                // no reference to sourcebank, -> take internal one, but dtd disallows
                // it!?? TODO
                /*
                 * 2:27 PM] <felix.jost> aus ims qti sao: [2:27 PM] <felix.jost> 3.2.1 <sourcebank_ref> Description: Identifies the objectbank to which the selection
                 * and ordering rules are to be applied. This objectbank may or may not be contained in the same <questestinterop> package. [2:27 PM] <felix.jost>
                 * aber dtd: [2:28 PM] <felix.jost> <!ELEMENT questestinterop (qticomment? , (objectbank | assessment | (section | item)+))>
                 */
                selectedItems = new ArrayList();
            } else {
                final String sourceBankRef = el_sourcebankref.getText();
                final Element objectBank = assessInstance.getResolver().getObjectBank(sourceBankRef);

                // traverse 1.: process "and" or "or" or "not" selection to get the
                // items, if existing, otherwise take all items
                // 2.: do the selection_number
                final Element andornot_selection = (Element) el_selection
                        .selectSingleNode("and_selection|or_selection|not_selection|selection_metadata");
                final StringBuilder select_expr = new StringBuilder("//item");
                if (andornot_selection != null) {
                    // some criteria, extend above xpath to select only the appropriate
                    // elements
                    select_expr.append("[");
                    final String elName = andornot_selection.getName();
                    final ExpressionBuilder eb = QTIHelper.getExpressionBuilder(elName);
                    eb.buildXPathExpression(andornot_selection, select_expr, false, true);
                    select_expr.append("]");
                }
                selectedItems = objectBank.selectNodes(select_expr.toString());
                el_items.addAll(selectedItems);
            }
            final Element el_selection_number = (Element) el_selection.selectSingleNode("selection_number");
            // --- 3. if selection_number exists, pick out some items
            if (el_selection_number != null) {
                final String sNum = el_selection_number.getText();
                int num = new Integer(sNum).intValue();
                // now choose some x out of the items if selection_number exists
                final List newList = new ArrayList();
                final Random r = new Random();
                int size = el_items.size();
                // if num > size ??e.g. 5 elements should be picked, but there are
                // only four
                if (num > size) {
                    num = size;
                }
                for (int i = 0; i < num; i++) {
                    final int n = r.nextInt(size--);
                    final Object o = el_items.remove(n);
                    newList.add(o);
                }
                el_items = newList;
                /*
                 * pick out items -> remove unused items from section
                 */
                items.removeAll(el_items);
                for (final Iterator iter = items.iterator(); iter.hasNext();) {
                    el_section.remove((Node) iter.next());
                }

            }
            // append found items to existing ones
        }
    } // end of el_ordering != null

    // if there is order = random -> shuffle
    // <order order_type="Random"/>
    if (el_selordering != null) {
        final Element el_order = (Element) el_selordering.selectSingleNode("order");
        if (el_order != null) {
            final String order_type = el_order.attributeValue("order_type");
            if (order_type.equals("Random")) {
                Collections.shuffle(el_items);
            }
        }
    }

    // now wrap all item contexts
    itemContexts = new ArrayList(10);
    for (final Iterator iter = el_items.iterator(); iter.hasNext();) {
        final Element item = (Element) iter.next();
        item.detach();
        final ItemContext itc = new ItemContext();
        itc.setUp(assessInstance, item, sw);
        if (durationLimit != -1 && assessInstance.isSectionPage()) {
            itc.clearDurationLimit();
        }
        itemContexts.add(itc);
    }

    // outcomesProcessing
    // <!ELEMENT section (qticomment? , duration? , qtimetadata* ,
    // objectives* , sectioncontrol* , sectionprecondition* ,
    // sectionpostcondition* ,
    // rubric* , presentation_material? ,
    // outcomes_processing* , sectionproc_extension? ,
    // sectionfeedback* , selection_ordering? ,
    // reference? , (itemref | item | sectionref | section)*)>

    // <!ELEMENT outcomes_processing (qticomment? , outcomes ,
    // objects_condition* , processing_parameter* , map_output* ,
    // outcomes_feedback_test*)>
    // <!ELEMENT outcomes (qticomment? , (decvar , interpretvar*)+)>

    // <!ELEMENT decvar (#PCDATA)>
    // <!ATTLIST decvar %I_VarName; .......cutvalue CDATA #IMPLIED >
    final Element el_outpro = (Element) el_section.selectSingleNode("outcomes_processing");
    if (el_outpro != null) {
        // get the scoring model: we need it later for calculating the score
        // <!ENTITY % I_ScoreModel " scoremodel CDATA #IMPLIED">
        scoremodel = el_outpro.attributeValue("scoremodel");
        // may be null -> then assume SumOfScores

        // set the cutvalue if given (only variable score)
        cutvalue = QTIHelper.getIntAttribute(el_outpro, "outcomes/decvar[@varname='SCORE']", "cutvalue");
        final List el_oft = el_outpro.selectNodes("outcomes_feedback_test");
        if (el_oft.size() != 0) {
            feedbacktesting = true;
        }
    }
}

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

License:Apache License

/**
 * Build resprocessing for single choice item. Set score to correct value and use mastery feedback
 * /*from  www  .  jav a 2 s . c  om*/
 * @param resprocessingXML
 */
private void buildRespconditionSC_mastery(final Element resprocessingXML) {
    final Element respcondition_correct = resprocessingXML.addElement("respcondition");
    respcondition_correct.addAttribute("title", "Mastery");
    respcondition_correct.addAttribute("continue", "Yes");

    final Element conditionvar = respcondition_correct.addElement("conditionvar");
    for (final Iterator i = getResponses().iterator(); i.hasNext();) {
        // fetch correct answer (there should be a single instance)
        final ChoiceResponse tmpChoice = (ChoiceResponse) i.next();
        if (!tmpChoice.isCorrect()) {
            continue;
        }

        // found correct answer
        final Element varequal = conditionvar.addElement("varequal");
        varequal.addAttribute("respident", getIdent());
        varequal.addAttribute("case", "Yes");
        varequal.addText(tmpChoice.getIdent());
        break;
    } // for loop

    // check if conditionvar has correct value
    if (conditionvar.elements().size() == 0) {
        resprocessingXML.remove(respcondition_correct);
        return;
    }

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

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

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

License:Apache License

/**
 * Build resprocessing for multiple choice item with a single correct answer. Set score to correct value and use mastery feedback.
 * //from ww  w  .  j  a v a  2 s . c  o  m
 * @param resprocessingXML
 */
private void buildRespconditionMCSingle_mastery(final Element resprocessingXML) {
    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.isCorrect()) { // correct answers
            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

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

    // 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);
        }
    }

}