Search in sources :

Example 1 with GlossaryItem

use of org.olat.core.commons.modules.glossary.GlossaryItem in project OpenOLAT by OpenOLAT.

the class TextMarkerJsGenerator method buildJSArrayString.

/*
	 * build array of glossaryTerms containing array with term, flexion, synonym...
	 */
public static StringBuilder buildJSArrayString(List<GlossaryItem> glossaryItemArr) {
    StringBuilder sb = new StringBuilder();
    sb.append("new Array(");
    for (Iterator<GlossaryItem> iterator = glossaryItemArr.iterator(); iterator.hasNext(); ) {
        GlossaryItem glossaryItem = iterator.next();
        ArrayList<String> allHighlightStrings = glossaryItem.getAllStringsToMarkup();
        sb.append("new Array(\"");
        for (Iterator<String> iterator2 = allHighlightStrings.iterator(); iterator2.hasNext(); ) {
            String termFlexionSynonym = iterator2.next();
            String javaEscapedTermFlexionSynonym = StringEscapeUtils.escapeJava(termFlexionSynonym);
            sb.append(javaEscapedTermFlexionSynonym).append("\"");
            if (!termFlexionSynonym.equals(javaEscapedTermFlexionSynonym)) {
                String htmlEscapedTermFlexionSynonym = StringHelper.escapeHtml(termFlexionSynonym);
                sb.append(",\"").append(htmlEscapedTermFlexionSynonym).append("\"");
            }
            if (iterator2.hasNext()) {
                sb.append(",\"");
            }
        }
        sb.append(")");
        if (iterator.hasNext()) {
            sb.append(",");
        }
    }
    sb.append(");");
    return sb;
}
Also used : GlossaryItem(org.olat.core.commons.modules.glossary.GlossaryItem)

Example 2 with GlossaryItem

use of org.olat.core.commons.modules.glossary.GlossaryItem in project openolat by klemens.

the class GlossaryDefinitionMapper method handle.

/**
 * @see org.olat.core.dispatcher.mapper.Mapper#handle(java.lang.String,
 *      javax.servlet.http.HttpServletRequest)
 */
public MediaResource handle(String relPath, HttpServletRequest request) {
    GlossaryItemManager gIM = GlossaryItemManager.getInstance();
    String[] parts = relPath.split("/");
    String glossaryId = parts[1];
    String glossaryFolderString = FolderConfig.getCanonicalRoot() + FolderConfig.getRepositoryHome() + "/" + glossaryId + "/" + GlossaryMarkupItemController.INTERNAL_FOLDER_NAME;
    File glossaryFolderFile = new File(glossaryFolderString);
    if (!glossaryFolderFile.isDirectory()) {
        logWarn("GlossaryDefinition delivery failed; path to glossaryFolder not existing: " + relPath, null);
        return new NotFoundMediaResource();
    }
    VFSContainer glossaryFolder = new LocalFolderImpl(glossaryFolderFile);
    if (!gIM.isFolderContainingGlossary(glossaryFolder)) {
        logWarn("GlossaryDefinition delivery failed; glossaryFolder doesn't contain a valid Glossary: " + glossaryFolder, null);
        return new NotFoundMediaResource();
    }
    String glossaryMainTerm = parts[2];
    if (parts.length > 2) {
        // this handle / or \ in a term
        for (int i = 3; i < parts.length; i++) {
            glossaryMainTerm += "/" + parts[i];
        }
    }
    // cut away ".html" if necessary
    if (glossaryMainTerm.endsWith(".html")) {
        glossaryMainTerm = glossaryMainTerm.substring(0, glossaryMainTerm.length() - 5);
    }
    glossaryMainTerm = glossaryMainTerm.toLowerCase();
    Set<String> alternatives = new HashSet<>();
    prepareAlternatives(glossaryMainTerm, alternatives);
    // Create a media resource
    StringMediaResource resource = new StringMediaResource() {

        @Override
        public void prepare(HttpServletResponse hres) {
        // don't use normal string media headers which prevent caching,
        // use standard browser caching based on last modified timestamp
        }
    };
    resource.setLastModified(gIM.getGlossaryLastModifiedTime(glossaryFolder));
    resource.setContentType("text/html");
    List<GlossaryItem> glossItems = gIM.getGlossaryItemListByVFSItem(glossaryFolder);
    GlossaryItem foundItem = null;
    for (GlossaryItem glossaryItem : glossItems) {
        String item = glossaryItem.getGlossTerm().toLowerCase();
        if (alternatives.contains(item)) {
            foundItem = glossaryItem;
            break;
        }
    }
    if (foundItem == null) {
        return new NotFoundMediaResource();
    }
    StringBuilder sb = new StringBuilder();
    sb.append("<dd><dt>").append(foundItem.getGlossTerm()).append("</dt><dl>").append(foundItem.getGlossDef()).append("</dl></dd>");
    resource.setData(sb.toString());
    resource.setEncoding("utf-8");
    if (isLogDebugEnabled())
        logDebug("loaded definition for " + glossaryMainTerm, null);
    return resource;
}
Also used : NotFoundMediaResource(org.olat.core.gui.media.NotFoundMediaResource) VFSContainer(org.olat.core.util.vfs.VFSContainer) HttpServletResponse(javax.servlet.http.HttpServletResponse) LocalFolderImpl(org.olat.core.util.vfs.LocalFolderImpl) GlossaryItemManager(org.olat.core.commons.modules.glossary.GlossaryItemManager) StringMediaResource(org.olat.core.gui.media.StringMediaResource) File(java.io.File) HashSet(java.util.HashSet) GlossaryItem(org.olat.core.commons.modules.glossary.GlossaryItem)

