| 
28 | 28 | import cz.cvut.kbss.jopa.model.annotations.Types;  | 
29 | 29 | import cz.cvut.kbss.jsonld.JsonLd;  | 
30 | 30 | import cz.cvut.kbss.jsonld.annotation.JsonLdAttributeOrder;  | 
 | 31 | +import cz.cvut.kbss.jsonld.annotation.JsonLdType;  | 
31 | 32 | import cz.cvut.kbss.jsonld.exception.JsonLdSerializationException;  | 
32 | 33 | 
 
  | 
33 | 34 | import java.lang.reflect.AnnotatedElement;  | 
@@ -69,30 +70,101 @@ public static void setPropertyAccessResolver(PropertyAccessResolver resolver) {  | 
69 | 70 |         propertyAccessResolver = Objects.requireNonNull(resolver);  | 
70 | 71 |     }  | 
71 | 72 | 
 
  | 
 | 73 | +    /**  | 
 | 74 | +     * Checks whether the specified class is annotated with the {@link OWLClass} or  | 
 | 75 | +	 * {@link JsonLdType} annotation. If it's annotated with {@link JsonLdType} the iri  | 
 | 76 | +	 * is not empty, and it's not an abstract class.  | 
 | 77 | +     *  | 
 | 78 | +     * @param cls The class to examine  | 
 | 79 | +     * @return Whether it is annotated with {@link OWLClass} or {@link JsonLdType}.  | 
 | 80 | +     */  | 
 | 81 | +	public static boolean isInstantiableMappedType(Class<?> cls) {  | 
 | 82 | +        return isOwlClassEntity(cls) || isJsonLdTypeEntity(cls);  | 
 | 83 | +    }  | 
 | 84 | + | 
 | 85 | +	    /**  | 
 | 86 | +     * Checks whether the specified class is annotated with the {@link OWLClass} or  | 
 | 87 | +	 * {@link JsonLdType} annotation. If it's annotated with {@link JsonLdType} the IRI  | 
 | 88 | +	 * 	may be empty and it may be an abstract class;  | 
 | 89 | +     *  | 
 | 90 | +     * @param cls The class to examine  | 
 | 91 | +     * @return Whether it is annotated with {@link OWLClass} or {@link JsonLdType}.  | 
 | 92 | +     */  | 
 | 93 | +	public static boolean isMappedType(Class<?> cls) {  | 
 | 94 | +        return isInstantiableMappedType(cls) || isJsonLdTypeAbstractEntity(cls);  | 
 | 95 | +    }  | 
 | 96 | + | 
