public String getPrefix(String namespace, boolean generatePrefix)
    {
        //assert namespace != null;
        if(!namesInterned) {
            // when String is interned we can do much faster namespace stack lookups ...
            namespace = namespace.intern();
        } else if(checkNamesInterned) {
            checkInterning(namespace);
            //assert namespace != namespace.intern();
        }
        if(namespace == null) {
            throw new IllegalArgumentException("namespace must be not null"+getLocation());
        } else if(namespace.length() == 0) {
            throw new IllegalArgumentException("default namespace cannot have prefix"+getLocation());
        }

        // first check if namespace is already in scope
        for (int i = namespaceEnd - 1; i >= 0 ; --i)
        {
            if(namespace == namespaceUri[ i ]) {
                final String prefix = namespacePrefix[ i ];
                // now check that prefix is still in scope
                for (int p = namespaceEnd - 1; p > i ; --p)
                {
                    if(prefix == namespacePrefix[ p ])
                        continue; // too bad - prefix is redeclared with different namespace
                }
                return prefix;
            }
        }

        // so not found it ...
        if(!generatePrefix) {
            return null;
        }
        return generatePrefix(namespace);
    }