Skip to content

Commit

Permalink
[Enhancement #298] Generate ASCII-only identifiers when configured to.
Browse files Browse the repository at this point in the history
  • Loading branch information
ledsoft committed Sep 16, 2024
1 parent 54f84f7 commit b00e962
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 10 deletions.
12 changes: 10 additions & 2 deletions src/main/java/cz/cvut/kbss/termit/service/IdentifierResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import cz.cvut.kbss.termit.exception.InvalidIdentifierException;
import cz.cvut.kbss.termit.exception.TermItException;
import cz.cvut.kbss.termit.util.Configuration;
import org.springframework.stereotype.Service;

import java.net.URI;
Expand Down Expand Up @@ -80,6 +81,12 @@ public class IdentifierResolver {
Arrays.sort(ILLEGAL_FILENAME_CHARS);
}

private final boolean asciiOnlyIdentifiers;

public IdentifierResolver(Configuration config) {
this.asciiOnlyIdentifiers = config.isAsciiIdentifiers();
}

/**
* Normalizes the specified value which includes:
* <ul>
Expand Down Expand Up @@ -139,11 +146,12 @@ public URI generateIdentifier(String namespace, String... components) {
if (!namespace.endsWith("/") && !namespace.endsWith("#")) {
namespace += "/";
}
final String localPart = asciiOnlyIdentifiers ? normalizeToAscii(comps) : normalize(comps);
try {
return URI.create(namespace + normalize(comps));
return URI.create(namespace + localPart);
} catch (IllegalArgumentException e) {
throw new InvalidIdentifierException(
"Generated identifier " + namespace + normalize(comps) + " is not a valid URI.",
"Generated identifier " + namespace + localPart + " is not a valid URI.",
"error.identifier.invalidCharacters");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import cz.cvut.kbss.termit.model.UserGroup;
import cz.cvut.kbss.termit.service.IdentifierResolver;
import cz.cvut.kbss.termit.service.business.UserGroupService;
import cz.cvut.kbss.termit.util.Configuration;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -56,7 +57,7 @@ class UserGroupControllerTest extends BaseControllerTestRunner {
private UserGroupService groupService;

@Spy
private IdentifierResolver identifierResolver = new IdentifierResolver();
private IdentifierResolver identifierResolver = new IdentifierResolver(new Configuration());

@InjectMocks
private UserGroupController sut;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import cz.cvut.kbss.termit.environment.Environment;
import cz.cvut.kbss.termit.environment.Generator;
import cz.cvut.kbss.termit.exception.InvalidIdentifierException;
import cz.cvut.kbss.termit.util.Configuration;
import cz.cvut.kbss.termit.util.Vocabulary;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -42,7 +43,7 @@ class IdentifierResolverTest {

@BeforeEach
void setUp() {
this.sut = new IdentifierResolver();
this.sut = new IdentifierResolver(new Configuration());
}

@Test
Expand Down Expand Up @@ -335,4 +336,15 @@ void generateIdentifierThrowsInvalidIdentifierExceptionWhenComponentsContainsUnf
final String label = "label with emoji \u3000"; // Ideographic space
assertThrows(InvalidIdentifierException.class, () -> sut.generateIdentifier(namespace, label));
}

@Test
void generateIdentifierNormalizesAccentedCharactersToAsciiWhenAsciiIdentifiersAreConfigured() {
final Configuration asciiOnlyConfig = new Configuration();
asciiOnlyConfig.setAsciiIdentifiers(true);
this.sut = new IdentifierResolver(asciiOnlyConfig);
final String namespace = "http://onto.fel.cvut.cz/ontologies/termit/test-slovnik";
final String label = "Délka dostřiku [m]";
final URI result = sut.generateIdentifier(namespace, label);
assertEquals(URI.create(namespace + "/delka-dostriku-m"), result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class ExcelImporterTest {

@SuppressWarnings("unused")
@Spy
private IdentifierResolver idResolver = new IdentifierResolver();
private IdentifierResolver idResolver = new IdentifierResolver(new Configuration());

@InjectMocks
private ExcelImporter sut;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class ResourceRepositoryServiceTest {
private TermOccurrenceDao occurrenceDao;

@Spy
private IdentifierResolver idResolver = new IdentifierResolver();
private IdentifierResolver idResolver = new IdentifierResolver(new Configuration());

@Spy
private Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ class UserRepositoryServiceTest {
private UserAccountDao userAccountDao;

@Spy
private IdentifierResolver identifierResolver;
private Configuration configuration = new Configuration();

@Spy
private Configuration configuration = new Configuration();
private IdentifierResolver identifierResolver = new IdentifierResolver(configuration);

@Spy
private Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ class SecurityUtilsTest {
private PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

@Spy
private IdentifierResolver idResolver = new IdentifierResolver();
private Configuration config = new Configuration();

@Spy
private Configuration config = new Configuration();
private IdentifierResolver idResolver = new IdentifierResolver(config);

@InjectMocks
private SecurityUtils sut;
Expand Down

0 comments on commit b00e962

Please sign in to comment.