Example usage for org.dom4j Element getTextTrim

List of usage examples for org.dom4j Element getTextTrim

Introduction

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

Prototype

String getTextTrim();

Source Link

Document

DOCUMENT ME!

Usage

From source file:org.olat.core.util.xml.Util.java

License:Apache License

private static void visit(StringBuilder sb, Element elem) {
    sb.append(elem.getTextTrim());
    List children = elem.elements();
    for (Iterator it_ch = children.iterator(); it_ch.hasNext();) {
        Element element = (Element) it_ch.next();
        visit(sb, element);// w w w .j  a  v a  2 s  .c om
    }
}

From source file:org.olat.fileresource.FileDetailsForm.java

License:Apache License

@Override
protected void initForm(final FormItemContainer formLayout, final Controller listener, final UserRequest ureq) {
    uifactory.addStaticTextElement("size", "fr.size", new Long(file.length() / 1024).toString() + " KB",
            formLayout);//from w  ww.  j ava  2 s  . c o m
    final DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, getLocale());
    uifactory.addStaticTextElement("last", "fr.last", df.format(new Date(file.lastModified())), formLayout);

    final String resType = res.getResourceableTypeName();
    if (resType.equals(TestFileResource.TYPE_NAME) || resType.equals(SurveyFileResource.TYPE_NAME)) {

        final FileResourceManager frm = FileResourceManager.getInstance();
        final File unzippedRoot = frm.unzipFileResource(res);

        // with VFS FIXME:pb:c: remove casts to LocalFileImpl and LocalFolderImpl if no longer needed.
        final VFSContainer vfsUnzippedRoot = new LocalFolderImpl(unzippedRoot);
        final VFSItem vfsQTI = vfsUnzippedRoot.resolve("qti.xml");
        // getDocument(..) ensures that InputStream is closed in every case.
        final Document doc = QTIHelper.getDocument((LocalFileImpl) vfsQTI);
        // if doc is null an error loading the document occured (IOException, qti.xml does not exist)
        if (doc != null) {

            // extract title
            final Element el_assess = (Element) doc.selectSingleNode("questestinterop/assessment");
            final String title = el_assess.attributeValue("title");
            uifactory.addStaticTextElement("title", "qti.title", title == null ? "-" : title, formLayout);

            // extract objectives
            // HTMLTextAreaElement htmlTA = new HTMLTextAreaElement("qti.objectives", 10, 60);
            // htmlTA.setReadOnly(true);

            String obj = "-";
            final Element el_objectives = (Element) doc
                    .selectSingleNode("//questestinterop/assessment/objectives");
            if (el_objectives != null) {
                final Element el_mat = (Element) el_objectives.selectSingleNode("material/mattext");
                if (el_mat != null) {
                    obj = el_mat.getTextTrim();
                }
            }
            uifactory.addStaticTextElement("obj", "qti.objectives", obj, formLayout);

            // extract num of questions
            final List items = doc.selectNodes("//item");
            uifactory.addStaticTextElement("qti.questions", "qti.questions",
                    items.size() > 0 ? "" + items.size() : "-", formLayout);

            // extract time limit
            String tl = "-";
            final Element el_duration = (Element) el_assess.selectSingleNode("duration");
            if (el_duration != null) {
                final long dur = QTIHelper.parseISODuration(el_duration.getTextTrim());
                final long min = dur / 1024 / 60;
                final long sec = (dur - (min * 60 * 1024)) / 1024;
                tl = "" + min + "' " + sec + "''";
            }
            uifactory.addStaticTextElement("qti.timelimit", "qti.timelimit", tl, formLayout);
        }
    } else if (resType.equals(ImsCPFileResource.TYPE_NAME) || resType.equals(ScormCPFileResource.TYPE_NAME)) {
        //
    }

    flc.setEnabled(false);
}

From source file:org.olat.ims.qti.container.qtielements.Duration.java

License:Apache License

/**
 * @param el_element//  w  w  w .j  a  v a  2s .co m
 */