72 | 97 |     /**  | 
73 | 98 |      * Checks whether the specified class is annotated with the {@link OWLClass} annotation.  | 
74 | 99 |      *  | 
75 | 100 |      * @param cls The class to examine  | 
76 | 101 |      * @return Whether it is annotated with {@link OWLClass}  | 
77 | 102 |      */  | 
78 |  | -    public static boolean isOwlClassEntity(Class<?> cls) {  | 
 | 103 | +    protected static boolean isOwlClassEntity(Class<?> cls) {  | 
79 | 104 |         return cls != null && cls.getDeclaredAnnotation(OWLClass.class) != null;  | 
80 | 105 |     }  | 
81 | 106 | 
 
  | 
 | 107 | +	/**  | 
 | 108 | +     * Checks whether the specified class is annotated with the {@link JsonLdType} annotation.  | 
 | 109 | +	 * Additionally, iri should also be provided and the class should not be abstract.  | 
 | 110 | +     *  | 
 | 111 | +     * @param cls The class to examine  | 
 | 112 | +     * @return Whether it is annotated with {@link JsonLdType} and iri is provided  | 
 | 113 | +     */  | 
 | 114 | +	protected static boolean isJsonLdTypeEntity(Class<?> cls) {  | 
 | 115 | +		if (cls == null) {  | 
 | 116 | +			return false;  | 
 | 117 | +		}  | 
 | 118 | +		if (Modifier.isAbstract(cls.getModifiers())) {  | 
 | 119 | +			return false;  | 
 | 120 | +		}  | 
 | 121 | +		JsonLdType jsonLdType = cls.getDeclaredAnnotation(JsonLdType.class);  | 
 | 122 | +		if (jsonLdType == null) {  | 
 | 123 | +			return false;  | 
 | 124 | +		}  | 
 | 125 | +		return jsonLdType.iri() != null && !jsonLdType.iri().isEmpty();  | 
 | 126 | +	}  | 
 | 127 | + | 
 | 128 | +	/**  | 
 | 129 | +     * Checks whether the specified class is annotated with the {@link JsonLdType} annotation.  | 
 | 130 | +	 * Additionally, iri should not be provided and the class should be abstract.  | 
 | 131 | +     *  | 
 | 132 | +     * @param cls The class to examine  | 
 | 133 | +     * @return Whether it is annotated with {@link JsonLdType} and iri is not provided  | 
 | 134 | +     */  | 
 | 135 | +	protected static boolean isJsonLdTypeAbstractEntity(Class<?> cls) {  | 
 | 136 | +		if (cls == null) {  | 
 | 137 | +			return false;  | 
 | 138 | +		}  | 
 | 139 | +		if (!Modifier.isAbstract(cls.getModifiers())) {  | 
 | 140 | +			return false;  | 
 | 141 | +		}  | 
 | 142 | +		JsonLdType jsonLdType = cls.getDeclaredAnnotation(JsonLdType.class);  | 
 | 143 | +		if (jsonLdType == null) {  | 
 | 144 | +			return false;  | 
 | 145 | +		}  | 
 | 146 | +		return jsonLdType.iri() != null && jsonLdType.iri().isEmpty();  | 
 | 147 | +	}  | 
 | 148 | + | 
82 | 149 |     /**  | 
83 |  | -     * Gets IRI of the OWL class represented by the specified Java class.  | 
 | 150 | +     * Gets IRI of the JsonLdType/OWLClass represented by the specified Java class.  | 
84 | 151 |      *  | 
85 | 152 |      * @param cls Java class to examine  | 
86 | 153 |      * @return Represented ontological class  | 
87 |  | -     * @throws IllegalArgumentException If the specified class is not mapped by {@link OWLClass}  | 
 | 154 | +     * @throws IllegalArgumentException If the specified class is not mapped by {@link JsonLdType} {@link OWLClass}  | 
88 | 155 |      */  | 
89 |  | -    public static String getOwlClass(Class<?> cls) {  | 
 | 156 | +    public static String getJsonLdTypeOrOwlClass(Class<?> cls) {  | 
90 | 157 |         Objects.requireNonNull(cls);  | 
91 | 158 |         final OWLClass owlClass = cls.getDeclaredAnnotation(OWLClass.class);  | 
92 |  | -        if (owlClass == null) {  | 
93 |  | -            throw new IllegalArgumentException(cls + " is not an OWL class entity.");  | 
 | 159 | +		if (owlClass != null) {  | 
 | 160 | +			return expandIriIfNecessary(owlClass.iri(), cls);  | 
 | 161 | +		}  | 
 | 162 | +        final JsonLdType jsonLdType = cls.getDeclaredAnnotation(JsonLdType.class);  | 
 | 163 | +        if (jsonLdType != null) {  | 
 | 164 | +			return expandIriIfNecessary(jsonLdType.iri(), cls);  | 
 | 165 | + | 
94 | 166 |         }  | 
95 |  | -        return expandIriIfNecessary(owlClass.iri(), cls);  | 
 | 167 | +        throw new IllegalArgumentException(cls + " is not an JsonLdType/OWLClass entity.");  | 
96 | 168 |     }  | 
97 | 169 | 
 
  | 
98 | 170 |     /**  | 
@@ -164,34 +236,41 @@ private static Optional<String> resolveNamespace(AnnotatedElement annotated, Str  | 
164 | 236 |     }  | 
165 | 237 | 
 
  | 
166 | 238 |     /**  | 
167 |  | -     * Resolves ontological types of the specified object, as specified by the {@link OWLClass} annotation.  | 
 | 239 | +     * Resolves ontological types of the specified object, as specified by the {@link JsonLdType}  | 
 | 240 | +     * and {@link OWLClass} annotation.  | 
168 | 241 |      *  | 
169 | 242 |      * @param object The object to resolve  | 
170 |  | -     * @return Resolved OWL types or an empty set if the object's class is not annotated with {@link OWLClass}  | 
171 |  | -     * @see #getOwlClasses(Class)  | 
 | 243 | +     * @return Resolved JsonLdType/OwlClass types or an empty set if the object's class is not annotated  | 
 | 244 | +	 * with {@link JsonLdType} or {@link OWLClass}  | 
 | 245 | +     * @see #getJsonLdTypeOrOwlClasses(Class)  | 
172 | 246 |      */  | 
173 |  | -    public static Set<String> getOwlClasses(Object object) {  | 
 | 247 | +    public static Set<String> getJsonLdTypeOrOwlClasses(Object object) {  | 
174 | 248 |         Objects.requireNonNull(object);  | 
175 | 249 |         final Class<?> cls = object.getClass();  | 
176 |  | -        return getOwlClasses(cls);  | 
 | 250 | +        return getJsonLdTypeOrOwlClasses(cls);  | 
177 | 251 |     }  | 
178 | 252 | 
 
  | 
179 | 253 |     /**  | 
180 |  | -     * Resolves a transitive closure of ontological types of the specified class, as specified by the {@link OWLClass}  | 
181 |  | -     * annotation.  | 
 | 254 | +     * Resolves a transitive closure of ontological types of the specified class, as specified by the {@link JsonLdType}  | 
 | 255 | +     * and {@link OWLClass} annotation.  | 
182 | 256 |      * <p>  | 
183 | 257 |      * I.e. the result contains also types mapped by the class's ancestors.  | 
184 | 258 |      *  | 
185 | 259 |      * @param cls The class to process  | 
186 | 260 |      * @return Set of mapped ontological classes (possibly empty)  | 
187 | 261 |      */  | 
188 |  | -    public static Set<String> getOwlClasses(Class<?> cls) {  | 
 | 262 | +    public static Set<String> getJsonLdTypeOrOwlClasses(Class<?> cls) {  | 
189 | 263 |         final Set<String> classes = new HashSet<>();  | 
190 | 264 |         getAncestors(cls).forEach(c -> {  | 
191 |  | -            final OWLClass owlClass = c.getDeclaredAnnotation(OWLClass.class);  | 
192 |  | -            if (owlClass != null) {  | 
193 |  | -                classes.add(expandIriIfNecessary(owlClass.iri(), c));  | 
194 |  | -            }  | 
 | 265 | +            final JsonLdType jsonLdType = c.getDeclaredAnnotation(JsonLdType.class);  | 
 | 266 | +            if (jsonLdType != null) {  | 
 | 267 | +                classes.add(expandIriIfNecessary(jsonLdType.iri(), c));  | 
 | 268 | +            } else {  | 
 | 269 | +				final OWLClass owlClass = c.getDeclaredAnnotation(OWLClass.class);  | 
 | 270 | +				if (owlClass != null) {  | 
 | 271 | +					classes.add(expandIriIfNecessary(owlClass.iri(), c));  | 
 | 272 | +				}  | 
 | 273 | +			}  | 
195 | 274 |         });  | 
196 | 275 |         return classes;  | 
197 | 276 |     }  | 
 | 
0 commit comments