001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.marshall.jena;
018
019import static org.apache.juneau.commons.utils.CollectionUtils.*;
020
021import java.util.*;
022
023import org.apache.juneau.marshall.*;
024import org.apache.juneau.marshall.xml.*;
025
026/**
027 * Metadata on classes specific to the RDF serializers and parsers pulled from the {@link Rdf @Rdf} annotation on the
028 * class.
029 *
030 * <h5 class='section'>See Also:</h5><ul>
031 *    <li class='link'>{doc jmr.RdfDetails}
032
033 * </ul>
034 */
035@SuppressWarnings({
036   "java:S1612" // Lambdas used instead of method references; annotation attribute access requires explicit lambda form
037})
038public class RdfClassMeta extends ExtendedClassMeta {
039
040   private final RdfCollectionFormat collectionFormat;
041   private final Namespace namespace;
042
043   /**
044    * Constructor.
045    *
046    * @param cm The class that this annotation is defined on.
047    *    Must not be <jk>null</jk>.
048    * @param mp RDF metadata provider (for finding information about other artifacts).
049    */
050   public RdfClassMeta(ClassMeta<?> cm, RdfMetaProvider mp) {
051      super(cm);
052      List<Rdf> rdfs = list();
053      List<RdfSchema> schemas = list();
054      cm.forEachAnnotation(Rdf.class, x -> true, x -> rdfs.add(x));
055      cm.forEachAnnotation(RdfSchema.class, x -> true, x -> schemas.add(x)); // HTT - @RdfSchema targets PACKAGE only; cannot annotate classes with it in tests
056
057      this.collectionFormat = rdfs.stream().map(x -> x.collectionFormat()).filter(x -> x != RdfCollectionFormat.DEFAULT).findFirst().orElse(RdfCollectionFormat.DEFAULT);
058      this.namespace = RdfUtils.findNamespace(rdfs, schemas);
059   }
060
061   /**
062    * Returns the {@link Rdf#collectionFormat() @Rdf(collectionFormat)} annotation defined on the class.
063    *
064    * @return The value of the annotation, or {@link RdfCollectionFormat#DEFAULT} if the annotation is not
065    * specified.
066    */
067   protected RdfCollectionFormat getCollectionFormat() { return collectionFormat; }
068
069   /**
070    * Returns the RDF namespace associated with this class.
071    *
072    * <p>
073    * Namespace is determined in the following order of {@link Rdf#prefix() @Rdf(prefix)} annotation:
074    * <ol>
075    *    <li>Class.
076    *    <li>Package.
077    *    <li>Superclasses.
078    *    <li>Superclass packages.
079    *    <li>Interfaces.
080    *    <li>Interface packages.
081    * </ol>
082    *
083    * @return The namespace associated with this class, or <jk>null</jk> if no namespace is associated with it.
084    */
085   protected Namespace getNamespace() { return namespace; }
086}