1 19 package org.openharmonise.commons.xml.namespace; 20 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.logging.*; 25 import java.util.logging.Level ; 26 27 import org.w3c.dom.Node ; 28 29 37 public abstract class AbstractNamespaceResolver implements NamespaceResolver { 38 39 42 private Map m_namespaces = new HashMap (11); 43 44 47 private int m_nNextPrefixAppend = 0; 48 49 52 private static final Logger m_logger = Logger.getLogger(AbstractNamespaceResolver.class.getName()); 53 54 59 protected void addDefaultNamespaces() { 60 try { 61 this.addNamespace(NamespaceType.COL_RDF); 62 this.addNamespace(NamespaceType.DAV); 63 this.addNamespace(NamespaceType.LOM_CLS); 64 this.addNamespace(NamespaceType.LOM_XML); 65 this.addNamespace(NamespaceType.RDF); 66 this.addNamespace(NamespaceType.RDF_SCHEMA); 67 this.addNamespace(NamespaceType.XML); 68 this.addNamespace(NamespaceType.XML_SCHEMA); 69 this.addNamespace(NamespaceType.XML_SCHEMA_INSTANCE); 70 this.addNamespace(NamespaceType.OHRM); 71 this.addNamespace(NamespaceType.XSLFO); 72 this.addNamespace(NamespaceType.XSLT); 73 } catch (NamespaceClashException e) { 74 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 75 } 76 } 77 78 85 private void addNamespace(NamespaceType namespace) throws NamespaceClashException { 86 this.addNamespace(namespace.getURI(), namespace.getPrefix()); 87 } 88 89 92 public void addNamespace(String sURI, String sPrefix) 93 throws NamespaceClashException { 94 95 boolean bFound = false; 96 97 synchronized (this.m_namespaces) { 98 Iterator itor = this.m_namespaces.values().iterator(); 99 while( itor.hasNext() ) { 100 Namespace ns = (Namespace)itor.next(); 101 if( ns.getPrefix().equals(sPrefix) ) { 102 bFound=true; 103 } 104 } 105 } 106 107 if( bFound ) { 108 throw new NamespaceClashException(); 109 } else { 110 m_namespaces.put(sPrefix, new Namespace(sURI, sPrefix)); 111 } 112 113 } 114 115 118 public String getNamespaceByPrefix(String sPrefix) { 119 String sReturn = null; 120 121 Namespace ns = (Namespace)m_namespaces.get(sPrefix); 122 if( ns!=null ) { 123 sReturn = ns.getURI(); 124 } 125 126 return sReturn; 127 } 128 129 132 public String getPrefixByNamespace(String sURI) { 133 String sPrefix=null; 134 135 synchronized (this.m_namespaces) { 136 Iterator itor = this.m_namespaces.values().iterator(); 137 while( itor.hasNext() ) { 138 Namespace ns = (Namespace)itor.next(); 139 if( ns.getURI().equals(sURI) ) { 140 sPrefix=ns.getPrefix(); 141 } 142 } 143 } 144 145 if( sPrefix==null ) { 146 sPrefix = this.createPrefix(sURI); 147 try { 148 this.addNamespace(sURI, sPrefix); 149 } catch (NamespaceClashException e) { 150 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 151 } 152 } 153 154 return sPrefix; 155 } 156 157 160 public String getPrefixByNode(Node node) throws NamespaceClashException { 161 String sPrefix = null; 162 163 String sURI = node.getNamespaceURI(); 164 String sNodePrefix = node.getPrefix(); 165 166 if( sNodePrefix!=null && this.m_namespaces.containsKey(sNodePrefix) ) { 167 Namespace ns = (Namespace)this.m_namespaces.get(sNodePrefix); 168 if( sURI==null || (sURI!=null && sURI.equals(ns.m_sURI )) ) { 169 sPrefix=ns.getPrefix(); 170 } else { 171 throw new NamespaceClashException("Node has a namespace prefix that is already in the resolver associated to a different namespace URI than the one associates to the node."); 172 } 173 } else if( sNodePrefix!=null && !this.m_namespaces.containsKey(sNodePrefix) ) { 174 if( sURI!=null ) { 175 this.addNamespace(sURI, sNodePrefix); 176 sPrefix=sNodePrefix; 177 } else { 178 throw new NamespaceClashException("Node has prefix but no namespace URI and resolver does not have a namespace URI for that prefix."); 179 } 180 } else if( sNodePrefix==null && sURI!=null ) { 181 sPrefix = this.getPrefixByNamespace(sURI); 182 } 183 184 return sPrefix; 185 } 186 187 190 public void removeNamespace(String sURI) { 191 String sPrefix=null; 192 193 synchronized (this.m_namespaces) { 194 Iterator itor = this.m_namespaces.values().iterator(); 195 while( itor.hasNext() ) { 196 Namespace ns = (Namespace)itor.next(); 197 if( ns.getURI().equals(sURI) ) { 198 sPrefix=ns.getPrefix(); 199 } 200 } 201 } 202 203 if( sPrefix!=null ) { 204 this.m_namespaces.remove(sPrefix); 205 } 206 } 207 208 214 private String createPrefix(String sURI) { 215 String sPrefix = "ns" + this.m_nNextPrefixAppend; 216 this.m_nNextPrefixAppend++; 217 218 while( this.m_namespaces.containsKey(sPrefix) ) { 219 sPrefix = "ns" + this.m_nNextPrefixAppend; 220 this.m_nNextPrefixAppend++; 221 } 222 223 return sPrefix; 224 } 225 226 233 private class Namespace { 234 235 238 private String m_sURI; 239 240 243 private String m_sPrefix; 244 245 251 public Namespace(String sURI, String sPrefix) { 252 this.m_sURI = sURI; 253 this.m_sPrefix = sPrefix; 254 } 255 256 261 public String getURI() { 262 return this.m_sURI; 263 } 264 265 270 public String getPrefix() { 271 return this.m_sPrefix; 272 } 273 274 277 public String toString() { 278 return m_sPrefix; 279 } 280 } 281 282 } 283 | Popular Tags |