public Duration(final Element el_element) {
    super(el_element);
    final String sDuration = el_element.getTextTrim();
    duration = QTIHelper.parseISODuration(sDuration);
}

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

License:Apache License

/**
 * Called by ItemParser/*from w  w w. j  a v a 2  s. com*/
 * 
 * @param item
 * @return
 */
public static FIBQuestion getInstance(final Element item) {
    final FIBQuestion instance = new FIBQuestion();

    final Element presentationXML = item.element("presentation");
    final List elementsXML = presentationXML.element("flow").elements();
    final List responses = instance.getResponses();
    final Element el_resprocessing = item.element("resprocessing");

    for (final Iterator i = elementsXML.iterator(); i.hasNext();) {
        final Element content = (Element) i.next();
        final FIBResponse fibresponse = new FIBResponse();
        final String name = content.getName();
        if (name.equalsIgnoreCase("material")) {
            final Material mat = (Material) parserManager.parse(content);
            if (mat != null) {
                fibresponse.setContent(mat);
            }
            fibresponse.setType(FIBResponse.TYPE_CONTENT);
            responses.add(fibresponse);
        } else if (name.equalsIgnoreCase("response_str")) {
            final String ident = content.attribute("ident").getValue();
            final Element render_fib = content.element("render_fib");
            content.element("render_fib").element("flow_label");
            fibresponse.setType(FIBResponse.TYPE_BLANK);
            fibresponse.setIdent(ident);
            fibresponse.setSizeFromColumns(render_fib.attribute("columns"));
            fibresponse.setMaxLengthFromMaxChar(render_fib.attribute("maxchars"));
            final List el_varequals = el_resprocessing.selectNodes(".//varequal[@respident='" + ident + "']");
            final List processedSolutions = new ArrayList(); // list of already process strings
            if (el_varequals != null) {
                String correctBlank = "";
                String correctBlankCaseAttribute = "No";
                for (final Iterator iter = el_varequals.iterator(); iter.hasNext();) {
                    final Element el_varequal = (Element) iter.next();
                    final String solution = el_varequal.getTextTrim();
                    if (!processedSolutions.contains(solution)) {
                        // Solutions are there twice because of the mastery feedback.
                        // Only process solutions once.
                        correctBlank = correctBlank + solution;
                        if (iter.hasNext()) {
                            correctBlank = correctBlank + ";";
                        }
                        correctBlankCaseAttribute = el_varequal.attributeValue("case");
                        processedSolutions.add(solution);
                    }
                }
                if (correctBlank.endsWith(";")) {
                    correctBlank = correctBlank.substring(0, correctBlank.length() - 1);
                }
                fibresponse.setCorrectBlank(correctBlank);
                fibresponse.setCaseSensitive(correctBlankCaseAttribute);
            }
            responses.add(fibresponse);
        }
    }

    final Element resprocessingXML = item.element("resprocessing");
    if (resprocessingXML != null) {

        final List respconditions = resprocessingXML.elements("respcondition");
        final HashMap points = QTIEditHelper.fetchPoints(respconditions, instance.getType());

        // postprocessing choices
        for (final Iterator i = responses.iterator(); i.hasNext();) {
            final FIBResponse fibResp = (FIBResponse) i.next();
            final Float fPoints = (Float) points.get(fibResp.getIdent());
            if (fPoints != null) {
                fibResp.setPoints(fPoints.floatValue());
                fibResp.setCorrect(true);
            }
        }

        // if does not contain any ANDs, assume only one combination
        // of answers is possible (which sets points by a setvar action="Set")
        if (resprocessingXML.selectNodes(".//setvar[@action='Add']").size() == 0) {
            instance.setSingleCorrect(true);
            final Collection values = points.values();
            if (values.size() > 0) {
                instance.setSingleCorrectScore(((Float) (values.iterator().next())).floatValue());
            }
        } else {
            instance.setSingleCorrect(false);
        }

        // set min/max score
        QTIEditHelper.configureMinMaxScore(instance, (Element) resprocessingXML.selectSingleNode(".//decvar"));
    }
    return instance;
}

