class AbstractContextSource implements BaseLdapPathContextSource, InitializingBean {

    static String formatForUrl(LdapName ldapName) {
        StringBuilder sb = new StringBuilder();
        ListIterator<Rdn> it = ldapName.getRdns().listIterator(ldapName.size());
        while (it.hasPrevious()) {
            Rdn component = it.previous();

            Attributes attributes = component.toAttributes();

            // Loop through all attribute of the rdn (usually just one, but more are supported by RFC)
            NamingEnumeration<? extends Attribute> allAttributes = attributes.getAll();
            while(allAttributes.hasMoreElements()) {
                Attribute oneAttribute = allAttributes.nextElement();
                String encodedAttributeName = nameEncodeForUrl(oneAttribute.getID());

                // Loop through all values of the attribute (usually just one, but more are supported by RFC)
                NamingEnumeration <?> allValues;
                try {
                    allValues = oneAttribute.getAll();
                } catch (NamingException e) {
                    throw new UncategorizedLdapException("Unexpected error occurred formatting base URL", e);
                }

                while(allValues.hasMoreElements()) {
                    sb.append(encodedAttributeName).append('=');

                    Object oneValue = allValues.nextElement();
                    if (oneValue instanceof String) {
                        String oneString = (String) oneValue;
                        sb.append(nameEncodeForUrl(oneString));
                    } else {
                        throw new IllegalArgumentException("Binary attributes not supported for base URL");
                    }

                    if(allValues.hasMoreElements()) {
                        sb.append('+');
                    }
                }
                if(allAttributes.hasMoreElements()) {
                    sb.append('+');
                }
            }

            if(it.hasPrevious()) {
                sb.append(',');
            }
        }
        return sb.toString();
    }

}