Search in sources :

Example 46 with StringOutput

use of org.olat.core.gui.render.StringOutput in project OpenOLAT by OpenOLAT.

the class I18nTest method testInlineTranslationReplaceLocalizationMarkupWithHTML.

/**
 * Test methods i18nManager inline translation tool and InlineTranslationInterceptHandlerController
 */
@Test
public void testInlineTranslationReplaceLocalizationMarkupWithHTML() {
    // enable inline translation markup
    i18nMgr.setMarkLocalizedStringsEnabled(null, true);
    Translator inlineTrans = Util.createPackageTranslator(InlineTranslationInterceptHandlerController.class, i18nMgr.getLocaleOrNull("de"));
    URLBuilder inlineTranslationURLBuilder = new jUnitURLBuilder();
    String testBundle = "org.olat.core.util.i18n.junittestdata";
    String testKey = "no.need.to.translate.this";
    String rawtext1 = "Lorem impsum<b>nice stuff</b>";
    String rawtext2 = "Holderadio <ul>lsdfsdf<y  asdf blblb";
    String combinedKey = testBundle + ":" + testKey;
    // test method that adds identifyers around the translation items
    String plainVanillaWrapped = i18nMgr.getLocalizedString(testBundle, testKey, null, i18nMgr.getLocaleOrNull("de"), false, false);
    String plainVanillaPlain = "just a test";
    Pattern plainVanillaWrappedPattern = Pattern.compile(I18nManager.IDENT_PREFIX + combinedKey + ":([0-9]*?)" + I18nManager.IDENT_START_POSTFIX + plainVanillaPlain + I18nManager.IDENT_PREFIX + combinedKey + ":\\1" + I18nManager.IDENT_END_POSTFIX);
    Matcher m = plainVanillaWrappedPattern.matcher(plainVanillaWrapped);
    assertTrue(m.find());
    // test find-replace translator identifyers with HTML markup
    StringOutput inlineTransLink = new StringOutput();
    String[] args = (combinedKey + ":1000000001").split(":");
    InlineTranslationInterceptHandlerController.buildInlineTranslationLink(args, inlineTransLink, inlineTrans, inlineTranslationURLBuilder);
    // Plain vanilla text
    String convertedToHTMLMarkup = InlineTranslationInterceptHandlerController.replaceLocalizationMarkupWithHTML(plainVanillaWrapped, inlineTranslationURLBuilder, inlineTrans);
    assertEquals("<span class=\"o_translation_i18nitem\">" + inlineTransLink.toString() + plainVanillaPlain + "</span>", convertedToHTMLMarkup);
    // Simple link
    String linkOPEN = "<a href=\"http://www.olat.org/bla/blu:bli#bla\" title='funny title' class=\"o_css O_anothercss\">";
    String linkCLOSE = "</a>";
    String inlineSpanOPEN = "<span class=\"o_translation_i18nitem\">";
    String inlineSpanCLOSE = "</span>";
    String translatedWithinLink = linkOPEN + plainVanillaWrapped + linkCLOSE + rawtext1;
    convertedToHTMLMarkup = InlineTranslationInterceptHandlerController.replaceLocalizationMarkupWithHTML(translatedWithinLink, inlineTranslationURLBuilder, inlineTrans);
    String convertedWithinLinkExpected = inlineSpanOPEN + inlineTransLink.toString() + linkOPEN + plainVanillaPlain + linkCLOSE + inlineSpanCLOSE + rawtext1;
    assertEquals(convertedWithinLinkExpected, convertedToHTMLMarkup);
    // Simple link with span
    String linkSpanOPEN = "<span class=\"bluber\">";
    String linkSpanCLOSE = "</span>";
    String translatedWithinLinkAndSpan = rawtext2 + linkOPEN + linkSpanOPEN + plainVanillaWrapped + linkSpanCLOSE + linkCLOSE;
    convertedToHTMLMarkup = InlineTranslationInterceptHandlerController.replaceLocalizationMarkupWithHTML(translatedWithinLinkAndSpan, inlineTranslationURLBuilder, inlineTrans);
    String convertedWithinLinkAndSpanExpected = rawtext2 + inlineSpanOPEN + inlineTransLink.toString() + linkOPEN + linkSpanOPEN + plainVanillaPlain + linkSpanCLOSE + linkCLOSE + inlineSpanCLOSE;
    assertEquals(convertedWithinLinkAndSpanExpected, convertedToHTMLMarkup);
    // Muliple links
    String translatedWithinMultipleLinks = translatedWithinLink + translatedWithinLinkAndSpan + translatedWithinLinkAndSpan;
    convertedToHTMLMarkup = InlineTranslationInterceptHandlerController.replaceLocalizationMarkupWithHTML(translatedWithinMultipleLinks, inlineTranslationURLBuilder, inlineTrans);
    String convertedWithinMultipleLinksExpected = convertedWithinLinkExpected + convertedWithinLinkAndSpanExpected + convertedWithinLinkAndSpanExpected;
    assertEquals(convertedWithinMultipleLinksExpected, convertedToHTMLMarkup);
    // Input elements
    String inputOPEN = "<input type='submit' class=\"bluber\" value=\"";
    String inputCLOSE = "\" />";
    String translatedWithinInput = inputOPEN + plainVanillaWrapped + inputCLOSE + rawtext1;
    convertedToHTMLMarkup = InlineTranslationInterceptHandlerController.replaceLocalizationMarkupWithHTML(translatedWithinInput, inlineTranslationURLBuilder, inlineTrans);
    String convertedWithinInputExpected = inlineSpanOPEN + inlineTransLink.toString() + inputOPEN + plainVanillaPlain + inputCLOSE + inlineSpanCLOSE + rawtext1;
    assertEquals(convertedWithinInputExpected, convertedToHTMLMarkup);
    // checkbox elements
    String checkboxOPEN = "<input type='submit' class=\"bluber\" type=\"checkbox\" value=\"";
    String checkboxCLOSE = "\" />";
    String translatedWithinCheckbox = checkboxOPEN + plainVanillaWrapped + checkboxCLOSE + rawtext1;
    convertedToHTMLMarkup = InlineTranslationInterceptHandlerController.replaceLocalizationMarkupWithHTML(translatedWithinCheckbox, inlineTranslationURLBuilder, inlineTrans);
    String convertedWithinCheckboxExpected = checkboxOPEN + plainVanillaPlain + checkboxCLOSE + rawtext1;
    assertEquals(convertedWithinCheckboxExpected, convertedToHTMLMarkup);
    // Input and links mixed
    String translatedWithinMultipleLinksAndInput = translatedWithinLink + rawtext1 + translatedWithinInput + translatedWithinLinkAndSpan + rawtext2 + translatedWithinInput + translatedWithinLinkAndSpan;
    convertedToHTMLMarkup = InlineTranslationInterceptHandlerController.replaceLocalizationMarkupWithHTML(translatedWithinMultipleLinksAndInput, inlineTranslationURLBuilder, inlineTrans);
    String convertedWithinMultipleLinksAndInputExpected = convertedWithinLinkExpected + rawtext1 + convertedWithinInputExpected + convertedWithinLinkAndSpanExpected + rawtext2 + convertedWithinInputExpected + convertedWithinLinkAndSpanExpected;
    assertEquals(convertedWithinMultipleLinksAndInputExpected, convertedToHTMLMarkup);
    // Within element attribute
    String attributeOPEN = "<a href='sdfsdf' title=\"";
    String attributeCLOSE = "\" class=\"o_bluber\">hello world</a>";
    String translatedWithinAttribute = attributeOPEN + plainVanillaWrapped + attributeCLOSE + rawtext1;
    convertedToHTMLMarkup = InlineTranslationInterceptHandlerController.replaceLocalizationMarkupWithHTML(translatedWithinAttribute, inlineTranslationURLBuilder, inlineTrans);
    String convertedWithinAttributeExpected = attributeOPEN + plainVanillaPlain + attributeCLOSE + rawtext1;
    assertEquals(convertedWithinAttributeExpected, convertedToHTMLMarkup);
    // Ultimate test
    String translatedUltimate = translatedWithinMultipleLinksAndInput + rawtext1 + translatedWithinAttribute + translatedWithinMultipleLinksAndInput + rawtext2 + translatedWithinInput + translatedWithinLinkAndSpan;
    convertedToHTMLMarkup = InlineTranslationInterceptHandlerController.replaceLocalizationMarkupWithHTML(translatedUltimate, inlineTranslationURLBuilder, inlineTrans);
    String convertedUltimateExpected = convertedWithinMultipleLinksAndInputExpected + rawtext1 + convertedWithinAttributeExpected + convertedWithinMultipleLinksAndInputExpected + rawtext2 + convertedWithinInputExpected + convertedWithinLinkAndSpanExpected;
    assertEquals(convertedUltimateExpected, convertedToHTMLMarkup);
    // don't do inline translation markup
    i18nMgr.setMarkLocalizedStringsEnabled(null, false);
}
Also used : Pattern(java.util.regex.Pattern) Translator(org.olat.core.gui.translator.Translator) Matcher(java.util.regex.Matcher) StringOutput(org.olat.core.gui.render.StringOutput) URLBuilder(org.olat.core.gui.render.URLBuilder) Test(org.junit.Test)

