01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: package org.apache.harmony.jndi.provider.ldap.asn1;
19:
20: import java.io.IOException;
21:
22: import org.apache.harmony.jndi.provider.ldap.Filter;
23: import org.apache.harmony.jndi.provider.ldap.asn1.ASN1ChoiceWrap.ChosenValue;
24: import org.apache.harmony.security.asn1.ASN1Choice;
25: import org.apache.harmony.security.asn1.ASN1Type;
26: import org.apache.harmony.security.asn1.BerInputStream;
27: import org.apache.harmony.security.asn1.BerOutputStream;
28:
29: /**
30: * This class deal with recursive definition of ASN.1 choice type, it's used to
31: * define Ldap Filter type schema.
32: */
33: public class ASN1LdapFilter extends ASN1Type {
34:
35: private static ASN1Choice type = null;
36:
37: public ASN1LdapFilter() {
38: super (TAG_CHOICE); // has not tag number
39: }
40:
41: @Override
42: public final boolean checkTag(int identifier) {
43: return true;
44: }
45:
46: @Override
47: public Object decode(BerInputStream in) throws IOException {
48: return null;
49: }
50:
51: @Override
52: public void encodeASN(BerOutputStream out) {
53: if (type == null) {
54: type = (ASN1Choice) LdapASN1Constant.Filter;
55: }
56: // has not been decoded
57: if (!(out.content instanceof byte[])) {
58: ChosenValue chosen = (ChosenValue) out.content;
59: int index = chosen.getIndex();
60: byte[] bytes = type.type[index].encode(chosen.getValue());
61: out.content = bytes;
62: out.length = bytes.length;
63: }
64: // TODO: Any way better to do this(append out.content to out.encoded)?
65: out.encodeString();
66: }
67:
68: @Override
69: public void encodeContent(BerOutputStream out) {
70: // FIXME: do nothing if never be called
71: throw new RuntimeException();
72: }
73:
74: @Override
75: public void setEncodingContent(BerOutputStream out) {
76: if (type == null) {
77: type = (ASN1Choice) LdapASN1Constant.Filter;
78: }
79:
80: ChosenValue chosen = null;
81: if (out.content instanceof Filter) {
82: Object[] values = new Object[1];
83: ((Filter) out.content).encodeValues(values);
84: chosen = (ChosenValue) values[0];
85: } else {
86: chosen = (ChosenValue) out.content;
87: }
88:
89: int index = chosen.getIndex();
90: byte[] bytes = type.type[index].encode(chosen.getValue());
91:
92: out.content = bytes;
93: out.length = bytes.length;
94: }
95:
96: }
|