package org.codehaus.xfire.util;

import java.util.StringTokenizer;

import org.codehaus.yom.Element;

/**
 * Namespace utilities.
 * 
 * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
 */
public class NamespaceHelper
{
    /**
     * Create a unique namespace uri/prefix combination.
     * 
     * @param nsUri
     * @return The namespace with the specified URI.  If one doesn't
     * exist, one is created.
     */
    public static String getUniquePrefix(Element element, String namespaceURI)
    {
        String prefix = element.getNamespacePrefix(namespaceURI);
        if (prefix == null)
        {
            prefix = getUniquePrefix(element);
            element.addNamespaceDeclaration(prefix, namespaceURI);
        }
        return prefix;
    }

    private static String getUniquePrefix( Element el )
    {
        int n = 1;
        
        while(true)
        {
            String nsPrefix = "ns" + n;
            
            if ( el.getNamespaceURI( nsPrefix ) == null )
                return nsPrefix;
            
            n++;
        }
    }
    
    public static String makeNamespaceFromClassName(String className, String protocol)
    {
        int index = className.lastIndexOf(".");

        if (index == -1)
        {
            return protocol + "://" + "DefaultNamespace";
        }

        String packageName = className.substring(0, index);
        
        StringTokenizer st = new StringTokenizer(packageName, ".");
        String[] words = new String[st.countTokens()];

        for (int i = 0; i < words.length; ++i)
        {
            words[i] = st.nextToken();
        }

        StringBuffer sb = new StringBuffer(80);

        for (int i = words.length - 1; i >= 0; --i)
        {
            String word = words[i];

            // seperate with dot
            if (i != words.length - 1)
            {
                sb.append('.');
            }

            sb.append(word);
        }

        return protocol + "://" + sb.toString();
    }
}

@@ -62,16 +62,102 @@
 nizer;%0A%0A
+import javax.xml.stream.XMLStreamException;%0Aimport javax.xml.stream.XMLStreamWriter;%0A%0A
 import o
@@ -1172,24 +1172,1056 @@
 %0A    %7D%0A    %0A
+    /**%0A     * Create a unique namespace uri/prefix combination.%0A     * %0A     * @param nsUri%0A     * @return The namespace with the specified URI.  If one doesn't%0A     * exist, one is created.%0A     * @throws XMLStreamException %0A     */%0A    public static String getUniquePrefix(XMLStreamWriter writer, String namespaceURI, boolean declare) %0A        throws XMLStreamException%0A    %7B%0A        String prefix = writer.getPrefix(namespaceURI);%0A        if (prefix == null)%0A        %7B%0A            prefix = getUniquePrefix(writer);%0A            %0A            if (declare)%0A                writer.writeNamespace(prefix, namespaceURI);%0A        %7D%0A        return prefix;%0A    %7D%0A%0A    public static String getUniquePrefix( XMLStreamWriter writer )%0A    %7B%0A        int n = 1;%0A        %0A        while(true)%0A        %7B%0A            String nsPrefix = %22ns%22 + n;%0A            %0A            if ( writer.getNamespaceContext().getNamespaceURI(nsPrefix) == null )%0A            %7B%0A                return nsPrefix;%0A            %7D%0A            %0A            n++;%0A        %7D%0A    %7D%0A%0A
     public s

Fix prefix/ns handling when writing.