Skip to content

Commit f5f671d

Browse files
Merge pull request #10 from AndreaCimminoArriaga/master
fixed pom
2 parents 17e62eb + 174a57b commit f5f671d

File tree

12 files changed

+229
-88
lines changed

12 files changed

+229
-88
lines changed

pom.xml

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
22
<modelVersion>4.0.0</modelVersion>
3-
<groupId>oeg.fi.upm</groupId>
3+
<groupId>es.upm.fi.oeg</groupId>
44
<artifactId>wot-jtd</artifactId>
55
<version>0.1.5-SNAPSHOT</version>
66
<url>https://oeg-upm.github.io/wot-jtd</url>
@@ -136,32 +136,6 @@
136136
<scope>test</scope>
137137
</dependency>
138138

139-
<dependency>
140-
<groupId>io.fogsy</groupId>
141-
<artifactId>empire-annotations</artifactId>
142-
<version>1.9.14</version>
143-
</dependency>
144-
145-
<dependency>
146-
<groupId>io.fogsy</groupId>
147-
<artifactId>empire-core</artifactId>
148-
<version>1.9.14</version>
149-
</dependency>
150-
151-
<dependency>
152-
<groupId>io.fogsy</groupId>
153-
<artifactId>empire-pinto</artifactId>
154-
<version>1.9.14</version>
155-
</dependency>
156-
157-
<!-- https://mvnrepository.com/artifact/com.hp.hpl.jena/jena -->
158-
<dependency>
159-
<groupId>com.hp.hpl.jena</groupId>
160-
<artifactId>jena</artifactId>
161-
<version>2.6.4</version>
162-
</dependency>
163-
164-
165139
</dependencies>
166140

167141

src/main/java/wot/jtd/RDFHandler.java

Lines changed: 101 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
package wot.jtd;
22

3+
import java.beans.PropertyDescriptor;
34
import java.io.ByteArrayInputStream;
45
import java.io.IOException;
56
import java.io.InputStream;
67
import java.io.StringWriter;
78
import java.io.Writer;
9+
import java.lang.annotation.Annotation;
10+
import java.lang.reflect.AnnotatedType;
11+
import java.lang.reflect.Field;
12+
import java.lang.reflect.Method;
813
import java.net.URI;
914
import java.net.URISyntaxException;
1015
import java.util.ArrayList;
16+
import java.util.Arrays;
1117
import java.util.HashMap;
1218
import java.util.List;
1319
import java.util.Map;
1420
import java.util.Optional;
21+
import java.util.stream.Collectors;
1522

1623
import org.apache.jena.datatypes.RDFDatatype;
1724
import org.apache.jena.graph.Graph;
@@ -23,6 +30,7 @@
2330
import org.apache.jena.rdf.model.ModelFactory;
2431
import org.apache.jena.rdf.model.NodeIterator;
2532
import org.apache.jena.rdf.model.Property;
33+
import org.apache.jena.rdf.model.RDFNode;
2634
import org.apache.jena.rdf.model.Resource;
2735
import org.apache.jena.rdf.model.ResourceFactory;
2836
import org.apache.jena.vocabulary.DC;
@@ -40,8 +48,11 @@
4048
import com.apicatalog.rdf.RdfValue;
4149
import com.google.gson.JsonObject;
4250

51+
import wot.jtd.annotations.RdfDatatypeProperty;
52+
import wot.jtd.exception.RDFValidationException;
4353
import wot.jtd.exception.SchemaValidationException;
4454
import wot.jtd.model.Thing;
55+
import wot.jtd.model.VersionInfo;
4556

