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 org.apache.juneau.commons.bean.*;
022import org.apache.juneau.commons.collections.*;
023import org.apache.juneau.commons.reflect.*;
024import org.apache.juneau.marshall.*;
025import org.apache.juneau.marshall.xml.*;
026
027/**
028 * Metadata on bean properties specific to the RDF serializers and parsers pulled from the {@link Rdf @Rdf} annotation
029 * on the bean property.
030 *
031 * <h5 class='section'>See Also:</h5><ul>
032 *    <li class='link'>{doc jmr.RdfDetails}
033
034 * </ul>
035 */
036public class RdfBeanPropertyMeta extends ExtendedBeanPropertyMeta {
037
038   /**
039    * Default instance.
040    */
041   public static final RdfBeanPropertyMeta DEFAULT = new RdfBeanPropertyMeta();
042
043   @SuppressWarnings({
044      "java:S9438" // Reassigned inside a lambda in the constructor; cannot be final
045   })
046   private RdfCollectionFormat collectionFormat = RdfCollectionFormat.DEFAULT;
047   private Namespace namespace;
048   private boolean isBeanUri;
049
050   /**
051    * Constructor.
052    *
053    * @param bpm The metadata of the bean property of this additional metadata.
054    *    Must not be <jk>null</jk>.
055    * @param mp RDF metadata provider (for finding information about other artifacts).
056    */
057   public RdfBeanPropertyMeta(BeanPropertyMeta bpm, RdfMetaProvider mp) {
058      super(bpm);
059
060      var bpmcm = (ClassMeta<?>) bpm.getBeanInfo();
061      var bmcm = (ClassMeta<?>) bpm.getBeanMeta().getBeanInfo();
062      var ap = bpmcm.getMarshallingContext().getAnnotationProvider();
063      var rdfs = new MultiList<>(
064         rstream(ap.find(Rdf.class, bmcm)).map(AnnotationInfo::inner).toList(),
065         reverse(bpm.getAnnotations(Rdf.class).map(AnnotationInfo::inner).toList())
066      );
067      var schemas = new MultiList<>(
068         rstream(ap.find(RdfSchema.class, bmcm)).map(AnnotationInfo::inner).toList(),
069         reverse(bpm.getAnnotations(RdfSchema.class).map(AnnotationInfo::inner).toList())
070      );
071
072      rdfs.forEach(x -> {
073         if (x.collectionFormat() != RdfCollectionFormat.DEFAULT)
074            collectionFormat = x.collectionFormat();
075         if (x.beanUri())
076            isBeanUri = true;
077      });
078
079      namespace = RdfUtils.findNamespace(rdfs, schemas);
080   }
081
082   private RdfBeanPropertyMeta() {
083      super(null);
084   }
085
086   /**
087    * Returns the RDF namespace associated with this bean property.
088    *
089    * <p>
090    * Namespace is determined in the following order of {@link Rdf#prefix() @Rdf(prefix)} annotations:
091    * <ol>
092    *    <li>Bean property field.
093    *    <li>Bean getter.
094    *    <li>Bean setter.
095    *    <li>Bean class.
096    *    <li>Bean package.
097    *    <li>Bean superclasses.
098    *    <li>Bean superclass packages.
099    *    <li>Bean interfaces.
100    *    <li>Bean interface packages.
101    * </ol>
102    *
103    * @return The namespace associated with this bean property, or <jk>null</jk> if no namespace is associated with it.
104    */
105   public Namespace getNamespace() { return namespace; }
106
107   /**
108    * Returns <jk>true</jk> if this bean property is marked with {@link Rdf#beanUri() @Rdf(beanUri)} as <jk>true</jk>.
109    *
110    * @return <jk>true</jk> if this bean property annotation is <jk>true</jk>.
111    */
112   public boolean isBeanUri() { return isBeanUri; }
113
114   /**
115    * Returns the RDF collection format of this property from the {@link Rdf#collectionFormat} annotation on this bean
116    * property.
117    *
118    * @return The RDF collection format, or {@link RdfCollectionFormat#DEFAULT} if annotation not specified.
119    */
120   protected RdfCollectionFormat getCollectionFormat() { return collectionFormat; }
121}