package jlibs.xml.sax.helpers;

import org.xml.sax.helpers.NamespaceSupport;
import jlibs.xml.Namespaces;
import jlibs.core.lang.Util;

import java.util.Properties;

/**
 * @author Santhosh Kumar T
 */
public class MyNamespaceSupport extends NamespaceSupport{
    private Properties suggested;

    public MyNamespaceSupport(){
        this(Namespaces.getSuggested());
    }

    public MyNamespaceSupport(Properties suggested){
        this.suggested = suggested;
    }

    public void suggestPrefix(String prefix, String uri){
        suggested.put(prefix, uri);
    }

    public String findPrefix(String uri){
        if(uri==null)
            uri = "";
        String prefix = getPrefix(uri);
        if(prefix==null){
            String defaultURI = getURI("");
            if(defaultURI==null)
                defaultURI = "";
            if(Util.equals(uri, defaultURI))
                prefix = "";
        }
        return prefix;
    }

    public String declarePrefix(String uri){
        String prefix = findPrefix(uri);
        if(prefix==null){
            prefix = suggested.getProperty(uri);
            if(prefix==null){
                int i = 1;
                while(getURI(prefix="ns"+i)!=null)
                    i++;

            }
            declarePrefix(prefix, uri);
        }
        return prefix;
    }
}

@@ -25,16 +25,74 @@
 lpers;%0A%0A
+import jlibs.core.lang.Util;%0Aimport jlibs.xml.Namespaces;%0A
 import o
@@ -128,24 +128,25 @@
 upport;%0A
+%0A
 import j
 libs.xml
@@ -141,86 +141,118 @@
 rt j
-libs.xml.Namespaces;%0Aimport jlibs.core.lang.Util;%0A%0Aimport java.util.Properties
+ava.net.URI;%0Aimport java.net.URISyntaxException;%0Aimport java.util.Properties;%0Aimport java.util.StringTokenizer
 ;%0A%0A/
@@ -1177,126 +1177,1391 @@
 gest
-ed.getProperty(uri);%0A            if(prefix==null)%7B%0A                int i = 1;%0A                while(getURI(prefix=%22ns%22
+Prefix(uri);%0A            declarePrefix(prefix, uri);%0A        %7D%0A        return prefix;%0A    %7D%0A%0A    private String suggestPrefix(String uri)%7B%0A        String prefix = suggested.getProperty(uri);%0A        if(prefix!=null)%0A            return prefix;%0A        %0A        try%7B%0A            URI _uri = new URI(uri);%0A%0A            String path = _uri.getPath();%0A            StringTokenizer stok = new StringTokenizer(path, %22/%22);%0A            while(stok.hasMoreTokens())%0A                prefix = stok.nextToken();%0A            if(prefix!=null)%7B%0A                if(getURI(prefix)==null)%0A                    return prefix;%0A            %7Delse%7B%0A                String host = _uri.getHost();%0A                if(host!=null)%7B%0A                    stok = new StringTokenizer(host, %22.%22);%0A                    String curPrefix = null;%0A                    while(stok.hasMoreTokens())%7B%0A                        prefix = curPrefix;%0A                        curPrefix = stok.nextToken();%0A                    %7D%0A                %7D                %0A                if(prefix!=null)%7B%0A                    if(getURI(prefix)==null)%0A                        return prefix;%0A                %7Delse%0A                    prefix = %22ns%22;%0A            %7D%0A        %7Dcatch(URISyntaxException ignore)%7B%0A            // xml spec doesn't guarantee that namespace uri should be valid uri%0A        %7D%0A        %0A        int i = 1;%0A        while(getURI(prefix
 +i)!
@@ -2579,24 +2579,29 @@
         
+i++;%0A
         
 i++;%0A%0A  
@@ -2596,99 +2596,457 @@
     
-i++;%0A%0A            %7D%0A            declarePrefix(prefix, uri);%0A        %7D%0A        return prefix
+return prefix+i;%0A    %7D%0A%0A    public static void main(String%5B%5D args)%7B%0A        MyNamespaceSupport ns = new MyNamespaceSupport();%0A        System.out.println(ns.declarePrefix(%22http://www.sonoasystems.com/schemas/2007/8/3/sci/%22));%0A        System.out.println(ns.declarePrefix(%22http://www.sonoasystems.com/schemas/2007/8/7/sci/%22));%0A        System.out.println(ns.declarePrefix(%22http://com%22));%0A        System.out.println(ns.declarePrefix(%22http://google.org%22))
 ;%0A  

suggest better prefixes for given uri