OSDN Git Service

Bug fix on XerEncoder.
[bm-asn1/bm-asn1.git] / jp / bitmeister / asn1 / sample / FrightStatusMain.java
1 /*
2  * Copyright 2011 BitMeister Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package jp.bitmeister.asn1.sample;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.ByteArrayOutputStream;
20
21 import jp.bitmeister.asn1.codec.ber.BerDecoder;
22 import jp.bitmeister.asn1.codec.ber.BerEncoder;
23 import jp.bitmeister.asn1.codec.ber.DerEncoder;
24 import jp.bitmeister.asn1.codec.xer.XerDecoder;
25 import jp.bitmeister.asn1.codec.xer.XerEncoder;
26 import jp.bitmeister.asn1.exception.ASN1DecodingException;
27 import jp.bitmeister.asn1.exception.ASN1EncodingException;
28 import jp.bitmeister.asn1.sample.FrightStatusTypes.Airport;
29 import jp.bitmeister.asn1.sample.FrightStatusTypes.AllFrights;
30 import jp.bitmeister.asn1.sample.FrightStatusTypes.FrightNumber;
31 import jp.bitmeister.asn1.sample.FrightStatusTypes.Information;
32 import jp.bitmeister.asn1.sample.FrightStatusTypes.Status;
33 import jp.bitmeister.asn1.type.ASN1TagClass;
34 import jp.bitmeister.asn1.type.ASN1Type;
35 import jp.bitmeister.asn1.type.builtin.INTEGER;
36 import jp.bitmeister.asn1.type.useful.UTCTime;
37 import jp.bitmeister.asn1.value.HexString;
38
39 public class FrightStatusMain {
40
41         public static void main(String[] args) {
42                 AllFrights frights = new AllFrights(
43                                 new FrightStatus(
44                                                 new FrightNumber("JP041"),
45                                                 new Information(
46                                                                 new Airport(Airport.tokyo),
47                                                                 new UTCTime("110627073000"),
48                                                                 new UTCTime("110627073000")
49                                                 ),
50                                                 new Information(
51                                                                 new Airport(Airport.osaka),
52                                                                 new UTCTime("110627090000"),
53                                                                 null
54                                                 )
55                                 ),
56                                 new FrightStatus(
57                                                 new FrightNumber("NI022"),
58                                                 new Information(
59                                                                 new Airport(Airport.fukuoka),
60                                                                 new UTCTime("110627080000"),
61                                                                 new UTCTime("110627081000")
62                                                 ),
63                                                 new Information(
64                                                                 new Airport(Airport.nagoya),
65                                                                 new UTCTime("110627093000"),
66                                                                 null
67                                                 ),
68                                                 new Status(ASN1TagClass.CONTEXT_SPECIFIC, 1, new INTEGER(10))
69                                                 )
70                                 );
71
72                 System.out.println("Source data:");
73                 System.out.println(frights);
74
75                 derEncAndDec(frights);
76                 xerEncAndDec(frights);
77
78         }
79
80         static void derEncAndDec(ASN1Type data) {
81                 System.out.println("BER encoding:");
82                 ByteArrayOutputStream bo = new ByteArrayOutputStream();
83                 BerEncoder enc = new DerEncoder(bo);
84                 try {
85                         enc.encode(data);
86                 } catch (ASN1EncodingException e) {
87                         e.printStackTrace();
88                 }
89
90                 System.out.println(new HexString(bo.toByteArray()));
91
92                 ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
93                 BerDecoder dec = new BerDecoder(FrightStatusTypes.class, bi);
94                 try {
95                         ASN1Type result = dec.decode();
96                         System.out.println("BER decoding:");
97                         System.out.println(result);
98                 } catch (ASN1DecodingException e) {
99                         e.printStackTrace();
100                 }
101
102         }
103
104         static void xerEncAndDec(ASN1Type data) {
105
106                 System.out.println("XER encoding:");
107                 ByteArrayOutputStream bo = new ByteArrayOutputStream();
108                 XerEncoder enc = new XerEncoder(bo);
109                 try {
110                         enc.encode(data);
111                 } catch (ASN1EncodingException e) {
112                         e.printStackTrace();
113                 }
114                 System.out.println(new String(bo.toByteArray()));
115
116                 ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
117                 XerDecoder dec = new XerDecoder(FrightStatusTypes.class, bi);
118                 try {
119                         ASN1Type result = dec.decode();
120                         System.out.println("XER decoding:");
121                         System.out.println(result);
122                 } catch (ASN1DecodingException e) {
123                         e.printStackTrace();
124                 }
125
126         }
127
128 }