Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2017, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
package com.force.i18n.grammar.parser;
import java.io.IOException;
import java.net.URL;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.logging.Logger;
import com.force.i18n.*;
import com.force.i18n.LanguageLabelSetDescriptor.GrammaticalLabelSetDescriptor;
import com.force.i18n.grammar.*;
import com.force.i18n.grammar.impl.LanguageDeclensionFactory;
import com.force.i18n.settings.*;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.guava.CaffeinatedGuava;
import com.google.common.cache.*;
import com.google.common.util.concurrent.UncheckedExecutionException;
/**
* GrammaticalLabelSetLoader which loads GrammaticalLabelSets from one or more files, and a dictionary file.
*
* The labelset is stored in one or more XML files with the ini-style schema. The dictionary is in an XML file, called
* names.xml. All of these files are organized in the filesystem relative to the locale to which they apply.
*
* This Loader implements an in-memory cache map to prevent loading the same labelset more than once.
*
* This uses an in-memory cache for the dictionaries.
*
* This has optional support for a parentLoader, however it will share a common dictionary per language
* if you use it (for performance reasons).
*
* @author nveeser,stamm
*/
public class GrammaticalLabelSetLoader implements GrammaticalLabelSetProvider {
private static final Logger logger = Logger.getLogger(GrammaticalLabelSetLoader.class.getName());
private static final boolean USE_SHARED_KEYS_DEFAULT = true; // You really want this, so it isn't an option
//@GuardedBy("itself") // this is passed as seed data to multiple threads and modifications need to be synchronized
private final SharedKeyMap> seedKeyMap;
private final GrammaticalLabelSetProvider parentProvider;
private final GrammaticalLabelSetDescriptor baseDesc;
private final LoadingCache cache;
// These leak to the computable above.
protected final boolean useSharedKeys;
protected final Set publicSections = new HashSet<>();
// respect HumanLanguage#isTranslatedLanguage. see #compute(GrammaticalLabelSetDescriptor) how it's used.
private boolean useTranslatedLanguage;
// used only if useTranslatedLanguage is ture. see #compute(GrammaticalLabelSetDescriptor) how it's used.
private boolean skipParsingLabelForPlatform = false;
@Override
public void init() {
// do nothing
}
@Override
public void initEnglish() {
init();
}
/**
* Used only for debugging purposes.
*/
@Override
public void resetMap() {
// invalidate everything, include parent.
resetMap(null, true);
}
/**
* Utility method to invalidate cached data. After calling this method, the next {@link #getSet(HumanLanguage)} call
* will load label data from the source XML file.
*
* @param languages
* collection of languages to remove from cache. if {@code languages} is {@code null} or
* {@code languages.isEmpty()} is {@code true}, removes everything from cache.
* @param resetParent
* if {@code true}, also calls parent provider to reset, {@code false} otherwise.
*/
public void resetMap(Collection extends HumanLanguage> languages, boolean resetParent) {
if (resetParent && parentProvider != null) {
// Invalidate parent loader cache.
if (parentProvider instanceof GrammaticalLabelSetLoader) {
((GrammaticalLabelSetLoader)parentProvider).resetMap(languages, resetParent);
} else {
parentProvider.resetMap();
}
}
if (languages == null || languages.isEmpty()) {
cache.invalidateAll();
} else {
for (HumanLanguage lang: languages) {
cache.invalidate(getDescriptor(lang));
}
}
}
/**
* @deprecated use {@link #GrammaticalLabelSetLoader(GrammaticalLabelSetDescriptor)}
*/
@Deprecated
public GrammaticalLabelSetLoader(URL baseDir, String labelSetName) {
this(baseDir, labelSetName, null);
}
public GrammaticalLabelSetLoader(URL baseDir, String labelSetName, GrammaticalLabelSetProvider parent) {
this(new LabelSetDescriptorImpl(baseDir, LanguageProviderFactory.get().getBaseLanguage(), labelSetName), parent);
}
public GrammaticalLabelSetLoader(GrammaticalLabelSetDescriptor baseDesc) {
this(baseDesc, USE_SHARED_KEYS_DEFAULT, null);
}
public GrammaticalLabelSetLoader(GrammaticalLabelSetDescriptor baseDesc, GrammaticalLabelSetProvider parent) {
this(baseDesc, USE_SHARED_KEYS_DEFAULT, parent);
}
public GrammaticalLabelSetLoader(GrammaticalLabelSetDescriptor baseDesc, boolean useSharedKeys, GrammaticalLabelSetProvider parent) {
this(new LabelSetLoaderConfig(baseDesc, parent), useSharedKeys);
}
public GrammaticalLabelSetLoader(LabelSetLoaderConfig config) {
this(config, USE_SHARED_KEYS_DEFAULT);
}
public GrammaticalLabelSetLoader(LabelSetLoaderConfig config, boolean useSharedKeys) {
this.baseDesc = config.getDescriptor();
this.useSharedKeys = useSharedKeys;
this.parentProvider = config.getParent();
setUseTranslatedLanguage(config.useTranslatedLanguage());
setSkipParsingLabelForPlatform(config.skipParsingLabelForPlatform());
// Share the keys of the parent loader if possible
if (this.useSharedKeys) {
if (parentProvider instanceof GrammaticalLabelSetLoader && ((GrammaticalLabelSetLoader) parentProvider).useSharedKeys) {
this.seedKeyMap = new SharedKeyMap<>(((GrammaticalLabelSetLoader) parentProvider).seedKeyMap);
} else {
this.seedKeyMap = new SharedKeyMap<>();
}
} else {
this.seedKeyMap = null;
}
this.cache = CaffeinatedGuava.build(getCacheBuilder(config), getCacheLoader());
}
protected Caffeine