Example 3 with GlossaryItem

use of org.olat.core.commons.modules.glossary.GlossaryItem in project OpenOLAT by OpenOLAT.

the class GlossaryDefinitionMapper method handle.

/**
 * @see org.olat.core.dispatcher.mapper.Mapper#handle(java.lang.String,
 *      javax.servlet.http.HttpServletRequest)
 */
public MediaResource handle(String relPath, HttpServletRequest request) {
    GlossaryItemManager gIM = GlossaryItemManager.getInstance();
    String[] parts = relPath.split("/");
    String glossaryId = parts[1];
    String glossaryFolderString = FolderConfig.getCanonicalRoot() + FolderConfig.getRepositoryHome() + "/" + glossaryId + "/" + GlossaryMarkupItemController.INTERNAL_FOLDER_NAME;
    File glossaryFolderFile = new File(glossaryFolderString);
    if (!glossaryFolderFile.isDirectory()) {
        logWarn("GlossaryDefinition delivery failed; path to glossaryFolder not existing: " + relPath, null);
        return new NotFoundMediaResource();
    }
    VFSContainer glossaryFolder = new LocalFolderImpl(glossaryFolderFile);
    if (!gIM.isFolderContainingGlossary(glossaryFolder)) {
        logWarn("GlossaryDefinition delivery failed; glossaryFolder doesn't contain a valid Glossary: " + glossaryFolder, null);
        return new NotFoundMediaResource();
    }
    String glossaryMainTerm = parts[2];
    if (parts.length > 2) {
        // this handle / or \ in a term
        for (int i = 3; i < parts.length; i++) {
            glossaryMainTerm += "/" + parts[i];
        }
    }
    // cut away ".html" if necessary
    if (glossaryMainTerm.endsWith(".html")) {
        glossaryMainTerm = glossaryMainTerm.substring(0, glossaryMainTerm.length() - 5);
    }
    glossaryMainTerm = glossaryMainTerm.toLowerCase();
    Set<String> alternatives = new HashSet<>();
    prepareAlternatives(glossaryMainTerm, alternatives);
    // Create a media resource
    StringMediaResource resource = new StringMediaResource() {

        @Override
        public void prepare(HttpServletResponse hres) {
        // don't use normal string media headers which prevent caching,
        // use standard browser caching based on last modified timestamp
        }
    };
    resource.setLastModified(gIM.getGlossaryLastModifiedTime(glossaryFolder));
    resource.setContentType("text/html");
    List<GlossaryItem> glossItems = gIM.getGlossaryItemListByVFSItem(glossaryFolder);
    GlossaryItem foundItem = null;
    for (GlossaryItem glossaryItem : glossItems) {
        String item = glossaryItem.getGlossTerm().toLowerCase();
        if (alternatives.contains(item)) {
            foundItem = glossaryItem;
            break;
        }
    }
    if (foundItem == null) {
        return new NotFoundMediaResource();
    }
    StringBuilder sb = new StringBuilder();
    sb.append("<dd><dt>").append(foundItem.getGlossTerm()).append("</dt><dl>").append(foundItem.getGlossDef()).append("</dl></dd>");
    resource.setData(sb.toString());
    resource.setEncoding("utf-8");
    if (isLogDebugEnabled())
        logDebug("loaded definition for " + glossaryMainTerm, null);
    return resource;
}
Also used : NotFoundMediaResource(org.olat.core.gui.media.NotFoundMediaResource) VFSContainer(org.olat.core.util.vfs.VFSContainer) HttpServletResponse(javax.servlet.http.HttpServletResponse) LocalFolderImpl(org.olat.core.util.vfs.LocalFolderImpl) GlossaryItemManager(org.olat.core.commons.modules.glossary.GlossaryItemManager) StringMediaResource(org.olat.core.gui.media.StringMediaResource) File(java.io.File) HashSet(java.util.HashSet) GlossaryItem(org.olat.core.commons.modules.glossary.GlossaryItem)

