001package org.apache.commons.ssl.org.bouncycastle.asn1.x509.qualified;
002
003import java.util.Enumeration;
004
005import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1EncodableVector;
006import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Object;
007import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1ObjectIdentifier;
008import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Primitive;
009import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Sequence;
010import org.apache.commons.ssl.org.bouncycastle.asn1.DERSequence;
011import org.apache.commons.ssl.org.bouncycastle.asn1.x509.GeneralName;
012
013/**
014 * The SemanticsInformation object.
015 * <pre>
016 *       SemanticsInformation ::= SEQUENCE {
017 *         semanticsIdentifier        OBJECT IDENTIFIER   OPTIONAL,
018 *         nameRegistrationAuthorities NameRegistrationAuthorities
019 *                                                         OPTIONAL }
020 *         (WITH COMPONENTS {..., semanticsIdentifier PRESENT}|
021 *          WITH COMPONENTS {..., nameRegistrationAuthorities PRESENT})
022 *
023 *     NameRegistrationAuthorities ::=  SEQUENCE SIZE (1..MAX) OF
024 *         GeneralName
025 * </pre>
026 */
027public class SemanticsInformation
028    extends ASN1Object
029{
030    private ASN1ObjectIdentifier semanticsIdentifier;
031    private GeneralName[] nameRegistrationAuthorities;
032    
033    public static SemanticsInformation getInstance(Object obj)
034    {
035        if (obj instanceof SemanticsInformation)
036        {
037            return (SemanticsInformation)obj;
038        }
039
040        if (obj != null)
041        {
042            return new SemanticsInformation(ASN1Sequence.getInstance(obj));            
043        }
044        
045        return null;
046    }
047        
048    private SemanticsInformation(ASN1Sequence seq)
049    {
050        Enumeration e = seq.getObjects();
051        if (seq.size() < 1)
052        {
053             throw new IllegalArgumentException("no objects in SemanticsInformation");
054        }
055        
056        Object object = e.nextElement();
057        if (object instanceof ASN1ObjectIdentifier)
058        {
059            semanticsIdentifier = ASN1ObjectIdentifier.getInstance(object);
060            if (e.hasMoreElements())
061            {
062                object = e.nextElement();
063            }
064            else
065            {
066                object = null;
067            }
068        }
069        
070        if (object != null)
071        {
072            ASN1Sequence generalNameSeq = ASN1Sequence.getInstance(object);
073            nameRegistrationAuthorities = new GeneralName[generalNameSeq.size()];
074            for (int i= 0; i < generalNameSeq.size(); i++)
075            {
076                nameRegistrationAuthorities[i] = GeneralName.getInstance(generalNameSeq.getObjectAt(i));
077            } 
078        }
079    }
080        
081    public SemanticsInformation(
082        ASN1ObjectIdentifier semanticsIdentifier,
083        GeneralName[] generalNames)
084    {
085        this.semanticsIdentifier = semanticsIdentifier;
086        this.nameRegistrationAuthorities = generalNames;
087    }
088
089    public SemanticsInformation(ASN1ObjectIdentifier semanticsIdentifier)
090    {
091        this.semanticsIdentifier = semanticsIdentifier;
092        this.nameRegistrationAuthorities = null;
093    }
094
095    public SemanticsInformation(GeneralName[] generalNames)
096    {
097        this.semanticsIdentifier = null;
098        this.nameRegistrationAuthorities = generalNames;
099    }        
100    
101    public ASN1ObjectIdentifier getSemanticsIdentifier()
102    {
103        return semanticsIdentifier;
104    }
105        
106    public GeneralName[] getNameRegistrationAuthorities()
107    {
108        return nameRegistrationAuthorities;
109    } 
110    
111    public ASN1Primitive toASN1Primitive()
112    {
113        ASN1EncodableVector seq = new ASN1EncodableVector();
114        
115        if (this.semanticsIdentifier != null)
116        {
117            seq.add(semanticsIdentifier);
118        }
119        if (this.nameRegistrationAuthorities != null)
120        {
121            ASN1EncodableVector seqname = new ASN1EncodableVector();
122            for (int i = 0; i < nameRegistrationAuthorities.length; i++) 
123            {
124                seqname.add(nameRegistrationAuthorities[i]);
125            }            
126            seq.add(new DERSequence(seqname));
127        }            
128        
129        return new DERSequence(seq);
130    }                   
131}