1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 package org.apache.jute;
20 
21 import java.io.IOException;
22 import java.io.OutputStream;
23 import java.lang.reflect.InvocationTargetException;
24 import java.lang.reflect.Method;
25 import java.util.HashMap;
26 
27 /**
28  * Front-end for serializers. Also serves as a factory for serializers.
29  */
30 public class RecordWriter {
31 
32     private OutputArchive archive;
33 
constructFactory()34     static HashMap<String, Method> constructFactory() {
35         HashMap<String, Method> factory = new HashMap<String, Method>();
36 
37         try {
38             factory.put(
39                     "binary",
40                     BinaryOutputArchive.class.getDeclaredMethod("getArchive", OutputStream.class));
41         } catch (SecurityException | NoSuchMethodException ex) {
42             ex.printStackTrace();
43         }
44 
45         return factory;
46     }
47 
48     private static HashMap<String, Method> archiveFactory = constructFactory();
49 
createArchive(OutputStream out, String format)50     private static OutputArchive createArchive(OutputStream out, String format) {
51         Method factory = archiveFactory.get(format);
52         if (factory != null) {
53             Object[] params = {out};
54             try {
55                 return (OutputArchive) factory.invoke(null, params);
56             } catch (IllegalArgumentException | InvocationTargetException | IllegalAccessException ex) {
57                 ex.printStackTrace();
58             }
59         }
60         return null;
61     }
62 
63     /**
64      * Creates a new instance of RecordWriter.
65      *
66      * @param out    Output stream where the records will be serialized
67      * @param format Serialization format ("binary", "xml", or "csv")
68      */
RecordWriter(OutputStream out, String format)69     public RecordWriter(OutputStream out, String format) {
70         archive = createArchive(out, format);
71     }
72 
73     /**
74      * Serialize a record.
75      *
76      * @param r record to be serialized
77      */
write(Record r)78     public void write(Record r) throws IOException {
79         r.serialize(archive, "");
80     }
81 }
82