-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Aman Prashant <aman.prashant@broadcom.com>
- Loading branch information
Showing
11 changed files
with
397 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
203 changes: 203 additions & 0 deletions
203
...ne/src/main/java/org/eclipse/lsp/cobol/core/messages/CobolLSPropertiesResourceBundle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
/* | ||
* Copyright (c) 2022 Broadcom. | ||
* The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Broadcom, Inc. - initial API and implementation | ||
* | ||
*/ | ||
package org.eclipse.lsp.cobol.core.messages; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import lombok.NonNull; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.io.IOUtils; | ||
import org.eclipse.lsp.cobol.core.engine.dialects.WorkingFolderService; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URI; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* This is a multiple properties resource bundle. Resource bundles contain locale-specific objects. | ||
* When your program needs a locale-specific resource, a String for example, your program can load | ||
* it from the resource bundle that is appropriate for the current user's locale. | ||
*/ | ||
@Slf4j | ||
public class CobolLSPropertiesResourceBundle extends ResourceBundle { | ||
private final String baseName; | ||
public static final char BUNDLE_SEPARATOR = '_'; | ||
private final Properties properties; | ||
private final WorkingFolderService workingFolderService; | ||
private final Locale locale; | ||
|
||
public CobolLSPropertiesResourceBundle( | ||
String basename, Locale locale, WorkingFolderService workingFolderService) { | ||
this.workingFolderService = workingFolderService; | ||
this.baseName = basename; | ||
this.properties = new Properties(); | ||
this.locale = locale; | ||
setParent(ResourceBundle.getBundle(basename, locale)); | ||
} | ||
|
||
/** | ||
* Updates resource bundle for a supplied dialect. | ||
* | ||
* @param dialectName dialect for which resource needs to be updated | ||
* @throws IOException when resources for a dialect is not found | ||
*/ | ||
public void updateMessageResourceBundle(String dialectName) throws IOException { | ||
properties.putAll(load(dialectName, locale)); | ||
} | ||
|
||
private Properties load(String dialectName, Locale locale) throws IOException { | ||
Properties properties = new Properties(); | ||
List<String> resourceName = toSuspectedBundleNames(locale); | ||
Collections.reverse(resourceName); | ||
URI workingFolder = this.workingFolderService.getWorkingFolder(); | ||
InputStream validResources = | ||
getDialectResources(resourceName, workingFolder, getJarName(dialectName)); | ||
properties.load(validResources); | ||
return properties; | ||
} | ||
|
||
private String getJarName(String dialectName) { | ||
return "dialect-" + dialectName + ".jar"; | ||
} | ||
|
||
@VisibleForTesting | ||
public InputStream getDialectResources( | ||
List<String> resourceName, URI workingFolder, String jarName) throws IOException { | ||
for (String s : resourceName) { | ||
URL url = new URL("jar:" + workingFolder + "/" + jarName + "!/" + s); | ||
if (jarUrlExists(url)) { | ||
return url.openStream(); | ||
} else { | ||
LOG.debug("Resource Bundle " + s + " not available at " + jarName); | ||
} | ||
} | ||
return IOUtils.toInputStream("", StandardCharsets.UTF_8); | ||
} | ||
|
||
private boolean jarUrlExists(URL url) { | ||
try { | ||
IOUtils.toString(url.openStream(), StandardCharsets.UTF_8); | ||
return true; | ||
} catch (IOException e) { | ||
return false; | ||
} | ||
} | ||
|
||
private List<String> toSuspectedBundleNames(String baseName, Locale locale) { | ||
String language = locale.getLanguage(); | ||
String script = locale.getScript(); | ||
String country = locale.getCountry(); | ||
String variant = locale.getVariant(); | ||
|
||
if (Objects.equals(language, "") | ||
&& Objects.equals(country, "") | ||
&& Objects.equals(variant, "")) { | ||
return Collections.singletonList(baseName); | ||
} | ||
|
||
StringBuilder sb = new StringBuilder(baseName); | ||
sb.append(BUNDLE_SEPARATOR); | ||
if (!Objects.equals(script, "")) { | ||
if (!Objects.equals(variant, "")) { | ||
sb.append(language) | ||
.append(BUNDLE_SEPARATOR) | ||
.append(script) | ||
.append(BUNDLE_SEPARATOR) | ||
.append(country) | ||
.append(BUNDLE_SEPARATOR) | ||
.append(variant); | ||
} else if (!Objects.equals(country, "")) { | ||
sb.append(language) | ||
.append(BUNDLE_SEPARATOR) | ||
.append(script) | ||
.append(BUNDLE_SEPARATOR) | ||
.append(country); | ||
} else { | ||
sb.append(language).append(BUNDLE_SEPARATOR).append(script); | ||
} | ||
} else { | ||
if (!Objects.equals(variant, "")) { | ||
sb.append(language) | ||
.append(BUNDLE_SEPARATOR) | ||
.append(country) | ||
.append(BUNDLE_SEPARATOR) | ||
.append(variant); | ||
} else if (!Objects.equals(country, "")) { | ||
sb.append(language).append(BUNDLE_SEPARATOR).append(country); | ||
} else { | ||
sb.append(language); | ||
} | ||
} | ||
return possibleResources(sb); | ||
} | ||
|
||
private List<String> toSuspectedBundleNames(Locale locale) { | ||
return toSuspectedBundleNames(this.baseName, locale); | ||
} | ||
|
||
private List<String> possibleResources(StringBuilder sb) { | ||
List<String> result = new ArrayList<>(); | ||
String[] s = sb.toString().split("_"); | ||
final String[] prevStr = {""}; | ||
Arrays.stream(s) | ||
.forEach( | ||
str -> { | ||
if (Objects.equals(prevStr[0], "")) { | ||
prevStr[0] = str; | ||
} else { | ||
prevStr[0] = String.join("_", prevStr[0], str); | ||
} | ||
result.add(prevStr[0] + ".properties"); | ||
}); | ||
return result; | ||
} | ||
|
||
/** | ||
* Gets an object for the given key from this resource bundle. Returns null if this resource | ||
* bundle does not contain an object for the given key. | ||
* | ||
* @param key the key for the desired object | ||
* @return the object for the given key, or Key | ||
*/ | ||
@Override | ||
protected Object handleGetObject(@NonNull String key) { | ||
try { | ||
return parent.getObject(key); | ||
} catch (MissingResourceException e) { | ||
if (properties != null && properties.get(key) != null) { | ||
return properties.get(key); | ||
} else { | ||
return key; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Returns an enumeration of the keys. | ||
* | ||
* @return an <code>Enumeration</code> of the keys contained in this <code>ResourceBundle</code> | ||
* and its parent bundles. | ||
*/ | ||
@Override | ||
public Enumeration<String> getKeys() { | ||
Set<String> parentKeys = parent.keySet(); | ||
Set<String> propKeys = properties.keySet().stream().map(o -> (String) o).collect(Collectors.toSet()); | ||
parentKeys.addAll(propKeys); | ||
return Collections.enumeration(parentKeys); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.