Debian Codegraph @

wdtk-rdf/src/main/java/org/wikidata/wdtk/rdf/values/StringValueConverter.java

raw
1 package org.wikidata.wdtk.rdf.values;
2
3 /*
4 * #%L
5 * Wikidata Toolkit RDF
6 * %%
7 * Copyright (C) 2014 Wikidata Toolkit Developers
8 * %%
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * #L%
21 */
22
23 import org.eclipse.rdf4j.model.Value;
24 import org.wikidata.wdtk.datamodel.interfaces.DatatypeIdValue;
25 import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
26 import org.wikidata.wdtk.datamodel.interfaces.StringValue;
27 import org.wikidata.wdtk.rdf.OwlDeclarationBuffer;
28 import org.wikidata.wdtk.rdf.PropertyRegister;
29 import org.wikidata.wdtk.rdf.RdfWriter;
30
31 import java.io.UnsupportedEncodingException;
32 import java.net.URLEncoder;
33 import java.nio.charset.StandardCharsets;
34
35 public class StringValueConverter extends AbstractValueConverter<StringValue> {
36
37 public StringValueConverter(RdfWriter rdfWriter,
38 PropertyRegister propertyRegister,
39 OwlDeclarationBuffer rdfConversionBuffer) {
40 super(rdfWriter, propertyRegister, rdfConversionBuffer);
41 }
42
43 @Override
44 public Value getRdfValue(StringValue value,
45 PropertyIdValue propertyIdValue, boolean simple) {
46 String datatype = this.propertyRegister.setPropertyTypeFromStringValue(
47 propertyIdValue, value);
48
49 String valueUriString = null;
50 switch (datatype) {
51 case DatatypeIdValue.DT_STRING:
52 case DatatypeIdValue.DT_EXTERNAL_ID:
53 case DatatypeIdValue.DT_MATH:
54 valueUriString = null;
55 break;
56 case DatatypeIdValue.DT_COMMONS_MEDIA:
57 if (simple) {
58 valueUriString = getCommonsFileUrl(value.getString());
59 }
60 break;
61 case DatatypeIdValue.DT_URL:
62 if (simple) {
63 valueUriString = value.getString();
64 }
65 break;
66 case DatatypeIdValue.DT_GEO_SHAPE:
67 case DatatypeIdValue.DT_TABULAR_DATA:
68 if (simple) {
69 valueUriString = getCommonsDataUrl(value.getString());
70 }
71 break;
72 default:
73 logIncompatibleValueError(propertyIdValue, datatype, "string");
74 return null;
75 }
76
77 if (valueUriString == null) {
78 if (simple) {
79 this.rdfConversionBuffer.addDatatypeProperty(propertyIdValue);
80 return this.rdfWriter.getLiteral(value.getString());
81 } else {
82 return null; // or blank node
83 }
84 } else {
85 this.rdfConversionBuffer.addObjectProperty(propertyIdValue);
86 try {
87 return this.rdfWriter.getUri(valueUriString);
88 } catch (IllegalArgumentException e) {
89 logger.error("Invalid URI \"" + valueUriString
90 + "\". Not serializing value.");
91 return null;
92 }
93 }
94 }
95
96 /**
97 * Returns the Wikimedia Commons page URL for the given page name.
98 *
99 * @param pageName
100 * name of a page on Wikimedia Commons
101 * @return URL of the page
102 */
103 static String getCommonsFileUrl(String pageName) {
104 try {
105 return "http://commons.wikimedia.org/wiki/File:"
106 + URLEncoder.encode(pageName.replace(' ', '_'), StandardCharsets.UTF_8.toString());
107 } catch (UnsupportedEncodingException e) {
108 // can't happen
109 throw new IllegalStateException(e);
110 }
111 }
112
113 static String getCommonsDataUrl(String pageName) {
114 try {
115 return "http://commons.wikimedia.org/data/main/"
116 + URLEncoder.encode(pageName.replace(' ', '_'), StandardCharsets.UTF_8.toString());
117 } catch (UnsupportedEncodingException e) {
118 throw new IllegalStateException(e);
119 }
120 }
121
122 }