From source file:org.olat.ims.qti.editor.beecom.parser.AssessmentParser.java

License:Apache License

/**
 * @see org.olat.ims.qti.editor.beecom.IParser#parse(org.dom4j.Element)
 *///from   w  ww  .  j  av a2  s.c  o m
@Override
public Object parse(final Element element) {
    // assert element.getName().equalsIgnoreCase("assessment");
    final Assessment assessment = new Assessment();

    // attributes

    Attribute attr = element.attribute("ident");
    if (attr != null) {
        assessment.setIdent(attr.getValue());
    }
    attr = element.attribute("title");
    if (attr != null) {
        assessment.setTitle(attr.getValue());
    }

    // elements

    // DURATION
    final QTIObject duration = (QTIObject) parserManager.parse(element.element("duration"));
    assessment.setDuration(duration);

    // ASSESSMENTCONTROL
    final List assessmentcontrolsXML = element.elements("assessmentcontrol");
    final List assessmentcontrols = new ArrayList();
    for (final Iterator i = assessmentcontrolsXML.iterator(); i.hasNext();) {
        assessmentcontrols.add(parserManager.parse((Element) i.next()));
        assessment.setInheritControls(true);
    }
    if (assessmentcontrols.size() == 0) {
        assessmentcontrols.add(new Control());
        assessment.setInheritControls(false);
    }
    assessment.setAssessmentcontrols(assessmentcontrols);

    // OUTCOMES PROCESSING
    final OutcomesProcessing outcomesProcessing = (OutcomesProcessing) parserManager
            .parse(element.element("outcomes_processing"));
    if (outcomesProcessing != null) {
        assessment.setOutcomes_processing(outcomesProcessing);
    }

    // SECTIONS
    final List sectionsXML = element.elements("section");
    final List sections = new ArrayList();
    for (final Iterator i = sectionsXML.iterator(); i.hasNext();) {
        sections.add(parserManager.parse((Element) i.next()));
    }
    assessment.setSections(sections);

    // ITEMS
    final List itemsXML = element.elements("item");
    final List items = new ArrayList();
    for (final Iterator i = itemsXML.iterator(); i.hasNext();) {
        items.add(parserManager.parse((Element) i.next()));
    }
    assessment.setItems(items);

    // OBJECTIVES
    final Element mattext = (Element) element.selectSingleNode("./objectives/material/mattext");
    if (mattext != null) {
        assessment.setObjectives(mattext.getTextTrim());
    }

    // METADATA
    final Metadata metadata = (Metadata) parserManager.parse(element.element("qtimetadata"));
    if (metadata != null) {
        assessment.setMetadata(metadata);
    }

    return assessment;
}

From source file:org.olat.ims.qti.editor.beecom.parser.DurationParser.java

License:Apache License

/**
 * @see org.olat.ims.qti.editor.beecom.IParser#parse(org.dom4j.Element)
 *//*from   w  ww .  ja v a2 s . c  o  m*/
@Override
public Object parse(final Element element) {
    // assert element.getName().equalsIgnoreCase("duration");

    // attributes
    final String sISODuration = element.getTextTrim();
    // null values no problems (default: null = false)
    final long millis = QTIHelper.parseISODuration(sISODuration);
    if (millis == 0) {
        return null;
    }
    return new Duration(millis);
}

From source file:org.olat.ims.qti.editor.beecom.parser.ItemParser.java

License:Apache License

/**
 * @see org.olat.ims.qti.editor.beecom.parser.IParser#parse(org.dom4j.Element)
 *//*  w  w  w  .  ja  v  a 2 s  .  c  om*/