Example 47 with StringOutput

use of org.olat.core.gui.render.StringOutput in project openolat by klemens.

the class JSAndCSSAdderImpl method addRequiredJsFile.

/**
 * @see org.olat.core.gui.control.JSAndCSSAdder#addRequiredJsFile(java.lang.Class,
 *      java.lang.String, java.lang.String, java.lang.String)
 */
private void addRequiredJsFile(String jsFileName, String fileEncoding, String AJAXAddJsCode) {
    StringOutput sb = new StringOutput(50);
    String jsPath;
    if (jsFileName.startsWith("http:") || jsFileName.startsWith("https:") || jsFileName.startsWith("//")) {
        jsPath = jsFileName;
    } else {
        Renderer.renderStaticURI(sb, jsFileName);
        jsPath = sb.toString();
    }
    if (!curJsList.contains(jsPath)) {
        curJsList.add(jsPath);
        jsPathToJsFileName.put(jsPath, jsFileName);
        if (StringHelper.containsNonWhitespace(AJAXAddJsCode)) {
            jsPathToEvalBeforeAJAXAddJsCode.put(jsPath, AJAXAddJsCode);
        }
        if (fileEncoding != null) {
            jsPathToEvalFileEncoding.put(jsPath, fileEncoding);
        } else {
            jsPathToEvalFileEncoding.put(jsPath, ENCODING_DEFAULT);
        }
    }
}
Also used : StringOutput(org.olat.core.gui.render.StringOutput)

