Skip to content

Commit

Permalink
Should honor @ru and ignore preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
agarciadom committed Dec 20, 2024
1 parent de83033 commit 8c4f1d7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,33 @@ public Collection<Object> getProperty(String property, IEolContext context) {
final RDFQualifiedName pName = RDFQualifiedName.from(property, this.owningModel::getNamespaceURI);

Collection<Object> value = getProperty(pName, context, LiteralMode.RAW);
if (!value.isEmpty() && !value.stream().anyMatch(p -> p instanceof RDFResource)) {
value = filterByPreferredLanguage(value, LiteralMode.VALUES_ONLY);
if (!value.isEmpty()) {
return value;
}
if (pName.languageTag == null && !value.stream().anyMatch(p -> p instanceof RDFResource)) {
value = filterByPreferredLanguage(value, LiteralMode.RAW);
}
if (!value.isEmpty()) {
return convertLiteralsToValues(value);
}

if (value.isEmpty() && pName.localName.endsWith(LITERAL_SUFFIX)) {
if (pName.localName.endsWith(LITERAL_SUFFIX)) {
final String localNameWithoutSuffix = pName.localName.substring(0,
pName.localName.length() - LITERAL_SUFFIX.length());
RDFQualifiedName withoutLiteral = pName.withLocalName(localNameWithoutSuffix);

value = getProperty(withoutLiteral, context, LiteralMode.RAW);
if (!value.isEmpty() && !value.stream().anyMatch(p -> p instanceof RDFResource)) {
if (pName.languageTag == null && !value.stream().anyMatch(p -> p instanceof RDFResource)) {
value = filterByPreferredLanguage(value, LiteralMode.RAW);
}
}

return value;
}

private Collection<Object> convertLiteralsToValues(Collection<Object> value) {
return value.stream()
.map(e -> e instanceof RDFLiteral ? ((RDFLiteral)e).getValue() : e)
.collect(Collectors.toList());
}

private Collection<Object> filterByPreferredLanguage(Collection<Object> value, LiteralMode literalMode) {
// If no preferred languages are specified, don't do any filtering
if (super.getModel().getLanguagePreference().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.junit.After;
import org.junit.Test;

@SuppressWarnings("unchecked")
public class RDFModelPreferredLanguagesTest {

private RDFModel model;
Expand All @@ -38,8 +39,8 @@ public class RDFModelPreferredLanguagesTest {

private static final String SPIDERMAN_MULTILANG_TTL = "resources/spiderman-multiLang.ttl";

private static final String LANGUAGE_PREFERENCE_INVALID_STRING = "e n,en-us,123,ja,ru";
private static final String LANGUAGE_PREFERENCE_JA_STRING = "en,en-us,ja,ru";
private static final String LANGUAGE_PREFERENCE_INVALID_STRING = "e n,en-us,123,ja";
private static final String LANGUAGE_PREFERENCE_JA_STRING = "en,en-us,ja";
private static final String LANGUAGE_PREFERENCE_EN_STRING = "en";

private static final String SPIDERMAN_URI = "http://example.org/#spiderman";
Expand Down Expand Up @@ -124,15 +125,15 @@ public void getNamesWithoutPrefixUsingPreferredLanguageTagEN() throws Exception
setupModel(LANGUAGE_PREFERENCE_EN_STRING);
RDFResource res = (RDFResource) model.getElementById(SPIDERMAN_URI);
Set<String> names = new HashSet<>((Collection<String>) pGetter.invoke(res, "name", context));
assertEquals("Should return untagged when language preference cant be matched",Collections.singleton(SPIDERMAN_NAME), names);
assertEquals("Should return untagged when language preference can't be matched",Collections.singleton(SPIDERMAN_NAME), names);
}

@Test
public void getNamesWithPrefixUsingPreferredLanguageTagEN() throws Exception {
setupModel(LANGUAGE_PREFERENCE_EN_STRING);
RDFResource res = (RDFResource) model.getElementById(SPIDERMAN_URI);
Set<String> names = new HashSet<>((Collection<String>) pGetter.invoke(res, "foaf:name", context));
assertEquals("Should return untagged when language preference cant be matched",Collections.singleton(SPIDERMAN_NAME), names);
assertEquals("Should return untagged when language preference can't be matched",Collections.singleton(SPIDERMAN_NAME), names);
}

@Test
Expand All @@ -143,7 +144,7 @@ public void getNameLiteralWithPrefixUsingPreferredLanguageTagEN() throws Excepti
for (RDFLiteral l : (Collection<RDFLiteral>) pGetter.invoke(res, "foaf:name_literal", context)) {
names.add((String) l.getValue());
}
assertEquals("Should return untagged when language preference cant be matched",Collections.singleton(SPIDERMAN_NAME), names);
assertEquals("Should return untagged when language preference can't be matched", Collections.singleton(SPIDERMAN_NAME), names);
}

@Test
Expand All @@ -154,7 +155,7 @@ public void getNameLiteralWithoutPrefixUsingPreferredLanguageTagEN() throws Exce
for (RDFLiteral l : (Collection<RDFLiteral>) pGetter.invoke(res, "name_literal", context)) {
names.add((String) l.getValue());
}
assertEquals("Should return untagged when language preference cant be matched",Collections.singleton(SPIDERMAN_NAME), names);
assertEquals("Should return untagged when language preference can't be matched",Collections.singleton(SPIDERMAN_NAME), names);
}

// JA preferred and available
Expand Down Expand Up @@ -197,7 +198,7 @@ public void getNameLiteralWithoutPrefixUsingPreferredLanguageTagJA() throws Exce
assertEquals(Collections.singleton(SPIDERMAN_NAME_JA), names);
}

// Empty Tag -- untagged
// Empty Tag - ignore language preference and use untagged value

@Test
public void getNamesWithoutPrefixAndNoTag() throws Exception {
Expand Down Expand Up @@ -236,7 +237,7 @@ public void getNameLiteralsWithPrefixAndNoTag() throws Exception {
assertEquals(Collections.singleton(SPIDERMAN_NAME), names);
}

// RU tag requested and available
// RU tag requested - ignore language preferences

@Test
public void getNamesWithoutPrefixWithRULanguageTag() throws Exception {
Expand Down

0 comments on commit 8c4f1d7

Please sign in to comment.