diff --git a/README.md b/README.md index ae3f92c..847b952 100755 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Java API for Thing Descriptions of WoT (JDTs) -[![Version](https://img.shields.io/badge/Version-0.2.1-orange)](https://github.com/oeg-upm/wot-jtd/releases)] [![Maven Central](https://img.shields.io/badge/Maven%20Central-v0.2.1-green)](https://search.maven.org/search?q=g:%22es.upm.fi.oeg%22%20AND%20a:%22wot-jtd%22) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![GitHub stars](https://img.shields.io/github/stars/Naereen/StrapDown.js.svg?style=social&label=Star&maxAge=2592000)](https://github.com/oeg-upm/wot-jtd/stargazers) +[![Version](https://img.shields.io/badge/Version-0.2.2-orange)](https://github.com/oeg-upm/wot-jtd/releases)] [![Maven Central](https://img.shields.io/badge/Maven%20Central-v0.2.2-green)](https://search.maven.org/search?q=g:%22es.upm.fi.oeg%22%20AND%20a:%22wot-jtd%22) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![GitHub stars](https://img.shields.io/github/stars/Naereen/StrapDown.js.svg?style=social&label=Star&maxAge=2592000)](https://github.com/oeg-upm/wot-jtd/stargazers) The JDT is an ORM implementation of the current [Thing Description](https://www.w3.org/TR/wot-thing-description/) model standardised by the [W3C Web of Things group](https://www.w3.org/WoT/). The current features are: * Serialise: @@ -11,8 +11,9 @@ The JDT is an ORM implementation of the current [Thing Description](https://www. * Translate a set of RDF triples into its equivalent JSON-LD framed document * Validation * Validate a JTD using [SHACL shapes](https://www.w3.org/TR/shacl/) - * Validate a JTD using [JSON schema](https://json-schema.org/) **(coming soon)** - * Validate a JTD according to the [restrictions specified in the standard](https://www.w3.org/TR/wot-thing-description/) **(coming soon)** + * Validate a JTD using [JSON schema](https://json-schema.org/) **(coming soon)** + * Validate a JTD according to the [restrictions specified in the standard](https://www.w3.org/TR/wot-thing-description/) **(coming soon)** + If you have any feedback or feature suggestion, please let us know posting an issue with the label **feedback** @@ -41,16 +42,16 @@ Import the JDTs library as a maven dependency, **be sure to specify the latest v es.upm.fi.oeg wot-jtd - 0.2.1 + 0.2.2 ``` Alternatively, the dependency can be installed manually. First, download the latest jar from the [releases section](), and then install the dependency as follows (**be sure to specify the latest version**): ```` -mvn install:install-file -Dfile=wot-jtd.jar -DgroupId=es.upm.fi.oeg -DartifactId=wot-jtd -Dversion=0.2.1 -Dpackaging=jar +mvn install:install-file -Dfile=wot-jtd.jar -DgroupId=es.upm.fi.oeg -DartifactId=wot-jtd -Dversion=0.2.2 -Dpackaging=jar ```` -Check our [Maven Central Repository page](https://search.maven.org/artifact/es.upm.fi.oeg/wot-jtd/0.2.1/jar) to discover other installation options like Gradle Groovy or Kotlin, Scala, and others. +Check our [Maven Central Repository page](https://search.maven.org/artifact/es.upm.fi.oeg/wot-jtd/0.2.2/jar) to discover other installation options like Gradle Groovy or Kotlin, Scala, and others. ## Model @@ -158,6 +159,7 @@ ValidationReport shapeReport = JTD.validateWithShape(thing, shapesGraph); ##### Using JSON schema (*comming soon*) ##### Using restrictions in the model (*comming soon*) + ### Support or Contact WoT-JTD was created by [Andrea Cimmino](https://scholar.google.es/citations?user=_6U9WMcAAAAJ&hl=es&oi=ao) (cimmino@fi.upm.es) and [Raúl García Castro](http://garcia-castro.com/) (rgarcia@fi.upm.es) at the [Universidad Politécnica de Madrid](https://www.upm.es/) in the research group [Ontology Engineering Group](https://oeg.fi.upm.es/). diff --git a/pom.xml b/pom.xml index ad4b939..f3617d1 100755 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 es.upm.fi.oeg wot-jtd - 0.2.1 + 0.2.2 https://oeg-upm.github.io/wot-jtd Java Thing Description API This API aims at assisting developers for handling WoT Thing Descriptions, providing special support for RDF @@ -14,7 +14,7 @@ UTF-8 - + The Apache License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0.txt @@ -212,6 +212,11 @@ test + + es.upm.fi.oeg + kehio + 0.1.0 + diff --git a/src/main/java/kehio/annotations/RdfContainer.java b/src/main/java/kehio/annotations/RdfContainer.java deleted file mode 100755 index 16744c0..0000000 --- a/src/main/java/kehio/annotations/RdfContainer.java +++ /dev/null @@ -1,17 +0,0 @@ -package kehio.annotations; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Inherited; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.FIELD) -@Inherited -public @interface RdfContainer { - String[] ignore() default {}; - RdfUrlMap[] prefixes() default {}; - RdfUrlMap[] aliases(); // if property is present in alias, overrides the ignore properties - String[] identifiers(); -} diff --git a/src/main/java/kehio/annotations/RdfDatatype.java b/src/main/java/kehio/annotations/RdfDatatype.java deleted file mode 100755 index fc3e34e..0000000 --- a/src/main/java/kehio/annotations/RdfDatatype.java +++ /dev/null @@ -1,19 +0,0 @@ -package kehio.annotations; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Inherited; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.FIELD) -@Inherited -public @interface RdfDatatype { - String value() default ""; - String lang() default ""; - String datatype() default ""; - boolean sinkLang() default false; - boolean sinkDatatype() default false; -} - diff --git a/src/main/java/kehio/annotations/RdfDatatypeGroup.java b/src/main/java/kehio/annotations/RdfDatatypeGroup.java deleted file mode 100755 index 7a2af68..0000000 --- a/src/main/java/kehio/annotations/RdfDatatypeGroup.java +++ /dev/null @@ -1,16 +0,0 @@ -package kehio.annotations; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Inherited; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.FIELD) -@Inherited -public @interface RdfDatatypeGroup { - String value() default ""; - boolean byLang() default false; - boolean byDatatype() default false; -} diff --git a/src/main/java/kehio/annotations/RdfId.java b/src/main/java/kehio/annotations/RdfId.java deleted file mode 100755 index f51948e..0000000 --- a/src/main/java/kehio/annotations/RdfId.java +++ /dev/null @@ -1,14 +0,0 @@ -package kehio.annotations; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Inherited; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.FIELD}) -@Inherited -public @interface RdfId { - String value() default ""; -} diff --git a/src/main/java/kehio/annotations/RdfObject.java b/src/main/java/kehio/annotations/RdfObject.java deleted file mode 100755 index 8793fd4..0000000 --- a/src/main/java/kehio/annotations/RdfObject.java +++ /dev/null @@ -1,17 +0,0 @@ -package kehio.annotations; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Inherited; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.FIELD) -@Inherited -public @interface RdfObject { - String value() default ""; - String base() default ""; - RdfUrlMap[] aliases() default {}; - boolean strict() default false; // if true only matches aliases -} diff --git a/src/main/java/kehio/annotations/RdfObjectGroup.java b/src/main/java/kehio/annotations/RdfObjectGroup.java deleted file mode 100755 index 6f09932..0000000 --- a/src/main/java/kehio/annotations/RdfObjectGroup.java +++ /dev/null @@ -1,18 +0,0 @@ -package kehio.annotations; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Inherited; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.FIELD) -@Inherited -public @interface RdfObjectGroup { - String value() default ""; - String key() default ""; - boolean includeKey() default true; - RdfUrlMap[] aliases() default {}; - -} diff --git a/src/main/java/kehio/annotations/RdfPropertiesContainer.java b/src/main/java/kehio/annotations/RdfPropertiesContainer.java deleted file mode 100755 index 3345895..0000000 --- a/src/main/java/kehio/annotations/RdfPropertiesContainer.java +++ /dev/null @@ -1,16 +0,0 @@ -package kehio.annotations; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Inherited; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.FIELD) -@Inherited -public @interface RdfPropertiesContainer { - //boolean onlyDatatypes() default false; - String[] ignore() default {}; - RdfUrlMap[] prefixes() default {}; -} diff --git a/src/main/java/kehio/annotations/RdfUrlMap.java b/src/main/java/kehio/annotations/RdfUrlMap.java deleted file mode 100755 index 25d1322..0000000 --- a/src/main/java/kehio/annotations/RdfUrlMap.java +++ /dev/null @@ -1,15 +0,0 @@ -package kehio.annotations; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Inherited; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.FIELD) -@Inherited -public @interface RdfUrlMap { - String value() default ""; - String key() default ""; -} diff --git a/src/main/java/kehio/mapper/Kehio.java b/src/main/java/kehio/mapper/Kehio.java deleted file mode 100755 index e54c4c6..0000000 --- a/src/main/java/kehio/mapper/Kehio.java +++ /dev/null @@ -1,225 +0,0 @@ -package kehio.mapper; - -import java.lang.reflect.AnnotatedType; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.net.URISyntaxException; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; - -import org.apache.commons.lang3.ArrayUtils; -import org.apache.jena.rdf.model.Model; -import org.apache.jena.rdf.model.ModelFactory; -import org.apache.jena.rdf.model.Property; -import org.apache.jena.rdf.model.Resource; -import org.apache.jena.rdf.model.ResourceFactory; -import org.apache.jena.vocabulary.RDF; - -public class Kehio { - - protected static final Resource KEHIO_TYPE = ResourceFactory.createResource("http://kehio.com/model#Thing"); - private static final List mappers = new ArrayList<>(); - static { - - // Order of inclusion in the list matters - mappers.add(new RdfIdMapper()); - mappers.add(new RdfDatatypeGroupMapper()); - mappers.add(new RdfDatatypeMapper()); - - mappers.add(new RdfObjectGroupMapper()); - mappers.add(new RdfObjectMapper()); - - } - - - public static Object serializeClass(Class clazz, Model model, Resource subject) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, URISyntaxException { - Object object = null; - - object = createInstance(clazz); - Class clazzFull = Class.forName(clazz.getName()); - // Retrieve fields from super-classes - Field[] fields = extractFields(clazzFull); - instantiateObject(object, fields, model, subject); - - return object; - } - - private static Field[] extractFields(Class clazz) throws ClassNotFoundException { - // Extract fields - Field[] fields = clazz.getDeclaredFields(); - // Extract super class fields - AnnotatedType superAnnotatedClazz = clazz.getAnnotatedSuperclass(); - if(superAnnotatedClazz!=null) { - Class clazzFull = Class.forName(superAnnotatedClazz.getType().getTypeName()); - if(!clazzFull.equals(Object.class)) { - Field[] superFields = extractFields(clazzFull); - // Join current fields and super fields - fields = ArrayUtils.addAll(fields, superFields); - } - } - return fields; - } - - private static void instantiateObject(Object object, Field[] fields, Model model, Resource subject) throws IllegalArgumentException, IllegalAccessException, URISyntaxException { - if(object!=null) { - Set processedProperties = new HashSet<>(); - for (int index=0; index < fields.length; index++) { - Field field = fields[index]; - Set correctlyProcessedProperty = processSerialiseFieldAnnotation(field, object, model, subject); - if(correctlyProcessedProperty!=null) - processedProperties.addAll(correctlyProcessedProperty); - } - // add unknown RDF properties - enhanceObjectWithUnknownProperties(fields, processedProperties, object, model, subject); - // add unknown RDF triples - enhanceObjectWithUnknownTriples(fields, processedProperties, object, model, subject); - } - } - - private static void enhanceObjectWithUnknownProperties(Field[] fields, Set processedProperties, Object object, Model model, Resource subject) throws IllegalArgumentException, IllegalAccessException, URISyntaxException { - RdfPropertiesContainerMapper mapperContainer = new RdfPropertiesContainerMapper(); - mapperContainer.setPropertiesNotToContain(processedProperties); - int unknownAnnotations = 0; - for (int index=0; index < fields.length; index++) { - Field field = fields[index]; - if(mapperContainer.hasProcesableAnnotation(field)) { - if(unknownAnnotations == 0) - mapperContainer.fromRdfToObject(field, object, model, subject); - unknownAnnotations++; - }else if(unknownAnnotations > 1) { - throw new IllegalArgumentException("A Java class can be annotated only with one Container notation"); - } - } - } - - private static void enhanceObjectWithUnknownTriples(Field[] fields, Set processedProperties, Object object, Model model, Resource subject) throws IllegalArgumentException, IllegalAccessException, URISyntaxException { - RdfContainerMapper mapperContainer = new RdfContainerMapper(); - mapperContainer.setPropertiesNotToContain(processedProperties); - int unknownAnnotations = 0; - for (int index=0; index < fields.length; index++) { - Field field = fields[index]; - if(mapperContainer.hasProcesableAnnotation(field)) { - if(unknownAnnotations == 0) - mapperContainer.fromRdfToObject(field, object, model, subject); - unknownAnnotations++; - }else if(unknownAnnotations > 1) { - throw new IllegalArgumentException("A Java class can be annotated only with one Container notation"); - } - } - } - - private static Set processSerialiseFieldAnnotation(Field field, Object object, Model model, Resource subject) { - Set properties = null; - try { - for (int index=0; index < mappers.size(); index++) { - RdfMapper serialiser = mappers.get(index); - if(serialiser.hasProcesableAnnotation(field)) { - Property property = serialiser.fromRdfToObject(field, object, model, subject); - - properties = new HashSet<>(); - if(property!=null) - properties.add(property); - break; - } - } - }catch(Exception e) { - throw new IllegalArgumentException(e.toString()); - } - return properties; - } - - private static Object createInstance(Class clazz) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { - return clazz.getConstructor().newInstance(); - } - - - - // -- Deserialisation methods - - - public static Model deserializeClass(Object object) throws IllegalArgumentException, IllegalAccessException, URISyntaxException, ClassNotFoundException { - return deserializeClassExtended(object).getValue(); - } - - public static Model deserializeClass(Object object, Map prefixes) throws IllegalArgumentException, IllegalAccessException, URISyntaxException, ClassNotFoundException { - return deserializeClassExtended(object, prefixes).getValue(); - } - - protected static Entry deserializeClassExtended(Object object) throws IllegalArgumentException, IllegalAccessException, URISyntaxException, ClassNotFoundException { - return deserializeClassExtended(object, null); - } - - protected static Entry deserializeClassExtended(Object object, Map prefixes) throws IllegalArgumentException, IllegalAccessException, URISyntaxException, ClassNotFoundException { - Model model = ModelFactory.createDefaultModel(); - if(prefixes!=null && !prefixes.isEmpty()) - model.setNsPrefixes(prefixes); - - Class clazzFull = object.getClass(); - // Retrieve fields from super-classes - Field[] fields = extractFields(clazzFull); - Resource subject = instantiateModel(object, fields, model); - Entry entry = Map.entry(subject, model); - return entry; - } - - - - private static Resource instantiateModel(Object object, Field[] fields, Model model) throws IllegalArgumentException, IllegalAccessException, URISyntaxException { - Resource subject = null; - if(object!=null) { - subject = findSubjectResource(fields,object, model); - model.remove(subject, RDF.type, Kehio.KEHIO_TYPE); - - for (int index=0; index < fields.length; index++) { - Field field = fields[index]; - field.setAccessible(true); - Object instantiatedField = field.get(object); - if(instantiatedField!=null) - processDeserialiseFieldAnnotation(field, object, model, subject); - } - } - return subject; - } - - private static void processDeserialiseFieldAnnotation(Field field, Object object, Model model, Resource subject) throws IllegalArgumentException, IllegalAccessException, URISyntaxException { - List mappersAux = new ArrayList<>(mappers); - mappersAux.add(new RdfPropertiesContainerMapper()); - mappersAux.add(new RdfContainerMapper()); - for (int index=0; index < mappersAux.size(); index++) { - RdfMapper mapper = mappersAux.get(index); - if(!(mapper instanceof RdfIdMapper) && mapper.hasProcesableAnnotation(field)) { - mapper.fromObjectToRdf(field, object, model, subject); - break; - - } - } - } - - private static Resource findSubjectResource(Field[] fields, Object object, Model model) throws IllegalArgumentException, IllegalAccessException, URISyntaxException { - RdfIdMapper idMapper = new RdfIdMapper(); - Resource subject = model.createResource(); - int rdfIds = 0; - for (int index=0; index < fields.length; index++) { - Field field = fields[index]; - if(idMapper.hasProcesableAnnotation(field)) { - rdfIds++; - if(rdfIds==1) { - idMapper.fromObjectToRdf(field, object, model, null); - }else { - throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfId can be used only once in a class,. Please review annotation for attribute ", field.getName())); - } - } - } - // We remove the mapper - if(model.isEmpty()) - model.add(subject, RDF.type, KEHIO_TYPE); - return model.listSubjectsWithProperty(RDF.type, KEHIO_TYPE).next(); - } - - - -} diff --git a/src/main/java/kehio/mapper/KehioUtils.java b/src/main/java/kehio/mapper/KehioUtils.java deleted file mode 100755 index 6e120db..0000000 --- a/src/main/java/kehio/mapper/KehioUtils.java +++ /dev/null @@ -1,253 +0,0 @@ -package kehio.mapper; - -import java.lang.reflect.Field; -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; -import java.net.URI; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Deque; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Queue; -import java.util.Set; -import java.util.SortedSet; -import java.util.TreeSet; -import org.apache.jena.query.Query; -import org.apache.jena.query.QueryExecution; -import org.apache.jena.query.QueryExecutionFactory; -import org.apache.jena.query.QueryFactory; -import org.apache.jena.query.ResultSet; -import org.apache.jena.rdf.model.Model; -import org.apache.jena.rdf.model.Property; -import org.apache.jena.rdf.model.RDFNode; -import org.apache.jena.rdf.model.Resource; -import org.apache.jena.rdf.model.ResourceFactory; -import org.apache.jena.sparql.util.ResultSetUtils; - -//TODO: add more support to SPARQL paths -class KehioUtils { - - protected static final String START_CLASS_TOKEN = "<"; - protected static final String START_SECOND_CLASS_TOKEN = ", "; - protected static final String END_CLASS_TOKEN = ">"; - - private KehioUtils() { - - } - - // -- General methods - - protected static String concatStrings(String ...message) { - StringBuilder builder = new StringBuilder(); - for(int index =0; index < message.length; index++) - builder.append(message[index]); - return builder.toString(); - } - - // -- Data retrieval in RDF methods - - /*protected static List retrieveFromRdfPath(Model model, Resource subject, String propertyPath, Boolean isPath) throws IllegalArgumentException, IllegalAccessException { - List range = null; - if(isPath) { - range = extractPathData( model, subject, propertyPath); - }else { - range = model.listObjectsOfProperty(subject, ResourceFactory.createProperty(propertyPath)).toList(); - } - return range; - }*/ - - protected static List objectNodes(Model model, Resource subject, Property property){ - return model.listObjectsOfProperty(subject, property).toList(); - } - - protected static List objectNodes(Model model, Resource subject, String property){ - return model.listObjectsOfProperty(subject, ResourceFactory.createProperty(property)).toList(); - } - - private static List extractPathData(Model model, Resource subject, String annotationPathValue) throws IllegalArgumentException, IllegalAccessException { - List output = new ArrayList<>(); - Query query = QueryFactory.create(concatStrings("SELECT DISTINCT ?s ?o WHERE { <", subject.getURI(),">",annotationPathValue, " ?o . }")); - QueryExecution qexec = null; - try{ - qexec = QueryExecutionFactory.create(query, model); - ResultSet results = qexec.execSelect(); - output = ResultSetUtils.resultSetToList(results, "?o"); - - } catch (Exception e) { - e.printStackTrace(); - output = null; - }finally { - if (qexec !=null) - qexec.close(); - } - return output; - } - - // -- Datatyped properties methods - protected static final String STRING_CLASS = "java.lang.String"; - protected static final String URI_CLASS = URI.class.getName(); - protected static final String COLLECTION_CLASS = Collection.class.getName(); - - private static final List WRAPPING_PRIMITIVE_TYPES = new ArrayList<>(); - static { - WRAPPING_PRIMITIVE_TYPES.add(STRING_CLASS); - WRAPPING_PRIMITIVE_TYPES.add("java.lang.Long"); - WRAPPING_PRIMITIVE_TYPES.add("java.lang.Float"); - WRAPPING_PRIMITIVE_TYPES.add("java.lang.Double"); - WRAPPING_PRIMITIVE_TYPES.add("java.lang.Character"); - WRAPPING_PRIMITIVE_TYPES.add("java.lang.Boolean"); - WRAPPING_PRIMITIVE_TYPES.add("java.lang.Integer"); - WRAPPING_PRIMITIVE_TYPES.add("java.lang.Short"); - WRAPPING_PRIMITIVE_TYPES.add("java.lang.Byte"); - WRAPPING_PRIMITIVE_TYPES.add("java.lang.Number"); - } - - - - protected static boolean isWrappingPrimitive(Field field) { - return isWrappingPrimitive(field.getType().getName()); - } - - protected static boolean isWrappingPrimitive(String fieldName) { - return WRAPPING_PRIMITIVE_TYPES.contains(fieldName); - } - - - - - protected static boolean isFieldClass(Field field) { - return !KehioUtils.isWrappingPrimitive(field) && !KehioUtils.isCollection(field); - } - - protected static boolean isFieldString(Field field) { - return field.getType().equals(String.class); - } - - protected static boolean isFieldURI(Field field) { - return field.getType().equals(URI.class); - } - - // Collections methods - - - - public static boolean isCollection(Field field) { - Type fieldType = field.getType(); - Boolean correctType = fieldType.equals(Collection.class) || fieldType.equals(List.class) || fieldType.equals(Set.class); - correctType = correctType || fieldType.equals(Queue.class) || fieldType.equals(Deque.class) || fieldType.equals(SortedSet.class); - return correctType; - } - - protected static String extractCollectionType(Field field) { - ParameterizedType pt = (ParameterizedType) field.getGenericType(); - String typeName = pt.getTypeName(); - typeName = typeName.substring(typeName.indexOf(KehioUtils.START_CLASS_TOKEN)+1, typeName.indexOf(KehioUtils.END_CLASS_TOKEN)); - return typeName; - } - - public static boolean isCollectionOfWrappingPrimitive(Field field) { - Boolean correctType = isCollection(field) ; - correctType &= field.getGenericType() instanceof ParameterizedType; - if(correctType) { - String innerValue = extractCollectionType(field); - correctType &= KehioUtils.isWrappingPrimitive(innerValue); - } - return correctType; - } - - - - - // Map methods - - protected static Entry extractMapTypes(Field field) { - ParameterizedType pt = (ParameterizedType) field.getGenericType(); - String typeName = pt.getTypeName(); - typeName = typeName.substring(typeName.indexOf(KehioUtils.START_CLASS_TOKEN)+1, typeName.indexOf(KehioUtils.END_CLASS_TOKEN)); - String[] mapTypes = typeName.split(", "); - Entry entry = Map.entry(mapTypes[0], mapTypes[1]); - return entry; - } - - protected static Class extractMapValueType(Field field){ - Class clazz = null; - try { - String mapType = KehioUtils.extractMapTypes(field).getValue(); - clazz = Class.forName(mapType); - }catch(Exception e) { - throw new IllegalArgumentException(e.toString()); - } - return clazz; - } - - // other - - public static boolean isURI(String url){ - Boolean isURL = false; - try { - URI uri = new URI(url); - isURL = uri.isAbsolute() ;//contains(":"); // true && (true is implicit as far as no exception is thrown) - } catch (Exception exception) { - isURL = false; - } - return isURL; - } - - public static boolean isValidResource(String url) { - Boolean isURL = false; - try { - ResourceFactory.createResource(url); - isURL = true; - }catch(Exception e) { - isURL = false; - } - return isURL; - } - - // -- Instantiation methods - - /** - * This method receives a field ({@link Collection}, {@link Set}, {@link List}, {@link Queue}, {@link Deque}, or {@link Sortedset}) belonging to an object, and also, a generic collection containing its instantiating values.

- * As a result, this method instantiates the field of the object with the provided collection. - * @param field the field ({@link Collection}, {@link Set}, {@link List}, {@link Queue}, {@link Deque}, or {@link Sortedset}) - * @param instantiatedCollection the instantiated collection - * @param object the generic object that has the provided field and which will be instantiated with the provided collection - * @throws IllegalArgumentException in case that the field is not one of {@link Collection}, {@link Set}, {@link List}, {@link Queue}, {@link Deque}, or {@link Sortedset} - * @throws IllegalAccessException in case the field does not belongs to the provided object - */ - protected static void instantiateCollection(Field field, Collection instantiatedCollection, Object object) throws IllegalArgumentException, IllegalAccessException { - Type fieldType = field.getType(); - Collection collection = null; - if(fieldType.equals(Collection.class) || fieldType.equals(List.class)) { - collection = instantiatedCollection; - }else if(fieldType.equals(Set.class)) { - collection = new HashSet<>(instantiatedCollection); - }else if(fieldType.equals(Queue.class) || fieldType.equals(Deque.class)) { - collection = new LinkedList<>(instantiatedCollection); - }else if(fieldType.equals(SortedSet.class)) { - collection = new TreeSet<>(instantiatedCollection); - }else{ - String illegalMessage = KehioUtils.concatStrings("@RdfDatatype and/or @RdfObject can be used with Collections, List, Set, SortedSet, Queue, Dequeue. Please create an issue to add more java data structures. Please review annotation for attribute ",field.getName()); - throw new IllegalArgumentException(illegalMessage); - } - if(collection!=null && !collection.isEmpty()) - field.set(object, collection); - } - - protected static void instantiateField(Field field, Object object, Object fieldValue) throws IllegalArgumentException, IllegalAccessException { - if(fieldValue!=null) - field.set(object, fieldValue); - } - - protected static Object instantiatedField(Field field, Object object) throws IllegalArgumentException, IllegalAccessException { - return field.get(object); - } - - - - -} diff --git a/src/main/java/kehio/mapper/RdfContainerMapper.java b/src/main/java/kehio/mapper/RdfContainerMapper.java deleted file mode 100755 index 7263867..0000000 --- a/src/main/java/kehio/mapper/RdfContainerMapper.java +++ /dev/null @@ -1,324 +0,0 @@ -package kehio.mapper; - -import java.lang.reflect.Field; -import java.net.URISyntaxException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Optional; -import java.util.Set; -import java.util.stream.Collectors; - -import org.apache.jena.rdf.model.Model; -import org.apache.jena.rdf.model.Property; -import org.apache.jena.rdf.model.RDFNode; -import org.apache.jena.rdf.model.Resource; -import org.apache.jena.rdf.model.ResourceFactory; -import org.apache.jena.rdf.model.Statement; -import org.apache.jena.rdf.model.StmtIterator; - -import kehio.annotations.RdfContainer; -import kehio.annotations.RdfUrlMap; - - -class RdfContainerMapper implements RdfMapper{ - - protected Set forbidenRdfProperties; - - public void setPropertiesNotToContain(Set processedProperties) { - this.forbidenRdfProperties = processedProperties; - } - - @Override - public boolean hasProcesableAnnotation(Field field) { - return field.isAnnotationPresent(RdfContainer.class); - } - - @Override - public Property fromRdfToObject(Field field, Object object, Model model, Resource subject) throws IllegalArgumentException, IllegalAccessException, URISyntaxException { - field.setAccessible(true); - - PropertiesContainerAnotation annotation = new PropertiesContainerAnotation(field); - Map collectionOfTriples = populateMapFromRDF(annotation, model, subject, true); - - field.set(object, collectionOfTriples); - - return null; - } - - - - private Map populateMapFromRDF(PropertiesContainerAnotation annotation, Model model, Resource subject, boolean firstCall) { - Set ignore = annotation.getPropertiesToIgnore(); - Map collectionOfTriples = new HashMap<>(); - StmtIterator stIterator = model.listStatements(subject, null, (RDFNode) null); - while(stIterator.hasNext()) { - Statement st = stIterator.next(); - - List inverseAliases = annotation.inverseAlias(st.getPredicate()); - if( (!inverseAliases.isEmpty() && !firstCall) || (firstCall && !forbidenRdfProperties.contains(st.getPredicate()) && !ignore.contains(st.getPredicate()))){ - if(inverseAliases.isEmpty()) // if empty add current property, otherwise replaces current predicate with its shorten forms, i.e., inverse alias - inverseAliases.add(st.getPredicate().toString()); - for(int index=0; index < inverseAliases.size(); index++) { - String shortenPredicate = inverseAliases.get(index); - shortenPredicate = shortenURI(model, shortenPredicate); - RDFNode object = st.getObject(); - if(object.isResource()) { - updateCollectionOfTriplesWithResources(annotation, model, collectionOfTriples, shortenPredicate, object.asResource()); - }else { - updateCollectionOfTriplesWithLiterals(collectionOfTriples, shortenPredicate, object.asLiteral().getValue()); - } - } - } - } - // add suject as property if specified in annotation - if(annotation.getIdentifiers()!=null && !annotation.getIdentifiers().isEmpty()) - annotation.getIdentifiers().stream().forEach(id -> collectionOfTriples.put(id, subject.toString())); - - return collectionOfTriples; - } - - - private void updateCollectionOfTriplesWithResources(PropertiesContainerAnotation annotation, Model model, Map collectionOfTriples, String predicate, Resource object){ - - if(!model.contains(object.asResource(), null, (RDFNode) null)) { - // DONE: Si el objecto no existe en el modelo como sujeto : guardarlo como - String shortenObject = shortenURI(model, object.toString()); - collectionOfTriples.put(predicate, shortenObject); - }else { - // DONE: Si el objecto es tambien sujeto en el modelo -> entonces se traduce como un Map) : llamada recurisva - Map nestedResource = populateMapFromRDF(annotation, model, object, false); - // DONE: Si el objecto es tambien sujeto en el modelo y ya existe en el mapa acutal : entonces traducirlos como una collection de Map - updateCollectionOfTriplesWithLiterals(collectionOfTriples, predicate, nestedResource); - - } - } - - /* - * Since objects here are literals we already know that the range of the property are always literals, and thus, as object it will be collection of strings or string - */ - @SuppressWarnings("unchecked") - private void updateCollectionOfTriplesWithLiterals(Map collectionOfTriples, String predicate, Object object) { - Object oldValue = collectionOfTriples.remove(predicate); - if(oldValue!=null) { // already existed - Collection newObjects = new ArrayList<>(); - if(oldValue instanceof Collection) { - newObjects.addAll((Collection) oldValue); - }else { - newObjects.add(oldValue); - } - newObjects.add(object); - collectionOfTriples.put(predicate, newObjects); - }else { - // new element - collectionOfTriples.put(predicate, object); - } - } - - private String shortenURI(Model model, String uri) { - String shortenUri = model.shortForm(uri); - if(shortenUri==null) - shortenUri = uri; - return shortenUri; - } - - // -- - - @Override - public void fromObjectToRdf(Field field, Object object, Model model, Resource subject) throws IllegalArgumentException, IllegalAccessException, URISyntaxException { - field.setAccessible(true); - - PropertiesContainerAnotation annotation = new PropertiesContainerAnotation(field); - @SuppressWarnings("unchecked") - Map values = (Map) annotation.getField().get(object); - populateModel(annotation, model, subject, values.entrySet()); - } - - @SuppressWarnings("unchecked") - private void populateModel(PropertiesContainerAnotation annotation, Model model, Resource subject, Set> entries) { - - for (Entry entry : entries) { - Object value = entry.getValue(); - Property property = computeFullProperty(entry.getKey(), model.getNsPrefixMap()); - if (isCollection(value)) { - Collection newProperties = (Collection) value; - nestedElements(annotation, new ArrayList<>(newProperties), model, subject, property); - } else if (isMap(value)) { - resourceObjectToRDF(annotation, model, subject, property, value); - } else if (!isCollection(value) && !isMap(value)) { - keyValueToRDF(annotation, model, subject, property, value); - } - } - } - - @SuppressWarnings("unchecked") - private void resourceObjectToRDF(PropertiesContainerAnotation annotation, Model model, Resource subject, Property property, Object value) { - Resource newSubject = model.createResource(); - Map newEntries = (Map) value; - Optional propertyIdentifier = newEntries.keySet().stream().filter(keyProperty -> annotation.isPropertyIdentifier(keyProperty)).findFirst(); - if(propertyIdentifier.isPresent()) { // if a property in the json/object was set as identifier create a subject with such - String newSubjectStr = newEntries.remove(propertyIdentifier.get()).toString(); - newSubject = model.createResource(newSubjectStr); - } - model.add(subject, property, newSubject); - populateModel(annotation, model, newSubject, newEntries.entrySet()); - } - - private static Boolean isCollection(Object value) { - return value instanceof Collection; - } - - private static Boolean isMap(Object value) { - return value instanceof Map; - } - - @SuppressWarnings("unchecked") - private void nestedElements(PropertiesContainerAnotation annotation, List elements, Model model, Resource subject, Property property ) { - for(int index=0; index < elements.size(); index++) { - Object value = elements.get(index); - if(isCollection(value)) { - Collection newProperties = (Collection) value; - nestedElements(annotation, new ArrayList<>(newProperties), model, subject, property ); - }else if(isMap(value)) { - resourceObjectToRDF(annotation, model, subject, property, value); - }else if( !isMap(value) && !isCollection(value)) { - keyValueToRDF( annotation,model, subject, property, value); - } - } - } - - private static void keyValueToRDF(PropertiesContainerAnotation annotation, Model model, Resource subject, Property property, Object value) { - String url = model.expandPrefix(value.toString()); - - if(!url.equals(value) && KehioUtils.isURI(url) ) { - Property alias = annotation.getAlias(property.getURI()); - model.add(subject, alias, ResourceFactory.createResource(url)); - }else if(KehioUtils.isURI(value.toString())) { - Property alias = annotation.getAlias(property.getURI()); - model.add(subject, alias, ResourceFactory.createResource(value.toString())); - }else { - model.add(subject, property, ResourceFactory.createTypedLiteral(value)); - } - } - - - private Property computeFullProperty(String property, Map prefixes) { - Property computedProperty = ResourceFactory.createProperty(property); - int index = property.indexOf(':'); - if(index>-1) { - String prefix = property.substring(0,index); - String value = property.substring(index+1,property.length()); - String url = prefixes.get(prefix); - if(url!=null) - computedProperty = ResourceFactory.createProperty(url+value); - } - return computedProperty; - } - - - - - - - class PropertiesContainerAnotation{ - - private Field field; - - public PropertiesContainerAnotation(Field field) { - this.field = field; - checkAnnotation(); - } - - - - public Property getAlias(String predicate) { - Map aliases = this.getAliases(); - Property aliasProperty = aliases.get(predicate); - if(aliasProperty==null) - aliasProperty = ResourceFactory.createProperty(predicate); - return aliasProperty; - } - - private void checkAnnotation() { - if(!field.getType().equals(Map.class)) - throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfBlankContainer must be used only with a Map attribute. Please review annotation in field ", field.getName())); - Entry mapTypes = KehioUtils.extractMapTypes(field); - if(!mapTypes.getKey().contains(KehioUtils.STRING_CLASS)) - throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfBlankContainer must be used only with a Map that has as key type a String. Please review annotation in field ", field.getName())); - - //if(!KehioUtils.extractMapValueType(field).toString().equals(Object.class.toString())) - // throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfContainer must be used only with a Map that has as value a java generic Object. Please review annotation in field ", field.getName())); - - /* - if (onlyDatatypes() && onlyObjects()) - throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfContainer can have only one flag marked as true, either 'onlyDatatypes' or 'onlyObjects'. Please review annotation for field ", field.getName())); - - if(onlyDatatypes() && (!mapTypes.getValue().equals(KehioUtils.STRING_CLASS) || ) - throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfContainer must be used only with a Map attribute. If 'onlyDatatypes' is true, then the values of the map must be String or a Collection of Strings. Please review annotation in field ", field.getName())); - */ - } - - - /* public boolean onlyDatatypes() { - return field.getAnnotation(RdfBlankContainer.class).onlyDatatypes(); - } - - public boolean onlyObjects() { - return field.getAnnotation(RdfBlankContainer.class).onlyObjects(); - }*/ - - public Field getField() { - return field; - } - - public Set getPropertiesToIgnore() { - Set ignore = new HashSet<>(); - String[] ignorable = field.getAnnotation(RdfContainer.class).ignore(); - for(int index=0; index < ignorable.length; index++) { - ignore.add(ResourceFactory.createProperty(ignorable[index])); - } - return ignore; - } - - public Set getIdentifiers(){ - Set identifiers = new HashSet<>(); - String[] rawIdentifiers = field.getAnnotation(RdfContainer.class).identifiers(); - for(int index=0; index < rawIdentifiers.length; index++) { - identifiers.add(rawIdentifiers[index]); - } - return identifiers; - } - - public Boolean isPropertyIdentifier(String property){ - return getIdentifiers().contains(property); - } - - public Map getAliases(){ - Map aliases = new HashMap<>(); - RdfUrlMap[] rawAliases = field.getAnnotation(RdfContainer.class).aliases(); - for(int index=0; index < rawAliases.length; index++) { - aliases.put(rawAliases[index].key(), ResourceFactory.createProperty(rawAliases[index].value())); - } - return aliases; - } - - public List inverseAlias(Property predicate) { - List inverseAlias = new ArrayList<>(); - inverseAlias.addAll( getAliases().entrySet().stream().filter(entry -> entry.getValue().equals(predicate)).map(Entry::getKey).collect(Collectors.toList())); - return inverseAlias; - } - - } - - - - - - - - -} diff --git a/src/main/java/kehio/mapper/RdfDatatypeGroupMapper.java b/src/main/java/kehio/mapper/RdfDatatypeGroupMapper.java deleted file mode 100755 index 248b60a..0000000 --- a/src/main/java/kehio/mapper/RdfDatatypeGroupMapper.java +++ /dev/null @@ -1,140 +0,0 @@ -package kehio.mapper; - -import java.lang.reflect.Field; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - -import org.apache.jena.datatypes.RDFDatatype; -import org.apache.jena.datatypes.TypeMapper; -import org.apache.jena.rdf.model.Literal; -import org.apache.jena.rdf.model.Model; -import org.apache.jena.rdf.model.Property; -import org.apache.jena.rdf.model.RDFNode; -import org.apache.jena.rdf.model.Resource; -import org.apache.jena.rdf.model.ResourceFactory; - -import kehio.annotations.RdfDatatypeGroup; - -class RdfDatatypeGroupMapper implements RdfMapper { - - - @Override - public boolean hasProcesableAnnotation(Field field) { - return field.isAnnotationPresent(RdfDatatypeGroup.class); - } - - @Override - public Property fromRdfToObject(Field field, Object object, Model model, Resource subject) throws IllegalAccessException { - field.setAccessible(true); - - DgroupAnotation anotation = new DgroupAnotation(field); - List rdfNodes = KehioUtils.objectNodes(model, subject, anotation.getAsProperty()); - - Map map = instantiateMap(rdfNodes, anotation); - if(!map.isEmpty()) - KehioUtils.instantiateField(field, object,map); - return anotation.getAsProperty(); - } - - private Map instantiateMap(List rdfNodes, DgroupAnotation anotation ){ - Map map = new HashMap<>(); - for(int index=0; index < rdfNodes.size(); index++) { - RDFNode node = rdfNodes.get(index); - if(node.isLiteral()) { - Literal literal = node.asLiteral(); - if(anotation.isByLang() && !literal.getLanguage().isEmpty()) { - map.put(literal.getLanguage(), literal.getValue().toString()); - }else if(anotation.isByDatatype()) { - map.put(literal.getDatatypeURI(), literal.getValue().toString()); - } - } - } - return map; - } - - - - - // -- from Object to RDF - - @Override - public void fromObjectToRdf(Field field, Object object, Model model, Resource subject) throws IllegalAccessException { - field.setAccessible(true); - - DgroupAnotation anotation = new DgroupAnotation(field); - - @SuppressWarnings("unchecked") - Map values = (Map) field.get(object); - if(values!=null) { - for(Entry entry : values.entrySet()) { - Literal nodeLiteral = buildLiteral(anotation, entry); - if(nodeLiteral!=null) - model.add(subject, anotation.getAsProperty(), nodeLiteral); - } - } - } - - private Literal buildLiteral(DgroupAnotation anotation, Entry entry) { - Literal literal = null; - if(anotation.isByLang()) - literal = ResourceFactory.createLangLiteral(entry.getValue(), entry.getKey()); - if(anotation.isByDatatype()) { - RDFDatatype dt = TypeMapper.getInstance().getTypeByName(entry.getKey()); - literal = ResourceFactory.createTypedLiteral(entry.getValue(), dt); - } - return literal; - } - - - -} - -class DgroupAnotation{ - - private Field field; - - public DgroupAnotation(Field field) { - this.field = field; - checkAnnotation(); - } - - private void checkAnnotation() { - if (getValue()==null || getValue().isEmpty()) - throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfDatatypeGroup must be initialised with a value, please review annotation for field ", field.getName())); - if(!field.getType().equals(Map.class)) - throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfDatatypeGroup must be used only with a Map wich keys are always a String and its values ajava wrapping primitive object. Please review annotation in field ", field.getName())); - if(isByLang() && isByDatatype()) - throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfDatatypeGroup must have as true only one flag, either 'byLang' or 'byDatatype'. Please review annotation in field ", field.getName())); - Entry mapTypes = KehioUtils.extractMapTypes(field); - if(!mapTypes.getKey().equals(KehioUtils.STRING_CLASS)) - throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfDatatypeGroup must be used with a Map wich keys are always a String. Please review annotation in field ", field.getName())); - if(!mapTypes.getValue().equals(KehioUtils.STRING_CLASS)) - throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfDatatypeGroup must be used with a Map wich values are always a String. Please review annotation in field ", field.getName())); - } - - public boolean isByLang() { - return field.getAnnotation(RdfDatatypeGroup.class).byLang(); - } - - public boolean isByDatatype() { - return field.getAnnotation(RdfDatatypeGroup.class).byDatatype(); - } - - - public Field getField() { - return field; - } - - - public String getValue() { - return field.getAnnotation(RdfDatatypeGroup.class).value(); - } - - public Property getAsProperty() { - return ResourceFactory.createProperty(field.getAnnotation(RdfDatatypeGroup.class).value()); - } - - -} diff --git a/src/main/java/kehio/mapper/RdfDatatypeMapper.java b/src/main/java/kehio/mapper/RdfDatatypeMapper.java deleted file mode 100755 index 21b7a15..0000000 --- a/src/main/java/kehio/mapper/RdfDatatypeMapper.java +++ /dev/null @@ -1,284 +0,0 @@ -package kehio.mapper; - -import java.lang.reflect.Field; -import java.net.URISyntaxException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; -import org.apache.jena.datatypes.RDFDatatype; -import org.apache.jena.datatypes.TypeMapper; -import org.apache.jena.rdf.model.Literal; -import org.apache.jena.rdf.model.Model; -import org.apache.jena.rdf.model.Property; -import org.apache.jena.rdf.model.RDFNode; -import org.apache.jena.rdf.model.Resource; -import org.apache.jena.rdf.model.ResourceFactory; -import org.apache.jena.vocabulary.XSD; - -import kehio.annotations.RdfDatatype; - -/** - * Implements all Collection interfaces from https://www.javatpoint.com/collections-in-java - * @author cimmino - * - */ -class RdfDatatypeMapper implements RdfMapper { - - // -- Compatibility method - - @Override - public boolean hasProcesableAnnotation(Field field) { - return field.isAnnotationPresent(RdfDatatype.class); - } - - // -- from RDF to Object methods - - @Override - public Property fromRdfToObject(Field field, Object object, Model model, Resource subject) throws IllegalAccessException { - field.setAccessible(true); - - DatatypeAnotation annotation = new DatatypeAnotation(field); - List fieldValues = serialiseToWrappingPrimitiveCollection(annotation, model, subject); - if(KehioUtils.isWrappingPrimitive(field)) { - if(!fieldValues.isEmpty()) - KehioUtils.instantiateField(field, object, fieldValues.get(0)); - }else if(KehioUtils.isCollectionOfWrappingPrimitive(field)) { - KehioUtils.instantiateCollection(field, fieldValues, object); - }else { - throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfDatatype must be used with wrapping primitive types: String, Integer, Boolean, [...]; or a Collection, a List, a Queue, or a Set containing one of these wrapping primitive types. Please review annotation for attribute ",field.getName())); - } - return annotation.getProperty(); - } - - private List serialiseToWrappingPrimitiveCollection(DatatypeAnotation annotation, Model model, Resource subject) { - List fieldValues = new ArrayList<>(); - Field field = annotation.getField(); - List literals = KehioUtils.objectNodes(model, subject, annotation.getProperty()); - for(int index=0; index < literals.size(); index++) { - Object fieldValue = null; - RDFNode node = literals.get(index); - if(node.isLiteral()) { - Literal literal = node.asLiteral(); - if(!KehioUtils.isFieldString(field) && (literal.getValue().getClass().equals(field.getType()) || (literal.getValue() instanceof Number && field.getType().equals(Number.class)))) { - // Non string attribute - fieldValue = literal.getValue(); - }else { - fieldValue = serialiseToString(annotation, literal); - } - } - if(fieldValue!=null) - fieldValues.add(fieldValue); - } - return fieldValues; - } - - private String serialiseToString(DatatypeAnotation annotation, Literal literal) { - String instantiatedValue = null; - String literalValue = literal.getValue().toString(); - if(annotation.isSinkLang()) { - instantiatedValue = KehioUtils.concatStrings(literalValue,"@",literal.getLanguage()); - }else if(annotation.isSinkDatatype()) { - instantiatedValue = KehioUtils.concatStrings(literalValue,"^^",literal.getDatatypeURI()); - }else if(!annotation.getLang().isEmpty() && !literal.getLanguage().isEmpty() && literal.getLanguage().equals(annotation.getLang())) { - instantiatedValue = literalValue; - }else if(!annotation.getDatatype().isEmpty() && literal.getDatatypeURI().equals(annotation.getDatatype())) { - instantiatedValue = literal.getValue().toString(); - }else if(annotation.getDatatype().isEmpty() && literal.getDatatypeURI().equals(XSD.xstring.getURI())){ - instantiatedValue = literal.getValue().toString(); - } - return instantiatedValue; - } - - - - /*private Collection serialiseToCollection(Object object, DatatypeAnotation annotation, Model model, Resource subject) throws IllegalArgumentException, IllegalAccessException { - Collection collection = new ArrayList<>(); - String collectionType = KehioUtils.extractCollectionType(annotation.getField()); - - List literals = KehioUtils.objectNodes(model, subject, annotation.getProperty()); - for(int index=0; index < literals.size(); index++) { - RDFNode node = literals.get(index); - if(node.isLiteral()) { - - } - if(fieldValue!=null) - break; - } - } - - - try { - - if(collectionType.equals(KehioUtils.STRING_CLASS)) { - instantiateNonStringCollection(object, dtypeAnnotation, model, subject, collectionClazz); - }else { - instantiateStringCollection(object, dtypeAnnotation, model, subject); - } - } catch(Exception e) { - throw new IllegalArgumentException(e.toString()); - } - - return collection; - } - - private void instantiateNonStringCollection(Object object, DatatypeAnotation annotation, Model model, Resource subject, Class collectionClazz) throws IllegalAccessException { - List nodes = KehioUtils.objectNodes(model, subject, annotation.get); - - // Collection is not string - String datatype = annotation.getDatatype(); - String expectedDatatype = TypeMapper.getInstance().getTypeByClass(collectionClazz).getURI(); - if(!datatype.isEmpty() && !datatype.equals(expectedDatatype)) { - String illegalMessage = KehioUtils.concatStrings("@RdfDatatype error, for field ",annotation.getField().getName(), " it was annotated with the datatype ",datatype," which is incompatible for the Collection<",collectionClazz.getName(),">. Please check the annotation for this field, the datatype should be ",expectedDatatype," or similar"); - throw new IllegalArgumentException(illegalMessage); - } - Collection instantiatedCollection = nodes.stream().filter(node -> node.asLiteral().getDatatypeURI().equals(expectedDatatype)).collect(Collectors.toList()); - KehioUtils.instantiateCollection(dtypeAnnotation.getField(), instantiatedCollection, object); - } - - private void instantiateStringCollection(Object object, DatatypeAnotation dtypeAnnotation, Model model, Resource subject) throws IllegalAccessException { - List literals = KehioUtils.retrieveFromRdfPath(model, subject, dtypeAnnotation.getValue(), dtypeAnnotation.isPath()); - Collection collection = new ArrayList<>(); - for(int index=0; index < literals.size(); index++) { - Literal literal = literals.get(index).asLiteral(); - String value = extractValueFromLiteral(dtypeAnnotation, literal); - collection.add(value); - } - KehioUtils.instantiateCollection(dtypeAnnotation.getField(), collection, object); - }*/ - - - // -- from Object to RDF methods - - - @Override - public void fromObjectToRdf(Field field, Object object, Model model, Resource subject) throws IllegalAccessException, URISyntaxException { - field.setAccessible(true); - - DatatypeAnotation dtypeAnnotation = new DatatypeAnotation(field); - Object attributeValue = field.get(object); - - if(KehioUtils.isWrappingPrimitive(field)) { - buildWrappingPrimitiveRdf(dtypeAnnotation, attributeValue, model, subject); - }else if(KehioUtils.isCollectionOfWrappingPrimitive(field)) { - buildCollectionOfWrappingPrimitiveRdf(dtypeAnnotation, attributeValue, model, subject); - }else { - throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfDatatype must be used with wrapping primitive types: String, Integer, Boolean, [...]; or a Collection, a List, a Queue, or a Set containing one of these wrapping primitive types. Please review annotation for attribute ",field.getName())); - } - - } - - @SuppressWarnings("unchecked") - private void buildCollectionOfWrappingPrimitiveRdf(DatatypeAnotation dtypeAnnotation, Object attributeValue, Model model, Resource subject) { - Collection values = (Collection) attributeValue; - Iterator valuesIterator = values.iterator(); - while(valuesIterator.hasNext()) { - Object innerValue = valuesIterator.next(); - buildWrappingPrimitiveRdf(dtypeAnnotation, innerValue, model, subject); - } - } - - private void buildWrappingPrimitiveRdf(DatatypeAnotation dtypeAnnotation, Object attributeValue, Model model, Resource subject) { - if(attributeValue!=null) { - Property property = ResourceFactory.createProperty(dtypeAnnotation.getValue()); - Literal literal = ResourceFactory.createTypedLiteral(attributeValue); - if(!dtypeAnnotation.getDatatype().isEmpty()) { - RDFDatatype dt = TypeMapper.getInstance().getTypeByName(dtypeAnnotation.getDatatype()); - literal = ResourceFactory.createTypedLiteral(attributeValue.toString(), dt); - }else if(!dtypeAnnotation.getLang().isEmpty()) { - literal = ResourceFactory.createLangLiteral(attributeValue.toString(), dtypeAnnotation.getLang()); - }else if(dtypeAnnotation.isSinkDatatype()) { - literal = buildSinkDatatypeLiteral(attributeValue); - }else if(dtypeAnnotation.isSinkLang()) { - literal = buildSinkLangLiteral(attributeValue); - } - model.add(subject, property, literal); - } - } - - - private Literal buildSinkDatatypeLiteral(Object attributeValue) { - String value = attributeValue.toString(); - int splitIndex = value.lastIndexOf("^^")+2; - String dtype = value.substring(splitIndex, value.length()); - dtype = dtype.substring(1, dtype.length()-1); - value = value.substring(0,splitIndex-2); - RDFDatatype dt = TypeMapper.getInstance().getTypeByName(dtype); - return ResourceFactory.createTypedLiteral(value, dt); - } - - private Literal buildSinkLangLiteral(Object attributeValue) { - String value = attributeValue.toString(); - int splitIndex = value.lastIndexOf('@')+1; - String lang = value.substring(splitIndex, value.length()); - value = value.substring(0,splitIndex-1); - return ResourceFactory.createLangLiteral(value, lang); - } - - - - class DatatypeAnotation { - - private Field field; - - public DatatypeAnotation(Field field){ - this.field = field; - checkAnnotationConfiguration(); - } - - private static final String ERROR = "@RdfDatatype error, for field "; - private void checkAnnotationConfiguration() { - if (getValue() == null || getValue().isEmpty()) - throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfDatatype must be initialised with a value, please review annotation for attribute ",field.getName())); - if(!getDatatype().isEmpty() && isSinkLang()) - throw new IllegalArgumentException(KehioUtils.concatStrings(ERROR,field.getName(), " datatype and flag 'sinkLang' were provided. Please review annotation for attribute ",field.getName()," and keep only one.")); - if(!getLang().isEmpty() && isSinkDatatype()) - throw new IllegalArgumentException(KehioUtils.concatStrings(ERROR,field.getName(), " lang and flag 'sinkDatatype' were provided. Please review annotation for attribute ",field.getName()," and keep only one.")); - if(isSinkLang() && isSinkDatatype()) - throw new IllegalArgumentException(KehioUtils.concatStrings(ERROR,field.getName(), " both flags 'sinkDatatype' and 'sinkLang' were set to True. Please review annotation for attribute ",field.getName()," and keep only one of these flags as True.")); - if(!getDatatype().isEmpty() && !getLang().isEmpty()) - throw new IllegalArgumentException(KehioUtils.concatStrings(ERROR,field.getName(), " a datatype and a lang were provided. Please review annotation for attribute ",field.getName()," and keep only one of these options.")); - if (!getDatatype().isEmpty() && isSinkDatatype()) - throw new IllegalArgumentException(KehioUtils.concatStrings(ERROR ,field.getName(), " a datatype was provided and also 'sinkDatatype' was set to True. Please review annotation for attribute ",field.getName()," and keep only the datatype or the sinkDatatype flag.")); - if (!getLang().isEmpty() && isSinkLang()) - throw new IllegalArgumentException(KehioUtils.concatStrings(ERROR ,field.getName(), " a lang was provided and also 'sinkLang' was set to True. Please review annotation for attribute ",field.getName()," and keep only the lang or the sinkLang flag.")); - if((isSinkLang() || isSinkDatatype()) && !KehioUtils.isCollectionOfWrappingPrimitive(field) && !field.getType().equals(String.class)) - throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfDatatype error, 'sinkLang' and 'sinkDatatype' are only applicable for String attributes, please review the annotation for attribute ", field.getName())); - //if(isSinkLang() || isSinkDatatype() || !getLang().isEmpty()) - // throw new IllegalArgumentException( KehioUtils.concatStrings(ERROR, field.getName(), " it was used with a Collection containing a wrapping primitive that is not String, in this case flags 'sinkLang' and 'sinkDatatype', and also, 'lang' can not be used.")); - - // TODO: datatype solo se puede usar con un field string y lang tambien - } - - // -- Getters & Setters - - public String getValue() { - return field.getAnnotation(RdfDatatype.class).value().trim(); - } - - public Property getProperty() { - return ResourceFactory.createProperty(field.getAnnotation(RdfDatatype.class).value().trim()); - } - - public String getDatatype() { - return field.getAnnotation(RdfDatatype.class).datatype().trim(); - } - - public String getLang() { - return field.getAnnotation(RdfDatatype.class).lang().trim(); - } - - public boolean isSinkLang() { - return field.getAnnotation(RdfDatatype.class).sinkLang(); - } - - public boolean isSinkDatatype() { - return field.getAnnotation(RdfDatatype.class).sinkDatatype(); - } - - public Field getField() { - return field; - } - } - -} \ No newline at end of file diff --git a/src/main/java/kehio/mapper/RdfIdMapper.java b/src/main/java/kehio/mapper/RdfIdMapper.java deleted file mode 100755 index c83c2e5..0000000 --- a/src/main/java/kehio/mapper/RdfIdMapper.java +++ /dev/null @@ -1,76 +0,0 @@ -package kehio.mapper; - -import java.lang.reflect.Field; -import java.net.URI; -import java.net.URISyntaxException; -import org.apache.jena.rdf.model.Model; -import org.apache.jena.rdf.model.Property; -import org.apache.jena.rdf.model.Resource; -import org.apache.jena.vocabulary.RDF; - -import kehio.annotations.RdfId; - -class RdfIdMapper implements RdfMapper{ - - - @Override - public boolean hasProcesableAnnotation(Field field) { - return field.isAnnotationPresent(RdfId.class); - } - - - @Override - public Property fromRdfToObject(Field field, Object object, Model model, Resource subject) throws IllegalAccessException, URISyntaxException { - field.setAccessible(true); - if(field.getType().equals(String.class)) { - instantiateStringField(field, subject, object, model); - }else if(field.getType().equals(URI.class)) { - instantiateURIField(field, subject, object, model); - }else { - throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfId can be only used with String or java.net.URI types. Please review annotation for attribute ", field.getName())); - } - return null; - } - - private void instantiateStringField(Field field, Resource objectProperty, Object object, Model model) throws IllegalArgumentException, IllegalAccessException { - if(objectProperty.asNode().isBlank()){ - field.set(object, objectProperty.asNode().getBlankNodeLabel()); - }else if( objectProperty.asNode().isURI()) { - field.set(object, objectProperty.toString()); - }else { - // Nothing happens - } - } - - private void instantiateURIField(Field field, Resource objectProperty, Object object, Model model) throws IllegalArgumentException, IllegalAccessException, URISyntaxException { - if(objectProperty.asNode().isBlank()){ - field.set(object, new URI(objectProperty.asNode().getBlankNodeLabel())); - }else if( objectProperty.asNode().isURI()) { - field.set(object, new URI(objectProperty.toString())); - }else { - // Nothing happens - } - } - - // -- - - @Override - public void fromObjectToRdf(Field field, Object object, Model model, Resource nullSubject) throws IllegalArgumentException, IllegalAccessException, URISyntaxException { - field.setAccessible(true); - checkFieldCompatibility(field); - Object id = field.get(object); - Resource subject = null; - if(id!=null && !id.toString().trim().isEmpty()) { - subject = model.createResource(id.toString().trim()); - model.add(subject, RDF.type, Kehio.KEHIO_TYPE); - } - } - - private void checkFieldCompatibility(Field field) { - if(!field.getType().equals(String.class) && !field.getType().equals(URI.class)) { - throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfId can be only used with String or java.net.URI types. Please review annotation for attribute ", field.getName())); - } - } - - -} diff --git a/src/main/java/kehio/mapper/RdfMapper.java b/src/main/java/kehio/mapper/RdfMapper.java deleted file mode 100755 index aa8ab2f..0000000 --- a/src/main/java/kehio/mapper/RdfMapper.java +++ /dev/null @@ -1,17 +0,0 @@ -package kehio.mapper; - -import java.lang.reflect.Field; -import java.net.URISyntaxException; - -import org.apache.jena.rdf.model.Model; -import org.apache.jena.rdf.model.Property; -import org.apache.jena.rdf.model.Resource; - -public interface RdfMapper { - - - public boolean hasProcesableAnnotation(Field field); - public Property fromRdfToObject(Field field, Object object, Model model, Resource subject) throws IllegalArgumentException, IllegalAccessException, URISyntaxException; - public void fromObjectToRdf(Field field, Object object, Model model, Resource subject) throws IllegalArgumentException, IllegalAccessException, URISyntaxException; - -} diff --git a/src/main/java/kehio/mapper/RdfObjectGroupMapper.java b/src/main/java/kehio/mapper/RdfObjectGroupMapper.java deleted file mode 100755 index ee775c5..0000000 --- a/src/main/java/kehio/mapper/RdfObjectGroupMapper.java +++ /dev/null @@ -1,191 +0,0 @@ -package kehio.mapper; - -import java.lang.reflect.Field; -import java.net.URISyntaxException; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - -import org.apache.jena.rdf.model.Model; -import org.apache.jena.rdf.model.ModelFactory; -import org.apache.jena.rdf.model.Property; -import org.apache.jena.rdf.model.RDFNode; -import org.apache.jena.rdf.model.Resource; -import org.apache.jena.rdf.model.ResourceFactory; - -import kehio.annotations.RdfObjectGroup; -import kehio.annotations.RdfUrlMap; - -class RdfObjectGroupMapper implements RdfMapper { - - - @Override - public boolean hasProcesableAnnotation(Field field) { - return field.isAnnotationPresent(RdfObjectGroup.class); - } - - @Override - public Property fromRdfToObject(Field field, Object object, Model model, Resource subject) throws IllegalAccessException { - field.setAccessible(true); - - ObjectGroupAnnotation annotation = new ObjectGroupAnnotation(field); - List objectProperties = KehioUtils.objectNodes(model, subject, annotation.getValue()); - - Map instantiation = new HashMap<>(); - for(int index=0; index < objectProperties.size(); index++) { - try { - RDFNode node = objectProperties.get(index); - Object nestedObject = instantiateClassField(field, node.asResource() , model); - String key = instantiateKeyProperty(model, annotation.getGroupKey(), node.asResource(), field); - if(key!=null && nestedObject!=null) { - // Replace agrupation key for mapped value if exists - key = annotation.getGroupKeyInstantiated(key); - // Instantiate object - instantiation.put(key, nestedObject); - } - }catch(Exception e) { - throw new IllegalArgumentException(e.toString()); - } - } - if(!instantiation.isEmpty()) - field.set(object, instantiation); - return annotation.getProperty(); - } - - private String instantiateKeyProperty(Model model, String keyProperty, Resource objectProperty, Field field) { - String key = null; - List objectProperties = KehioUtils.objectNodes(model, objectProperty, keyProperty); - if(objectProperties.size()>1) { - throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfObjectGroup must point to a unique literal through they 'key' property, more than one were retrieved. please review annotation for attribute ", field.getName())); - }else if(objectProperties.isEmpty()) { - throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfObjectGroup must point to a unique literal through they 'key' property, zero were retrieved. please review annotation for attribute ", field.getName())); - }else { - RDFNode node = objectProperties.get(0); - if(node.isLiteral()) { - key = node.asLiteral().getString(); - }else { - throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfObjectGroup must point to a unique literal through they 'key' property, a non-literal was retrieved. please review annotation for attribute ", field.getName())); - } - } - return key; - } - - private Object instantiateClassField(Field field, Resource objectProperty, Model model) { - Object nestedObject = null; - try { - Class clazzFull = KehioUtils.extractMapValueType(field); - nestedObject = Kehio.serializeClass(clazzFull, model, objectProperty.asResource()); - }catch(Exception e) { - throw new IllegalArgumentException(e.toString()); - } - return nestedObject; - } - - - // --- - - - @Override - public void fromObjectToRdf(Field field, Object object, Model model, Resource subject) throws IllegalAccessException, URISyntaxException { - field.setAccessible(true); - - ObjectGroupAnnotation annotation = new ObjectGroupAnnotation(field); - - @SuppressWarnings("unchecked") - Map values = (Map) field.get(object); - - Model nestedModel = buildObjectRdf(annotation, values, subject, model.getNsPrefixMap()); - model.add(nestedModel); - } - - - private Model buildObjectRdf(ObjectGroupAnnotation annotation, Map values, Resource subject, Map prefixes) throws IllegalAccessException { - Model nestedModel = ModelFactory.createDefaultModel(); - - for(Entry entry : values.entrySet()) { - try { - Entry nestedObjects = Kehio.deserializeClassExtended(entry.getValue(), prefixes); - nestedModel.add(subject, ResourceFactory.createProperty(annotation.getValue()), nestedObjects.getKey()); - nestedModel.add(nestedObjects.getValue()); - if(annotation.addKey()) { - // Replace agrgupation key for mapped value if exists - String key = annotation.getGroupKeyInstantiated(entry.getKey()); - // inject into RDF - String groupKey = annotation.getGroupKey(); - nestedModel.add(nestedObjects.getKey(), ResourceFactory.createProperty(groupKey), key); - } - }catch(Exception e) { - throw new IllegalArgumentException(e.toString()); - } - } - return nestedModel; - } - - - // --- - - - private class ObjectGroupAnnotation{ - private Field field; - - public ObjectGroupAnnotation(Field field) { - this.field = field; - checkAnnotationRestrictions(); - } - - private void checkAnnotationRestrictions() { - String annotationValue = getValue(); - String anotationKey = getGroupKey(); - if (annotationValue==null || annotationValue.isEmpty()) - throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfObjectGroup must be initialised with a value. Please review annotation for attribute ", field.getName())); - if(anotationKey==null || anotationKey.isEmpty()) - throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfObjectGroup must be initialised with a key that must be a datatype property. Please review annotation for attribute ", field.getName())); - Entry mapTypes = KehioUtils.extractMapTypes(field); - if(!mapTypes.getKey().equals(KehioUtils.STRING_CLASS)) - throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfObjectGroup must be used with a Map which keys are Strings. Please review annotation for attribute ", field.getName())); - if(KehioUtils.isWrappingPrimitive(mapTypes.getValue())) - throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfObjectGroup must be used with a Map which values are non-wrapping primitive objects. Please review annotation for attribute ", field.getName())); - - } - - private String getValue() { - return field.getAnnotation(RdfObjectGroup.class).value(); - } - - private Property getProperty() { - return ResourceFactory.createProperty(field.getAnnotation(RdfObjectGroup.class).value()); - } - - private String getGroupKey() { - return field.getAnnotation(RdfObjectGroup.class).key(); - } - - private Boolean addKey() { - return field.getAnnotation(RdfObjectGroup.class).includeKey(); - } - - - private Map getMaps(){ - Map processedMap = new HashMap<>(); - RdfUrlMap[] maps = field.getAnnotation(RdfObjectGroup.class).aliases(); - if(maps!=null) { - for(int index=0; index < maps.length; index++) { - RdfUrlMap map = maps[index]; - processedMap.put(map.key(), map.value()); - } - } - return processedMap; - } - - private String getGroupKeyInstantiated(String key) { - Map maps = getMaps(); - if(maps.containsKey(key)) - key = maps.get(key); - return key; - } - - } - -} - diff --git a/src/main/java/kehio/mapper/RdfObjectMapper.java b/src/main/java/kehio/mapper/RdfObjectMapper.java deleted file mode 100755 index e6d7016..0000000 --- a/src/main/java/kehio/mapper/RdfObjectMapper.java +++ /dev/null @@ -1,305 +0,0 @@ -package kehio.mapper; - -import java.lang.reflect.Field; -import java.net.URI; -import java.net.URISyntaxException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - -import org.apache.jena.rdf.model.Model; -import org.apache.jena.rdf.model.Property; -import org.apache.jena.rdf.model.RDFNode; -import org.apache.jena.rdf.model.Resource; -import org.apache.jena.rdf.model.ResourceFactory; - -import kehio.annotations.RdfObject; -import kehio.annotations.RdfUrlMap; - -class RdfObjectMapper implements RdfMapper { - - - @Override - public boolean hasProcesableAnnotation(Field field) { - return field.isAnnotationPresent(RdfObject.class); - } - - - @Override - public Property fromRdfToObject(Field field, Object object, Model model, Resource subject) throws IllegalAccessException, URISyntaxException { - field.setAccessible(true); - - ObjectAnnotation annotation = new ObjectAnnotation(field); - - if(KehioUtils.isCollection(field)) { - // Collection case - Collection instantiatedCollection = initialiseObjectCollection(annotation, subject, model); - KehioUtils.instantiateCollection(field, instantiatedCollection, object); - } else { - // Class, String, or URI case - Object instantiatedField = serialiseUnitary(annotation, subject, model); - KehioUtils.instantiateField(field, object, instantiatedField); - } - return annotation.getProperty(); - } - - - private Object serialiseUnitary(ObjectAnnotation annotation, Resource subject, Model model) { - Object instantiatedObject = null; - List objectProperties = KehioUtils.objectNodes(model, subject, annotation.getProperty()); - if(objectProperties.size()==1) { - RDFNode objectProperty = objectProperties.get(0); - if(isResource(objectProperty)) { - Field field = annotation.getField(); - if(KehioUtils.isFieldString(field)) { - instantiatedObject = serialiseStringField(objectProperty, annotation); - }else if(KehioUtils.isFieldURI(field)) { - instantiatedObject = serialiseURIField(objectProperty); - } else { - instantiatedObject = serialiseClassField(field.getType().getCanonicalName(), objectProperty, model); - } - } - } - - return instantiatedObject; - } - - private String serialiseStringField(RDFNode objectProperty, ObjectAnnotation annotation) { - String instantiatedObject = null; - String base = annotation.getBase(); - if(objectProperty.asNode().isBlank()){ - instantiatedObject = objectProperty.asNode().getBlankNodeLabel().replace(base, ""); - }else if( objectProperty.asNode().isURI()) { - instantiatedObject = objectProperty.toString(); - instantiatedObject = instantiatedObject.replace(base, ""); - } - // Change URL for alias value - if(instantiatedObject!=null) { - String instantiatedObjectAlias = annotation.getAliasKeyInstantiated(instantiatedObject); - if(instantiatedObjectAlias!=null) - instantiatedObject = instantiatedObjectAlias; - if(annotation.getStrict() && instantiatedObjectAlias==null) - instantiatedObject=null; - } - - return instantiatedObject; - } - - private URI serialiseURIField(RDFNode objectProperty) { - URI instantiatedObject = null; - try { - if(objectProperty.asNode().isBlank()){ - instantiatedObject = new URI(objectProperty.asNode().getBlankNodeLabel()); - }else if( objectProperty.asNode().isURI()) { - instantiatedObject = new URI(objectProperty.toString()); - } - }catch(Exception e) { - throw new IllegalArgumentException(e.toString()); - } - return instantiatedObject; - } - - private Object serialiseClassField(String className, RDFNode objectProperty, Model model) { - Object instantiatedObject = null; - try { - Class clazzFull = Class.forName(className); - instantiatedObject = Kehio.serializeClass(clazzFull, model, objectProperty.asResource()); - }catch(Exception e) { - throw new IllegalArgumentException(e.toString()); - } - return instantiatedObject; - } - - private boolean isResource(RDFNode objectProperty) { - return objectProperty.asNode().isBlank() || objectProperty.asNode().isURI(); - } - - private Collection initialiseObjectCollection(ObjectAnnotation annotation, Resource subject, Model model) throws IllegalAccessException { - Collection collection = new ArrayList<>(); - List objectProperties = KehioUtils.objectNodes(model, subject, annotation.getProperty()); - for(int index=0; index < objectProperties.size(); index++) { - RDFNode node = objectProperties.get(index); - if(node.isResource()) { - try { - String subtype = KehioUtils.extractCollectionType(annotation.getField()); - Object instantiatedObject = null; - if(subtype.equals(KehioUtils.STRING_CLASS)) { - instantiatedObject = serialiseStringField(node, annotation) ; - }else if( subtype.equals(KehioUtils.URI_CLASS)) { - instantiatedObject = serialiseURIField( node) ; - }else { - instantiatedObject= serialiseClassField( subtype, node, model); - } - if(instantiatedObject!=null) - collection.add(instantiatedObject); - }catch(Exception e) { - throw new IllegalArgumentException(e.toString()); - } - }else { - String illegalMessage = KehioUtils.concatStrings("@RdfObject must be used for properties which range in the RDF are a blank node or a URI (String or java URI) and/or a Class"); - throw new IllegalArgumentException(illegalMessage); - } - } - return collection; - } - - // -- From object to to RDF methods - - @Override - public void fromObjectToRdf(Field field, Object object, Model model, Resource subject) throws IllegalAccessException, URISyntaxException { - field.setAccessible(true); - - ObjectAnnotation annotation = new ObjectAnnotation(field); - - Object instantiatedField = KehioUtils.instantiatedField(field, object); - if(instantiatedField!=null) { - if(KehioUtils.isFieldClass(field) && !KehioUtils.isFieldURI(field)) { - // Class case - deserialiseClass(instantiatedField, annotation, model, subject); - }else if(KehioUtils.isCollection(field) ) { - // Collection case - deserialiseClassCollection(instantiatedField, annotation, model, subject); - }else if(KehioUtils.isFieldString(field) || KehioUtils.isFieldURI(field)) { - // String or URI case - deserialiseStringOrURI(instantiatedField.toString(), annotation, model, subject); - }else { - throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfObject must be used with a non-wrapping primitive or a Collection of non-wrapping primitives, except with String or URI which are also allowed. Please review annotation for attribute ",field.getName())); - } - } - } - - private void deserialiseStringOrURI(String instantiatedField, ObjectAnnotation annotation, Model model, Resource subject) throws IllegalArgumentException, IllegalAccessException { - String objectProperty = annotation.getBase()+annotation.getGroupValueInstantiated(instantiatedField); - - if(KehioUtils.isValidResource(objectProperty)) { - model.add(subject, annotation.getProperty(), ResourceFactory.createResource(objectProperty)); - }else { - throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfObject used with a String or a URI was resolved incorrectly as ", objectProperty,". Please review annotation for attribute ",annotation.getField().getName())); - } - } - - - private void deserialiseClass(Object instantiatedField, ObjectAnnotation annotation, Model model, Resource subject) { - try { - Entry nested = Kehio.deserializeClassExtended(instantiatedField, model.getNsPrefixMap()); - Resource nestedSubject = nested.getKey(); - if(nestedSubject!=null && !nested.getValue().isEmpty()) { - model.add(nested.getValue()); - model.add(subject, annotation.getProperty(), nestedSubject); - }//else { - // throw new IllegalArgumentException(KehioUtils.concatStrings("An exception occured while processing annotation @RdfObject with value, ",annotation.getProperty().toString(),". Please review attribute ",annotation.getField().getName())); - //} - }catch(Exception e) { - throw new IllegalArgumentException(e.toString()); - } - } - - @SuppressWarnings("unchecked") - private void deserialiseClassCollection(Object instantiatedField, ObjectAnnotation annotation, Model model, Resource subject) throws IllegalArgumentException, IllegalAccessException { - Collection nestedObjects = (Collection) instantiatedField; - Iterator nestedObjectsIterator = nestedObjects.iterator(); - while(nestedObjectsIterator.hasNext()) { - Object nestedObject = nestedObjectsIterator.next(); - if(nestedObject instanceof String || nestedObject instanceof URI) { - deserialiseStringOrURI(nestedObject.toString(), annotation, model, subject); - }else if(!isNotProcessable(nestedObject)) { - deserialiseClass(nestedObject, annotation, model, subject); - }else { - throw new IllegalArgumentException("@RdfObject error, the annotation was used with a collection of wrapping primitive that is not String. Instead, use a collection of String, or URI, or a Class. Please review field"); - } - } - } - - private boolean isNotProcessable(Object nestedObject) { - return nestedObject instanceof Long || nestedObject instanceof Float || nestedObject instanceof Double || nestedObject instanceof Character || nestedObject instanceof Boolean || nestedObject instanceof Integer || nestedObject instanceof Short || nestedObject instanceof Byte; - } - - // -- Annotation values methods - - - class ObjectAnnotation{ - - private Field field; - - public ObjectAnnotation(Field field) { - this.field = field; - checkout(); - } - - private void checkout() { - if (getValue()==null || getValue().isEmpty()) - throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfObject must be initialised with a value. Please review annotation for attribute ", field.getName())); - String value = getValue(); - if(!KehioUtils.isURI(value)) - throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfObject must be initialised with a value that is a valid URI. Please review annotation for attribute ", field.getName())); - if(!KehioUtils.isFieldClass(field) & !KehioUtils.isFieldURI(field) & !KehioUtils.isFieldString(field) && !(KehioUtils.isCollection(field) && isCollectionCompatible())) - throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfObject must be used with attributes that are a URI, a String, a Class or a Collection containing one of the previous. Please review attribute ", field.getName())); - //if(!getBase().isEmpty() && !getMaps().isEmpty()) - // throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfObject can not be used with 'map' and 'base' at the same time. Please review attribute ", field.getName())); - - } - - private boolean isCollectionCompatible() { - String collectionType = KehioUtils.extractCollectionType(field); - return collectionType.equals(KehioUtils.STRING_CLASS) || !KehioUtils.isWrappingPrimitive(collectionType) ; - } - - private String getValue() { - return field.getAnnotation(RdfObject.class).value(); - } - - private Property getProperty() { - return ResourceFactory.createProperty(field.getAnnotation(RdfObject.class).value()); - } - - private String getBase() { - return field.getAnnotation(RdfObject.class).base(); - } - - private boolean getStrict() { - return field.getAnnotation(RdfObject.class).strict(); - } - - - private Field getField() { - return field; - } - - private Map getAliases(){ - Map processedMap = new HashMap<>(); - RdfUrlMap[] maps = field.getAnnotation(RdfObject.class).aliases(); - if(maps!=null) { - for(int index=0; index < maps.length; index++) { - RdfUrlMap map = maps[index]; - processedMap.put(map.key(), map.value()); - } - } - return processedMap; - } - - private String getAliasKeyInstantiated(String key) { - return getAliases().get(key); - } - - private String getGroupValueInstantiated(String simplifiedvalue) { - Map maps = getAliases(); - Iterator> iterator = maps.entrySet().iterator(); - while(iterator.hasNext()) { - Entry entry = iterator.next(); - if(simplifiedvalue.equals(entry.getValue())) { - simplifiedvalue = entry.getKey(); - } - } - return simplifiedvalue; - } - - - - - - } -} \ No newline at end of file diff --git a/src/main/java/kehio/mapper/RdfPropertiesContainerMapper.java b/src/main/java/kehio/mapper/RdfPropertiesContainerMapper.java deleted file mode 100755 index 3c8fd2a..0000000 --- a/src/main/java/kehio/mapper/RdfPropertiesContainerMapper.java +++ /dev/null @@ -1,175 +0,0 @@ -package kehio.mapper; - -import java.lang.reflect.Field; -import java.net.URISyntaxException; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; - -import org.apache.jena.rdf.model.Model; -import org.apache.jena.rdf.model.Property; -import org.apache.jena.rdf.model.RDFNode; -import org.apache.jena.rdf.model.Resource; -import org.apache.jena.rdf.model.ResourceFactory; -import org.apache.jena.rdf.model.Statement; -import org.apache.jena.rdf.model.StmtIterator; - -import kehio.annotations.RdfPropertiesContainer; -import kehio.annotations.RdfUrlMap; - - -class RdfPropertiesContainerMapper implements RdfMapper{ - - protected Set forbidenRdfProperties; - - @Override - public boolean hasProcesableAnnotation(Field field) { - return field.isAnnotationPresent(RdfPropertiesContainer.class); - } - - public void setPropertiesNotToContain(Set forbidenRdfProperties) { - this.forbidenRdfProperties = forbidenRdfProperties; - } - - @Override - public Property fromRdfToObject(Field field, Object object, Model model, Resource subject) throws IllegalArgumentException, IllegalAccessException, URISyntaxException { - field.setAccessible(true); - - PropertiesContainerAnotation annotation = new PropertiesContainerAnotation(field); - Map collectionOfTriples = populateMapFromRDF(annotation, model, subject); - - field.set(object, collectionOfTriples); - - return null; - } - - - - private Map populateMapFromRDF(PropertiesContainerAnotation annotation , Model model, Resource subject) { - Set ignore = annotation.getPropertiesToIgnore(); - Map collectionOfTriples = new HashMap<>(); - StmtIterator stIterator = model.listStatements(subject, null, (RDFNode) null); - while(stIterator.hasNext()) { - Statement st = stIterator.next(); - if(!forbidenRdfProperties.contains(st.getPredicate()) && !ignore.contains(st.getPredicate())){ - String shortenPredicate = shortenURI(model, st.getPredicate().toString()); - String shortenObject = st.getObject().toString(); - if(st.getObject().isResource()) - shortenObject = shortenURI(model, shortenObject); - collectionOfTriples.put(shortenPredicate, shortenObject); - } - } - return collectionOfTriples; - } - - private String shortenURI(Model model, String uri) { - String shortenUri = model.shortForm(uri); - if(shortenUri==null) - shortenUri = uri; - return shortenUri; - } - - // -- - - @Override - public void fromObjectToRdf(Field field, Object object, Model model, Resource subject) throws IllegalArgumentException, IllegalAccessException, URISyntaxException { - field.setAccessible(true); - - PropertiesContainerAnotation annotation = new PropertiesContainerAnotation(field); - @SuppressWarnings("unchecked") - Map values = (Map) annotation.getField().get(object); - for(Entry entry : values.entrySet()) { - try { - Property rdfProperty = computeFullProperty(entry.getKey(), model.getNsPrefixMap()); - populateModel(model, subject, rdfProperty, entry.getValue()); - }catch(Exception e) { - throw new IllegalArgumentException(e.toString()); - } - } - } - - private Property computeFullProperty(String property, Map prefixes) { - Property computedProperty = ResourceFactory.createProperty(property); - int index = property.indexOf(':'); - if(index>-1) { - String prefix = property.substring(0,index); - String value = property.substring(index+1,property.length()); - String url = prefixes.get(prefix); - if(url!=null) - computedProperty = ResourceFactory.createProperty(url+value); - } - return computedProperty; - } - - private void populateModel(Model model, Resource subject, Property rdfProperty, String objectValue) { - - try { - RDFNode objectNode = model.createLiteral(objectValue); - model.add(subject, rdfProperty, objectNode); - }catch (Exception e) { - throw new IllegalArgumentException(e.toString()); - } - - } - - - - - class PropertiesContainerAnotation{ - - private Field field; - - public PropertiesContainerAnotation(Field field) { - this.field = field; - checkAnnotation(); - } - - private void checkAnnotation() { - if(!field.getType().equals(Map.class)) - throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfContainer must be used only with a Map attribute. Please review annotation in field ", field.getName())); - Entry mapTypes = KehioUtils.extractMapTypes(field); - if(!mapTypes.getKey().contains(KehioUtils.STRING_CLASS)) - throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfContainer must be used only with a Map that has as key type a String. Please review annotation in field ", field.getName())); - - //if(!KehioUtils.extractMapValueType(field).toString().equals(Object.class.toString())) - // throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfContainer must be used only with a Map that has as value a java generic Object. Please review annotation in field ", field.getName())); - - /* - if (onlyDatatypes() && onlyObjects()) - throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfContainer can have only one flag marked as true, either 'onlyDatatypes' or 'onlyObjects'. Please review annotation for field ", field.getName())); - - if(onlyDatatypes() && (!mapTypes.getValue().equals(KehioUtils.STRING_CLASS) || ) - throw new IllegalArgumentException(KehioUtils.concatStrings("@RdfContainer must be used only with a Map attribute. If 'onlyDatatypes' is true, then the values of the map must be String or a Collection of Strings. Please review annotation in field ", field.getName())); - */ - } - - - /* public boolean onlyDatatypes() { - return field.getAnnotation(RdfPropertiesContainer.class).onlyDatatypes(); - } - - public boolean onlyObjects() { - return field.getAnnotation(RdfPropertiesContainer.class).onlyObjects(); - }*/ - - public Field getField() { - return field; - } - - public Set getPropertiesToIgnore() { - Set ignore = new HashSet<>(); - String[] ignorable = field.getAnnotation(RdfPropertiesContainer.class).ignore(); - for(int index=0; index < ignorable.length; index++) { - ignore.add(ResourceFactory.createProperty(ignorable[index])); - } - return ignore; - } - - - - } - -} diff --git a/src/main/java/wot/jtd/JsonHandler.java b/src/main/java/wot/jtd/JsonHandler.java index 7ba7871..db91e9a 100755 --- a/src/main/java/wot/jtd/JsonHandler.java +++ b/src/main/java/wot/jtd/JsonHandler.java @@ -6,6 +6,7 @@ import java.util.SortedSet; import java.util.TreeSet; import java.util.UUID; +import java.util.regex.Matcher; import java.util.regex.Pattern; import com.fasterxml.jackson.core.JsonProcessingException; @@ -92,7 +93,9 @@ private static void removeNestedIds(JsonObject json, boolean first) { private static void removeIdentifier(JsonObject json, JsonElement element, String key) { Boolean isURN = URN_PATTERN.matcher(element.getAsString()).matches(); - Boolean isUUID = isUUID(element.getAsString()); + try{ + Boolean isUUID = isUUID(element.getAsString()); + if(isURN && JTD.getRemoveNestedURNIds()) { json.remove(key); }else if(isUUID && JTD.getRemoveNestedUUIds()) { @@ -100,18 +103,27 @@ private static void removeIdentifier(JsonObject json, JsonElement element, Strin }else if(!isURN && !isUUID){ json.remove(key); } + }catch(Exception e) { + System.out.println("the error was given by: "+element.getAsString()); + } } private static final Pattern URN_PATTERN = Pattern.compile( "^urn:[a-z0-9][a-z0-9-]{0,31}:([a-z0-9()+,\\-.:=@;$_!*']|%[0-9a-f]{2})+/?.*$", Pattern.CASE_INSENSITIVE); + private static final Pattern UUID_PATTERN = Pattern.compile("[a-f0-9\\-]{36}"); private static boolean isUUID(String uri) { Boolean isUUID = false; try { - UUID.fromString(uri); - isUUID = true; + Matcher extracted = UUID_PATTERN.matcher(uri); + // TODO: extract UUIds code + if(extracted.find()){ + UUID.fromString(extracted.group()); + isUUID = true; + } + }catch(Exception e) { e.printStackTrace(); } diff --git a/src/main/java/wot/jtd/RDFHandler.java b/src/main/java/wot/jtd/RDFHandler.java index afbd473..a667255 100755 --- a/src/main/java/wot/jtd/RDFHandler.java +++ b/src/main/java/wot/jtd/RDFHandler.java @@ -25,7 +25,7 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; -import kehio.mapper.Kehio; +import kehio.Kehio; import wot.jtd.exception.SchemaValidationException; import wot.jtd.model.Thing; diff --git a/src/test/java/kehio/fromRDF/datatype/tests/DatatypeGroupRDFTest.java b/src/test/java/kehio/fromRDF/datatype/tests/DatatypeGroupRDFTest.java deleted file mode 100755 index cfc2476..0000000 --- a/src/test/java/kehio/fromRDF/datatype/tests/DatatypeGroupRDFTest.java +++ /dev/null @@ -1,38 +0,0 @@ -package kehio.fromRDF.datatype.tests; - -import java.lang.reflect.InvocationTargetException; -import java.net.URISyntaxException; - -import org.apache.jena.rdf.model.Model; -import org.apache.jena.rdf.model.Resource; -import org.apache.jena.rdf.model.ResourceFactory; -import org.junit.Assert; -import org.junit.Test; - -import kehio.mapper.Kehio; -import kehio.models.test.TestPost1; -import kehio.models.test.TestPost2; - -public class DatatypeGroupRDFTest { - - - @Test - public void testDatatype1() throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException, InstantiationException, InvocationTargetException, NoSuchMethodException, SecurityException { - Model model = TestPost1.expectedModel; - Resource subject = model.listSubjectsWithProperty(ResourceFactory.createProperty("http://xmlns.com/foaf/0.1/description")).next(); - TestPost1 object = (TestPost1) Kehio.serializeClass(TestPost1.class, model, subject); - TestPost1 expectedObject = new TestPost1(true); - - Assert.assertTrue(expectedObject.equals(object)); - } - - @Test - public void testDatatype2() throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException, InstantiationException, InvocationTargetException, NoSuchMethodException, SecurityException { - Model model = TestPost2.expectedModel; - Resource subject = model.listSubjectsWithProperty(ResourceFactory.createProperty("http://xmlns.com/foaf/0.1/numbers")).next(); - TestPost2 object = (TestPost2) Kehio.serializeClass(TestPost2.class, model, subject); - TestPost2 expectedObject = new TestPost2(true); - - Assert.assertTrue(expectedObject.equals(object)); - } -} \ No newline at end of file diff --git a/src/test/java/kehio/fromRDF/datatype/tests/DatatypeRDFTest.java b/src/test/java/kehio/fromRDF/datatype/tests/DatatypeRDFTest.java deleted file mode 100755 index f628bf3..0000000 --- a/src/test/java/kehio/fromRDF/datatype/tests/DatatypeRDFTest.java +++ /dev/null @@ -1,37 +0,0 @@ -package kehio.fromRDF.datatype.tests; - -import java.lang.reflect.InvocationTargetException; -import java.net.URISyntaxException; - -import org.apache.jena.rdf.model.Model; -import org.apache.jena.rdf.model.Resource; -import org.apache.jena.rdf.model.ResourceFactory; -import org.junit.Assert; -import org.junit.Test; - -import kehio.mapper.Kehio; -import kehio.models.test.TestPost1; -import kehio.models.test.TestPost2; - -public class DatatypeRDFTest { - - - - @Test - public void testDatatypeGroup1() throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, URISyntaxException { - TestPost1 expectedObject = new TestPost1(true); - Model model = TestPost1.expectedModel; - Resource subject = model.listSubjectsWithProperty(ResourceFactory.createProperty("http://xmlns.com/foaf/0.1/description")).next(); - TestPost1 object = (TestPost1) Kehio.serializeClass(TestPost1.class, model, subject); - Assert.assertTrue(expectedObject.equals(object)); - } - - @Test - public void testDatatypeGroup2() throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, URISyntaxException { - TestPost2 expectedObject = new TestPost2(true); - Model model = TestPost2.expectedModel; - Resource subject = model.listSubjectsWithProperty(ResourceFactory.createProperty("http://xmlns.com/foaf/0.1/numbers")).next(); - TestPost2 object = (TestPost2) Kehio.serializeClass(TestPost2.class, model, subject); - Assert.assertTrue(expectedObject.equals(object)); - } -} \ No newline at end of file diff --git a/src/test/java/kehio/fromRDF/datatype/tests/ObjectRdfTest.java b/src/test/java/kehio/fromRDF/datatype/tests/ObjectRdfTest.java deleted file mode 100755 index 068e3f9..0000000 --- a/src/test/java/kehio/fromRDF/datatype/tests/ObjectRdfTest.java +++ /dev/null @@ -1,69 +0,0 @@ -package kehio.fromRDF.datatype.tests; - -import java.io.ByteArrayInputStream; -import java.lang.reflect.InvocationTargetException; -import java.net.URISyntaxException; -import org.apache.jena.rdf.model.Model; -import org.apache.jena.rdf.model.ModelFactory; -import org.apache.jena.rdf.model.Resource; -import org.apache.jena.rdf.model.ResourceFactory; -import org.junit.Assert; -import org.junit.Test; - -import kehio.mapper.Kehio; -import kehio.models.test.TestPerson1; -import kehio.models.test.TestPerson2; - -public class ObjectRdfTest { - - - private static final String TEST1 = " \n" + - " \"John\" ;\n" + - " \n" + - " [ \n" + - " \"+34 54 65 23\" ] ;\n" + - " \n" + - " \"Fold\" , \"Doe\"\n" + - " ."; - - - @Test - public void test1() throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException, InstantiationException, InvocationTargetException, NoSuchMethodException, SecurityException { - - Model model = ModelFactory.createDefaultModel(); - model.read(new ByteArrayInputStream(TEST1.getBytes()), null, "Turtle"); - TestPerson1 person = (TestPerson1) Kehio.serializeClass(TestPerson1.class, model, ResourceFactory.createResource("http://kehio-tests.com/instance1")); - - TestPerson1 expectedPerson = new TestPerson1(true); - Assert.assertTrue(expectedPerson.equals(person)); - } - - private static final String TEST2 = "[ \n" + - " \"John\" ;\n" + - " \n" + - " [ \n" + - " \"+34 54 65 23\" ] ;\n" + - " \n" + - " [ \n" + - " \"+34 54 65 23\" ] ;\n" + - " \n" + - " \"Fold\" , \"Doe\"\n" + - "] ."; - - @Test - public void test2() throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException, InstantiationException, InvocationTargetException, NoSuchMethodException, SecurityException { - - Model model = ModelFactory.createDefaultModel(); - model.read(new ByteArrayInputStream(TEST2.getBytes()), null, "Turtle"); - Resource subject = model.listSubjectsWithProperty(ResourceFactory.createProperty("http://xmlns.com/foaf/0.1/name")).next(); - TestPerson2 person = (TestPerson2) Kehio.serializeClass(TestPerson2.class, model, subject); - - - TestPerson2 expectedPerson = new TestPerson2(true); - System.out.println(person.toString()); - System.out.println(expectedPerson.toString()); - Assert.assertTrue(expectedPerson.equals(person)); - } - - -} diff --git a/src/test/java/kehio/models/test/ORM4RDF1DTTest.java b/src/test/java/kehio/models/test/ORM4RDF1DTTest.java deleted file mode 100755 index 36b9de6..0000000 --- a/src/test/java/kehio/models/test/ORM4RDF1DTTest.java +++ /dev/null @@ -1,117 +0,0 @@ -package kehio.models.test; - -import kehio.annotations.RdfDatatype; -import kehio.annotations.RdfId; - -public class ORM4RDF1DTTest { - - @RdfId - public String id; - - @RdfDatatype("http://xmlns.com/foaf/0.1/age") - public Integer attrInteger; - - @RdfDatatype("http://xmlns.com/foaf/0.1/age") - public String attrString; - - @RdfDatatype("http://xmlns.com/foaf/0.1/age") - public Boolean attrBool; - - - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("ID: ").append(id).append("\n"); - builder.append("AttrInt: ").append(attrInteger).append("\n"); - builder.append("AttrStr: ").append(attrString).append("\n"); - builder.append("AttrBool: ").append(attrBool).append("\n"); - return builder.toString(); - } - - - public String getId() { - return id; - } - - - public void setId(String id) { - this.id = id; - } - - - public Integer getAttrInteger() { - return attrInteger; - } - - - public void setAttrInteger(Integer attrInteger) { - this.attrInteger = attrInteger; - } - - - public String getAttrString() { - return attrString; - } - - - public void setAttrString(String attrString) { - this.attrString = attrString; - } - - - public Boolean getAttrBool() { - return attrBool; - } - - - public void setAttrBool(Boolean attrBool) { - this.attrBool = attrBool; - } - - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((attrBool == null) ? 0 : attrBool.hashCode()); - result = prime * result + ((attrInteger == null) ? 0 : attrInteger.hashCode()); - result = prime * result + ((attrString == null) ? 0 : attrString.hashCode()); - result = prime * result + ((id == null) ? 0 : id.hashCode()); - return result; - } - - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (!(obj instanceof ORM4RDF1DTTest)) - return false; - ORM4RDF1DTTest other = (ORM4RDF1DTTest) obj; - if (attrBool == null) { - if (other.attrBool != null) - return false; - } else if (!attrBool.equals(other.attrBool)) - return false; - if (attrInteger == null) { - if (other.attrInteger != null) - return false; - } else if (!attrInteger.equals(other.attrInteger)) - return false; - if (attrString == null) { - if (other.attrString != null) - return false; - } else if (!attrString.equals(other.attrString)) - return false; - if (id == null) { - if (other.id != null) - return false; - } else if (!id.equals(other.id)) - return false; - return true; - } - - - -} diff --git a/src/test/java/kehio/models/test/ORM4RDF2DTTest.java b/src/test/java/kehio/models/test/ORM4RDF2DTTest.java deleted file mode 100755 index 9fc2817..0000000 --- a/src/test/java/kehio/models/test/ORM4RDF2DTTest.java +++ /dev/null @@ -1,118 +0,0 @@ -package kehio.models.test; - -import kehio.annotations.RdfDatatype; -import kehio.annotations.RdfId; - -public class ORM4RDF2DTTest { - - @RdfId - public String id; - - @RdfDatatype("http://xmlns.com/foaf/0.1/age") - public Integer attrInteger; - - @RdfDatatype(value="http://xmlns.com/foaf/0.1/age", datatype="http://www.w3.org/2001/XMLSchema#int") - public String attrString1; - - @RdfDatatype("http://xmlns.com/foaf/0.1/age") - public String attrString2; - - - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("ID: ").append(id).append("\n"); - builder.append("AttrInt: ").append(attrInteger).append("\n"); - builder.append("AttrStr: ").append(attrString1).append("\n"); - builder.append("AttrStr: ").append(attrString2).append("\n"); - return builder.toString(); - } - - - public String getId() { - return id; - } - - - public void setId(String id) { - this.id = id; - } - - - public Integer getAttrInteger() { - return attrInteger; - } - - - public void setAttrInteger(Integer attrInteger) { - this.attrInteger = attrInteger; - } - - - public String getAttrString1() { - return attrString1; - } - - - public void setAttrString1(String attrString1) { - this.attrString1 = attrString1; - } - - - public String getAttrString2() { - return attrString2; - } - - - public void setAttrString2(String attrString2) { - this.attrString2 = attrString2; - } - - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((attrInteger == null) ? 0 : attrInteger.hashCode()); - result = prime * result + ((attrString1 == null) ? 0 : attrString1.hashCode()); - result = prime * result + ((attrString2 == null) ? 0 : attrString2.hashCode()); - result = prime * result + ((id == null) ? 0 : id.hashCode()); - return result; - } - - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (!(obj instanceof ORM4RDF2DTTest)) - return false; - ORM4RDF2DTTest other = (ORM4RDF2DTTest) obj; - if (attrInteger == null) { - if (other.attrInteger != null) - return false; - } else if (!attrInteger.equals(other.attrInteger)) - return false; - if (attrString1 == null) { - if (other.attrString1 != null) - return false; - } else if (!attrString1.equals(other.attrString1)) - return false; - if (attrString2 == null) { - if (other.attrString2 != null) - return false; - } else if (!attrString2.equals(other.attrString2)) - return false; - if (id == null) { - if (other.id != null) - return false; - } else if (!id.equals(other.id)) - return false; - return true; - } - - - - -} diff --git a/src/test/java/kehio/models/test/TestPerson1.java b/src/test/java/kehio/models/test/TestPerson1.java deleted file mode 100755 index b0d24a9..0000000 --- a/src/test/java/kehio/models/test/TestPerson1.java +++ /dev/null @@ -1,82 +0,0 @@ -package kehio.models.test; - -import java.util.HashSet; -import java.util.Set; - -import kehio.annotations.RdfDatatype; -import kehio.annotations.RdfObject; - -public class TestPerson1 { - - @RdfDatatype("http://xmlns.com/foaf/0.1/name") - private String name; - @RdfDatatype("http://xmlns.com/foaf/0.1/surnames") - private Set surnames; - @RdfObject("http://xmlns.com/foaf/0.1/phone") - private TestTelephone phone; - - public TestPerson1(){ - - } - - public TestPerson1(boolean init){ - if(init) { - name = "John"; - surnames = new HashSet<>(); - surnames.add("Doe"); - surnames.add("Fold"); - TestTelephone phone = new TestTelephone(true); - this.phone = phone; - } - - } - - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("Name: ").append(name).append("\n"); - builder.append("Surname: ").append(surnames).append("\n"); - builder.append("Phone: ").append(phone).append("\n"); - return builder.toString(); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((name == null) ? 0 : name.hashCode()); - result = prime * result + ((phone == null) ? 0 : phone.hashCode()); - result = prime * result + ((surnames == null) ? 0 : surnames.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (!(obj instanceof TestPerson1)) - return false; - TestPerson1 other = (TestPerson1) obj; - if (name == null) { - if (other.name != null) - return false; - } else if (!name.equals(other.name)) - return false; - if (phone == null) { - if (other.phone != null) - return false; - } else if (!phone.equals(other.phone)) - return false; - if (surnames == null) { - if (other.surnames != null) - return false; - } else if (!surnames.equals(other.surnames)) - return false; - return true; - } - - - - -} diff --git a/src/test/java/kehio/models/test/TestPerson2.java b/src/test/java/kehio/models/test/TestPerson2.java deleted file mode 100755 index 92ce38f..0000000 --- a/src/test/java/kehio/models/test/TestPerson2.java +++ /dev/null @@ -1,81 +0,0 @@ -package kehio.models.test; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import kehio.annotations.RdfDatatype; -import kehio.annotations.RdfObject; - -public class TestPerson2 { - @RdfDatatype("http://xmlns.com/foaf/0.1/name") - private String name; - @RdfDatatype("http://xmlns.com/foaf/0.1/surnames") - private Set surnames; - @RdfObject("http://xmlns.com/foaf/0.1/phone") - private List phones; - - public TestPerson2() { - - } - public TestPerson2(boolean init){ - if(init) { - name = "John"; - surnames = new HashSet<>(); - surnames.add("Doe"); - surnames.add("Fold"); - TestTelephone phone1 = new TestTelephone(true); - TestTelephone phone2 = new TestTelephone(true); - this.phones = new ArrayList<>(); - this.phones.add(phone1); - this.phones.add(phone2); - } - - } - - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("Name: ").append(name).append("\n"); - builder.append("Surname: ").append(surnames).append("\n"); - builder.append("Phones: ").append(phones).append("\n"); - return builder.toString(); - } - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((name == null) ? 0 : name.hashCode()); - result = prime * result + ((phones == null) ? 0 : phones.hashCode()); - result = prime * result + ((surnames == null) ? 0 : surnames.hashCode()); - return result; - } - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (!(obj instanceof TestPerson2)) - return false; - TestPerson2 other = (TestPerson2) obj; - if (name == null) { - if (other.name != null) - return false; - } else if (!name.equals(other.name)) - return false; - if (phones == null) { - if (other.phones != null) - return false; - } else if (!phones.equals(other.phones)) - return false; - if (surnames == null) { - if (other.surnames != null) - return false; - } else if (!surnames.equals(other.surnames)) - return false; - return true; - } - - -} diff --git a/src/test/java/kehio/models/test/TestPost1.java b/src/test/java/kehio/models/test/TestPost1.java deleted file mode 100755 index fb691ae..0000000 --- a/src/test/java/kehio/models/test/TestPost1.java +++ /dev/null @@ -1,75 +0,0 @@ -package kehio.models.test; - -import java.io.ByteArrayInputStream; -import java.util.HashMap; -import java.util.Map; - -import org.apache.jena.rdf.model.Model; -import org.apache.jena.rdf.model.ModelFactory; - -import kehio.annotations.RdfDatatypeGroup; - -public class TestPost1 { - - private static final String OUTPUT = "[ \"esto es un ejemplo\"@es , \"Ceci est un exemple\"@fr , \"questo é un esempio\"@it , \"this is an example\"@en ] ."; - public static Model expectedModel = ModelFactory.createDefaultModel(); - static{ - expectedModel.read(new ByteArrayInputStream(OUTPUT.getBytes()), null, "Turtle"); - } - - @RdfDatatypeGroup(value="http://xmlns.com/foaf/0.1/description", byLang=true) - private Map post; - - public TestPost1() { - - } - - public TestPost1(boolean init) { - if(init) { - post = new HashMap<>(); - post.put("en", "this is an example"); - post.put("es", "esto es un ejemplo"); - post.put("it", "questo é un esempio"); - post.put("fr", "Ceci est un exemple"); - } - } - - public Map getPost() { - return post; - } - - public void setPost(Map post) { - this.post = post; - } - - public String toString() { - return this.post.toString(); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((post == null) ? 0 : post.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (!(obj instanceof TestPost1)) - return false; - TestPost1 other = (TestPost1) obj; - if (post == null) { - if (other.post != null) - return false; - } else if (!post.equals(other.post)) - return false; - return true; - } - - -} diff --git a/src/test/java/kehio/models/test/TestPost2.java b/src/test/java/kehio/models/test/TestPost2.java deleted file mode 100755 index aea092f..0000000 --- a/src/test/java/kehio/models/test/TestPost2.java +++ /dev/null @@ -1,79 +0,0 @@ -package kehio.models.test; - -import java.io.ByteArrayInputStream; -import java.util.HashMap; -import java.util.Map; - -import org.apache.jena.rdf.model.Model; -import org.apache.jena.rdf.model.ModelFactory; - -import kehio.annotations.RdfDatatypeGroup; - -public class TestPost2 { - - // -- RDF representation - private static final String OUTPUT = "[ \"12\"^^ , \"12.2\"^^ , \"this is an example\" , \"2002-09-24\"^^ ] ."; - public static Model expectedModel = ModelFactory.createDefaultModel(); - static{ - expectedModel.read(new ByteArrayInputStream(OUTPUT.getBytes()), null, "Turtle"); - } - - // ---- - - - @RdfDatatypeGroup(value="http://xmlns.com/foaf/0.1/numbers", byDatatype=true) - private Map post; - - public TestPost2() { - - } - - public TestPost2(boolean init) { - if(init) { - post = new HashMap<>(); - post.put("http://www.w3.org/2001/XMLSchema#string", "this is an example"); - post.put("http://www.w3.org/2001/XMLSchema#int", "12"); - post.put("http://www.w3.org/2001/XMLSchema#double", "12.2"); - post.put("http://www.w3.org/2001/XMLSchema#date", "2002-09-24"); - } - } - - public Map getPost() { - return post; - } - - public void setPost(Map post) { - this.post = post; - } - - public String toString() { - return this.post.toString(); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((post == null) ? 0 : post.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (!(obj instanceof TestPost2)) - return false; - TestPost2 other = (TestPost2) obj; - if (post == null) { - if (other.post != null) - return false; - } else if (!post.equals(other.post)) - return false; - return true; - } - - -} diff --git a/src/test/java/kehio/models/test/TestPost3.java b/src/test/java/kehio/models/test/TestPost3.java deleted file mode 100755 index f3de163..0000000 --- a/src/test/java/kehio/models/test/TestPost3.java +++ /dev/null @@ -1,23 +0,0 @@ -package kehio.models.test; - -import java.util.Map; - -import kehio.annotations.RdfDatatypeGroup; - -public class TestPost3 { - - // -- RDF representation - // throws exception - // ---- - - @RdfDatatypeGroup(value="http://xmlns.com/foaf/0.1/numbers", byDatatype=true) - private Map post; - - public TestPost3() { - - } - - public TestPost3(boolean init) { - - } -} diff --git a/src/test/java/kehio/models/test/TestPost4.java b/src/test/java/kehio/models/test/TestPost4.java deleted file mode 100755 index f1431d4..0000000 --- a/src/test/java/kehio/models/test/TestPost4.java +++ /dev/null @@ -1,23 +0,0 @@ -package kehio.models.test; - -import java.util.Map; - -import kehio.annotations.RdfDatatypeGroup; - -public class TestPost4 { - - // -- RDF representation - // throws exception - // ---- - - @RdfDatatypeGroup(value="http://xmlns.com/foaf/0.1/numbers", byDatatype=true) - private Map post; - - public TestPost4() { - - } - - public TestPost4(boolean init) { - - } -} diff --git a/src/test/java/kehio/models/test/TestTelephone.java b/src/test/java/kehio/models/test/TestTelephone.java deleted file mode 100755 index 7c15e2b..0000000 --- a/src/test/java/kehio/models/test/TestTelephone.java +++ /dev/null @@ -1,50 +0,0 @@ -package kehio.models.test; - -import kehio.annotations.RdfDatatype; - -public class TestTelephone { - @RdfDatatype("http://xmlns.com/foaf/0.1/number") - private String number; - - public TestTelephone() { - - } - - public TestTelephone(boolean b) { - this.number = "+34 54 65 23"; - } - - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("number: ").append(number).append("\n"); - return builder.toString(); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((number == null) ? 0 : number.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (!(obj instanceof TestTelephone)) - return false; - TestTelephone other = (TestTelephone) obj; - if (number == null) { - if (other.number != null) - return false; - } else if (!number.equals(other.number)) - return false; - return true; - } - - - -} diff --git a/src/test/java/kehio/toRDF/datatype/tests/DatatypeCollectionTest.java b/src/test/java/kehio/toRDF/datatype/tests/DatatypeCollectionTest.java deleted file mode 100755 index 7488224..0000000 --- a/src/test/java/kehio/toRDF/datatype/tests/DatatypeCollectionTest.java +++ /dev/null @@ -1,433 +0,0 @@ -package kehio.toRDF.datatype.tests; - -import java.io.ByteArrayInputStream; -import java.net.URISyntaxException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Deque; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Queue; -import java.util.SortedSet; -import java.util.TreeSet; - -import org.apache.jena.rdf.model.Model; -import org.apache.jena.rdf.model.ModelFactory; -import org.apache.jena.rdf.model.ResourceFactory; -import org.apache.jena.sparql.vocabulary.FOAF; -import org.junit.Assert; -import org.junit.Test; - -import kehio.annotations.RdfDatatype; -import kehio.annotations.RdfId; -import kehio.mapper.Kehio; - -public class DatatypeCollectionTest { - - private static final String OUTPUT = " \"21\" , \"42\"^^ , \"32\"^^ , \"12\"^^ , \"20\" , \"31\"^^ , \"11\"^^ , \"41\"^^ ;\n" + - " \"text 12\"@en , \"Texto@algo mas 21\"@es , \"Text@ 21\"@en , \"text 11\"@en ."; - private static Model expectedModel = ModelFactory.createDefaultModel(); - static { - expectedModel.read(new ByteArrayInputStream(OUTPUT.getBytes()), null, "Turtle"); - } - - @Test - public void testDatatype1() - throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - ORMTestDtypeCollection1 testingObject = new ORMTestDtypeCollection1(); - Model model = Kehio.deserializeClass(testingObject); - Assert.assertTrue(expectedModel.containsAll(model)); - } - - @Test - public void testDatatype2() - throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - ORMTestDtypeCollection2 testingObject = new ORMTestDtypeCollection2(); - Boolean correct = false; - try { - Kehio.deserializeClass(testingObject); - } catch (Exception e) { - correct = true; - } - Assert.assertTrue(correct); - } - - @Test - public void testDatatype3() - throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - ORMTestDtypeCollection3 testingObject = new ORMTestDtypeCollection3(); - Boolean correct = false; - try { - Kehio.deserializeClass(testingObject); - } catch (Exception e) { - correct = true; - } - Assert.assertTrue(correct); - } - - @Test - public void testDatatype4() - throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - ORMTestDtypeCollection4 testingObject = new ORMTestDtypeCollection4(); - Boolean correct = false; - try { - Kehio.deserializeClass(testingObject); - } catch (Exception e) { - correct = true; - } - Assert.assertTrue(correct); - } - - @Test - public void testDatatype5() - throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - ORMTestDtypeCollection5 testingObject = new ORMTestDtypeCollection5(); - Boolean correct = false; - try { - Kehio.deserializeClass(testingObject); - } catch (Exception e) { - correct = true; - } - Assert.assertTrue(correct); - } - - @Test - public void testDatatype6() - throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - ORMTestDtypeCollection6 testingObject = new ORMTestDtypeCollection6(); - Boolean correct = false; - try { - Kehio.deserializeClass(testingObject); - } catch (Exception e) { - correct = true; - } - Assert.assertTrue(correct); - } - - @Test - public void testDatatype7() - throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - ORMTestDtypeCollection7 testingObject = new ORMTestDtypeCollection7(); - Boolean correct = false; - try { - Kehio.deserializeClass(testingObject); - } catch (Exception e) { - correct = true; - } - Assert.assertTrue(correct); - } - - @Test - public void testDatatype8() - throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - ORMTestDtypeCollection8 testingObject = new ORMTestDtypeCollection8(); - Model model = Kehio.deserializeClass(testingObject); - boolean correct = model.contains(null, FOAF.age, ResourceFactory.createPlainLiteral("1")); - Assert.assertTrue(correct); - } - - @Test - public void testDatatype9() - throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - ORMTestDtypeCollection9 testingObject = new ORMTestDtypeCollection9(); - Model model = Kehio.deserializeClass(testingObject); - boolean correct = model.contains(null, FOAF.age, - ResourceFactory.createLangLiteral("1", "http://www.w3.org/2001/XMLSchema#double")); - Assert.assertTrue(correct); - } - - @Test - public void testDatatype10() - throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - ORMTestDtypeCollection10 testingObject = new ORMTestDtypeCollection10(); - Model model = Kehio.deserializeClass(testingObject); - boolean correct = model.contains(null, FOAF.age, ResourceFactory.createPlainLiteral("1")); - Assert.assertTrue(correct); - } - - @Test - public void testDatatype11() - throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - ORMTestDtypeCollection11 testingObject = new ORMTestDtypeCollection11(); - Model model = Kehio.deserializeClass(testingObject); - boolean correct = model.contains(null, FOAF.age, ResourceFactory.createPlainLiteral("1")); - Assert.assertTrue(correct); - } - - @Test - public void testDatatype12() - throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - ORMTestDtypeCollection12 testingObject = new ORMTestDtypeCollection12(); - Model model = Kehio.deserializeClass(testingObject); - boolean correct = model.contains(null, FOAF.age, ResourceFactory.createPlainLiteral("1")); - Assert.assertTrue(correct); - } - - @Test - public void testDatatype13() - throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - ORMTestDtypeCollection13 testingObject = new ORMTestDtypeCollection13(); - Model model = Kehio.deserializeClass(testingObject); - boolean correct = model.contains(null, FOAF.age, ResourceFactory.createPlainLiteral("1")); - Assert.assertTrue(correct); - } - - @Test - public void testDatatype14() - throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - ORMTestDtypeCollection14 testingObject = new ORMTestDtypeCollection14(); - Model model = Kehio.deserializeClass(testingObject); - boolean correct = model.contains(null, FOAF.age, ResourceFactory.createPlainLiteral("1")); - Assert.assertTrue(correct); - } - - @Test - public void testDatatype15() - throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - ORMTestDtypeCollection15 testingObject = new ORMTestDtypeCollection15(); - Model model = Kehio.deserializeClass(testingObject); - boolean correct = model.contains(null, FOAF.age, ResourceFactory.createPlainLiteral("1")); - Assert.assertTrue(correct); - } - - - @Test - public void testDatatype16() - throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - ORMTestDtypeCollection16 testingObject = new ORMTestDtypeCollection16(); - Model model = Kehio.deserializeClass(testingObject); - boolean correct = model.contains(null, FOAF.age, ResourceFactory.createPlainLiteral("1")); - Assert.assertTrue(correct); - } - - @Test - public void testDatatype17() throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - ORMTestDtypeCollection17 testingObject = new ORMTestDtypeCollection17(); - Boolean correct = false; - try { - Kehio.deserializeClass(testingObject); - } catch (Exception e) { - correct = true; - } - Assert.assertTrue(correct); - } - -} - -class ORMTestDtypeCollection1 { - // Ok - @RdfId - public String id = "http://example-instance.com/resource"; - - @RdfDatatype("http://xmlns.com/foaf/0.1/age") - public Collection age_1 = new ArrayList<>(); - - @RdfDatatype("http://xmlns.com/foaf/0.1/age") - public Collection age_2 = new ArrayList<>(); - - @RdfDatatype(value = "http://xmlns.com/foaf/0.1/age", datatype = "http://www.w3.org/2001/XMLSchema#int") - public Collection age_3 = new ArrayList<>(); - - @RdfDatatype(value = "http://xmlns.com/foaf/0.1/age", sinkDatatype = true) - public Collection age_4 = new ArrayList<>(); - - @RdfDatatype(value = "http://xmlns.com/foaf/0.1/text", lang = "en") - public Collection text_1 = new ArrayList<>(); - - @RdfDatatype(value = "http://xmlns.com/foaf/0.1/text", sinkLang = true) - public Collection text_2 = new ArrayList<>(); - - public ORMTestDtypeCollection1() { - age_1.add(11); - age_1.add(12); - age_2.add("20"); - age_2.add("21"); - age_3.add("31"); - age_3.add("32"); - age_4.add("41^^"); - age_4.add("42^^"); - text_1.add("text 11"); - text_1.add("text 12"); - text_2.add("Text@ 21@en"); - text_2.add("Texto@algo mas 21@es"); - } -} - -class ORMTestDtypeCollection2 { - // Exception - @RdfDatatype(value = "http://xmlns.com/foaf/0.1/age", datatype = "http://www.w3.org/2001/XMLSchema#double", sinkLang = true) - public Collection age_1 = new ArrayList<>(); - - public ORMTestDtypeCollection2() { - age_1.add("1@en"); - age_1.add("2@en"); - } - -} - -class ORMTestDtypeCollection3 { - // Exception - @RdfDatatype(value = "http://xmlns.com/foaf/0.1/age", datatype = "http://www.w3.org/2001/XMLSchema#double", sinkDatatype = true) - public Collection age_1 = new ArrayList<>(); - - public ORMTestDtypeCollection3() { - age_1.add("1^^http://www.w3.org/2001/XMLSchema#double"); - age_1.add("2^^http://www.w3.org/2001/XMLSchema#double"); - } -} - -class ORMTestDtypeCollection4 { - // Exception - @RdfDatatype(value = "http://xmlns.com/foaf/0.1/age", lang = "en", sinkDatatype = true) - public Collection age_1 = new ArrayList<>(); - - public ORMTestDtypeCollection4() { - age_1.add("1^^http://www.w3.org/2001/XMLSchema#double"); - age_1.add("2^^http://www.w3.org/2001/XMLSchema#double"); - } -} - -class ORMTestDtypeCollection5 { - // Exception - @RdfDatatype(value = "http://xmlns.com/foaf/0.1/age", lang = "en", sinkLang = true) - public Collection age_1 = new ArrayList<>(); - - public ORMTestDtypeCollection5() { - age_1.add("1@en"); - age_1.add("1@en"); - } -} - -class ORMTestDtypeCollection6 { - // Exception - @RdfDatatype(value = "http://xmlns.com/foaf/0.1/age", datatype = "http://www.w3.org/2001/XMLSchema#double", lang = "en") - public Collection age_1 = new ArrayList<>(); - - public ORMTestDtypeCollection6() { - age_1.add("1"); - age_1.add("1"); - } -} - -class ORMTestDtypeCollection7 { - // Exception - @RdfDatatype(value = "http://xmlns.com/foaf/0.1/age", sinkDatatype = true, sinkLang = true) - public Collection age_1 = new ArrayList<>(); - - public ORMTestDtypeCollection7() { - age_1.add("1"); - age_1.add("1"); - } -} - -class ORMTestDtypeCollection8 { - // Ok - @RdfDatatype(value = "http://xmlns.com/foaf/0.1/age", datatype = "es") - public Collection age_1 = new ArrayList<>(); - - public ORMTestDtypeCollection8() { - age_1.add("1"); - age_1.add("1"); - } -} - -class ORMTestDtypeCollection9 { - // Ok - @RdfDatatype(value = "http://xmlns.com/foaf/0.1/age", lang = "http://www.w3.org/2001/XMLSchema#double") - public Collection age_1 = new ArrayList<>(); - - public ORMTestDtypeCollection9() { - age_1.add("1"); - age_1.add("1"); - } -} - -class ORMTestDtypeCollection10 { - // Ok - @RdfDatatype(value = "http://xmlns.com/foaf/0.1/age", lang = " ") - public Collection age_1 = new ArrayList<>(); - - public ORMTestDtypeCollection10() { - age_1.add("1"); - age_1.add("1"); - } -} - -class ORMTestDtypeCollection11 { - // Ok - @RdfDatatype(value = "http://xmlns.com/foaf/0.1/age", datatype = " ") - public Collection age_1 = new ArrayList<>(); - - public ORMTestDtypeCollection11() { - age_1.add("1"); - age_1.add("1"); - } -} - -class ORMTestDtypeCollection12 { - // Ok - @RdfDatatype(value = "http://xmlns.com/foaf/0.1/age", datatype = " ") - public List age_1 = new ArrayList<>(); - - public ORMTestDtypeCollection12() { - age_1.add("1"); - age_1.add("1"); - } -} - -class ORMTestDtypeCollection13 { - // Ok - @RdfDatatype(value = "http://xmlns.com/foaf/0.1/age", datatype = " ") - public Queue age_1 = new LinkedList<>(); - - public ORMTestDtypeCollection13() { - age_1.add("1"); - age_1.add("1"); - } -} - -class ORMTestDtypeCollection14 { - // Ok - @RdfDatatype(value = "http://xmlns.com/foaf/0.1/age", datatype = " ") - public List age_1 = new LinkedList<>(); - - public ORMTestDtypeCollection14() { - age_1.add("1"); - age_1.add("1"); - } -} - -class ORMTestDtypeCollection15 { - // Ok - @RdfDatatype(value = "http://xmlns.com/foaf/0.1/age", datatype = " ") - public Deque age_1 = new LinkedList<>(); - - public ORMTestDtypeCollection15() { - age_1.add("1"); - age_1.add("1"); - } -} - -class ORMTestDtypeCollection16 { - // Ok - @RdfDatatype(value = "http://xmlns.com/foaf/0.1/age", datatype = " ") - public SortedSet age_1 = new TreeSet<>(); - - public ORMTestDtypeCollection16() { - age_1.add("1"); - age_1.add("1"); - } -} - -class ORMTestDtypeCollection17 { - // Exception - @RdfDatatype(value = "http://xmlns.com/foaf/0.1/age", datatype = " ") - public Map age_1 = new HashMap<>(); - - public ORMTestDtypeCollection17() { - age_1.put("1","1"); - age_1.put("1","1"); - } -} \ No newline at end of file diff --git a/src/test/java/kehio/toRDF/datatype/tests/DatatypeGroupTest.java b/src/test/java/kehio/toRDF/datatype/tests/DatatypeGroupTest.java deleted file mode 100755 index f20bf7b..0000000 --- a/src/test/java/kehio/toRDF/datatype/tests/DatatypeGroupTest.java +++ /dev/null @@ -1,61 +0,0 @@ -package kehio.toRDF.datatype.tests; - -import java.net.URISyntaxException; - -import org.apache.jena.rdf.model.Model; -import org.junit.Assert; -import org.junit.Test; - -import kehio.mapper.Kehio; -import kehio.models.test.TestPost1; -import kehio.models.test.TestPost2; -import kehio.models.test.TestPost3; -import kehio.models.test.TestPost4; - -public class DatatypeGroupTest { - - - - @Test - public void testDatatype1() throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - TestPost1 testingObject = new TestPost1(true); - Model model = Kehio.deserializeClass(testingObject); - Assert.assertTrue(TestPost1.expectedModel.getGraph().isIsomorphicWith(model.getGraph())); - } - - @Test - public void testDatatype2() throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - TestPost2 testingObject = new TestPost2(true); - Model model = Kehio.deserializeClass(testingObject); - Assert.assertTrue(TestPost2.expectedModel.getGraph().isIsomorphicWith(model.getGraph())); - } - - - @Test - public void testDatatype3() throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - Boolean correct = false; - try { - TestPost3 testingObject = new TestPost3(true); - Kehio.deserializeClass(testingObject); - }catch(Exception e) { - correct = true; - } - Assert.assertTrue(correct); - } - - - @Test - public void testDatatype4() throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - Boolean correct = false; - try { - TestPost4 testingObject = new TestPost4(true); - Kehio.deserializeClass(testingObject); - }catch(Exception e) { - correct = true; - } - Assert.assertTrue(correct); - } - - - -} \ No newline at end of file diff --git a/src/test/java/kehio/toRDF/datatype/tests/DatatypeTest.java b/src/test/java/kehio/toRDF/datatype/tests/DatatypeTest.java deleted file mode 100755 index 92d7787..0000000 --- a/src/test/java/kehio/toRDF/datatype/tests/DatatypeTest.java +++ /dev/null @@ -1,225 +0,0 @@ -package kehio.toRDF.datatype.tests; - -import java.io.ByteArrayInputStream; -import java.net.URISyntaxException; - -import org.apache.jena.rdf.model.Model; -import org.apache.jena.rdf.model.ModelFactory; -import org.apache.jena.rdf.model.ResourceFactory; -import org.apache.jena.sparql.vocabulary.FOAF; -import org.junit.Assert; -import org.junit.Test; - -import kehio.annotations.RdfDatatype; -import kehio.annotations.RdfId; -import kehio.mapper.Kehio; - -public class DatatypeTest { - - private static final String OUTPUT = " \"4\"^^ , \"3\"^^, \"2\", \"1\"^^ ;\n" + - " \"Text@ 2\"@en , \"Text 1\"@en ."; - private static Model expectedModel = ModelFactory.createDefaultModel(); - static{ - expectedModel.read(new ByteArrayInputStream(OUTPUT.getBytes()), null, "Turtle"); - } - - @Test - public void testDatatype1() throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - ORMTestDtype1 testingObject = new ORMTestDtype1(); - Model model = Kehio.deserializeClass(testingObject); - Assert.assertTrue(expectedModel.containsAll(model)); - } - - @Test - public void testDatatype2() throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - ORMTestDtype2 testingObject = new ORMTestDtype2(); - Boolean correct = false; - try { - Kehio.deserializeClass(testingObject); - }catch(Exception e) { - correct = true; - } - Assert.assertTrue(correct); - } - - @Test - public void testDatatype3() throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - ORMTestDtype3 testingObject = new ORMTestDtype3(); - Boolean correct = false; - try { - Kehio.deserializeClass(testingObject); - }catch(Exception e) { - correct = true; - } - Assert.assertTrue(correct); - } - - @Test - public void testDatatype4() throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - ORMTestDtype4 testingObject = new ORMTestDtype4(); - Boolean correct = false; - try { - Kehio.deserializeClass(testingObject); - }catch(Exception e) { - correct = true; - } - Assert.assertTrue(correct); - } - - @Test - public void testDatatype5() throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - ORMTestDtype5 testingObject = new ORMTestDtype5(); - Boolean correct = false; - try { - Kehio.deserializeClass(testingObject); - }catch(Exception e) { - correct = true; - } - Assert.assertTrue(correct); - } - - @Test - public void testDatatype6() throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - ORMTestDtype6 testingObject = new ORMTestDtype6(); - Boolean correct = false; - try { - Kehio.deserializeClass(testingObject); - }catch(Exception e) { - correct = true; - } - Assert.assertTrue(correct); - } - - @Test - public void testDatatype7() throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - ORMTestDtype7 testingObject = new ORMTestDtype7(); - Boolean correct = false; - try { - Kehio.deserializeClass(testingObject); - }catch(Exception e) { - correct = true; - } - Assert.assertTrue(correct); - } - - @Test - public void testDatatype8() throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - ORMTestDtype8 testingObject = new ORMTestDtype8(); - Model model = Kehio.deserializeClass(testingObject); - boolean correct = model.contains(null, FOAF.age, ResourceFactory.createPlainLiteral("1")); - Assert.assertTrue(correct); - } - @Test - public void testDatatype9() throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - ORMTestDtype9 testingObject = new ORMTestDtype9(); - Model model = Kehio.deserializeClass(testingObject); - boolean correct = model.contains(null, FOAF.age, ResourceFactory.createLangLiteral("1","http://www.w3.org/2001/XMLSchema#double")); - Assert.assertTrue(correct); - } - @Test - public void testDatatype10() throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - ORMTestDtype10 testingObject = new ORMTestDtype10(); - Model model = Kehio.deserializeClass(testingObject); - boolean correct = model.contains(null, FOAF.age, ResourceFactory.createPlainLiteral("1")); - Assert.assertTrue(correct); - } - @Test - public void testDatatype11() throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - ORMTestDtype11 testingObject = new ORMTestDtype11(); - Model model = Kehio.deserializeClass(testingObject); - boolean correct = model.contains(null, FOAF.age, ResourceFactory.createPlainLiteral("1")); - Assert.assertTrue(correct); - } - -} - -class ORMTestDtype1 { - - public ORMTestDtype1() { - - } - - // Ok - @RdfId - public String id = "http://example-instance.com/resource"; - - @RdfDatatype("http://xmlns.com/foaf/0.1/age") - public Integer age_1 = 1; - - @RdfDatatype("http://xmlns.com/foaf/0.1/age") - public String age_2 = "2"; - - @RdfDatatype(value="http://xmlns.com/foaf/0.1/age", datatype="http://www.w3.org/2001/XMLSchema#int") - public String age_3= "3"; - - @RdfDatatype(value="http://xmlns.com/foaf/0.1/age", sinkDatatype=true) - public String age_4 = "4^^"; - - @RdfDatatype(value="http://xmlns.com/foaf/0.1/text", lang="en") - public String text_1 = "Text 1"; - - @RdfDatatype(value="http://xmlns.com/foaf/0.1/text", sinkLang=true) - public String text_2 = "Text@ 2@en"; - -} - -class ORMTestDtype2 { - // Exception - @RdfDatatype(value="http://xmlns.com/foaf/0.1/age", datatype="http://www.w3.org/2001/XMLSchema#double", sinkLang=true) - public String age_1 = "1@en"; -} - -class ORMTestDtype3 { - // Exception - @RdfDatatype(value="http://xmlns.com/foaf/0.1/age", datatype="http://www.w3.org/2001/XMLSchema#double", sinkDatatype=true) - public String age_1 = "1^^http://www.w3.org/2001/XMLSchema#double"; -} - - -class ORMTestDtype4 { - // Exception - @RdfDatatype(value="http://xmlns.com/foaf/0.1/age", lang="en", sinkDatatype=true) - public String age_1 = "1^^http://www.w3.org/2001/XMLSchema#double"; -} - -class ORMTestDtype5 { - // Exception - @RdfDatatype(value="http://xmlns.com/foaf/0.1/age", lang="en", sinkLang=true) - public String age_1 = "1@en"; -} - -class ORMTestDtype6 { - // Exception - @RdfDatatype(value="http://xmlns.com/foaf/0.1/age", datatype="http://www.w3.org/2001/XMLSchema#double", lang="en") - public String age_1 = "1"; -} - -class ORMTestDtype7 { - // Exception - @RdfDatatype(value="http://xmlns.com/foaf/0.1/age", sinkDatatype=true, sinkLang=true) - public String age_1 = "1"; -} - -class ORMTestDtype8 { - // Ok - @RdfDatatype(value="http://xmlns.com/foaf/0.1/age", datatype="es") - public String age_1 = "1"; -} - -class ORMTestDtype9 { - // Ok - @RdfDatatype(value="http://xmlns.com/foaf/0.1/age", lang="http://www.w3.org/2001/XMLSchema#double") - public String age_1 = "1"; -} - -class ORMTestDtype10 { - // Ok - @RdfDatatype(value="http://xmlns.com/foaf/0.1/age", lang=" ") - public String age_1 = "1"; -} - -class ORMTestDtype11 { - // Ok - @RdfDatatype(value="http://xmlns.com/foaf/0.1/age", datatype=" ") - public String age_1 = "1"; -} \ No newline at end of file diff --git a/src/test/java/kehio/toRDF/datatype/tests/IdTest.java b/src/test/java/kehio/toRDF/datatype/tests/IdTest.java deleted file mode 100755 index 57d6459..0000000 --- a/src/test/java/kehio/toRDF/datatype/tests/IdTest.java +++ /dev/null @@ -1,155 +0,0 @@ -package kehio.toRDF.datatype.tests; - -import java.io.ByteArrayInputStream; -import java.net.URI; -import java.net.URISyntaxException; - -import org.apache.jena.rdf.model.Model; -import org.apache.jena.rdf.model.ModelFactory; -import org.apache.jena.rdf.model.Resource; -import org.apache.jena.sparql.vocabulary.FOAF; -import org.junit.Test; - -import org.junit.Assert; - -import kehio.annotations.RdfDatatype; -import kehio.annotations.RdfId; -import kehio.mapper.Kehio; - -public class IdTest { - - private static final String OUTPUT = " \"test\" ."; - private static Model expectedModel = ModelFactory.createDefaultModel(); - static{ - expectedModel.read(new ByteArrayInputStream(OUTPUT.getBytes()), null, "Turtle"); - } - - @Test - public void testId1() throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - ORMTestId1 testingObject = new ORMTestId1(); - Model model = Kehio.deserializeClass(testingObject); - Assert.assertTrue(expectedModel.containsAll(model)); - } - - @Test - public void testId2() throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - ORMTestId2 testingObject = new ORMTestId2(); - Model model = Kehio.deserializeClass(testingObject); - Assert.assertTrue(expectedModel.containsAll(model)); - } - - @Test - public void testId3() throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - ORMTestId3 testingObject = new ORMTestId3(); - Model model = Kehio.deserializeClass(testingObject); - Resource subject = model.listSubjectsWithProperty(FOAF.name, "test").next(); - Assert.assertTrue(subject.isAnon() && model.contains(null, FOAF.name, "test")); - } - - @Test - public void testId4() throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - ORMTestId4 testingObject = new ORMTestId4(); - Model model = Kehio.deserializeClass(testingObject); - Resource subject = model.listSubjectsWithProperty(FOAF.name, "test").next(); - Assert.assertTrue(subject.isAnon() && model.contains(null, FOAF.name, "test")); - } - - @Test - public void testId5() { - ORMTestId5 testingObject = new ORMTestId5(); - Boolean test = false; - try { - Model model = Kehio.deserializeClass(testingObject); - model.write(System.out, "Turtle"); - }catch(Exception e) { - test = true; - } - Assert.assertTrue(test); - } - - @Test - public void testId6() throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - ORMTestId6 testingObject = new ORMTestId6(); - Model model = Kehio.deserializeClass(testingObject); - Resource subject = model.listSubjectsWithProperty(FOAF.name, "test").next(); - Assert.assertTrue(subject.isAnon() && model.contains(null, FOAF.name, "test")); - } - - @Test - public void testId7() throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - ORMTestId7 testingObject = new ORMTestId7(); - Boolean test = false; - try { - Model model = Kehio.deserializeClass(testingObject); - model.write(System.out, "Turtle"); - }catch(Exception e) { - test = true; - } - Assert.assertTrue(test); - } - -} - -class ORMTestId1 { - // Ok - @RdfId - public String id = "http://example-instance.com/resource"; - @RdfDatatype(value="http://xmlns.com/foaf/0.1/name") - public String test = "test"; -} - -class ORMTestId2 { - // Ok - @RdfId - public URI id; - @RdfDatatype(value="http://xmlns.com/foaf/0.1/name") - public String test = "test"; - public ORMTestId2() { - try { - id = new URI("http://example-instance.com/resource"); - } catch (URISyntaxException e) { - e.printStackTrace(); - } - } -} - -class ORMTestId3 { - // Ok - @RdfId - public URI id; - @RdfDatatype(value="http://xmlns.com/foaf/0.1/name") - public String test = "test"; -} - -class ORMTestId4 { - // Ok - @RdfId - public String id; - @RdfDatatype(value="http://xmlns.com/foaf/0.1/name") - public String test = "test"; -} - -class ORMTestId5 { - // Exception - @RdfId - public Integer id; - @RdfDatatype(value="http://xmlns.com/foaf/0.1/name") - public String test = "test"; -} - -class ORMTestId6 { - // Ok - @RdfDatatype(value="http://xmlns.com/foaf/0.1/name") - public String test = "test"; -} - -class ORMTestId7 { - // Exception - @RdfId - public String id_1 = "http://example-instance.com/resource"; - @RdfId - public URI id_2; - - @RdfDatatype(value="http://xmlns.com/foaf/0.1/name") - public String test = "test"; -} diff --git a/src/test/java/kehio/toRDF/datatype/tests/ObjectTest.java b/src/test/java/kehio/toRDF/datatype/tests/ObjectTest.java deleted file mode 100755 index 0ebddd5..0000000 --- a/src/test/java/kehio/toRDF/datatype/tests/ObjectTest.java +++ /dev/null @@ -1,60 +0,0 @@ -package kehio.toRDF.datatype.tests; - -import java.io.ByteArrayInputStream; -import java.net.URISyntaxException; -import org.apache.jena.rdf.model.Model; -import org.apache.jena.rdf.model.ModelFactory; -import org.junit.Assert; -import org.junit.Test; - -import kehio.mapper.Kehio; -import kehio.models.test.TestPerson1; -import kehio.models.test.TestPerson2; - -public class ObjectTest { - - - private static final String TEST1 = "[ \n" + - " \"John\" ;\n" + - " \n" + - " [ \n" + - " \"+34 54 65 23\" ] ;\n" + - " \n" + - " \"Fold\" , \"Doe\"\n" + - "] ."; - - - @Test - public void test1() throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - TestPerson1 person = new TestPerson1(true); - Model model = Kehio.deserializeClass(person); - Model expectedModel = ModelFactory.createDefaultModel(); - expectedModel.read(new ByteArrayInputStream(TEST1.getBytes()), null, "Turtle"); - Assert.assertTrue(expectedModel.getGraph().isIsomorphicWith(model.getGraph())); - } - - private static final String TEST2 = "[ \n" + - " \"John\" ;\n" + - " \n" + - " [ \n" + - " \"+34 54 65 23\" ] ;\n" + - " \n" + - " [ \n" + - " \"+34 54 65 23\" ] ;\n" + - " \n" + - " \"Fold\" , \"Doe\"\n" + - "] ."; - - @Test - public void test2() throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, URISyntaxException { - TestPerson2 person = new TestPerson2(true); - Model model = Kehio.deserializeClass(person); - Model expectedModel = ModelFactory.createDefaultModel(); - expectedModel.read(new ByteArrayInputStream(TEST2.getBytes()), null, "Turtle"); - - Assert.assertTrue(expectedModel.getGraph().isIsomorphicWith(model.getGraph())); - } - - -} - diff --git a/src/test/java/wot/jdt/tds/tests/LinkSmartToRdfTest.java b/src/test/java/wot/jdt/tds/tests/LinkSmartToRdfTest.java index 8c4cf7f..6a52846 100755 --- a/src/test/java/wot/jdt/tds/tests/LinkSmartToRdfTest.java +++ b/src/test/java/wot/jdt/tds/tests/LinkSmartToRdfTest.java @@ -1,18 +1,12 @@ package wot.jdt.tds.tests; import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Scanner; import org.apache.jena.graph.Graph; import org.apache.jena.query.ARQ; import org.apache.jena.rdf.model.Model; -import org.apache.jena.rdf.model.ModelFactory; -import org.apache.jena.rdf.model.RDFNode; -import org.apache.jena.rdf.model.Resource; -import org.apache.jena.rdf.model.Statement; -import org.apache.jena.rdf.model.StmtIterator; import org.apache.jena.riot.Lang; import org.apache.jena.riot.RDFDataMgr; import org.apache.jena.shacl.ShaclValidator; @@ -27,7 +21,6 @@ import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; import com.google.gson.JsonObject; -import kehio.mapper.Kehio; import wot.jtd.JTD; import wot.jtd.JsonHandler; import wot.jtd.model.Thing;