Skip to content

Commit

Permalink
Refactor coupling from classes
Browse files Browse the repository at this point in the history
  • Loading branch information
alaugks committed Apr 25, 2024
1 parent 9c47c41 commit 9dc283e
Show file tree
Hide file tree
Showing 30 changed files with 473 additions and 535 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.alaugks.spring.messagesource.xliff;

import io.github.alaugks.spring.messagesource.xliff.catalog.CatalogBuilder;
import io.github.alaugks.spring.messagesource.xliff.catalog.CatalogHandler;
import io.github.alaugks.spring.messagesource.xliff.catalog.XliffCatalogBuilder;
import io.github.alaugks.spring.messagesource.xliff.catalog.XliffVersionInterface.XliffIdentifierInterface;
import io.github.alaugks.spring.messagesource.xliff.ressources.ResourcesLoader;
import java.text.MessageFormat;
Expand All @@ -24,21 +24,24 @@ public class XliffTranslationMessageSource implements MessageSource {
private final CatalogHandler catalogHandler;

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

this.catalogHandler = new CatalogHandler(
CatalogBuilder.builder(resourcesLoader)
.transUnitIdentifier(builder.transUnitIdentifier)
.build(),
builder.cache, builder.defaultLocale,
builder.defaultDomain
ResourcesLoader resourcesLoader = new ResourcesLoader(
builder.defaultLocale,
builder.basenames,
List.of("xlf", "xliff")
);

XliffCatalogBuilder xliffCatalogBuilder = new XliffCatalogBuilder(
resourcesLoader.getTranslationFiles(),
builder.defaultDomain,
builder.defaultLocale,
builder.transUnitIdentifier
);

this.catalogHandler.initCache();
this.catalogHandler = new CatalogHandler(
xliffCatalogBuilder.getBaseCatalog(),
builder.cache
);
}

public static Builder builder(Cache cacheManager) {
Expand Down Expand Up @@ -142,10 +145,6 @@ public String getMessage(MessageSourceResolvable resolvable, Locale locale) thro
throw new NoSuchMessageException(codes != null && codes.length > 0 ? codes[codes.length - 1] : "", locale);
}

public void initCache() {
this.catalogHandler.initCache();
}

private String internalMessage(String code, Locale locale) throws NoSuchMessageException {
return this.findInCatalog(locale, code);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package io.github.alaugks.spring.messagesource.xliff.catalog;

import io.github.alaugks.spring.messagesource.xliff.records.Translation;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public final class Catalog extends CatalogAbstractHandler {
public final class BaseCatalog extends CatalogAbstractHandler {

private final HashMap<String, Map<String, String>> catalogMap;
private final Locale defaultLocale;
private final String defaultDomain;

public Catalog(Locale defaultLocal, String defaultDomain) {
public BaseCatalog(List<Translation> translations, Locale defaultLocal, String defaultDomain) {
this.catalogMap = new HashMap<>();
this.defaultLocale = defaultLocal;
this.defaultDomain = defaultDomain;
translations.forEach(t -> this.put(t.locale(), t.domain(), t.code(), t.value()));
}

@Override
Expand All @@ -40,8 +43,7 @@ public String get(Locale locale, String code) {
return super.get(locale, code);
}

@Override
public void put(Locale locale, String domain, String code, String value) {
private void put(Locale locale, String domain, String code, String value) {
if (!locale.toString().isEmpty() && !code.isEmpty()) {
String localeKey = CatalogUtilities.localeToLocaleKey(locale);
this.catalogMap.putIfAbsent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.cache.Cache;

public final class CatalogCache extends CatalogAbstractHandler {
public final class CacheCatalog extends CatalogAbstractHandler {

private final Cache cache;

public CatalogCache(Cache cache) {
public CacheCatalog(Cache cache) {
this.cache = cache;
this.initCache();
}

@Override
Expand Down Expand Up @@ -44,7 +45,6 @@ public String get(Locale locale, String code) {
return null;
}

// Find in Cache
String value = this.find(locale, code);
if (value != null) {
return value;
Expand All @@ -59,12 +59,7 @@ public String get(Locale locale, String code) {
return value;
}

@Override
public void put(Locale locale, String domain, String code, String value) {
this.put(locale, CatalogUtilities.concatCode(domain, code), value);
}

public void put(Locale locale, String code, String targetValue) {
private void put(Locale locale, String code, String targetValue) {
if (!locale.toString().isEmpty() && !code.isEmpty()) {
this.cache.putIfAbsent(
CatalogUtilities.createCode(locale, code),
Expand All @@ -73,7 +68,7 @@ public void put(Locale locale, String code, String targetValue) {
}
}

public void initCache() {
private void initCache() {
super.getAll().forEach((langCode, catalogDomain) -> catalogDomain.forEach((code, value) ->
this.put(
Locale.forLanguageTag(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ abstract class CatalogAbstractHandler implements CatalogInterface {

protected CatalogInterface nextHandler;

public CatalogInterface nextHandle(CatalogInterface handler) {
public CatalogInterface nextHandler(CatalogInterface handler) {
this.nextHandler = handler;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,25 @@

public final class CatalogHandler {

private final CatalogCache catalogCache;
private final CatalogInterface catalog;

public CatalogHandler(
CatalogBuilder catalogBuilder,
Cache cache,
Locale defaultLocale,
String defaultDomain
BaseCatalog baseCatalog,
Cache cache
) {
this.catalogCache = new CatalogCache(cache);
this.catalogCache.nextHandle(
catalogBuilder.createCatalog(new Catalog(defaultLocale, defaultDomain))
);
if (cache != null) {
this.catalog = new CacheCatalog(cache);
this.catalog.nextHandler(baseCatalog);
} else {
this.catalog = baseCatalog;
}
}

public Map<String, Map<String, String>> getAll() {
return this.catalogCache.getAll();
return this.catalog.getAll();
}

public String get(Locale locale, String code) {
return this.catalogCache.get(locale, code);
}

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

public void initCache() {
this.catalogCache.initCache();
return this.catalog.get(locale, code);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@

public interface CatalogInterface {

CatalogInterface nextHandle(CatalogInterface handler);
CatalogInterface nextHandler(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);
}
Original file line number Diff line number Diff line change
@@ -1,81 +1,68 @@
package io.github.alaugks.spring.messagesource.xliff.catalog;

import io.github.alaugks.spring.messagesource.xliff.catalog.XliffVersionInterface.XliffIdentifierInterface;
import io.github.alaugks.spring.messagesource.xliff.exception.SaxErrorHandler;
import io.github.alaugks.spring.messagesource.xliff.exception.XliffMessageSourceRuntimeException;
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 io.github.alaugks.spring.messagesource.xliff.ressources.ResourcesLoader.TranslationFile;
import io.github.alaugks.spring.messagesource.xliff.records.Translation;
import io.github.alaugks.spring.messagesource.xliff.records.TranslationFile;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.springframework.util.Assert;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public final class CatalogBuilder {
public final class XliffCatalogBuilder {

private final ResourcesLoader resourceLoader;
private Catalog catalog;
private final List<TranslationFile> translationFiles;
private final String defaultDomain;
private final Locale defaultLocale;
private final List<XliffVersionInterface.XliffIdentifierInterface> transUnitIdentifier;
private final List<Translation> translations;
List<XliffVersionInterface> supportedVersions = List.of(
new XliffVersion12(),
new XliffVersion2()
);

public CatalogBuilder(Builder builder) {
this.resourceLoader = builder.resourceLoader;
this.transUnitIdentifier = builder.transUnitIdentifier;
public XliffCatalogBuilder(
List<TranslationFile> translationFiles,
String defaultDomain,
Locale defaultLocale,
List<XliffIdentifierInterface> transUnitIdentifier
) {
this.translationFiles = translationFiles;
this.defaultDomain = defaultDomain;
this.defaultLocale = defaultLocale;
this.transUnitIdentifier = transUnitIdentifier;
this.translations = new ArrayList<>();
}

public static Builder builder(ResourcesLoader resourceLoader) {
return new Builder(resourceLoader);
}

public static final class Builder {

private final ResourcesLoader resourceLoader;
private List<XliffVersionInterface.XliffIdentifierInterface> transUnitIdentifier;

private Builder(ResourcesLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}

public Builder transUnitIdentifier(
List<XliffVersionInterface.XliffIdentifierInterface> translationUnitIdentifiers) {
if (translationUnitIdentifiers != null) {
this.transUnitIdentifier = translationUnitIdentifiers;
}
return this;
}

public CatalogBuilder build() {
return new CatalogBuilder(this);
}
public BaseCatalog getBaseCatalog() {
try {
// Default Domain
Assert.notNull(this.defaultDomain, "Default domain is null");
Assert.isTrue(!this.defaultDomain.trim().isEmpty(), "Default domain is empty");

}
// Default Locale
Assert.notNull(this.defaultLocale, "Default locale is null");
Assert.isTrue(!this.defaultLocale.toString().trim().isEmpty(), "Default locale is empty");

public Catalog createCatalog(Catalog catalog) {
try {
this.catalog = catalog;
this.readFile(resourceLoader.getTranslationFiles());
return this.catalog;
this.readFiles(translationFiles);
return new BaseCatalog(translations, this.defaultLocale, this.defaultDomain);
} catch (ParserConfigurationException | IOException e) {
throw new XliffMessageSourceRuntimeException(e);
throw new XliffMessageSourceSAXParseFatalErrorException(e);
}
}

public XliffVersionInterface getReader(String version) {
return this.supportedVersions
.stream()
.filter(o -> o.support(version))
.findFirst()
.orElse(null);
}

private void readFile(List<TranslationFile> translationTranslationFiles)
private void readFiles(List<TranslationFile> translationTranslationFiles)
throws ParserConfigurationException, IOException {

for (TranslationFile translationFile : translationTranslationFiles) {
Expand All @@ -100,11 +87,15 @@ private void readFile(List<TranslationFile> translationTranslationFiles)

String xliffVersion = xliffDocument.getXliffVersion();

XliffVersionInterface xliffReader = this.getReader(xliffVersion);
XliffVersionInterface xliffReader = this.supportedVersions
.stream()
.filter(o -> o.support(xliffVersion))
.findFirst()
.orElse(null);

if (xliffReader != null) {
xliffReader.setTransUnitIdentifier(this.transUnitIdentifier);
xliffReader.read(this.catalog, xliffDocument, translationFile.domain(), translationFile.locale());
xliffReader.read(this.translations, xliffDocument, translationFile.domain(), translationFile.locale());
} else {
throw new XliffMessageSourceVersionSupportException(
String.format("XLIFF version \"%s\" not supported.", xliffVersion)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ private String getAttributeValue(Node translationNode, String attributeName) {
}

public record TransUnit(String code, String value) {

}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.alaugks.spring.messagesource.xliff.catalog;

import io.github.alaugks.spring.messagesource.xliff.records.Translation;
import java.util.List;
import java.util.Locale;

Expand All @@ -22,9 +23,9 @@ public void setTransUnitIdentifier(List<XliffIdentifierInterface> unitIdentifier
}

@Override
public void read(CatalogInterface catalog, XliffDocument document, String domain, Locale locale) {
public void read(List<Translation> translations, XliffDocument document, String domain, Locale locale) {
document.getTransUnits("trans-unit", this.transUnitIdentifier.getList()).forEach(
transUnit -> catalog.put(locale, domain, transUnit.code(), transUnit.value())
transUnit -> translations.add(new Translation(locale, transUnit.code(), transUnit.value(), domain))
);
}

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

import io.github.alaugks.spring.messagesource.xliff.records.Translation;
import java.util.List;
import java.util.Locale;

Expand All @@ -22,9 +23,9 @@ public void setTransUnitIdentifier(List<XliffIdentifierInterface> unitIdentifier
}

@Override
public void read(CatalogInterface catalog, XliffDocument document, String domain, Locale locale) {
public void read(List<Translation> translations, XliffDocument document, String domain, Locale locale) {
document.getTransUnits("segment", this.transUnitIdentifier.getList()).forEach(
transUnit -> catalog.put(locale, domain, transUnit.code(), transUnit.value())
transUnit -> translations.add(new Translation(locale, transUnit.code(), transUnit.value(), domain))
);
}

Expand Down
Loading

0 comments on commit 9dc283e

Please sign in to comment.