11package wot .jtd ;
22
3+ import java .beans .PropertyDescriptor ;
34import java .io .ByteArrayInputStream ;
45import java .io .IOException ;
56import java .io .InputStream ;
67import java .io .StringWriter ;
78import 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 ;
813import java .net .URI ;
914import java .net .URISyntaxException ;
1015import java .util .ArrayList ;
16+ import java .util .Arrays ;
1117import java .util .HashMap ;
1218import java .util .List ;
1319import java .util .Map ;
1420import java .util .Optional ;
21+ import java .util .stream .Collectors ;
1522
1623import org .apache .jena .datatypes .RDFDatatype ;
1724import org .apache .jena .graph .Graph ;
2330import org .apache .jena .rdf .model .ModelFactory ;
2431import org .apache .jena .rdf .model .NodeIterator ;
2532import org .apache .jena .rdf .model .Property ;
33+ import org .apache .jena .rdf .model .RDFNode ;
2634import org .apache .jena .rdf .model .Resource ;
2735import org .apache .jena .rdf .model .ResourceFactory ;
2836import org .apache .jena .vocabulary .DC ;
4048import com .apicatalog .rdf .RdfValue ;
4149import com .google .gson .JsonObject ;
4250
51+ import wot .jtd .annotations .RdfDatatypeProperty ;
52+ import wot .jtd .exception .RDFValidationException ;
4353import wot .jtd .exception .SchemaValidationException ;
4454import wot .jtd .model .Thing ;
55+ import wot .jtd .model .VersionInfo ;
4556
4657public 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
0 commit comments