| 1 |
/** |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
*/ |
| 18 |
package org.apache.commons.rdf.simple; |
| 19 |
|
| 20 |
import java.util.IllformedLocaleException; |
| 21 |
import java.util.Locale; |
| 22 |
import java.util.Objects; |
| 23 |
import java.util.Optional; |
| 24 |
|
| 25 |
import org.apache.commons.rdf.api.IRI; |
| 26 |
import org.apache.commons.rdf.api.Literal; |
| 27 |
|
| 28 |
/** |
| 29 |
* A simple implementation of Literal. |
| 30 |
*/ |
| 31 |
final class LiteralImpl implements Literal, SimpleRDF.SimpleRDFTerm { |
| 32 |
|
| 33 |
private static final String QUOTE = "\""; |
| 34 |
|
| 35 |
private final IRI dataType; |
| 36 |
private final String languageTag; |
| 37 |
private final String lexicalForm; |
| 38 |
|
| 39 |
public LiteralImpl(final String literal) { |
| 40 |
this(literal, Types.XSD_STRING); |
| 41 |
} |
| 42 |
|
| 43 |
public LiteralImpl(final String lexicalForm, final IRI dataType) { |
| 44 |
this.lexicalForm = Objects.requireNonNull(lexicalForm); |
| 45 |
this.dataType = Types.get(Objects.requireNonNull(dataType)).orElse(dataType); |
| 46 |
if (Types.RDF_LANGSTRING.equals(this.dataType)) { |
| 47 |
throw new IllegalArgumentException( |
| 48 |
"Cannot create a non-language literal with type " + Types.RDF_LANGSTRING); |
| 49 |
} |
| 50 |
this.languageTag = null; |
| 51 |
} |
| 52 |
|
| 53 |
public LiteralImpl(final String literal, final String languageTag) { |
| 54 |
this.lexicalForm = Objects.requireNonNull(literal); |
| 55 |
this.languageTag = Objects.requireNonNull(lowerCase(languageTag)); |
| 56 |
if (languageTag.isEmpty()) { |
| 57 |
// TODO: Check against |
| 58 |
// http://www.w3.org/TR/n-triples/#n-triples-grammar |
| 59 |
throw new IllegalArgumentException("Language tag can't be null"); |
| 60 |
} |
| 61 |
try { |
| 62 |
new Locale.Builder().setLanguageTag(languageTag); |
| 63 |
} catch (final IllformedLocaleException ex) { |
| 64 |
throw new IllegalArgumentException("Invalid languageTag: " + languageTag, ex); |
| 65 |
} |
| 66 |
|
| 67 |
// System.out.println(aLocale); |
| 68 |
this.dataType = Types.RDF_LANGSTRING; |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public IRI getDatatype() { |
| 73 |
return dataType; |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public Optional<String> getLanguageTag() { |
| 78 |
return Optional.ofNullable(languageTag); |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public String getLexicalForm() { |
| 83 |
return lexicalForm; |
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 87 |
public String ntriplesString() { |
| 88 |
final StringBuilder sb = new StringBuilder(); |
| 89 |
sb.append(QUOTE); |
| 90 |
// Escape special characters |
| 91 |
sb.append(getLexicalForm().replace("\\", "\\\\"). // escaped to \\ |
| 92 |
replace("\"", "\\\""). // escaped to \" |
| 93 |
replace("\r", "\\r"). // escaped to \r |
| 94 |
replace("\n", "\\n")); // escaped to \n |
| 95 |
sb.append(QUOTE); |
| 96 |
|
| 97 |
// getLanguageTag().ifPresent(s -> sb.append("@" + s)); |
| 98 |
if (getLanguageTag().isPresent()) { |
| 99 |
sb.append("@"); |
| 100 |
sb.append(getLanguageTag().get()); |
| 101 |
|
| 102 |
} else if (!getDatatype().equals(Types.XSD_STRING)) { |
| 103 |
sb.append("^^"); |
| 104 |
sb.append(getDatatype().ntriplesString()); |
| 105 |
} |
| 106 |
return sb.toString(); |
| 107 |
} |
| 108 |
|
| 109 |
@Override |
| 110 |
public String toString() { |
| 111 |
return ntriplesString(); |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
public int hashCode() { |
| 116 |
return Objects.hash(lexicalForm, dataType, languageTag); |
| 117 |
} |
| 118 |
|
| 119 |
private static String lowerCase(final String langTag) { |
| 120 |
return langTag.toLowerCase(Locale.ROOT); |
| 121 |
} |
| 122 |
|
| 123 |
@Override |
| 124 |
public boolean equals(final Object obj) { |
| 125 |
if (this == obj) { |
| 126 |
return true; |
| 127 |
} |
| 128 |
if (obj == null || !(obj instanceof Literal)) { |
| 129 |
return false; |
| 130 |
} |
| 131 |
final Literal literal = (Literal) obj; |
| 132 |
return getDatatype().equals(literal.getDatatype()) && getLexicalForm().equals(literal.getLexicalForm()) |
| 133 |
&& getLanguageTag().equals(literal.getLanguageTag().map(LiteralImpl::lowerCase)); |
| 134 |
} |
| 135 |
|
| 136 |
} |