Search in sources :

Example 6 with OLATRuntimeException

use of org.olat.core.logging.OLATRuntimeException in project OpenOLAT by OpenOLAT.

the class FileUtils method getInputStream.

public static InputStream getInputStream(String data, String encoding) {
    try {
        byte[] ba = data.getBytes(StringHelper.check4xMacRoman(encoding));
        ByteArrayInputStream bis = new ByteArrayInputStream(ba);
        return bis;
    } catch (IOException e) {
        throw new OLATRuntimeException(FileUtils.class, "could not save to output stream", e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) IOException(java.io.IOException)

Example 7 with OLATRuntimeException

use of org.olat.core.logging.OLATRuntimeException in project OpenOLAT by OpenOLAT.

the class I18nManager method createNewLanguage.

public boolean createNewLanguage(String localeKey, String languageInEnglish, String languageTranslated, String authors) {
    if (!i18nModule.isTransToolEnabled()) {
        throw new AssertException("Can not create a new language when the translation tool is not enabled and the transtool source pathes are not configured! Check your olat.properties files");
    }
    if (i18nModule.getAvailableLanguageKeys().contains(localeKey)) {
        return false;
    }
    // Create new property file in the brasato bundle and re-initialize
    // everything
    String coreFallbackBundle = i18nModule.getApplicationFallbackBundle();
    File transToolCoreLanguagesDir = i18nModule.getTransToolApplicationOptLanguagesSrcDir();
    String i18nDirRelPath = "/" + coreFallbackBundle.replace(".", "/") + "/" + I18nManager.I18N_DIRNAME;
    File transToolCoreLanguagesDir_I18n = new File(transToolCoreLanguagesDir, i18nDirRelPath);
    File newPropertiesFile = new File(transToolCoreLanguagesDir_I18n, I18nModule.LOCAL_STRINGS_FILE_PREFIX + localeKey + I18nModule.LOCAL_STRINGS_FILE_POSTFIX);
    // Prepare property file
    // Use a sorted properties object that saves the keys sorted alphabetically to disk
    Properties newProperties = new SortedProperties();
    if (StringHelper.containsNonWhitespace(languageInEnglish)) {
        newProperties.setProperty("this.language.in.english", languageInEnglish);
    }
    if (StringHelper.containsNonWhitespace(languageTranslated)) {
        newProperties.setProperty("this.language.translated", languageTranslated);
    }
    if (StringHelper.containsNonWhitespace(authors)) {
        newProperties.setProperty("this.language.translator.names", authors);
    }
    OutputStream fileStream = null;
    try {
        // Create necessary directories
        File directory = newPropertiesFile.getParentFile();
        if (!directory.exists())
            directory.mkdirs();
        // Write to file file now
        fileStream = new FileOutputStream(newPropertiesFile);
        newProperties.store(fileStream, null);
        fileStream.flush();
        // Now set new language as enabled to allow user to translate the language.
        Collection<String> enabledLangKeys = i18nModule.getEnabledLanguageKeys();
        enabledLangKeys.add(localeKey);
        // Reinitialize languages with new language
        i18nModule.reInitializeAndFlushCache();
        // Now add new language as new language (will re-initialize everything a second time)
        i18nModule.setEnabledLanguageKeys(enabledLangKeys);
        return true;
    } catch (FileNotFoundException e) {
        throw new OLATRuntimeException("Could not create new language file::" + newPropertiesFile.getAbsolutePath(), e);
    } catch (IOException e) {
        throw new OLATRuntimeException("Could not create new language file::" + newPropertiesFile.getAbsolutePath() + ", maybe permission denied? Check your directory permissions", e);
    } finally {
        try {
            if (fileStream != null)
                fileStream.close();
        } catch (IOException e) {
            log.error("Could not close stream after creating new language file::" + newPropertiesFile.getAbsolutePath(), e);
        }
    }
}
Also used : AssertException(org.olat.core.logging.AssertException) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) JarOutputStream(java.util.jar.JarOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) SortedProperties(org.olat.core.util.SortedProperties) Properties(java.util.Properties) SortedProperties(org.olat.core.util.SortedProperties) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 8 with OLATRuntimeException

use of org.olat.core.logging.OLATRuntimeException in project OpenOLAT by OpenOLAT.

the class TranslationDevManager method getLostTranslationsFromBranch.

/**
 * once again check for keys in branch (lost keys) and move them to Head
 *
 * reallyCopy: set to true to create Props/keys in Head, false: only log them
 */
public void getLostTranslationsFromBranch(boolean reallyCopy, String[] referenceLanguages, String pathToOlatBranch, String pathToCoreBranch) {
    List<String> allBundles = new ArrayList<String>(i18nModule.getBundleNamesContainingI18nFiles());
    Set<String> allLangs = getAllLanguages();
    // loop over all langs
    int totalCounter = 0;
    for (String langKey : allLangs) {
        int langCounter = 0;
        // ignore ref langs
        boolean isRefLang = false;
        for (String refLangKey : referenceLanguages) {
            if (refLangKey.equals(langKey)) {
                isRefLang = true;
                break;
            }
        }
        if (isRefLang)
            continue;
        // load current language
        Locale locale = i18nMgr.getLocaleOrNull(langKey);
        for (String bundleName : allBundles) {
            int bundleCounter = 0;
            // get valid keys from ref langs and this bundle
            Set<String> allValidKeys = new HashSet<String>();
            for (String refLangKey : referenceLanguages) {
                Properties properties = i18nMgr.getPropertiesWithoutResolvingRecursively(i18nMgr.getLocaleOrNull(refLangKey), bundleName);
                if (properties == null) {
                    throw new OLATRuntimeException("Invalid reference language::" + refLangKey, null);
                } else {
                    for (Object keyObj : properties.keySet()) {
                        String key = (String) keyObj;
                        allValidKeys.add(key);
                    }
                }
            }
            // for
            // check if bundle + this locale exists in branch
            String bundlePath = bundleName.replace(".", "/");
            String langPropFileName = I18nModule.LOCAL_STRINGS_FILE_PREFIX + langKey + I18nModule.LOCAL_STRINGS_FILE_POSTFIX;
            File bundleInOlat = new File(pathToOlatBranch + bundlePath + "/" + I18nManager.I18N_DIRNAME + "/" + langPropFileName);
            File bundleInCore = new File(pathToCoreBranch + bundlePath + "/" + I18nManager.I18N_DIRNAME + "/" + langPropFileName);
            File bundleToUse;
            if (bundleInOlat.exists()) {
                bundleToUse = bundleInOlat;
            } else if (bundleInCore.exists()) {
                bundleToUse = bundleInCore;
            } else {
                // no bundle found in branch, its not even worth to look after keys
                log.debug("getLostTrans: no OLD prop file found in BRANCH for locale: " + locale + " and bundle: " + bundleName + " => continue with next bundle");
                continue;
            }
            // look after all valid keys in given lang in Head
            Properties targetProperties = i18nMgr.getPropertiesWithoutResolvingRecursively(locale, bundleName);
            Set<Object> targetLangBundleKeys = targetProperties.keySet();
            Properties oldProps = new Properties();
            FileInputStream is;
            try {
                is = new FileInputStream(bundleToUse);
                oldProps.load(is);
                is.close();
            } catch (Exception e) {
                log.error("", e);
            }
            for (Object keyObj : allValidKeys) {
                String key = (String) keyObj;
                if (targetLangBundleKeys.contains(key)) {
                // everything ok
                } else {
                    // try to load key from branch
                    if (oldProps.containsKey(key)) {
                        String oldValue = oldProps.getProperty(key);
                        if (StringHelper.containsNonWhitespace(oldValue) && !oldValue.trim().startsWith("TODO")) {
                            langCounter++;
                            bundleCounter++;
                            totalCounter++;
                            if (reallyCopy) {
                                addKey(locale, bundleName, key, oldValue);
                            } else {
                                log.debug("getLostTrans: add a key from BRANCH to locale: " + locale + " and bundle: " + bundleName + " key: " + key);
                            }
                        } else {
                            log.debug("getLostTrans: ignoring invalid value::'" + oldValue + "' from BRANCH to locale: " + locale + " and bundle: " + bundleName + " key: " + key);
                        }
                    }
                }
            }
            // for: keys
            if (bundleCounter > 0)
                log.info("Changed " + bundleCounter + " keys for locale: " + locale + " and bundle: " + bundleName);
        }
        // for: bundles
        if (langCounter > 0)
            log.info("Changed " + langCounter + " keys for locale: " + locale);
    }
    // for: langs
    if (totalCounter > 0)
        log.info("Changed " + totalCounter + " keys in total");
}
Also used : Locale(java.util.Locale) ArrayList(java.util.ArrayList) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) IOException(java.io.IOException) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) File(java.io.File) HashSet(java.util.HashSet)