@Override
public Object parse(final Element element) {
    // assert element.getName().equalsIgnoreCase("item");
    final Item item = new Item();
    Attribute tmp = element.attribute("ident");
    if (tmp != null) {
        item.setIdent(tmp.getValue());
    } else {
        item.setIdent("" + CodeHelper.getRAMUniqueID());
    }

    tmp = element.attribute("title");
    if (tmp != null) {
        item.setTitle(tmp.getValue());
    }

    tmp = element.attribute("label");
    if (tmp != null) {
        item.setLabel(tmp.getValue());
    }

    tmp = element.attribute("maxattempts");
    if (tmp != null) {
        try {
            item.setMaxattempts(Integer.parseInt(tmp.getValue()));
        } catch (final NumberFormatException nfe) {
            item.setMaxattempts(0);
        }
    }

    // if editor can't handle type of item, just keep raw XML
    if (!(item.getIdent().startsWith(ITEM_PREFIX_SCQ) || item.getIdent().startsWith(ITEM_PREFIX_MCQ)
            || item.getIdent().startsWith(ITEM_PREFIX_FIB) || item.getIdent().startsWith(ITEM_PREFIX_ESSAY)
            || item.getIdent().startsWith(ITEM_PREFIX_KPRIM))) {
        item.setRawXML(new QTIXMLWrapper(element));
        return item;
    }

    // exported olat surveys don't have the correct essay prefix. Search
    // for render_fib that contains rows attribute and convert them to essay
    if (item.getIdent().startsWith(ITEM_PREFIX_FIB) && element.selectNodes(".//render_fib[@rows]").size() > 0) {
        item.setIdent(item.getIdent().replaceFirst("FIB", "ESSAY"));
    }

    // DURATION
    final Duration duration = (Duration) parserManager.parse(element.element("duration"));
    item.setDuration(duration);

    // CONTROLS
    final List itemcontrolsXML = element.elements("itemcontrol");
    final List itemcontrols = new ArrayList();
    for (final Iterator i = itemcontrolsXML.iterator(); i.hasNext();) {
        itemcontrols.add(parserManager.parse((Element) i.next()));
    }
    if (itemcontrols.size() == 0) {
        itemcontrols.add(new Control());
    }
    item.setItemcontrols(itemcontrols);

    // OBJECTIVES
    final Element mattext = (Element) element.selectSingleNode("./objectives/material/mattext");
    if (mattext != null) {
        item.setObjectives(mattext.getTextTrim());
    }

    // QUESTIONS
    if (item.getIdent().startsWith(ITEM_PREFIX_SCQ)) {
        item.setQuestion(ChoiceQuestion.getInstance(element));
    } else if (item.getIdent().startsWith(ITEM_PREFIX_MCQ)) {
        item.setQuestion(ChoiceQuestion.getInstance(element));
    } else if (item.getIdent().startsWith(ITEM_PREFIX_FIB)) {
        item.setQuestion(FIBQuestion.getInstance(element));
    } else if (item.getIdent().startsWith(ITEM_PREFIX_ESSAY)) {
        item.setQuestion(EssayQuestion.getInstance(element));
    } else if (item.getIdent().startsWith(ITEM_PREFIX_KPRIM)) {
        item.setQuestion(ChoiceQuestion.getInstance(element));
    }

    // FEEDBACKS
    final List feedbacksXML = element.elements("itemfeedback");
    final List feedbacks = new ArrayList();
    item.setItemfeedbacks(feedbacks);
    final Question question = item.getQuestion();
    for (final Iterator i = feedbacksXML.iterator(); i.hasNext();) {
        final Element el_feedback = (Element) i.next();
        if (el_feedback.element("solution") != null) { // fetch solution
            final Element el_solution = el_feedback.element("solution");
            question.setSolutionText(getMaterialAsString(el_solution));
        } else if (el_feedback.element("hint") != null) { // fetch hint
            final Element el_hint = el_feedback.element("hint");
            question.setHintText(getMaterialAsString(el_hint));
        } else {
            final QTIObject tmpObj = (QTIObject) parserManager.parse(el_feedback);
            if (tmpObj != null) {
                feedbacks.add(tmpObj);
            }
        }
    }

    return item;
}

