Coverage Report - org.apache.any23.rdf.PopularPrefixes
 
Classes in this File Line Coverage Branch Coverage Complexity
PopularPrefixes
0%
0/30
0%
0/8
2.6
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *  http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 
 18  
 package org.apache.any23.rdf;
 19  
 
 20  
 import org.slf4j.Logger;
 21  
 import org.slf4j.LoggerFactory;
 22  
 
 23  
 import java.io.IOException;
 24  
 import java.io.InputStream;
 25  
 import java.net.URI;
 26  
 import java.net.URISyntaxException;
 27  
 import java.util.Map;
 28  
 import java.util.Properties;
 29  
 
 30  
 /**
 31  
  * This class act as a container for various well-known and adopted <i>RDF</i> Vocabulary prefixes.
 32  
  */
 33  0
 public class PopularPrefixes {
 34  
 
 35  0
     private static final Logger logger = LoggerFactory.getLogger(PopularPrefixes.class);
 36  
 
 37  
     private static final String RESOURCE_NAME = "/org/apache/any23/prefixes/prefixes.properties";
 38  
 
 39  0
     private static Prefixes popularPrefixes = getPrefixes();
 40  
 
 41  
     private static Prefixes getPrefixes() {
 42  0
         Prefixes prefixes = new Prefixes();
 43  0
         Properties properties = new Properties();
 44  
         try {
 45  0
             logger.info(String.format("Loading prefixes from %s", RESOURCE_NAME));
 46  0
             properties.load(getResourceAsStream());
 47  0
         } catch (IOException e) {
 48  0
             logger.error(String.format("Error while loading prefixes from %s", RESOURCE_NAME), e);
 49  0
             throw new RuntimeException(String.format("Error while loading prefixes from %s", RESOURCE_NAME));
 50  0
         }
 51  0
         popularPrefixes = new Prefixes();
 52  0
         for (Map.Entry entry : properties.entrySet()) {
 53  0
             if (testURICompliance((String) entry.getValue())) {
 54  0
                 prefixes.add(
 55  
                         (String) entry.getKey(),
 56  
                         (String) entry.getValue()
 57  
                 );
 58  
             } else {
 59  0
                 logger.warn(String.format("Prefixes entry '%s' is not a well-formad URI. Skipped.", entry.getValue()));
 60  
             }
 61  
         }
 62  0
         return prefixes;
 63  
     }
 64  
 
 65  
     /**
 66  
      * This method perform a prefix lookup. Given a set of prefixes it returns {@link Prefixes} bag
 67  
      * class containing them.
 68  
      *
 69  
      * @param prefixes the input prefixes where perform the lookup
 70  
      * @return a {@link Prefixes} containing all the prefixes mathing the input parameter
 71  
      */
 72  
     public static Prefixes createSubset(String... prefixes) {
 73  0
         return popularPrefixes.createSubset(prefixes);
 74  
     }
 75  
 
 76  
     /**
 77  
      * @return a {@link Prefixes} with a set of well-known prefixes
 78  
      */
 79  
     public static Prefixes get() {
 80  0
         return popularPrefixes;
 81  
     }
 82  
 
 83  
     /**
 84  
      * Checks the compliance of the <i>URI</i>.
 85  
      *
 86  
      * @param stringUri the string of the URI to be checked
 87  
      * @return <code>true</code> if <i> stringUri</i> is a valid URI,
 88  
      *         <code>false</code> otherwise.
 89  
      */
 90  
     private static boolean testURICompliance(String stringUri) {
 91  
         try {
 92  0
             new URI(stringUri);
 93  0
         } catch (URISyntaxException e) {
 94  0
             return false;
 95  0
         }
 96  0
         return true;
 97  
     }
 98  
 
 99  
     /**
 100  
       * Loads the prefixes list configuration file.
 101  
       *
 102  
       * @return the input stream containing the configuration.
 103  
       */
 104  
      private static InputStream getResourceAsStream() {
 105  
          InputStream result;
 106  0
          result = PopularPrefixes.class.getResourceAsStream(RESOURCE_NAME);
 107  0
          if (result == null) {
 108  0
              result = PopularPrefixes.class.getClassLoader().getResourceAsStream(RESOURCE_NAME);
 109  0
              if (result == null) {
 110  0
                  result = ClassLoader.getSystemResourceAsStream(RESOURCE_NAME);
 111  
              }
 112  
          }
 113  0
          return result;
 114  
      }
 115  
 
 116  
 
 117  
 }