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