RecordWriter.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. package com.yahoo.jute;
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import java.io.DataOutputStream;
  22. import java.io.InputStream;
  23. import java.io.UnsupportedEncodingException;
  24. import java.lang.reflect.InvocationTargetException;
  25. import java.lang.reflect.Method;
  26. import java.util.HashMap;
  27. import javax.xml.parsers.ParserConfigurationException;
  28. import org.xml.sax.SAXException;
  29. /**
  30. * Front-end for serializers. Also serves as a factory for serializers.
  31. *
  32. * @author Milind Bhandarkar
  33. */
  34. public class RecordWriter {
  35. private OutputArchive archive;
  36. static private OutputArchive getBinaryArchive(OutputStream out) {
  37. return new BinaryOutputArchive(new DataOutputStream(out));
  38. }
  39. static private OutputArchive getCsvArchive(OutputStream out)
  40. throws IOException {
  41. try {
  42. return new CsvOutputArchive(out);
  43. } catch (UnsupportedEncodingException ex) {
  44. throw new IOException("Unsupported encoding UTF-8");
  45. }
  46. }
  47. static private OutputArchive getXmlArchive(OutputStream out)
  48. throws IOException {
  49. return new XmlOutputArchive(out);
  50. }
  51. static HashMap constructFactory() {
  52. HashMap factory = new HashMap();
  53. Class[] params = { OutputStream.class };
  54. try {
  55. factory.put("binary",
  56. BinaryOutputArchive.class.getDeclaredMethod(
  57. "getArchive", params));
  58. factory.put("csv",
  59. CsvOutputArchive.class.getDeclaredMethod(
  60. "getArchive", params));
  61. factory.put("xml",
  62. XmlOutputArchive.class.getDeclaredMethod(
  63. "getArchive", params));
  64. } catch (SecurityException ex) {
  65. ex.printStackTrace();
  66. } catch (NoSuchMethodException ex) {
  67. ex.printStackTrace();
  68. }
  69. return factory;
  70. }
  71. static private HashMap archiveFactory = constructFactory();
  72. static private OutputArchive createArchive(OutputStream out,
  73. String format)
  74. throws IOException {
  75. Method factory = (Method) archiveFactory.get(format);
  76. if (factory != null) {
  77. Object[] params = { out };
  78. try {
  79. return (OutputArchive) factory.invoke(null, params);
  80. } catch (IllegalArgumentException ex) {
  81. ex.printStackTrace();
  82. } catch (InvocationTargetException ex) {
  83. ex.printStackTrace();
  84. } catch (IllegalAccessException ex) {
  85. ex.printStackTrace();
  86. }
  87. }
  88. return null;
  89. }
  90. /**
  91. * Creates a new instance of RecordWriter
  92. * @param out Output stream where the records will be serialized
  93. * @param format Serialization format ("binary", "xml", or "csv")
  94. */
  95. public RecordWriter(OutputStream out, String format)
  96. throws IOException {
  97. archive = createArchive(out, format);
  98. }
  99. /**
  100. * Serialize a record
  101. * @param r record to be serialized
  102. */
  103. public void write(Record r) throws IOException {
  104. r.serialize(archive, "");
  105. }
  106. }