Home | History | Annotate | Download | only in asn1
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 
     18 /**
     19 * @author Vladimir N. Molotkov, Stepan M. Mishura
     20 * @version $Revision$
     21 */
     22 
     23 package org.apache.harmony.security.asn1;
     24 
     25 
     26 /**
     27  * This abstract class represents ASN.1 type that is a collection of ASN.1 types.
     28  *
     29  * @see <a href="http://asn1.elibel.tm.fr/en/standards/index.htm">ASN.1</a>
     30  */
     31 
     32 public abstract class ASN1TypeCollection extends ASN1Constructured {
     33 
     34     public final ASN1Type[] type; //TODO comment me
     35 
     36     public final boolean[] OPTIONAL; //TODO comment me
     37 
     38     public final Object[] DEFAULT; //TODO comment me
     39 
     40     /**
     41      * Constructs ASN.1 collection type.
     42      *
     43      * @param tagNumber - ASN.1 tag number
     44      * @param type - a collection of one or more ASN.1 types.
     45      * @throws IllegalArgumentException - if tagNumber is invalid
     46      */
     47     public ASN1TypeCollection(int tagNumber, ASN1Type[] type) {
     48         super(tagNumber);
     49         // FIXME what about empty sequence?
     50         //        if (type.length == 0) {
     51         //            throw new ASN1Exception("ASN1 collection type: "
     52         //                    + getClass().getName()
     53         //                    + " MUST have at least one component");
     54         //        }
     55 
     56         this.type = type;
     57         this.OPTIONAL = new boolean[type.length];
     58         this.DEFAULT = new Object[type.length];
     59     }
     60 
     61     /**
     62      * Sets a collection component as optional
     63      *
     64      * @param index - an index of a component
     65      */
     66     protected final void setOptional(int index) {
     67         OPTIONAL[index] = true;
     68     }
     69 
     70     /**
     71      * Sets a default value for a collection component.
     72      * The component also became an optional component.
     73      *
     74      * @param object - a component's default value
     75      * @param index - an index of a component
     76      */
     77     protected final void setDefault(Object object, int index) {
     78         OPTIONAL[index] = true;
     79         DEFAULT[index] = object;
     80     }
     81 
     82     /**
     83      * Provides an object's values to be encoded
     84      *
     85      * Derived classes should override this method to provide
     86      * encoding for a selected class of objects.
     87      *
     88      * The default implementation throws RuntimeException.
     89      *
     90      * @param object - an object to be encoded
     91      * @param values - an array to store an object's values to be encoded
     92      */
     93     protected void getValues(Object object, Object[] values) {
     94         throw new RuntimeException("ASN.1 type is not designed to be encoded: " + getClass().getName());
     95     }
     96 }
     97