View Javadoc

    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.Locale;
    28  import java.util.Map;
    29  import java.util.Properties;
    30
    31  /**
    32   * This class act as a container for various well-known and adopted <i>RDF</i> Vocabulary prefixes.
    33   */
    34  public class PopularPrefixes {
    35
    36      private static final Logger logger = LoggerFactory.getLogger(PopularPrefixes.class);
    37
    38      private static final String RESOURCE_NAME = "/org/apache/any23/prefixes/prefixes.properties";
    39
    40      private static final Prefixes popularPrefixes = getPrefixes();
    41
    42      private static Prefixes getPrefixes() {
    43          Prefixes prefixes = new Prefixes();
    44          Properties properties = new Properties();
    45          try {
    46              logger.trace(String.format(Locale.ROOT, "Loading prefixes from %s", RESOURCE_NAME));
    47              properties.load(getResourceAsStream());
    48          } catch (IOException e) {
    49              logger.error(String.format(Locale.ROOT, "Error while loading prefixes from %s", RESOURCE_NAME), e);
    50              throw new RuntimeException(
    51                      String.format(Locale.ROOT, "Error while loading prefixes from %s", RESOURCE_NAME));
    52          }
    53          for (Map.Entry entry : properties.entrySet()) {
    54              if (testIRICompliance((String) entry.getValue())) {
    55                  prefixes.add((String) entry.getKey(), (String) entry.getValue());
    56              } else {
    57                  logger.warn(String.format(Locale.ROOT, "Prefixes entry '%s' is not a well-formad IRI. Skipped.",
    58                          entry.getValue()));
    59              }
    60          }
    61          return prefixes;
    62      }
    63
    64      /**
    65       * This method perform a prefix lookup. Given a set of prefixes it returns {@link Prefixes} bag class containing
    66       * them.
    67       *
    68       * @param prefixes
    69       *            the input prefixes where perform the lookup
    70       *
    71       * @return a {@link Prefixes} containing all the prefixes mathing the input parameter
    72       */
    73      public static Prefixes createSubset(String... prefixes) {
    74          return popularPrefixes.createSubset(prefixes);
    75      }
    76
    77      /**
    78       * @return a {@link Prefixes} with a set of well-known prefixes
    79       */
    80      public static Prefixes get() {
    81          return popularPrefixes;
    82      }
    83
    84      /**
    85       * Checks the compliance of the <i>IRI</i>.
    86       *
    87       * @param stringUri
    88       *            the string of the IRI to be checked
    89       *
    90       * @return <code>true</code> if <i> stringUri</i> is a valid IRI, <code>false</code> otherwise.
    91       */
    92      private static boolean testIRICompliance(String stringUri) {
    93          try {
    94              new URI(stringUri);
    95          } catch (URISyntaxException e) {
    96              return false;
    97          }
    98          return true;
    99      }
    100
    101     /**
    102      * Loads the prefixes list configuration file.
    103      *
    104      * @return the input stream containing the configuration.
    105      */
    106     private static InputStream getResourceAsStream() {
    107         InputStream result;
    108         result = PopularPrefixes.class.getResourceAsStream(RESOURCE_NAME);
    109         if (result == null) {
    110             result = PopularPrefixes.class.getClassLoader().getResourceAsStream(RESOURCE_NAME);
    111             if (result == null) {
    112                 result = ClassLoader.getSystemResourceAsStream(RESOURCE_NAME);
    113             }
    114         }
    115         return result;
    116     }
    117
    118 }