Skip to content

Commit

Permalink
Replace DTO with record
Browse files Browse the repository at this point in the history
  • Loading branch information
André Laugks committed Apr 15, 2024
1 parent 65e61a7 commit 91dc5c6
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 160 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.github.alaugks.spring.messagesource.xliff.exception.XliffMessageSourceRuntimeException;
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 java.io.IOException;
import java.util.List;
import javax.xml.XMLConstants;
Expand Down Expand Up @@ -78,17 +79,18 @@ public XliffVersionInterface getReader(String version) {
.orElse(null);
}

private void readFile(List<ResourcesLoader.Dto> translationFiles) throws ParserConfigurationException, IOException {
private void readFile(List<TranslationFile> translationTranslationFiles)
throws ParserConfigurationException, IOException {

for (ResourcesLoader.Dto translationFile : translationFiles) {
for (TranslationFile translationFile : translationTranslationFiles) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
documentBuilder.setErrorHandler(new SaxErrorHandler());
Document document;
try {
document = documentBuilder.parse(translationFile.getInputStream());
document = documentBuilder.parse(translationFile.inputStream());
} catch (SAXException e) {
throw new XliffMessageSourceRuntimeException(e);
}
Expand All @@ -106,7 +108,7 @@ private void readFile(List<ResourcesLoader.Dto> translationFiles) throws ParserC

if (xliffReader != null) {
xliffReader.setTransUnitIdentifier(this.transUnitIdentifier);
xliffReader.read(this.catalog, xliffDocument, translationFile.getDomain(), translationFile.getLocale());
xliffReader.read(this.catalog, 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 @@ -117,23 +117,7 @@ private String getAttributeValue(Node translationNode, String attributeName) {
);
}

public static class TransUnit {

private final String code;
private final String value;

public TransUnit(String code, String value) {
this.code = code;
this.value = value;
}

public String getCode() {
return code;
}

public String getTargetValue() {
return value;
}
public record TransUnit(String code, String value) {
}

}
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.code(), transUnit.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("segment", this.transUnitIdentifier.getList()).forEach(
transUnit -> catalog.put(locale, domain, transUnit.getCode(), transUnit.getTargetValue())
transUnit -> catalog.put(locale, domain, transUnit.code(), transUnit.value())
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,53 +14,32 @@ public ResourcesFileNameParser(String filename) {
this.filename = filename;
}

public Dto parse() {
public Filename parse() {
String regexp = "^(?<domain>[a-z0-9]+)(?:([_-](?<language>[a-z]+))(?:[_-](?<region>[a-z]+))?)?";
Pattern pattern = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(this.filename);

if (matcher.find()) {
String domain = this.getGroup(matcher, "domain");
String language = this.getGroup(matcher, "language");
String region = this.getGroup(matcher, "region");
return new Dto(
domain,
language,
region
return new Filename(
this.getGroup(matcher, "domain"),
this.getGroup(matcher, "language"),
this.getGroup(matcher, "region")
);
}
return null;
}

public static class Dto {

private final String domain;
private final String language;
private final String region;

public Dto(String domain, String lang, String country) {
this.domain = domain;
this.language = lang;
this.region = country;
}

public String getDomain() {
return domain;
}

public String getLanguage() {
return language;
}

public String getRegion() {
return region;
}
public record Filename(String domain, String language, String region) {

public boolean hasLocale() {
return getLocale() != null && !getLocale().toString().isEmpty();
Locale locale = locale();
if (locale != null) {
return !locale.toString().isEmpty();
}
return false;
}

public Locale getLocale() {
public Locale locale() {
try {
return CatalogUtilities.buildLocale(this.language, this.region);
} catch (IllformedLocaleException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.alaugks.spring.messagesource.xliff.ressources;

import io.github.alaugks.spring.messagesource.xliff.ressources.ResourcesFileNameParser.Filename;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
Expand Down Expand Up @@ -46,31 +47,31 @@ public ResourcesLoader build() {

}

public List<Dto> getTranslationFiles() throws IOException {
ArrayList<Dto> translationFiles = new ArrayList<>();
public List<TranslationFile> getTranslationFiles() throws IOException {
ArrayList<TranslationFile> translationTranslationFiles = new ArrayList<>();
PathMatchingResourcePatternResolver resourceLoader = new PathMatchingResourcePatternResolver();
for (String basename : getBasenameSet()) {
Resource[] resources = resourceLoader.getResources(basename);
for (Resource resource : resources) {
if (this.isFileExtensionSupported(resource)) {
Dto dto = this.parseFileName(resource);
if (dto != null) {
translationFiles.add(dto);
TranslationFile translationFile = this.parseFileName(resource);
if (translationFile != null) {
translationTranslationFiles.add(translationFile);
}
}
}
}

return translationFiles;
return translationTranslationFiles;
}

private Dto parseFileName(Resource resource) throws IOException {
ResourcesFileNameParser.Dto dto = new ResourcesFileNameParser(resource.getFilename()).parse();
if (dto != null) {
return new Dto(
dto.getDomain(),
dto.hasLocale()
? dto.getLocale()
private TranslationFile parseFileName(Resource resource) throws IOException {
Filename filename = new ResourcesFileNameParser(resource.getFilename()).parse();
if (filename != null) {
return new TranslationFile(
filename.domain(),
filename.hasLocale()
? filename.locale()
: this.defaultLocale,
resource.getInputStream()
);
Expand All @@ -83,29 +84,7 @@ private boolean isFileExtensionSupported(Resource resource) {
return fileExtension != null && this.fileExtensions.contains(fileExtension.toLowerCase());
}

public static class Dto {

private final String domain;
private final Locale locale;
private final InputStream inputStream;

public Dto(String domain, Locale locale, InputStream inputStream) {
this.domain = domain;
this.locale = locale;
this.inputStream = inputStream;
}

public String getDomain() {
return domain;
}

public Locale getLocale() {
return locale;
}

public InputStream getInputStream() {
return inputStream;
}
public record TranslationFile(String domain, Locale locale, InputStream inputStream) {
}

private String getFileExtension(String filename) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void test_getElementValue_getCharacterDataFromElement_TextNode()

var xliffDocument = new XliffDocument(TestUtilities.getDocument("fixtures/xliff-value-test.xliff"));
xliffDocument.getTransUnits("segment", List.of("id")).forEach(
transUnit -> transUnits.put(transUnit.getCode(), transUnit.getTargetValue())
transUnit -> transUnits.put(transUnit.code(), transUnit.value())
);

assertEquals("value", transUnits.get("element"));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package io.github.alaugks.spring.messagesource.xliff.ressources;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import io.github.alaugks.spring.messagesource.xliff.ressources.ResourcesFileNameParser.Filename;
import org.junit.jupiter.api.Test;

class ResourcesFilenameParserTest {

@Test
void test_domain_withoutLocale() {
Filename filename = new ResourcesFileNameParser("message").parse();
assertEquals("message", filename.domain());
assertNull(filename.language());
assertNull(filename.region());
}

@Test
void test_domain_en() {
Filename filename = new ResourcesFileNameParser("message_en").parse();
assertEquals("message", filename.domain());
assertEquals("en", filename.language());
assertNull(filename.region());
assertEquals("en", filename.locale().toString());
}

@Test
void test_domain_en_withDash() {
Filename filename = new ResourcesFileNameParser("message-en").parse();
assertEquals("message", filename.domain());
assertEquals("en", filename.language());
assertNull(filename.region());
assertEquals("en", filename.locale().toString());
}

@Test
void test_domain_enGB() {
Filename filename = new ResourcesFileNameParser("message_en_GB").parse();
assertEquals("message", filename.domain());
assertEquals("en", filename.language());
assertEquals("GB", filename.region());
assertEquals("en_GB", filename.locale().toString());
}

@Test
void test_domain_enGB_withDash() {
Filename filename = new ResourcesFileNameParser("message-en-GB").parse();
assertEquals("message", filename.domain());
assertEquals("en", filename.language());
assertEquals("GB", filename.region());
assertEquals("en_GB", filename.locale().toString());
}

@Test
void test_hasNoLocale() {
Filename filename = new ResourcesFileNameParser("message").parse();
assertFalse(filename.hasLocale());
}

@Test
void test_hasLocale() {
Filename filename = new ResourcesFileNameParser("message_de").parse();
assertTrue(filename.hasLocale());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertInstanceOf;

import io.github.alaugks.spring.messagesource.xliff.TestUtilities;
import io.github.alaugks.spring.messagesource.xliff.ressources.ResourcesLoader.TranslationFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
Expand Down Expand Up @@ -64,10 +65,10 @@ void test_Dto() throws IOException {
.basenamesPattern(TestUtilities.listToSet("translations_en_US/*"))
.build();

ResourcesLoader.Dto dto = resourcesLoader.getTranslationFiles().get(0);
TranslationFile translationFile = resourcesLoader.getTranslationFiles().get(0);

assertEquals("messages", dto.getDomain());
assertEquals("en_US", dto.getLocale().toString());
assertInstanceOf(InputStream.class, dto.getInputStream());
assertEquals("messages", translationFile.domain());
assertEquals("en_US", translationFile.locale().toString());
assertInstanceOf(InputStream.class, translationFile.inputStream());
}
}

0 comments on commit 91dc5c6

Please sign in to comment.