Example 48 with StringOutput

use of org.olat.core.gui.render.StringOutput in project openolat by klemens.

the class ProjectListController method createTableController.

private TableController createTableController(final UserRequest ureq, WindowControl wControl) {
    numberOfCustomFieldInTable = 0;
    numberOfEventInTable = 0;
    TableGuiConfiguration tableConfig = new TableGuiConfiguration();
    tableConfig.setTableEmptyMessage(translate("projectlist.no.projects"));
    tableConfig.setPreferencesOffered(true, "projectbrokerList");
    // Do not allow show all because many entries takes too long to render
    tableConfig.setShowAllLinkEnabled(false);
    removeAsListenerAndDispose(tableController);
    tableController = new TableController(tableConfig, ureq, wControl, this.getTranslator(), true);
    listenTo(tableController);
    int dataColumn = 0;
    tableController.addColumnDescriptor(new DefaultColumnDescriptor("projectlist.tableheader.title", dataColumn++, TABLE_ACTION_SHOW_DETAIL, getLocale()));
    CustomRenderColumnDescriptor projectManagerDescriptor = new CustomRenderColumnDescriptor("projectlist.tableheader.account.manager", dataColumn++, TABLE_ACTION_ACCOUNT_MANAGER, ureq.getLocale(), ColumnDescriptor.ALIGNMENT_LEFT, new ProjectManagerColumnRenderer()) {

        /**
         * @see org.olat.core.gui.components.table.DefaultColumnDescriptor#compareTo(int, int)
         */
        @Override
        public int compareTo(int rowa, int rowb) {
            return super.compareTo(rowa, rowb);
        }

        /**
         * @see org.olat.core.gui.components.table.CustomRenderColumnDescriptor#renderValue(org.olat.core.gui.render.StringOutput, int, org.olat.core.gui.render.Renderer)
         */
        @Override
        public void renderValue(StringOutput sb, int row, Renderer renderer) {
            Object val = getModelData(row);
            // to get info about row in Renderer!
            String rowSt = Integer.toString(row);
            getCustomCellRenderer().render(sb, renderer, val, getLocale(), getAlignment(), rowSt);
        }
    };
    tableController.addColumnDescriptor(projectManagerDescriptor);
    // Custom-Fields
    List<CustomField> customFieldList = moduleConfig.getCustomFields();
    for (Iterator<CustomField> iterator = customFieldList.iterator(); iterator.hasNext(); ) {
        CustomField customField = iterator.next();
        if (customField.isTableViewEnabled()) {
            numberOfCustomFieldInTable++;
            DefaultColumnDescriptor columnDescriptor = new DefaultColumnDescriptor(customField.getName(), dataColumn++, null, getLocale());
            columnDescriptor.setTranslateHeaderKey(false);
            tableController.addColumnDescriptor(columnDescriptor);
        }
    }
    // Project Events
    for (Project.EventType eventType : Project.EventType.values()) {
        if (moduleConfig.isProjectEventEnabled(eventType) && moduleConfig.isProjectEventTableViewEnabled(eventType)) {
            numberOfEventInTable++;
            tableController.addColumnDescriptor(new CustomRenderColumnDescriptor("projectlist.tableheader.event." + eventType.getI18nKey(), dataColumn++, null, getLocale(), ColumnDescriptor.ALIGNMENT_LEFT, new ProjectEventColumnRenderer()));
        }
    }
    tableController.addColumnDescriptor(new CustomRenderColumnDescriptor("projectlist.tableheader.state", dataColumn++, null, ureq.getLocale(), ColumnDescriptor.ALIGNMENT_LEFT, new ProjectStateColumnRenderer()));
    tableController.addColumnDescriptor(new DefaultColumnDescriptor("projectlist.tableheader.numbers", dataColumn++, null, getLocale()) {

        @Override
        public int compareTo(int rowa, int rowb) {
            Object a = table.getTableDataModel().getValueAt(rowa, dataColumn);
            Object b = table.getTableDataModel().getValueAt(rowb, dataColumn);
            if (a == null || b == null) {
                boolean bb = (b == null);
                return (a == null) ? (bb ? 0 : -1) : (bb ? 1 : 0);
            }
            try {
                Long la = new Long((String) a);
                Long lb = new Long((String) b);
                return la.compareTo(lb);
            } catch (NumberFormatException e) {
                return super.compareTo(rowa, rowb);
            }
        }
    });
    String selectCmd = userCourseEnv.isCourseReadOnly() ? null : TABLE_ACTION_SELECT;
    tableController.addColumnDescriptor(new BooleanColumnDescriptor("projectlist.tableheader.select", dataColumn++, selectCmd, translate("table.action.select"), "-"));
    String cancelCmd = userCourseEnv.isCourseReadOnly() ? null : TABLE_ACTION_CANCEL_SELECT;
    tableController.addColumnDescriptor(new BooleanColumnDescriptor("projectlist.tableheader.cancel.select", dataColumn++, cancelCmd, translate("projectlist.tableheader.cancel.select"), "-"));
    return tableController;
}
Also used : CustomRenderColumnDescriptor(org.olat.core.gui.components.table.CustomRenderColumnDescriptor) TableController(org.olat.core.gui.components.table.TableController) StringOutput(org.olat.core.gui.render.StringOutput) BooleanColumnDescriptor(org.olat.core.gui.components.table.BooleanColumnDescriptor) Project(org.olat.course.nodes.projectbroker.datamodel.Project) Renderer(org.olat.core.gui.render.Renderer) CustomField(org.olat.course.nodes.projectbroker.datamodel.CustomField) TableGuiConfiguration(org.olat.core.gui.components.table.TableGuiConfiguration) DefaultColumnDescriptor(org.olat.core.gui.components.table.DefaultColumnDescriptor)