Example 9 with OLATRuntimeException

use of org.olat.core.logging.OLATRuntimeException in project OpenOLAT by OpenOLAT.

the class TranslationDevManager method removeDeletedKeys.

/**
 * Check for keys that exist in target languages but not in EN or DE. Delete
 * such keys in the target languages
 *
 * @param reallyRemoveIt true: really delete; false: verbose dry run
 * @param referenceLanguages array that contains the language keys that serves
 *          as a reference (e.g. en and de)
 * @param languages the languages that should be cleaned up
 */
public void removeDeletedKeys(boolean reallyRemoveIt, String[] referenceLanguages, Set<String> languages) {
    // first get all available keys from de and en language
    Set<String> validCombinedKeys = new HashSet<String>();
    // copy list to prevent concurrent modification exception
    List<String> allBundles = new ArrayList<String>(i18nModule.getBundleNamesContainingI18nFiles());
    for (String bundleName : allBundles) {
        for (String refLangKey : referenceLanguages) {
            Properties properties = i18nMgr.getPropertiesWithoutResolvingRecursively(i18nMgr.getLocaleOrNull(refLangKey), bundleName);
            if (properties == null) {
                throw new OLATRuntimeException("Invalid reference language::" + refLangKey, null);
            } else {
                for (Object keyObj : properties.keySet()) {
                    String key = (String) keyObj;
                    String combinedKey = bundleName + ":" + key;
                    validCombinedKeys.add(combinedKey);
                }
            }
        }
    }
    log.info("removeDeletedKeys: found " + validCombinedKeys.size() + " valid keys in " + referenceLanguages);
    // remove keys not in the valid set
    for (String langKey : languages) {
        boolean isRefLang = false;
        for (String refLangKey : referenceLanguages) {
            if (refLangKey.equals(langKey)) {
                isRefLang = true;
                break;
            }
        }
        if (isRefLang)
            continue;
        // Not a reference language - delete from here
        Locale locale = i18nMgr.getLocaleOrNull(langKey);
        for (String bundleName : allBundles) {
            Properties properties = i18nMgr.getPropertiesWithoutResolvingRecursively(locale, bundleName);
            int propCount = properties.size();
            // copy keys to prevent concurrent modification
            Set<String> availableKeys = new HashSet<String>();
            for (Object key : properties.keySet()) {
                availableKeys.add((String) key);
            }
            for (String key : availableKeys) {
                String combinedKey = bundleName + ":" + key;
                if (!validCombinedKeys.contains(combinedKey)) {
                    if (reallyRemoveIt) {
                        log.info("Deleting " + langKey + ":" + bundleName + ":" + key + " - does not exist in " + referenceLanguages);
                        properties.remove(key);
                    } else {
                        log.info("Should be deleted: " + langKey + ":" + bundleName + ":" + key + " - does not exist in " + referenceLanguages);
                    }
                }
            }
            int delCount = (propCount - properties.size());
            if (reallyRemoveIt && delCount > 0) {
                // only save when changed
                i18nMgr.saveOrUpdateProperties(properties, locale, bundleName);
                log.info("For language::" + langKey + " the in bundle:: " + bundleName + " deleted " + delCount + " keys");
            }
            // delete empty bundles
            if (reallyRemoveIt && properties.size() == 0) {
                i18nMgr.deleteProperties(locale, bundleName);
                log.info("Bundle:: " + bundleName + " deleted for language " + langKey + "entirely because it was empty");
            }
        }
    }
}
Also used : Locale(java.util.Locale) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) ArrayList(java.util.ArrayList) Properties(java.util.Properties) HashSet(java.util.HashSet)

