@SuppressWarnings("unchecked")
    public static Collection<X509Certificate> parseCertificates(InputStream pemRepresentation) throws IOException,
            CertificateException {

        CertificateFactory certificateFactory;
        try {
            certificateFactory = CertificateFactory.getInstance("X509", "BC");
        } catch (NoSuchProviderException e) {
            certificateFactory = CertificateFactory.getInstance("X509");
        }
        return (Collection<X509Certificate>) certificateFactory.generateCertificates(pemRepresentation);
    }
Parses a certificate chain from a PEM representation.

@param pemRepresentation a PEM representation of a certificate or certificate chain (cannot be null or empty)
@return A collection of certificates (possibly empty, but never null).
@throws IOException never
@throws CertificateException if there was a problem parsing certificates
xmppserver/src/main/java/org/jivesoftware/util/CertificateManager.java