PageRenderTime 57ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/org/beanio/internal/parser/BeanWriterImpl.java

http://beanio.googlecode.com/
Java | 161 lines | 83 code | 20 blank | 58 comment | 8 complexity | bffed7dcbcbc9a20b8e758f3d0a81cbb MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Copyright 2011-2012 Kevin Seim
  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 org.beanio.internal.parser;
  17. import java.io.IOException;
  18. import java.util.Map;
  19. import org.beanio.*;
  20. import org.beanio.internal.util.StatefulWriter;
  21. import org.beanio.stream.RecordWriter;
  22. /**
  23. * A {@link BeanReader} implementation.
  24. *
  25. * @author Kevin Seim
  26. * @since 2.0
  27. */
  28. public class BeanWriterImpl implements BeanWriter, StatefulWriter {
  29. private Selector layout;
  30. private MarshallingContext context;
  31. /**
  32. * Constructs a new <tt>BeanWriterImpl</tt>.
  33. * @param context the {@link MarshallingContext}
  34. * @param layout the root {@link Selector} node in the parsing tree
  35. */
  36. public BeanWriterImpl(MarshallingContext context, Selector layout) {
  37. this.context = context;
  38. this.layout = layout;
  39. }
  40. /*
  41. * (non-Javadoc)
  42. * @see org.beanio.BeanWriter#write(java.lang.Object)
  43. */
  44. public void write(Object bean) throws BeanWriterException {
  45. write(null, bean);
  46. }
  47. /*
  48. * (non-Javadoc)
  49. * @see org.beanio.BeanWriter#write(java.lang.String, java.lang.Object)
  50. */
  51. public void write(String recordName, Object bean) throws BeanWriterException {
  52. ensureOpen();
  53. if (bean == null) {
  54. throw new BeanWriterException("null bean object");
  55. }
  56. try {
  57. // set the name of the component to be marshalled (may be null if we're just matching on bean)
  58. context.setComponentName(recordName);
  59. // set the bean to be marshalled on the context
  60. context.setBean(bean);
  61. // find the parser in the layout that defines the given bean
  62. Selector matched = layout.matchNext(context);
  63. if (matched == null) {
  64. throw new BeanWriterException("Bean identification failed: No record or group mapping for class '"
  65. + bean.getClass() + "' at the current stream position");
  66. }
  67. // marshal the bean object
  68. matched.marshal(context);
  69. }
  70. catch (IOException e) {
  71. throw new BeanWriterIOException(e);
  72. }
  73. catch (BeanWriterException ex) {
  74. throw ex;
  75. }
  76. catch (BeanIOException ex) {
  77. // wrap the generic exception in a BeanReaderException
  78. throw new BeanWriterException("Fatal BeanIOException caught", ex);
  79. }
  80. }
  81. /*
  82. * (non-Javadoc)
  83. * @see org.beanio.BeanWriter#flush()
  84. */
  85. public void flush() throws BeanWriterIOException {
  86. ensureOpen();
  87. try {
  88. context.getRecordWriter().flush();
  89. }
  90. catch (IOException e) {
  91. throw new BeanWriterIOException(e);
  92. }
  93. }
  94. /*
  95. * (non-Javadoc)
  96. * @see org.beanio.BeanWriter#close()
  97. */
  98. public void close() throws BeanWriterIOException {
  99. ensureOpen();
  100. try {
  101. context.getRecordWriter().close();
  102. }
  103. catch (IOException e) {
  104. throw new BeanWriterIOException(e);
  105. }
  106. finally {
  107. context = null;
  108. layout = null;
  109. }
  110. }
  111. /*
  112. * Throws an exception if the stream has already been closed.
  113. */
  114. private void ensureOpen() {
  115. if (context == null) {
  116. throw new BeanWriterIOException("Stream closed");
  117. }
  118. }
  119. /*
  120. * (non-Javadoc)
  121. * @see org.beanio.parser.StatefulBeanWriter#updateState(java.lang.String, java.util.Map)
  122. */
  123. public void updateState(String namespace, Map<String, Object> state) {
  124. layout.updateState(context, namespace + ".m", state);
  125. RecordWriter writer = context.getRecordWriter();
  126. if (writer instanceof StatefulWriter) {
  127. ((StatefulWriter)writer).updateState(namespace + ".w", state);
  128. }
  129. }
  130. /*
  131. * (non-Javadoc)
  132. * @see org.beanio.parser.StatefulBeanWriter#restoreState(java.lang.String, java.util.Map)
  133. */
  134. public void restoreState(String namespace, Map<String, Object> state) throws IllegalStateException {
  135. layout.restoreState(context, namespace + ".m", state);
  136. RecordWriter writer = context.getRecordWriter();
  137. if (writer instanceof StatefulWriter) {
  138. ((StatefulWriter)writer).restoreState(namespace + ".w", state);
  139. }
  140. }
  141. }