Example 10 with OLATRuntimeException

use of org.olat.core.logging.OLATRuntimeException in project OpenOLAT by OpenOLAT.

the class OWASPAntiSamyXSSFilter method printOriginStackTrace.

private void printOriginStackTrace() {
    // use stacktrace to find out more where the filter was used
    OLATRuntimeException ore = new OLATRuntimeException("XSSFilter dummy", null);
    final Writer result = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(result);
    ore.printStackTrace(printWriter);
}
Also used : StringWriter(java.io.StringWriter) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) PrintWriter(java.io.PrintWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter)

Aggregations

OLATRuntimeException (org.olat.core.logging.OLATRuntimeException)268 IOException (java.io.IOException)104 File (java.io.File)50 ModuleConfiguration (org.olat.modules.ModuleConfiguration)26 ArrayList (java.util.ArrayList)22 AssertException (org.olat.core.logging.AssertException)22 FileOutputStream (java.io.FileOutputStream)20 OutputStream (java.io.OutputStream)20 Properties (java.util.Properties)20 FileInputStream (java.io.FileInputStream)18 HashMap (java.util.HashMap)18 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)18 QTIItemObject (org.olat.ims.qti.export.helper.QTIItemObject)18 DefaultElement (org.dom4j.tree.DefaultElement)16 Element (org.jdom.Element)16 InputStream (java.io.InputStream)14 BufferedInputStream (java.io.BufferedInputStream)12 List (java.util.List)12 Document (org.dom4j.Document)12 CPItem (org.olat.ims.cp.objects.CPItem)12