001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.jndi.provider.ldap.asn1;
019:
020: import java.io.IOException;
021:
022: import org.apache.harmony.security.asn1.ASN1Choice;
023: import org.apache.harmony.security.asn1.ASN1Type;
024: import org.apache.harmony.security.asn1.BerInputStream;
025:
026: /**
027: * When encoding, the value bind to ASN1ChoiceWrap must be Object array length
028: * of two or ChosenValue instance. For array, Object[0] is Integer, which is the
029: * index of the chosen type (NOTE: not the tag number), Object[1] is the bind
030: * value of the chosen type. For ChosenValue instance, methods
031: * <code>getIndex()</code> and <code>getValue</code> should return chosen
032: * index and value respectively.
033: * <p>
034: *
035: * When decoding, we always return ChosenValue instance
036: */
037: public class ASN1ChoiceWrap extends ASN1Choice {
038:
039: public ASN1ChoiceWrap(ASN1Type[] types) {
040: super (types);
041: }
042:
043: @Override
044: public int getIndex(Object object) {
045: if (object instanceof ChosenValue) {
046: ChosenValue chosen = (ChosenValue) object;
047: return chosen.getIndex();
048: }
049:
050: if (object instanceof ASN1Encodable) {
051: Object[] value = new Object[1];
052: ((ASN1Encodable) object).encodeValues(value);
053: return getIndex(value[0]);
054: }
055:
056: Object[] values = (Object[]) object;
057: return ((Integer) values[0]).intValue();
058: }
059:
060: @Override
061: public Object getObjectToEncode(Object object) {
062: if (object instanceof ChosenValue) {
063: ChosenValue chosen = (ChosenValue) object;
064: return chosen.getValue();
065: }
066:
067: if (object instanceof ASN1Encodable) {
068: Object[] value = new Object[1];
069: ((ASN1Encodable) object).encodeValues(value);
070: return getObjectToEncode(value[0]);
071: }
072:
073: return ((Object[]) object)[1];
074: }
075:
076: @Override
077: public Object decode(BerInputStream in) throws IOException {
078: super .decode(in);
079: return new ChosenValue(in.choiceIndex, getDecodedObject(in));
080: }
081:
082: public static class ChosenValue {
083: private int index;
084:
085: private Object value;
086:
087: public ChosenValue(int index, Object value) {
088: this .index = index;
089: this .value = value;
090: }
091:
092: public int getIndex() {
093: return index;
094: }
095:
096: public Object getValue() {
097: return value;
098: }
099: }
100: }
|