Coverage Report - org.apache.any23.rdf.Prefixes
 
Classes in this File Line Coverage Branch Coverage Complexity
Prefixes
0%
0/81
0%
0/44
2.25
 
 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.openrdf.model.URI;
 21  
 import org.openrdf.model.impl.ValueFactoryImpl;
 22  
 
 23  
 import java.util.Collections;
 24  
 import java.util.HashMap;
 25  
 import java.util.HashSet;
 26  
 import java.util.Map;
 27  
 import java.util.Map.Entry;
 28  
 import java.util.Set;
 29  
 
 30  
 /**
 31  
  * A mapping from prefixes to namespace URIs. Supports "volatile mappings",
 32  
  * which will be overwritten without notice when mappings are merged,
 33  
  * while for normal mappings this causes an exception. This allows
 34  
  * combining "hard" mappings (which must be retained or something breaks)
 35  
  * and "soft" mappings (which might be read from input RDF files and
 36  
  * should be retained only if they are not in conflict with the hard ones).
 37  
  *
 38  
  * @author Richard Cyganiak (richard@cyganiak.de)
 39  
  */
 40  
 public class Prefixes {
 41  
 
 42  
     public static Prefixes create1(String prefix, String namespaceURI) {
 43  0
         Prefixes result = new Prefixes();
 44  0
         result.add(prefix, namespaceURI);
 45  0
         return result;
 46  
     }
 47  
 
 48  
     public static Prefixes createFromMap(Map<String, String> prefixesToNamespaceURIs, boolean areVolatile) {
 49  0
         Prefixes result = new Prefixes();
 50  0
         for (Entry<String, String> entry : prefixesToNamespaceURIs.entrySet()) {
 51  0
             if (areVolatile) {
 52  0
                 result.addVolatile(entry.getKey(), entry.getValue());
 53  
             } else {
 54  0
                 result.add(entry.getKey(), entry.getValue());
 55  
             }
 56  
         }
 57  0
         return result;
 58  
     }
 59  
 
 60  0
     public static Prefixes EMPTY = new Prefixes(Collections.<String, String>emptyMap());
 61  
 
 62  
     private final Map<String, String> mappings;
 63  0
     private final Set<String> volatilePrefixes = new HashSet<String>();
 64  
 
 65  
     public Prefixes() {
 66  0
         this(new HashMap<String, String>());
 67  0
     }
 68  
 
 69  
     public Prefixes(Prefixes initial) {
 70  0
         this();
 71  0
         add(initial);
 72  0
     }
 73  
 
 74  0
     private Prefixes(Map<String, String> mappings) {
 75  0
         this.mappings = mappings;
 76  0
     }
 77  
 
 78  
     public URI expand(String curie) {
 79  0
         String prefix = parsePrefix(curie);
 80  0
         if (prefix == null || !hasPrefix(prefix)) {
 81  0
             return null;
 82  
         }
 83  0
         return ValueFactoryImpl.getInstance().createURI(
 84  
                 getNamespaceURIFor(prefix) + parseLocalName(curie));
 85  
     }
 86  
 
 87  
     public String abbreviate(String uri) {
 88  0
         for (Entry<String, String> namespace : mappings.entrySet()) {
 89  0
             if (uri.startsWith(namespace.getValue())) {
 90  0
                 return namespace.getKey() + ":" +
 91  
                         uri.substring(namespace.getValue().length());
 92  
             }
 93  
         }
 94  0
         return null;
 95  
     }
 96  
 
 97  
     public boolean canExpand(String curie) {
 98  0
         String prefix = parsePrefix(curie);
 99  0
         return prefix != null && hasPrefix(prefix);
 100  
     }
 101  
 
 102  
     public boolean canAbbreviate(String uri) {
 103  0
         for (Entry<String, String> namespace : mappings.entrySet()) {
 104  0
             if (uri.startsWith(namespace.getValue())) {
 105  0
                 return true;
 106  
             }
 107  
         }
 108  0
         return false;
 109  
     }
 110  
 
 111  
     public String getNamespaceURIFor(String prefix) {
 112  0
         return mappings.get(prefix);
 113  
     }
 114  
 
 115  
     public boolean hasNamespaceURI(String uri) {
 116  0
         return mappings.containsValue(uri);
 117  
     }
 118  
 
 119  
     public boolean hasPrefix(String prefix) {
 120  0
         return mappings.containsKey(prefix);
 121  
     }
 122  
 
 123  
     public Set<String> allPrefixes() {
 124  0
         return mappings.keySet();
 125  
     }
 126  
 
 127  
     public boolean isEmpty() {
 128  0
         return mappings.isEmpty();
 129  
     }
 130  
 
 131  
     public void add(String prefix, String namespaceURI) {
 132  0
         if (isVolatile(prefix)) {
 133  0
             volatilePrefixes.remove(prefix);
 134  
         } else {
 135  0
             if (hasPrefix(prefix)) {
 136  0
                 if (getNamespaceURIFor(prefix).equals(namespaceURI)) {
 137  0
                     return;    // re-assigned same prefix to same URI, let's just ignore it
 138  
                 }
 139  0
                 throw new IllegalStateException("Attempted to re-assign prefix '" + prefix +
 140  
                         "'; clashing values '" + getNamespaceURIFor(prefix) + "' and '" +
 141  
                         namespaceURI);
 142  
             }
 143  
         }
 144  0
         mappings.put(prefix, namespaceURI);
 145  0
     }
 146  
 
 147  
     public void add(Prefixes other) {
 148  0
         for (String otherPrefix : other.allPrefixes()) {
 149  0
             if (other.isVolatile(otherPrefix)) {
 150  0
                 addVolatile(otherPrefix, other.getNamespaceURIFor(otherPrefix));
 151  
             } else {
 152  0
                 add(otherPrefix, other.getNamespaceURIFor(otherPrefix));
 153  
             }
 154  
         }
 155  0
     }
 156  
 
 157  
     public void removePrefix(String prefix) {
 158  0
         mappings.remove(prefix);
 159  0
         volatilePrefixes.remove(prefix);
 160  0
     }
 161  
 
 162  
     public Prefixes createSubset(String... prefixes) {
 163  0
         Prefixes result = new Prefixes();
 164  0
         for (String prefix : prefixes) {
 165  0
             if (!hasPrefix(prefix)) {
 166  0
                 throw new IllegalArgumentException("No namespace URI declared for prefix " + prefix);
 167  
             }
 168  0
             result.add(prefix, getNamespaceURIFor(prefix));
 169  
         }
 170  0
         return result;
 171  
     }
 172  
 
 173  
     public void addVolatile(String prefix, String namespaceURI) {
 174  0
         if (hasPrefix(prefix)) {
 175  0
             return;    // new prefix is volatile, so we don't overwrite the old one
 176  
         }
 177  0
         mappings.put(prefix, namespaceURI);
 178  0
         volatilePrefixes.add(prefix);
 179  0
     }
 180  
 
 181  
     public void addVolatile(Prefixes other) {
 182  0
         for (String otherPrefix : other.allPrefixes()) {
 183  0
             addVolatile(otherPrefix, other.getNamespaceURIFor(otherPrefix));
 184  
         }
 185  0
     }
 186  
 
 187  
     public boolean isVolatile(String prefix) {
 188  0
         return volatilePrefixes.contains(prefix);
 189  
     }
 190  
 
 191  0
     private Map<String, String> mapUnmodifiable = null;
 192  
 
 193  
     public Map<String, String> asMap() {
 194  
         // Optimization: Create the unmodifiable map only once, lazily
 195  0
         if (mapUnmodifiable == null) {
 196  0
             mapUnmodifiable = Collections.unmodifiableMap(mappings);
 197  
         }
 198  0
         return mapUnmodifiable;
 199  
     }
 200  
 
 201  
     private String parsePrefix(String curie) {
 202  0
         int index = curie.indexOf(':');
 203  0
         if (index == -1) {
 204  0
             throw new IllegalArgumentException("Not a CURIE: '" + curie + "'");
 205  
         }
 206  0
         return curie.substring(0, index);
 207  
     }
 208  
 
 209  
     private String parseLocalName(String curie) {
 210  0
         int index = curie.indexOf(':');
 211  0
         if (index == -1) {
 212  0
             throw new IllegalArgumentException("Not a CURIE: '" + curie + "'");
 213  
         }
 214  0
         return curie.substring(index + 1);
 215  
     }
 216  
     
 217  
 }