Example 49 with StringOutput

use of org.olat.core.gui.render.StringOutput in project openolat by klemens.

the class HotspotAssessmentItemBuilder method extractHotspotInteraction.

private void extractHotspotInteraction() {
    try (StringOutput sb = new StringOutput()) {
        List<Block> blocks = assessmentItem.getItemBody().getBlocks();
        for (Block block : blocks) {
            if (block instanceof HotspotInteraction) {
                hotspotInteraction = (HotspotInteraction) block;
                responseIdentifier = hotspotInteraction.getResponseIdentifier();
                break;
            } else {
                serializeJqtiObject(block, sb);
            }
        }
        question = sb.toString();
    } catch (IOException e) {
        log.error("", e);
    }
}
Also used : HotspotInteraction(uk.ac.ed.ph.jqtiplus.node.item.interaction.HotspotInteraction) AssessmentItemFactory.appendHotspotInteraction(org.olat.ims.qti21.model.xml.AssessmentItemFactory.appendHotspotInteraction) Block(uk.ac.ed.ph.jqtiplus.node.content.basic.Block) StringOutput(org.olat.core.gui.render.StringOutput) IOException(java.io.IOException)

Example 50 with StringOutput

use of org.olat.core.gui.render.StringOutput in project openolat by klemens.

the class MatchAssessmentItemBuilder method extractMatchInteraction.