Example 4 with GlossaryItem

use of org.olat.core.commons.modules.glossary.GlossaryItem in project openolat by klemens.

the class TextMarkerJsGenerator method buildJSArrayString.

/*
	 * build array of glossaryTerms containing array with term, flexion, synonym...
	 */
public static StringBuilder buildJSArrayString(List<GlossaryItem> glossaryItemArr) {
    StringBuilder sb = new StringBuilder();
    sb.append("new Array(");
    for (Iterator<GlossaryItem> iterator = glossaryItemArr.iterator(); iterator.hasNext(); ) {
        GlossaryItem glossaryItem = iterator.next();
        ArrayList<String> allHighlightStrings = glossaryItem.getAllStringsToMarkup();
        sb.append("new Array(\"");
        for (Iterator<String> iterator2 = allHighlightStrings.iterator(); iterator2.hasNext(); ) {
            String termFlexionSynonym = iterator2.next();
            String javaEscapedTermFlexionSynonym = StringEscapeUtils.escapeJava(termFlexionSynonym);
            sb.append(javaEscapedTermFlexionSynonym).append("\"");
            if (!termFlexionSynonym.equals(javaEscapedTermFlexionSynonym)) {
                String htmlEscapedTermFlexionSynonym = StringHelper.escapeHtml(termFlexionSynonym);
                sb.append(",\"").append(htmlEscapedTermFlexionSynonym).append("\"");
            }
            if (iterator2.hasNext()) {
                sb.append(",\"");
            }
        }
        sb.append(")");
        if (iterator.hasNext()) {
            sb.append(",");
        }
    }
    sb.append(");");
    return sb;
}
Also used : GlossaryItem(org.olat.core.commons.modules.glossary.GlossaryItem)

Aggregations

GlossaryItem (org.olat.core.commons.modules.glossary.GlossaryItem)4 File (java.io.File)2 HashSet (java.util.HashSet)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 GlossaryItemManager (org.olat.core.commons.modules.glossary.GlossaryItemManager)2 NotFoundMediaResource (org.olat.core.gui.media.NotFoundMediaResource)2 StringMediaResource (org.olat.core.gui.media.StringMediaResource)2 LocalFolderImpl (org.olat.core.util.vfs.LocalFolderImpl)2 VFSContainer (org.olat.core.util.vfs.VFSContainer)2