Skip to content

Commit

Permalink
Enable lang selection without need for 'available-langs.properties' (#…
Browse files Browse the repository at this point in the history
…169)

* Enable lang selection without need for 'available-langs.properties'
Co-authored-by: Andrew Woods <awoods@duraspace.org>
  • Loading branch information
Andrew Woods authored Oct 6, 2020
1 parent 284bb4a commit f2dd7c1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package edu.cornell.mannlib.vitro.webapp.servlet.setup;

import static edu.cornell.mannlib.vitro.webapp.modelaccess.ModelAccess.WhichService.CONTENT;
import static edu.cornell.mannlib.vitro.webapp.servlet.setup.RDFFilesLoader.getEnabledLocales;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
Expand Down Expand Up @@ -46,6 +47,7 @@ public class FileGraphSetup implements ServletContextListener {
private static final Log log = LogFactory.getLog(FileGraphSetup.class);

private static final String RDF = "rdf";
private static final String I18N = "i18n";
private static final String ABOX = "abox";
private static final String TBOX = "tbox";
private static final String FILEGRAPH = "filegraph";
Expand Down Expand Up @@ -76,6 +78,12 @@ public void contextInitialized(ServletContextEvent sce) {
// ABox files
Set<Path> paths = getFilegraphPaths(ctx, RDF, ABOX, FILEGRAPH);

// Load ABox files from enabled languages
Set<String> enabledLocales = getEnabledLocales(ctx);
for (String locale : enabledLocales) {
paths.addAll(getFilegraphPaths(ctx, RDF, I18N, locale, ABOX, FILEGRAPH));
}

cleanupDB(dataset, pathsToURIs(paths, ABOX), ABOX);

// Just update the ABox filegraphs in the DB; don't attach them to a base model.
Expand All @@ -84,6 +92,11 @@ public void contextInitialized(ServletContextEvent sce) {
// TBox files
paths = getFilegraphPaths(ctx, RDF, TBOX, FILEGRAPH);

// Load TBox files from enabled languages
for (String locale : enabledLocales) {
paths.addAll(getFilegraphPaths(ctx, RDF, I18N, locale, TBOX, FILEGRAPH));
}

cleanupDB(dataset, pathsToURIs(paths, TBOX),TBOX);

OntModel tboxBaseModel = ModelAccess.on(ctx).getOntModel(ModelNames.TBOX_ASSERTIONS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,13 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.TreeSet;

import edu.cornell.mannlib.vitro.webapp.beans.ApplicationBean;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
import edu.cornell.mannlib.vitro.webapp.i18n.VitroResourceBundle;
import edu.cornell.mannlib.vitro.webapp.i18n.selection.SelectedLocale;
import edu.cornell.mannlib.vitro.webapp.modelaccess.ModelAccess;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

Expand All @@ -43,13 +36,12 @@ public class RDFFilesLoader {

private static final String DEFAULT_RDF_FORMAT = "RDF/XML";
private static final String RDF = "rdf";
private static final String I18N = "i18n";
private static final String FIRST_TIME = "firsttime";
private static final String EVERY_TIME = "everytime";

private static List<String> omittedLocales;

/**
* Path filter that ignores sub-directories, hidden files, markdown and non-enabled language
* Path filter that ignores sub-directories, hidden files and markdown
* files.
*/
private static final DirectoryStream.Filter<Path> RDF_FILE_FILTER = new DirectoryStream.Filter<Path>() {
Expand All @@ -66,15 +58,6 @@ public boolean accept(Path p) throws IOException {
if (p.toString().endsWith(".md")) {
return false;
}
// Skip language files that are not enabled
// ..Assumes all language files take the form: path/to/file/name-<locale>.extension
String basename = FilenameUtils.getBaseName(p.toString());
for (String omit : omittedLocales) {
if (basename.endsWith(omit)) {
log.info("Ignoring file not enabled in i18n configuration: " + p);
return false;
}
}
return true;
}
};
Expand All @@ -91,7 +74,16 @@ public static void loadFirstTimeFiles(ServletContext ctx, String modelPath, Mode
boolean firstTime) {
if (firstTime) {
String home = locateHomeDirectory();
Set<Path> paths = getPaths(ctx, home, RDF, modelPath, FIRST_TIME);

// Load common files
Set<Path> paths = getPaths(home, RDF, modelPath, FIRST_TIME);

// Load enabled languages
Set<String> enabledLocales = getEnabledLocales(ctx);
for (String locale : enabledLocales) {
paths.addAll(getPaths(home, RDF, I18N, locale, modelPath, FIRST_TIME));
}

for (Path p : paths) {
log.info("Loading " + relativePath(p, home));
readOntologyFileIntoModel(p, model);
Expand All @@ -114,14 +106,40 @@ public static void loadEveryTimeFiles(ServletContext ctx, String modelPath, OntM
OntModel everytimeModel = ModelFactory
.createOntologyModel(OntModelSpec.OWL_MEM);
String home = locateHomeDirectory();
Set<Path> paths = getPaths(ctx, home, RDF, modelPath, EVERY_TIME);

// Load common files
Set<Path> paths = getPaths(home, RDF, modelPath, EVERY_TIME);

// Load enabled languages
Set<String> enabledLocales = getEnabledLocales(ctx);
for (String locale : enabledLocales) {
paths.addAll(getPaths(home, RDF, I18N, locale, modelPath, EVERY_TIME));
}

for (Path p : paths) {
log.info("Loading " + relativePath(p, home));
readOntologyFileIntoModel(p, everytimeModel);
}
model.addSubModel(everytimeModel);
}

public static Set<String> getEnabledLocales(ServletContext ctx) {
Set<String> enabledLocales = new HashSet<>();

// Which locales are enabled in runtime.properties?
List<Locale> locales = SelectedLocale.getSelectableLocales(ctx);
for (Locale locale : locales) {
enabledLocales.add(locale.toLanguageTag().replace('-', '_'));
}

// If no languages were enabled in runtime.properties, add 'en_US' as the default
if (enabledLocales.isEmpty()) {
enabledLocales.add("en_US");
}

return enabledLocales;
}

private static Path relativePath(Path p, String home) {
try {
return Paths.get(home).relativize(p);
Expand All @@ -134,10 +152,7 @@ private static Path relativePath(Path p, String home) {
* Find the paths to RDF files in this directory. Sub-directories, hidden
* files, markdown, and non-enabled language files are ignored.
*/
private static Set<Path> getPaths(ServletContext ctx, String parentDir, String... strings) {
// Ensure that omitted locales are loaded and available
loadOmittedLocales(ctx);

private static Set<Path> getPaths(String parentDir, String... strings) {
Path dir = Paths.get(parentDir, strings);

Set<Path> paths = new TreeSet<>();
Expand All @@ -157,49 +172,6 @@ private static Set<Path> getPaths(ServletContext ctx, String parentDir, String..
return paths;
}

private static void loadOmittedLocales(ServletContext ctx) {
// Only load if 'omittedLocales' has not been initialized
if (omittedLocales == null) {
omittedLocales = new LinkedList<>();
List<String> enabledLocales = new LinkedList<>();

// Which locales are enabled in runtime.properties?
List<Locale> locales = SelectedLocale.getSelectableLocales(ctx);
for (Locale locale : locales) {
enabledLocales.add(locale.toLanguageTag().replace('-', '_'));
}

// Get ResourceBundle that contains listing of all available i18n languages
WebappDaoFactory wadf = ModelAccess.on(ctx).getWebappDaoFactory();
ApplicationBean app = wadf.getApplicationDao().getApplicationBean();
String availableLangsFilename = app.getAvailableLangsFile();
String themeI18nPath = "/not-used";
String appI18nPath = "/i18n";
VitroResourceBundle bundle = VitroResourceBundle.getBundle(
availableLangsFilename, ctx, appI18nPath, themeI18nPath,
ResourceBundle.Control.getControl(ResourceBundle.Control.FORMAT_PROPERTIES));

// This should not happen
if (bundle == null) {
throw new RuntimeException("Unable to find bundle: " + availableLangsFilename);
}

// If no languages were enabled in runtime.properties, add 'en_US' as the default
if (enabledLocales.isEmpty()) {
enabledLocales.add("en_US");
}

// Omitted locales are the available locales minus the enabled locales
Enumeration langs = bundle.getKeys();
while (langs.hasMoreElements()) {
String available = (String) langs.nextElement();
if (!enabledLocales.contains(available)) {
omittedLocales.add(available);
}
}
}
}

private static void readOntologyFileIntoModel(Path p, Model model) {
String format = getRdfFormat(p);
log.debug("Loading " + p);
Expand Down

0 comments on commit f2dd7c1

Please sign in to comment.