-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathYAMLTriplifier.java
169 lines (153 loc) · 6.59 KB
/
YAMLTriplifier.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Copyright (c) 2024 SPARQL Anything Contributors @ http://github.com/sparql-anything
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.sparqlanything.yaml;
import io.github.sparqlanything.model.*;
import io.github.sparqlanything.model.annotations.Option;
import com.google.common.collect.Sets;
import org.snakeyaml.engine.v2.api.Load;
import org.snakeyaml.engine.v2.api.LoadSettings;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
@io.github.sparqlanything.model.annotations.Triplifier
public class YAMLTriplifier implements Triplifier {
@Option(description = "Yaml 1.2 forbids duplicate keys, raising an error (default behaviour). When true, duplicate keys are tolerated (last wins). ", validValues = "true/false")
public final static IRIArgument PROPERTY_ALLOW_DUPLICATE_KEYS = new IRIArgument("yaml.allow-duplicate-keys", "false");
protected void transform(Properties properties, FacadeXGraphBuilder builder)
throws IOException, TriplifierHTTPException {
final InputStream is = Triplifier.getInputStream(properties);
LoadSettings settings = LoadSettings.builder().setLabel("Custom user configuration")
.setAllowDuplicateKeys(PropertyUtils.getBooleanProperty(properties, PROPERTY_ALLOW_DUPLICATE_KEYS))
.build();
Load load = new Load(settings);
try {
// Only 1 data source expected
String dataSourceId = SPARQLAnythingConstants.DATA_SOURCE_ID;
Iterable<Object> iter = load.loadAllFromInputStream(is);
transformYAML(iter, dataSourceId, builder);
} finally {
is.close();
}
}
private void transformYAML(Iterable<Object> iter, String dataSourceId, FacadeXGraphBuilder builder) {
builder.addRoot(dataSourceId);
for (Object value : iter) {
if (value instanceof String) {
builder.addValue(dataSourceId, SPARQLAnythingConstants.ROOT_ID, (Integer) 1, value);
} else if (value instanceof Integer) {
builder.addValue(dataSourceId, SPARQLAnythingConstants.ROOT_ID, (Integer) 1, value);
} else if (value instanceof Boolean) {
builder.addValue(dataSourceId, SPARQLAnythingConstants.ROOT_ID, (Integer) 1, value);
} else if (value instanceof Double) {
builder.addValue(dataSourceId, SPARQLAnythingConstants.ROOT_ID, (Integer) 1, value);
} else if (value instanceof Map) {
// Directly link to the map
transformMap((Map) value, dataSourceId, SPARQLAnythingConstants.ROOT_ID, builder);
} else if (value instanceof List) {
List list = (List) value;
for (int x = 0; x < list.size(); x++) {
transformIntKeyValue(x + 1, list.get(x), dataSourceId, SPARQLAnythingConstants.ROOT_ID, builder);
}
} else {
throw new UnsupportedOperationException("Unknown structure: " + value.getClass());
}
}
}
private void transformMap(Map<?,?> o, String dataSourceId, String containerId, FacadeXGraphBuilder builder) {
for(Map.Entry<?,?> entry: o.entrySet()){
Object key = entry.getKey();
Object value = entry.getValue();
if( value == null){
log.warn("skipping {} because value is null", key);
continue;
}
transformStringKeyValue((String) key, value, dataSourceId, containerId, builder);
}
}
private void transformStringKeyValue(String key, Object value, String dataSourceId, String containerId, FacadeXGraphBuilder builder) {
if(value == null){
log.warn("skipping {} because value is null", key);
return;
}
// key = Triplifier.toSafeURIString(key);
if(value instanceof String){
builder.addValue(dataSourceId, containerId, (String) key, value);
} else if(value instanceof Integer){
builder.addValue(dataSourceId, containerId, (String) key, value);
} else if(value instanceof Boolean){
builder.addValue(dataSourceId, containerId, (String) key, value);
} else if(value instanceof Double){
builder.addValue(dataSourceId, containerId, (String) key, value);
} else if(value instanceof Map){
String childId = containerId + "/" + key;
builder.addContainer(dataSourceId, containerId,(String) key, childId);
transformMap((Map) value, dataSourceId, childId, builder);
} else if(value instanceof List){
String childId = containerId + "/" + key;
List list = (List) value;
builder.addContainer(dataSourceId, containerId, (String) key, childId);
for(int x=0; x<list.size(); x++){
transformIntKeyValue(x+1, list.get(x), dataSourceId, childId, builder);
}
} else {
throw new UnsupportedOperationException("Unknown structure: " + value.getClass());
}
}
private void transformIntKeyValue(int key, Object value, String dataSourceId, String containerId, FacadeXGraphBuilder builder) {
if(value == null){
log.warn("skipping {} because value is null", key);
return;
}
if(value instanceof String){
builder.addValue(dataSourceId, containerId, (Integer) key, value);
} else if(value instanceof Integer){
builder.addValue(dataSourceId, containerId, (Integer) key, value);
} else if(value instanceof Boolean){
builder.addValue(dataSourceId, containerId, (Integer) key, value);
} else if(value instanceof Double){
builder.addValue(dataSourceId, containerId, (Integer) key, value);
} else if(value instanceof Map){
String childId = containerId + "/" + key;
builder.addContainer(dataSourceId, containerId,(Integer) key, childId);
transformMap((Map) value, dataSourceId, childId, builder);
} else if(value instanceof List){
String childId = containerId + "/" + key;
builder.addContainer(dataSourceId, containerId,(Integer) key, childId);
List list = (List) value;
for(int x=0; x<list.size(); x++){
transformIntKeyValue(x+1, list.get(x), dataSourceId, childId, builder);
}
} else {
throw new UnsupportedOperationException("Unknown structure: " + value.getClass());
}
}
@Override
public void triplify(Properties properties, FacadeXGraphBuilder builder)
throws IOException, TriplifierHTTPException {
transform(properties, builder);
}
@Override
public Set<String> getMimeTypes() {
return Sets.newHashSet("application/yaml", "text/yaml", "x-text/yaml");
}
@Override
public Set<String> getExtensions() {
return Sets.newHashSet("yaml");
}
}