001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.jena;
014
015import java.util.*;
016
017import org.apache.juneau.*;
018import org.apache.juneau.jena.annotation.*;
019import org.apache.juneau.xml.*;
020
021/**
022 * Utility classes.
023 */
024public class RdfUtils {
025
026   /**
027    * Find the namespace given a list of <ja>@Rdf</ja> and <ja>@RdfSchema</ja> annotations.
028    *
029    * <p>
030    * The annotations should be a child-to-parent ordering of annotations found on a class or method.
031    *
032    * @param rdfs The <c>@Rdf</c> annotations to search.
033    * @param schemas The list of known RDF schemas.
034    * @return The resolved namespace, or <jk>null</jk> if the namespace could not be resolved.
035    */
036   public static Namespace findNamespace(List<Rdf> rdfs, List<RdfSchema> schemas) {
037
038      for (Rdf rdf : rdfs) {
039         Namespace ns = findNamespace(rdf.prefix(), rdf.namespace(), rdfs, schemas);
040         if (ns != null)
041            return ns;
042      }
043
044      for (RdfSchema schema : schemas) {
045         Namespace ns = findNamespace(schema.prefix(), schema.namespace(), null, schemas);
046         if (ns != null)
047            return ns;
048      }
049
050      return null;
051   }
052
053   private static Namespace findNamespace(String prefix, String ns, List<Rdf> rdfs, List<RdfSchema> schemas) {
054
055      // If both prefix and namespace specified, use that Namespace mapping.
056      if (! (prefix.isEmpty() || ns.isEmpty()))
057         return Namespace.create(prefix, ns);
058
059      // If only prefix specified, need to search for namespaceURI.
060      if (! prefix.isEmpty()) {
061         if (rdfs != null)
062            for (Rdf rdf2 : rdfs)
063               if (rdf2.prefix().equals(prefix) && ! rdf2.namespace().isEmpty())
064                  return Namespace.create(prefix, rdf2.namespace());
065         for (RdfSchema schema : schemas) {
066            if (schema.prefix().equals(prefix) && ! schema.namespace().isEmpty())
067               return Namespace.create(prefix, schema.namespace());
068            for (RdfNs rdfNs : schema.rdfNs())
069               if (rdfNs.prefix().equals(prefix))
070                  return Namespace.create(prefix, rdfNs.namespaceURI());
071         }
072         throw new BeanRuntimeException("Found @Rdf.prefix annotation with no matching URI.  prefix='"+prefix+"'");
073      }
074
075      // If only namespaceURI specified, need to search for prefix.
076      if (! ns.isEmpty()) {
077         if (rdfs != null)
078            for (Rdf rdf2 : rdfs)
079               if (rdf2.namespace().equals(ns) && ! rdf2.prefix().isEmpty())
080                  return Namespace.create(rdf2.prefix(), ns);
081         for (RdfSchema schema : schemas) {
082            if (schema.namespace().equals(ns) && ! schema.prefix().isEmpty())
083               return Namespace.create(schema.prefix(), ns);
084            for (RdfNs rdfNs : schema.rdfNs())
085               if (rdfNs.namespaceURI().equals(ns))
086                  return Namespace.create(rdfNs.prefix(), ns);
087         }
088      }
089
090      return null;
091   }
092}