OSDN Git Service

9c3d557ea6d22fc5e53110e00ee58aa2fed981ac
[bm-asn1/bm-asn1.git] / jp / bitmeister / asn1 / sample / FrightStatusMain.java
1 /*
2  * Copyright 2011-2012 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.ASN1Type;
34 import jp.bitmeister.asn1.type.builtin.INTEGER;
35 import jp.bitmeister.asn1.type.useful.UTCTime;
36 import jp.bitmeister.asn1.value.HexString;
37
38 public class FrightStatusMain {
39
40         public static void main(String[] args) {
41                 
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(new INTEGER(10))
69                                                 )
70                                 );
71
72                 System.out.println("Source data:");
73                 System.out.println(frights);
74                 derEncAndDec(frights);
75                 xerEncAndDec(frights);          
76         }
77
78         static void derEncAndDec(ASN1Type data) {
79                 System.out.println("BER encoding:");
80                 ByteArrayOutputStream bo = new ByteArrayOutputStream();
81                 BerEncoder enc = new DerEncoder(bo);
82                 try {
83                         enc.encode(data);
84                 } catch (ASN1EncodingException e) {
85                         e.printStackTrace();
86                 }
87
88                 System.out.println(new HexString(bo.toByteArray()));
89
90                 ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
91                 BerDecoder dec = new BerDecoder(FrightStatusTypes.class, bi);
92                 try {
93                         ASN1Type result = dec.decode();
94                         System.out.println("BER decoding:");
95                         System.out.println(result);
96                 } catch (ASN1DecodingException e) {
97                         e.printStackTrace();
98                 }
99
100         }
101
102         static void xerEncAndDec(ASN1Type data) {
103
104                 System.out.println("XER encoding:");
105                 ByteArrayOutputStream bo = new ByteArrayOutputStream();
106                 XerEncoder enc = new XerEncoder(bo);
107                 try {
108                         enc.encode(data);
109                 } catch (ASN1EncodingException e) {
110                         e.printStackTrace();
111                 }
112                 System.out.println(new String(bo.toByteArray()));
113
114                 ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
115                 XerDecoder dec = new XerDecoder(FrightStatusTypes.class, bi);
116                 try {
117                         ASN1Type result = dec.decode();
118                         System.out.println("XER decoding:");
119                         System.out.println(result);
120                 } catch (ASN1DecodingException e) {
121                         e.printStackTrace();
122                 }
123
124         }
125
126 }