1 6 7 package com.hp.hpl.jena.shared.impl; 8 9 import com.hp.hpl.jena.rdf.model.impl.Util; 10 import com.hp.hpl.jena.shared.*; 11 import com.hp.hpl.jena.util.CollectionFactory; 12 13 import java.util.*; 14 import org.apache.xerces.util.XMLChar; 15 16 24 public class PrefixMappingImpl implements PrefixMapping 25 { 26 protected Map prefixToURI; 27 protected Map URItoPrefix; 28 protected boolean locked; 29 30 public PrefixMappingImpl() 31 { prefixToURI = CollectionFactory.createHashedMap(); 32 URItoPrefix = CollectionFactory.createHashedMap(); } 33 34 protected void set( String prefix, String uri ) 35 { prefixToURI.put( prefix, uri ); 36 URItoPrefix.put( uri, prefix ); } 37 38 protected String get( String prefix ) 39 { return (String ) prefixToURI.get( prefix ); } 40 41 public PrefixMapping lock() 42 { 43 locked = true; 44 return this; 45 } 46 47 public PrefixMapping setNsPrefix( String prefix, String uri ) 48 { 49 checkUnlocked(); 50 checkLegal( prefix ); 51 if (!prefix.equals( "" )) 52 { checkProper( uri ); 53 } 54 set( prefix, uri ); 55 return this; 56 } 57 58 public PrefixMapping removeNsPrefix( String prefix ) 59 { 60 checkUnlocked(); 61 String uri = (String ) prefixToURI.remove( prefix ); 62 regenerateReverseMapping(); 63 return this; 64 } 65 66 protected void regenerateReverseMapping() 67 { 68 URItoPrefix.clear(); 69 Iterator it = prefixToURI.entrySet().iterator(); 70 while (it.hasNext()) 71 { 72 Map.Entry e = (Map.Entry) it.next(); 73 URItoPrefix.put( e.getValue(), e.getKey() ); 74 } 75 } 76 77 protected void checkUnlocked() 78 { if (locked) throw new JenaLockedException( this ); } 79 80 private void checkProper( String uri ) 81 { } 84 85 public static boolean isNiceURI( String uri ) 86 { 87 if (uri.equals( "" )) return false; 88 char last = uri.charAt( uri.length() - 1 ); 89 return Util.notNameChar( last ); 90 } 91 92 98 public PrefixMapping setNsPrefixes( PrefixMapping other ) 99 { return setNsPrefixes( other.getNsPrefixMap() ); } 100 101 106 public PrefixMapping withDefaultMappings( PrefixMapping other ) 107 { 108 checkUnlocked(); 109 Iterator it = other.getNsPrefixMap().entrySet().iterator(); 110 while (it.hasNext()) 111 { 112 Map.Entry e = (Map.Entry) it.next(); 113 String prefix = (String ) e.getKey(), uri = (String ) e.getValue(); 114 if (getNsPrefixURI( prefix ) == null && getNsURIPrefix( uri ) == null) 115 setNsPrefix( prefix, uri ); 116 } 117 return this; 118 } 119 120 128 public PrefixMapping setNsPrefixes( Map other ) 129 { 130 checkUnlocked(); 131 Iterator it = other.entrySet().iterator(); 132 while (it.hasNext()) 133 { 134 Map.Entry e = (Map.Entry) it.next(); 135 setNsPrefix( (String ) e.getKey(), (String ) e.getValue() ); 136 } 137 return this; 138 } 139 140 143 private void checkLegal( String prefix ) 144 { 145 if (prefix.length() > 0 && !XMLChar.isValidNCName( prefix )) 146 throw new PrefixMapping.IllegalPrefixException( prefix ); 147 } 148 149 public String getNsPrefixURI( String prefix ) 150 { return get( prefix ); } 151 152 public Map getNsPrefixMap() 153 { return CollectionFactory.createHashedMap( prefixToURI ); } 154 155 public String getNsURIPrefix( String uri ) 156 { 157 return (String ) URItoPrefix.get( uri ); 158 } 161 162 167 public String expandPrefix( String prefixed ) 168 { 169 int colon = prefixed.indexOf( ':' ); 170 if (colon < 0) 171 return prefixed; 172 else 173 { 174 String prefix = prefixed.substring( 0, colon ); 175 String uri = get( prefix ); 176 return uri == null ? prefixed : uri + prefixed.substring( colon + 1 ); 177 } 178 } 179 180 183 public String toString() 184 { return "pm:" + prefixToURI; } 185 186 196 public String qnameFor( String uri ) 197 { 198 int split = Util.splitNamespace( uri ); 199 String ns = uri.substring( 0, split ), local = uri.substring( split ); 200 if (local.equals( "" )) return null; 201 String prefix = (String ) URItoPrefix.get( ns ); 202 return prefix == null ? null : prefix + ":" + local; 203 } 206 207 211 public String usePrefix( String uri ) 212 { return shortForm( uri ); } 213 214 221 public String shortForm( String uri ) 222 { 223 Map.Entry e = findMapping( uri, true ); 224 return e == null ? uri : e.getKey() + ":" + uri.substring( ((String ) e.getValue()).length() ); 225 } 226 227 237 private Map.Entry findMapping( String uri, boolean partial ) 238 { 239 Iterator it = prefixToURI.entrySet().iterator(); 240 while (it.hasNext()) 241 { 242 Map.Entry e = (Map.Entry) it.next(); 243 String ss = (String ) e.getValue(); 244 if (uri.startsWith( ss ) && (partial || ss.length() == uri.length())) return e; 245 } 246 return null; 247 } 248 249 } 250 251 252 | Popular Tags |