Skip to content

Commit

Permalink
Reformate with custom Google Java Style
Browse files Browse the repository at this point in the history
  • Loading branch information
André Laugks committed Apr 10, 2024
1 parent 5554d47 commit 0c56b5b
Show file tree
Hide file tree
Showing 44 changed files with 225 additions and 208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ public class XliffTranslationMessageSource implements MessageSource {

private XliffTranslationMessageSource(Builder builder) {
ResourcesLoader resourcesLoader = ResourcesLoader
.builder()
.defaultLocale(builder.defaultLocale)
.basenamesPattern(builder.basenames)
.build();
.builder()
.defaultLocale(builder.defaultLocale)
.basenamesPattern(builder.basenames)
.build();

this.catalogHandler = new CatalogHandler(
CatalogBuilder.builder(resourcesLoader)
.transUnitIdentifier(builder.transUnitIdentifier)
.build(),
CatalogBuilder.builder(resourcesLoader)
.transUnitIdentifier(builder.transUnitIdentifier)
.build(),
builder.cache, builder.defaultLocale,
builder.defaultDomain
);
Expand Down Expand Up @@ -99,16 +99,15 @@ public XliffTranslationMessageSource build() {
// Basenames
Assert.notEmpty(this.basenames, "Basename(s) is not set");


return new XliffTranslationMessageSource(this);
}
}

@Nullable
public String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, Locale locale) {
return this.format(
this.internalMessageWithDefaultMessage(code, defaultMessage, locale),
args
this.internalMessageWithDefaultMessage(code, defaultMessage, locale),
args
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import io.github.alaugks.spring.messagesource.xliff.catalog.finder.CatalogFileAdapter;
import io.github.alaugks.spring.messagesource.xliff.catalog.finder.CatalogFinder;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -31,9 +30,9 @@ public Map<String, Map<String, String>> getAll() {
@Override
public String get(Locale locale, String code) {
CatalogFinder finder = new CatalogFinder(
new CatalogFileAdapter(this.getAll()),
this.defaultLocale,
this.domain
new CatalogFileAdapter(this.getAll()),
this.defaultLocale,
this.domain
);

String message = finder.find(locale, code);
Expand All @@ -49,12 +48,12 @@ public void put(Locale locale, String domain, String code, String value) {
if (!locale.toString().isEmpty()) {
String localeKey = CatalogUtilities.localeToLocaleKey(locale);
this.catalogMap.putIfAbsent(
localeKey,
new HashMap<>()
localeKey,
new HashMap<>()
);
this.catalogMap.get(localeKey).putIfAbsent(
CatalogUtilities.concatCode(domain, code),
value
CatalogUtilities.concatCode(domain, code),
value
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Map;

abstract class CatalogAbstractHandler implements CatalogInterface {

protected CatalogInterface nextHandler;

public CatalogInterface setNextHandler(CatalogInterface handler) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,24 @@
import io.github.alaugks.spring.messagesource.xliff.exception.XliffMessageSourceSAXParseFatalErrorException;
import io.github.alaugks.spring.messagesource.xliff.exception.XliffMessageSourceVersionSupportException;
import io.github.alaugks.spring.messagesource.xliff.ressources.ResourcesLoader;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.util.List;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.List;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public final class CatalogBuilder {

private final ResourcesLoader resourceLoader;
private Catalog catalog;
private final List<XliffIdentifierInterface> transUnitIdentifier;
List<XliffVersionInterface> supportedVersions = List.of(
new XliffVersion12(),
new XliffVersion2()
new XliffVersion12(),
new XliffVersion2()
);

public CatalogBuilder(Builder builder) {
Expand Down Expand Up @@ -111,7 +110,7 @@ private void readFile(List<ResourcesLoader.Dto> translationFiles) throws ParserC
xliffReader.read(this.catalog, xliffDocument, translationFile.getDomain(), translationFile.getLocale());
} else {
throw new XliffMessageSourceVersionSupportException(
String.format("XLIFF version \"%s\" not supported.", xliffVersion)
String.format("XLIFF version \"%s\" not supported.", xliffVersion)
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ public Map<String, Map<String, String>> getAll() {
items.forEach((code, value) -> {
String[] split = code.toString().split("\\|");
result.putIfAbsent(
split[0],
new HashMap<>()
split[0],
new HashMap<>()
);
result.get(split[0]).putIfAbsent(
split[1],
value.toString()
split[1],
value.toString()
);
});
return result;
Expand All @@ -46,9 +46,9 @@ public Map<String, Map<String, String>> getAll() {
public String get(Locale locale, String code) {

CatalogFinder finder = new CatalogFinder(
new CatalogCacheAdapter(this.cache),
this.defaultLocale,
this.domain
new CatalogCacheAdapter(this.cache),
this.defaultLocale,
this.domain
);

String message = finder.find(locale, code);
Expand All @@ -67,19 +67,19 @@ public void put(Locale locale, String domain, String code, String value) {
public void put(Locale locale, String code, String value) {
if (!locale.toString().isEmpty()) {
this.cache.putIfAbsent(
CatalogUtilities.createCode(locale, code),
value
CatalogUtilities.createCode(locale, code),
value
);
}
}

public void initCache() {
super.getAll().forEach((langCode, catalogDomain) -> catalogDomain.forEach((code, value) ->
this.put(
Locale.forLanguageTag(langCode.replace("_", "-")),
code,
value
)
this.put(
Locale.forLanguageTag(langCode.replace("_", "-")),
code,
value
)
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
import org.springframework.cache.Cache;

public final class CatalogHandler {

private final CatalogCache catalogCache;

public CatalogHandler(
CatalogBuilder catalogBuilder,
CatalogBuilder catalogBuilder,
Cache cache,
Locale defaultLocale,
Locale defaultLocale,
String defaultDomain
) {
this.catalogCache = new CatalogCache(defaultLocale, defaultDomain, cache);
this.catalogCache.setNextHandler(
catalogBuilder.createCatalog(new Catalog(defaultLocale, defaultDomain))
catalogBuilder.createCatalog(new Catalog(defaultLocale, defaultDomain))
);
}

Expand All @@ -43,9 +44,9 @@ public Translation get(Locale locale, String code) {

void put(Locale locale, String code, String value) {
this.catalogCache.put(
locale,
code,
value
locale,
code,
value
);
}

Expand All @@ -54,6 +55,7 @@ public void initCache() {
}

public static class Translation {

String code;
String value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import java.util.Map;

public interface CatalogInterface {

CatalogInterface setNextHandler(CatalogInterface handler);

// HashMap<"language+region", HashMap<"code", "value">>
Map<String, Map<String, String>> getAll();

String get(Locale locale, String code);

void put(Locale locale, String domain, String code, String value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
import java.util.Locale;

public interface CatalogAdapterInterface {

String find(Locale locale, String code);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package io.github.alaugks.spring.messagesource.xliff.catalog.finder;

import io.github.alaugks.spring.messagesource.xliff.catalog.CatalogUtilities;
import org.springframework.cache.Cache;

import java.util.Locale;
import java.util.Objects;
import org.springframework.cache.Cache;

public class CatalogCacheAdapter implements CatalogAdapterInterface {

private final Cache cache;

public CatalogCacheAdapter(Cache cache) {
Expand All @@ -16,7 +16,7 @@ public CatalogCacheAdapter(Cache cache) {
@Override
public String find(Locale locale, String code) {
Cache.ValueWrapper valueWrapper = this.cache.get(
CatalogUtilities.createCode(locale, code)
CatalogUtilities.createCode(locale, code)
);

if (valueWrapper != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.github.alaugks.spring.messagesource.xliff.catalog.finder;

import io.github.alaugks.spring.messagesource.xliff.catalog.CatalogUtilities;

import java.util.Locale;
import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package io.github.alaugks.spring.messagesource.xliff.catalog.finder;

import io.github.alaugks.spring.messagesource.xliff.catalog.CatalogUtilities;

import java.util.Locale;

public final class CatalogFinder {

private final CatalogAdapterInterface adapter;
private final Locale defaultLocale;
private final String domain;
Expand Down Expand Up @@ -50,11 +50,7 @@ private String fromCatalog(Locale locale, String code) {

// Code+DefaultLanguage / DomainCode+DefaultLanguage
value = this.fromCatalogSubStep(defaultLocaleLang, code);
if (value != null) {
return value;
}

return null;
return value;
}

private String fromCatalogSubStep(Locale locale, String code) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ public Set<TransUnit> getTransUnits(String transUnitName, List<String> translati
}

public boolean isXliffDocument() {
// Simple test: Filter if root element not <xliff>
// Simple test: Filter if root element <xliff>
return root.getNodeName().equals("xliff");
}

public String getXliffVersion() {
return this.getAttributeValue(
root.getAttributes().getNamedItem("version")
root.getAttributes().getNamedItem("version")
);
}

Expand All @@ -59,10 +59,10 @@ private Set<TransUnit> getNodes() {
String code = this.getCode(node);
if (code != null) {
transUnits.add(
new TransUnit(
code,
this.getTargetValue(node)
)
new TransUnit(
code,
this.getTargetValue(node)
)
);
}
}
Expand All @@ -71,7 +71,7 @@ private Set<TransUnit> getNodes() {
}

private String getCode(
Element translationUnit
Element translationUnit
) {
if (this.translationUnitIdentifiers != null) {
for (String name : this.translationUnitIdentifiers) {
Expand All @@ -91,9 +91,9 @@ private String getTargetValue(Element translationNodeElement) {

private String getElementValue(Element translationNodeElement) {
return this.getCharacterDataFromElement(
this.getFirstChild(
translationNodeElement.getElementsByTagName("target")
)
this.getFirstChild(
translationNodeElement.getElementsByTagName("target")
)
);
}

Expand All @@ -113,11 +113,12 @@ private Node getFirstChild(NodeList nodeList) {

private String getAttributeValue(Node translationNode, String attributeName) {
return this.getAttributeValue(
translationNode.getAttributes().getNamedItem(attributeName)
translationNode.getAttributes().getNamedItem(attributeName)
);
}

public static class TransUnit {

private final String code;
private final String value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void setTransUnitIdentifier(List<XliffIdentifierInterface> unitIdentifier
@Override
public void read(CatalogInterface catalog, XliffDocument document, String domain, Locale locale) {
document.getTransUnits("trans-unit", this.transUnitIdentifier.getList()).forEach(
transUnit -> catalog.put(locale, domain, transUnit.getCode(), transUnit.getTargetValue())
transUnit -> catalog.put(locale, domain, transUnit.getCode(), transUnit.getTargetValue())
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void setTransUnitIdentifier(List<XliffIdentifierInterface> unitIdentifier
@Override
public void read(CatalogInterface catalog, XliffDocument document, String domain, Locale locale) {
document.getTransUnits("segment", this.transUnitIdentifier.getList()).forEach(
transUnit -> catalog.put(locale, domain, transUnit.getCode(), transUnit.getTargetValue())
transUnit -> catalog.put(locale, domain, transUnit.getCode(), transUnit.getTargetValue())
);
}

Expand Down
Loading

0 comments on commit 0c56b5b

Please sign in to comment.