1 package org.codehaus.xfire.util; 2 3 import org.dom4j.DocumentFactory; 4 import org.dom4j.Element; 5 import org.dom4j.Namespace; 6 7 /*** 8 * DOM4J Namespace functions. 9 * 10 * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a> 11 */ 12 public class NamespaceHelper 13 { 14 /*** 15 * @param nsUri 16 * @return The namespace with the specified URI. If one doesn't 17 * exist, one is created. 18 */ 19 public static Namespace createNamespace( String prefix, String nsUri ) 20 { 21 return DocumentFactory.getInstance().createNamespace( prefix, nsUri ); 22 } 23 24 /*** 25 * Create a unique namespace uri/prefix combination. 26 * 27 * @param nsUri 28 * @return The namespace with the specified URI. If one doesn't 29 * exist, one is created. 30 */ 31 public static Namespace getNamespace( Element el, String nsUri ) 32 { 33 Namespace ns = el.getNamespaceForURI( nsUri ); 34 35 if ( ns == null || ns.getPrefix().equals("") ) 36 { 37 ns = DocumentFactory.getInstance().createNamespace( getUniquePrefix( el ), nsUri ); 38 el.add( ns ); 39 } 40 41 return ns; 42 } 43 44 private static String getUniquePrefix( Element el ) 45 { 46 int n = 1; 47 48 while(true) 49 { 50 String nsPrefix = "ns" + n; 51 52 if ( el.getNamespaceForPrefix( nsPrefix ) == null ) 53 return nsPrefix; 54 55 n++; 56 } 57 } 58 }