001package org.apache.commons.ssl.org.bouncycastle.asn1.x509;
002
003import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Object;
004import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1ObjectIdentifier;
005import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Primitive;
006import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Sequence;
007import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1TaggedObject;
008import org.apache.commons.ssl.org.bouncycastle.asn1.DERSequence;
009
010public class GeneralNames
011    extends ASN1Object
012{
013    private final GeneralName[] names;
014
015    public static GeneralNames getInstance(
016        Object  obj)
017    {
018        if (obj instanceof GeneralNames)
019        {
020            return (GeneralNames)obj;
021        }
022
023        if (obj != null)
024        {
025            return new GeneralNames(ASN1Sequence.getInstance(obj));
026        }
027
028        return null;
029    }
030
031    public static GeneralNames getInstance(
032        ASN1TaggedObject obj,
033        boolean          explicit)
034    {
035        return getInstance(ASN1Sequence.getInstance(obj, explicit));
036    }
037
038    public static GeneralNames fromExtensions(Extensions extensions, ASN1ObjectIdentifier extOID)
039    {
040        return GeneralNames.getInstance(extensions.getExtensionParsedValue(extOID));
041    }
042
043    /**
044     * Construct a GeneralNames object containing one GeneralName.
045     * 
046     * @param name the name to be contained.
047     */
048    public GeneralNames(
049        GeneralName  name)
050    {
051        this.names = new GeneralName[] { name };
052    }
053
054
055    public GeneralNames(
056        GeneralName[]  names)
057    {
058        this.names = names;
059    }
060
061    private GeneralNames(
062        ASN1Sequence  seq)
063    {
064        this.names = new GeneralName[seq.size()];
065
066        for (int i = 0; i != seq.size(); i++)
067        {
068            names[i] = GeneralName.getInstance(seq.getObjectAt(i));
069        }
070    }
071
072    public GeneralName[] getNames()
073    {
074        GeneralName[] tmp = new GeneralName[names.length];
075
076        System.arraycopy(names, 0, tmp, 0, names.length);
077
078        return tmp;
079    }
080
081    /**
082     * Produce an object suitable for an ASN1OutputStream.
083     * <pre>
084     * GeneralNames ::= SEQUENCE SIZE {1..MAX} OF GeneralName
085     * </pre>
086     */
087    public ASN1Primitive toASN1Primitive()
088    {
089        return new DERSequence(names);
090    }
091
092    public String toString()
093    {
094        StringBuffer  buf = new StringBuffer();
095        String        sep = System.getProperty("line.separator");
096
097        buf.append("GeneralNames:");
098        buf.append(sep);
099
100        for (int i = 0; i != names.length; i++)
101        {
102            buf.append("    ");
103            buf.append(names[i]);
104            buf.append(sep);
105        }
106        return buf.toString();
107    }
108}