From source file:org.olat.ims.qti.editor.beecom.parser.ItemParser.java

License:Apache License

private String getMaterialAsString(final Element el_root) {
    final StringBuilder result = new StringBuilder();
    final List materials = el_root.selectNodes(".//mattext");
    for (final Iterator iter = materials.iterator(); iter.hasNext();) {
        final Element el_mattext = (Element) iter.next();
        result.append(el_mattext.getTextTrim() + "\n");
    }/*  ww  w  .  ja  va2s  .  c o m*/
    return result.toString();
}

From source file:org.olat.ims.qti.editor.beecom.parser.MattextParser.java

License:Apache License

/**
 * @see org.olat.ims.qti.editor.beecom.parser.IParser#parse(org.dom4j.Element)
 *///from  w w  w .j a v a2s  .  co  m
@Override
public Object parse(final Element element) {
    // assert element.getName().equalsIgnoreCase("mattext");
    final String text = element.getTextTrim();
    if (text != null && text.length() > 0) {
        return new Mattext(text);
    }
    return null;
}

From source file:org.olat.ims.qti.editor.beecom.parser.SectionParser.java

License:Apache License

@Override
public Object parse(final Element element) {
    // assert element.getName().equalsIgnoreCase("questestinterop");
    final Section section = new Section();

    // attributes
    section.setIdent(element.attribute("ident").getValue());
    section.setTitle(element.attribute("title").getValue());

    // elements// w ww  .j a va2 s  .  c o  m

    // DURATION
    final QTIObject duration = (QTIObject) parserManager.parse(element.element("duration"));
    section.setDuration(duration);

    final List sectioncontrolsXML = element.elements("sectioncontrol");
    final List sectioncontrols = new ArrayList();
    for (final Iterator i = sectioncontrolsXML.iterator(); i.hasNext();) {
        sectioncontrols.add(parserManager.parse((Element) i.next()));
    }
    if (sectioncontrols.size() == 0) {
        sectioncontrols.add(new Control());
    }
    section.setSectioncontrols(sectioncontrols);

    // SELECTION ORDERING
    final SelectionOrdering selectionOrdering = (SelectionOrdering) parserManager
            .parse(element.element("selection_ordering"));
    if (selectionOrdering != null) {
        section.setSelection_ordering(selectionOrdering);
    } else {
        section.setSelection_ordering(new SelectionOrdering());
    }

    // SECTIONS
    final List sectionsXML = element.elements("section");
    final List sections = new ArrayList();
    for (final Iterator i = sectionsXML.iterator(); i.hasNext();) {
        sections.add(parserManager.parse((Element) i.next()));
    }
    section.setSections(sections);

    // ITEMS
    final List itemsXML = element.elements("item");
    final List items = new ArrayList();
    for (final Iterator i = itemsXML.iterator(); i.hasNext();) {
        items.add(parserManager.parse((Element) i.next()));
    }
    section.setItems(items);

    // OBJECTIVES
    final Element mattext = (Element) element.selectSingleNode("./objectives/material/mattext");
    if (mattext != null) {
        section.setObjectives(mattext.getTextTrim());
    }

    // FEEDBACKS
    final List feedbacksXML = element.elements("sectionfeedback");
    final List feedbacks = new ArrayList();
    for (final Iterator i = feedbacksXML.iterator(); i.hasNext();) {
        final QTIObject tmp = (QTIObject) parserManager.parse((Element) i.next());
        feedbacks.add(tmp);
    }
    section.setSectionfeedbacks(feedbacks);

    // OUTCOMES_PROCESSING
    // TODO: maybe we should use the OutcomesProcessing object and parser here? Same as on
    // assessment level?
    final QTIObject outcomes_processing = (QTIObject) parserManager
            .parse(element.element("outcomes_processing"));
    section.setOutcomes_processing(outcomes_processing);

    return section;
}