private void extractMatchInteraction() {
    try (StringOutput sb = new StringOutput()) {
        List<Block> blocks = assessmentItem.getItemBody().getBlocks();
        for (Block block : blocks) {
            if (block instanceof MatchInteraction) {
                matchInteraction = (MatchInteraction) block;
                responseIdentifier = matchInteraction.getResponseIdentifier();
                shuffle = matchInteraction.getShuffle();
                break;
            } else {
                serializeJqtiObject(block, sb);
            }
        }
        question = sb.toString();
    } catch (IOException e) {
        log.error("", e);
    }
}
Also used : AssessmentItemFactory.appendMatchInteraction(org.olat.ims.qti21.model.xml.AssessmentItemFactory.appendMatchInteraction) MatchInteraction(uk.ac.ed.ph.jqtiplus.node.item.interaction.MatchInteraction) Block(uk.ac.ed.ph.jqtiplus.node.content.basic.Block) StringOutput(org.olat.core.gui.render.StringOutput) IOException(java.io.IOException)

Aggregations

StringOutput (org.olat.core.gui.render.StringOutput)188 IOException (java.io.IOException)48 URLBuilder (org.olat.core.gui.render.URLBuilder)30 Renderer (org.olat.core.gui.render.Renderer)26 Block (uk.ac.ed.ph.jqtiplus.node.content.basic.Block)24 RenderResult (org.olat.core.gui.render.RenderResult)22 Component (org.olat.core.gui.components.Component)10 Translator (org.olat.core.gui.translator.Translator)10 Locale (java.util.Locale)8 Window (org.olat.core.gui.components.Window)8 Form (org.olat.core.gui.components.form.flexible.impl.Form)8 VelocityRenderDecorator (org.olat.core.gui.render.velocity.VelocityRenderDecorator)8 ArrayList (java.util.ArrayList)6 StreamResult (javax.xml.transform.stream.StreamResult)6 Test (org.junit.Test)6 GlobalSettings (org.olat.core.gui.GlobalSettings)6 ComponentRenderer (org.olat.core.gui.components.ComponentRenderer)6 DefaultColumnDescriptor (org.olat.core.gui.components.table.DefaultColumnDescriptor)6 OLATRuntimeException (org.olat.core.logging.OLATRuntimeException)6 Matcher (java.util.regex.Matcher)5