本文整理了Java中org.eclipse.rdf4j.model.util.Literals.isValidLanguageTag()
方法的一些代码示例,展示了Literals.isValidLanguageTag()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Literals.isValidLanguageTag()
方法的具体详情如下:
包路径:org.eclipse.rdf4j.model.util.Literals
类名称:Literals
方法名:isValidLanguageTag
[英]Checks if the given string is a BCP47 language tag according to the rules defined by the JDK in the Locale API.
[中]根据JDK在LocaleAPI中定义的规则,检查给定字符串是否为BCP47语言标记。
代码示例来源:origin: org.eclipse.rdf4j/rdf4j-client
public Literal createLiteral(String label, String language) {
if (!Literals.isValidLanguageTag(language)) {
throw new IllegalArgumentException("Not a valid language tag: " + language);
}
return delegate.createLiteral(label, language);
}
代码示例来源:origin: eclipse/rdf4j
@Override
public Literal createLiteral(String label, String language) {
if (!Literals.isValidLanguageTag(language)) {
throw new IllegalArgumentException("Not a valid language tag: " + language);
}
return delegate.createLiteral(label, language);
}
代码示例来源:origin: apache/incubator-rya
/**
* Validates the language based on the data type.
* <p>
* This will do one of the following:
* <ul>
* <li>Return the original {@code language} if the {@code dataType} is
* {@link RDF#LANGSTRING} and it's of a VALID format.</li>
* <li>Returns {@link UNDETERMINED_LANGUAGE} if the {@code dataType} is
* {@link RDF#LANGSTRING} and it's of an INVALID format.</li>
* <li>Return {@code null} if the dataType is NOT {@link RDF#LANGSTRING}.</li>
* </ul>
* @param language the language to validate.
* @param dataType the {@link IRI} data type to validate against.
* @return the validated language.
*/
public static String validateLanguage(final String language, final IRI dataType) {
String result = null;
if (RDF.LANGSTRING.equals(dataType)) {
if (language != null && Literals.isValidLanguageTag(language)) {
result = language;
} else {
result = UNDETERMINED_LANGUAGE;
}
}
return result;
}
}
代码示例来源:origin: apache/incubator-rya
@Override
public RyaType deserialize(final byte[] bytes) throws RyaTypeResolverException {
if (!deserializable(bytes)) {
throw new RyaTypeResolverException("Bytes not deserializable");
}
final RyaType rt = newInstance();
rt.setDataType(getRyaDataType());
String data = new String(bytes, 0, bytes.length - 2, StandardCharsets.UTF_8);
if (RDF.LANGSTRING.equals(rt.getDataType())) {
final int langDelimiterPos = data.lastIndexOf(LiteralLanguageUtils.LANGUAGE_DELIMITER);
final String parsedData = data.substring(0, langDelimiterPos);
final String language = data.substring(langDelimiterPos + 1, data.length());
if (language != null && Literals.isValidLanguageTag(language)) {
rt.setLanguage(language);
} else {
rt.setLanguage(LiteralLanguageUtils.UNDETERMINED_LANGUAGE);
}
data = parsedData;
}
rt.setData(deserializeData(data));
return rt;
}
代码示例来源:origin: apache/incubator-rya
@Override
public RyaType deserialize(final byte[] bytes) throws RyaTypeResolverException {
if (!deserializable(bytes)) {
throw new RyaTypeResolverException("Bytes not deserializable");
}
final RyaType rt = newInstance();
final int length = bytes.length;
final int indexOfType = Bytes.indexOf(bytes, TYPE_DELIM_BYTE);
if (indexOfType < 1) {
throw new RyaTypeResolverException("Not a datatype literal");
}
String data = deserializeData(new String(bytes, 0, indexOfType, StandardCharsets.UTF_8));
rt.setDataType(SimpleValueFactory.getInstance().createIRI(new String(bytes, indexOfType + 1, (length - indexOfType) - 3, StandardCharsets.UTF_8)));
if (RDF.LANGSTRING.equals(rt.getDataType())) {
final int langDelimiterPos = data.lastIndexOf(LiteralLanguageUtils.LANGUAGE_DELIMITER);
final String parsedData = data.substring(0, langDelimiterPos);
final String language = data.substring(langDelimiterPos + 1, data.length());
if (language != null && Literals.isValidLanguageTag(language)) {
rt.setLanguage(language);
} else {
rt.setLanguage(LiteralLanguageUtils.UNDETERMINED_LANGUAGE);
}
data = parsedData;
}
rt.setData(data);
return rt;
}
}
代码示例来源:origin: apache/incubator-rya
/**
* Converts a {@link Literal} into a {@link RyaType} representation of the
* {@code literal}.
* @param literal the {@link Literal} to convert.
* @return the {@link RyaType} representation of the {@code literal}.
*/
public static RyaType convertLiteral(final Literal literal) {
if (literal == null) {
return null;
}
if (literal.getDatatype() != null) {
if (Literals.isLanguageLiteral(literal)) {
final String language = literal.getLanguage().get();
if (Literals.isValidLanguageTag(language)) {
return new RyaType(literal.getDatatype(), literal.stringValue(), language);
} else {
log.warn("Invalid language (" + LogUtils.clean(language) + ") found in Literal. Defaulting to: " + UNDETERMINED_LANGUAGE);
// Replace invalid language with "und"
return new RyaType(literal.getDatatype(), literal.stringValue(), UNDETERMINED_LANGUAGE);
}
}
return new RyaType(literal.getDatatype(), literal.stringValue());
}
return new RyaType(literal.stringValue());
}
代码示例来源:origin: apache/incubator-rya
@Test
public void testConvertLiteral_invalidLanguage() {
final String expectedData = "Hello";
final List<String> badLanguages = Lists.newArrayList(
"bad language",
"en-",
"en-US-"
);
for (final String badLanguage : badLanguages) {
// This only checks the validity of the format. Not that the language tag actually exists.
assertFalse(Literals.isValidLanguageTag(badLanguage));
final RyaType ryaType = new RyaType(RDF.LANGSTRING, expectedData, badLanguage);
final Literal literal = RyaToRdfConversions.convertLiteral(ryaType);
assertEquals(RDF.LANGSTRING, literal.getDatatype());
assertEquals(expectedData, literal.getLabel());
assertTrue(literal.getLanguage().isPresent());
// Check that the invalid language is replaced with "und"
assertEquals(LiteralLanguageUtils.UNDETERMINED_LANGUAGE, literal.getLanguage().get());
}
}
代码示例来源:origin: apache/incubator-rya
/**
* Converts a {@link RyaType} into a {@link Literal} representation of the
* {@code ryaType}.
* @param ryaType the {@link RyaType} to convert.
* @return the {@link Literal} representation of the {@code ryaType}.
*/
public static Literal convertLiteral(final RyaType ryaType) {
if (XMLSchema.STRING.equals(ryaType.getDataType())) {
return VF.createLiteral(ryaType.getData());
} else if (RDF.LANGSTRING.equals(ryaType.getDataType())) {
final String data = ryaType.getData();
final String language = ryaType.getLanguage();
if (language != null && Literals.isValidLanguageTag(language)) {
return VF.createLiteral(data, language);
} else {
return VF.createLiteral(data, LiteralLanguageUtils.UNDETERMINED_LANGUAGE);
}
}
return VF.createLiteral(ryaType.getData(), ryaType.getDataType());
}
代码示例来源:origin: apache/incubator-rya
@Test
public void testConvertLiteral_undeterminedLanguage() {
final String expectedData = "Hello";
final String language = LiteralLanguageUtils.UNDETERMINED_LANGUAGE;
assertTrue(Literals.isValidLanguageTag(language));
final RyaType ryaType = new RyaType(RDF.LANGSTRING, expectedData, language);
final Literal literal = RyaToRdfConversions.convertLiteral(ryaType);
assertEquals(RDF.LANGSTRING, literal.getDatatype());
assertEquals(expectedData, literal.getLabel());
assertTrue(literal.getLanguage().isPresent());
assertEquals(LiteralLanguageUtils.UNDETERMINED_LANGUAGE, literal.getLanguage().get());
final Literal expectedLiteral = VF.createLiteral(expectedData, language);
assertEquals(expectedLiteral, literal);
}
代码示例来源:origin: apache/incubator-rya
@Test
public void testConvertLiteral_validLanguage() {
final String expectedData = "Hello";
for (final String language : LANGUAGE_CODES) {
// This only checks the validity of the format. Not that the language tag actually exists.
assertTrue(Literals.isValidLanguageTag(language));
final RyaType ryaType = new RyaType(RDF.LANGSTRING, expectedData, language);
final Literal literal = RyaToRdfConversions.convertLiteral(ryaType);
assertEquals(RDF.LANGSTRING, literal.getDatatype());
assertEquals(expectedData, literal.getLabel());
assertTrue(literal.getLanguage().isPresent());
assertEquals(language, literal.getLanguage().get());
final Literal expectedLiteral = VF.createLiteral(expectedData, language);
assertEquals(expectedLiteral, literal);
}
}
代码示例来源:origin: apache/incubator-rya
@Test
public void testConvertLiteral_invalidLanguage() {
final String expectedData = "Hello";
final List<String> badLanguages = Lists.newArrayList(
"bad language",
"en-",
"en-US-"
);
for (final String badLanguage : badLanguages) {
// This only checks the validity of the format. Not that the language tag actually exists.
assertFalse(Literals.isValidLanguageTag(badLanguage));
final Literal literal = VF.createLiteral(expectedData, badLanguage);
final RyaType ryaType = RdfToRyaConversions.convertLiteral(literal);
assertEquals(RDF.LANGSTRING, ryaType.getDataType());
assertEquals(expectedData, ryaType.getData());
// Check that the invalid language is replaced with "und"
assertEquals(LiteralLanguageUtils.UNDETERMINED_LANGUAGE, ryaType.getLanguage());
}
}
代码示例来源:origin: apache/incubator-rya
@Test
public void testConvertLiteral_undeterminedLanguage() {
final String expectedData = "Hello";
final String language = LiteralLanguageUtils.UNDETERMINED_LANGUAGE;
assertTrue(Literals.isValidLanguageTag(language));
final Literal literal = VF.createLiteral(expectedData, language);
final RyaType ryaType = RdfToRyaConversions.convertLiteral(literal);
assertEquals(RDF.LANGSTRING, ryaType.getDataType());
assertEquals(expectedData, ryaType.getData());
final RyaType expectedRyaType = new RyaType(RDF.LANGSTRING, expectedData, language);
assertEquals(expectedRyaType, ryaType);
assertEquals(LiteralLanguageUtils.UNDETERMINED_LANGUAGE, ryaType.getLanguage());
}
代码示例来源:origin: apache/incubator-rya
@Test
public void testConvertLiteral_validLanguage() {
final String expectedData = "Hello";
for (final String language : LANGUAGE_CODES) {
// This only checks the validity of the format. Not that the language tag actually exists.
assertTrue(Literals.isValidLanguageTag(language));
final Literal literal = VF.createLiteral(expectedData, language);
final RyaType ryaType = RdfToRyaConversions.convertLiteral(literal);
assertEquals(RDF.LANGSTRING, ryaType.getDataType());
assertEquals(expectedData, ryaType.getData());
assertEquals(language, ryaType.getLanguage());
final RyaType expectedRyaType = new RyaType(RDF.LANGSTRING, expectedData, language);
assertEquals(expectedRyaType, ryaType);
}
}
代码示例来源:origin: org.eclipse.rdf4j/rdf4j-client
result = Literals.isValidLanguageTag(value);
代码示例来源:origin: eclipse/rdf4j
result = Literals.isValidLanguageTag(value);
内容来源于网络,如有侵权,请联系作者删除!