4657
public class RDFHandler {
4758

@@ -50,6 +61,37 @@ public class RDFHandler {
5061
private static final Resource THING = ResourceFactory.createResource("https://www.w3.org/2019/wot/td#Thing");
5162
private static final Property SECURITY_DEFINITIONS = ResourceFactory.createProperty("https://www.w3.org/2019/wot/td#securityDefinitions");
5263

64+
65+
public static void serialize() {
66+
VersionInfo version = new VersionInfo();
67+
68+
for (Field field: version.getClass().getSuperclass().getDeclaredFields()) {
69+
System.out.println(field.getName());
70+
if (field.isAnnotationPresent(RdfDatatypeProperty.class)) {
71+
System.out.print(" -- > "+getSerializedKey(field));
72+
}
73+
}
74+
for (Field field: version.getClass().getDeclaredFields()) {
75+
System.out.println(field.getName());
76+
if (field.isAnnotationPresent(RdfDatatypeProperty.class)) {
77+
System.out.print(" -- > "+getSerializedKey(field));
78+
}
79+
}
80+
81+
}
82+
83+
private static String getSerializedKey(Field field) {
84+
String annotationValue = field.getAnnotation(RdfDatatypeProperty.class).value();
85+
86+
if (annotationValue.isEmpty()) {
87+
return field.getName();
88+
}
89+
else {
90+
return annotationValue;
91+
}
92+
}
93+
94+
5395
public List<Thing> fromRDF(Model model) throws JsonLdError, IOException, SchemaValidationException {
5496
List<Thing> things = new ArrayList<>();
5597
// 1. Write model as JSON-LD 1.0
@@ -112,15 +154,66 @@ private void setLiteralElements(Thing thing, Resource resource, Model thingModel
112154
}
113155
}*/
114156

115-
private Map<String,String> getLangElements(Resource resource, Model model, Property property){
116-
Map<String,String> langElements = new HashMap<>();
117-
NodeIterator iterator = model.listObjectsOfProperty(resource, property);
118-
while(iterator.hasNext()) {
119-
Literal literal = iterator.next().asLiteral();
120-
if(literal.getLanguage()!=null)
121-
langElements.put(literal.getLanguage(),literal.getString());
157+
//
158+
159+
public static void fromRDF() {
160+
VersionInfo thing = new VersionInfo();
161+
162+
Method[] annotations = VersionInfo.class.getMethods();
163+
for(int index=0; index < annotations.length; index++) {
164+
Method annotation = annotations[index];
165+
System.out.println(annotation.getName());
166+
List<Annotation> annon = Arrays.asList(annotation.getAnnotations());
167+
annon.forEach(elem -> System.out.println(elem));
168+
}
169+
/*PropertyDescriptor[] properties = PropertyUtils.getPropertyDescriptors(thing);
170+
for(int index=0;index < properties.length; index++) {
171+
PropertyDescriptor property = properties[index];
172+
System.out.println(property.getDisplayName());
173+
System.out.println(property.getName());
174+
Method method = property.getPropertyType()
175+
if (method.isAnnotationPresent(Init.class)) {
176+
method.setAccessible(true);
177+
method.invoke(object);
178+
}
179+
for(Annotation anon: .getAnnotations()) {
180+
System.out.println(anon);
181+
}
182+
}*/
183+
}
184+
185+
protected Literal extractUnitaryObjectLiteral(Model model, Resource subject, Property property) throws RDFValidationException {
186+
Literal literal = null;
187+
RDFNode object = extractUnitaryObject(model, subject, property);
188+
if (object == null)
189+
throw new RDFValidationException(concatStrings("The porperty ", property.toString(),
190+
" must point to an existing literal, currently is not been used"));
191+
if (object.isLiteral()) {
192+
literal = object.asLiteral();
193+
} else {
194+
throw new RDFValidationException(concatStrings("The porperty ", property.toString(),
195+
" must point to a literal, currently is pointing to a non-literal object"));
122196
}
123-
return langElements;
197+
198+
return literal;
199+
}
200+
201+
protected RDFNode extractUnitaryObject(Model model, Resource subject, Property property) throws RDFValidationException {
202+
RDFNode node = null;
203+
List<RDFNode> objects = model.listObjectsOfProperty(property).toList();
204+
if(objects.size()>1) {
205+
throw new RDFValidationException(concatStrings("The porperty ", property.toString()," is unitary but it was used more than once with the subject ", subject.toString()));
206+
}else {
207+
node = objects.get(0);
208+
}
209+
return node;
210+
}
211+
212+
protected String concatStrings(String ...message) {
213+
StringBuilder builder = new StringBuilder();
214+
for(int index =0; index < message.length; index++)
215+
builder.append(message[index]);
216+
return builder.toString();
124217
}
125218

126219

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package wot.jtd.annotations;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Inherited;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
@Retention(RetentionPolicy.RUNTIME)
10+
@Target(ElementType.FIELD)
11+
@Inherited
12+
public @interface RdfAnyProperty {
13+
String value() default "";
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package wot.jtd.annotations;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Inherited;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
@Retention(RetentionPolicy.RUNTIME)
10+
@Target(ElementType.FIELD)
11+
@Inherited
12+
public @interface RdfDatatypeProperty {
13+
String value() default "";
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package wot.jtd.annotations;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Inherited;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
@Retention(RetentionPolicy.RUNTIME)
10+
@Target(ElementType.FIELD)
11+
@Inherited
12+
public @interface RdfObjectProperty {
13+
String value() default "";
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package wot.jtd.annotations;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Inherited;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
@Retention(RetentionPolicy.RUNTIME)
10+
@Target({ElementType.FIELD, ElementType.TYPE})
11+
@Inherited
12+
public @interface RdfType {
13+
String[] types() default {};
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package wot.jtd.exception;
2+
3+
public class RDFValidationException extends Exception {
4+
5+
private static final long serialVersionUID = 1L;
6+
7+
public RDFValidationException(String message) {
8+
super(message);
9+
}
10+
11+
}

src/main/java/wot/jtd/model/AbstractJTDObject.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,56 @@
11
package wot.jtd.model;
22

3+
import java.net.URI;
4+
import java.util.Collection;
35
import java.util.HashMap;
46
import java.util.Map;
57
import java.util.logging.Level;
68
import java.util.logging.Logger;
9+
710
import org.apache.jena.rdf.model.Model;
811
import com.apicatalog.jsonld.JsonLdError;
912
import com.fasterxml.jackson.annotation.JsonAnyGetter;
1013
import com.fasterxml.jackson.annotation.JsonAnySetter;
1114
import com.fasterxml.jackson.annotation.JsonIgnore;
15+
import com.fasterxml.jackson.annotation.JsonProperty;
1216
import com.fasterxml.jackson.databind.ObjectMapper;
1317
import com.google.gson.JsonObject;
14-
1518
import wot.jtd.JTD;
1619
import wot.jtd.RDFHandler;
20+
import wot.jtd.annotations.RdfDatatypeProperty;
21+
import wot.jtd.vocabulary.Vocabulary;
1722

1823

19-
public class AbstractJTDObject {
24+
public abstract class AbstractJTDObject {
2025

2126
protected static final Logger LOGGER = Logger.getLogger(AbstractJTDObject.class.getName());
2227
static {
2328
LOGGER.setLevel(Level.WARNING);
2429
}
2530

31+
// Shared common attributes
32+
@JsonProperty(Vocabulary.JSONLD_TYPE)
33+
@RdfDatatypeProperty(value="https://www.w3.org/2019/wot/td#type")
34+
protected Collection<String> type;
35+
protected URI id;
36+
37+
38+
public Collection<String> getType() {
39+
return type;
40+
}
41+
public void setType(Collection<String> type) {
42+
this.type = type;
43+
}
44+
45+
public URI getId() {
46+
return id;
47+
}
48+
public void setId(URI id) {
49+
this.id = id;
50+
}
51+
52+
53+
2654
// Any other property outside the standard
2755
protected Map<String,Object> unknownProperties = new HashMap<>();
2856

@@ -89,5 +117,4 @@ public boolean equals(Object obj) {
89117
}
90118

91119

92-
93120
}

src/main/java/wot/jtd/model/Link.java

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
import javax.validation.constraints.NotEmpty;
1111
import javax.validation.constraints.NotNull;
1212
import com.fasterxml.jackson.annotation.JsonInclude;
13+
import com.fasterxml.jackson.annotation.JsonProperty;
1314
import com.fasterxml.jackson.core.JsonProcessingException;
1415
import com.google.gson.JsonObject;
1516

1617
import wot.jtd.JTD;
1718
import wot.jtd.exception.LinkValidationException;
1819
import wot.jtd.exception.SchemaValidationException;
20+
import wot.jtd.vocabulary.Vocabulary;
1921

2022

2123
/**
@@ -31,7 +33,7 @@ public class Link extends AbstractJTDObject{
3133
@NotEmpty(message= "'href' must be a valid non-empty URI")
3234
@NotNull(message = "'href' must be a valid non-null URI")
3335
private URI href;
34-
private String type; //from RFC2046
36+
@JsonProperty(Vocabulary.TYPE)
3537
private String mediaType; //from RFC2046
3638
private String rel;
3739
private URI anchor;
@@ -103,21 +105,6 @@ public void setHref(URI href) {
103105
this.href = href;
104106
}
105107

106-
/**
107-
*
108-
* @return a mime type from the <a href="https://tools.ietf.org/html/rfc2046">RFC 2046</a> specification
109-
*/
110-
public String getType() {
111-
return type;
112-
}
113-
114-
/**
115-
*
116-
* @param type a valid mime type from the <a href="https://tools.ietf.org/html/rfc2046">RFC 2046</a> specification
117-
*/
118-
public void setType(String type) {
119-
this.type = type;
120-
}
121108

122109
/**
123110
*

0 commit comments

Comments
 (0)