View Javadoc
1   /*
2    * ObjectLab, http://www.objectlab.co.uk/open is supporting FlatPack.
3    *
4    * Based in London, we are world leaders in the design and development
5    * of bespoke applications for the securities financing markets.
6    *
7    * <a href="http://www.objectlab.co.uk/open">Click here to learn more</a>
8    *           ___  _     _           _   _          _
9    *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
10   *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
11   *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
12   *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
13   *                   |__/
14   *
15   *                     www.ObjectLab.co.uk
16   *
17   * $Id: ColorProvider.java 74 2006-10-24 22:19:05Z benoitx $
18   *
19   * Copyright 2006 the original author or authors.
20   *
21   * Licensed under the Apache License, Version 2.0 (the "License"); you may not
22   * use this file except in compliance with the License. You may obtain a copy of
23   * the License at
24   *
25   * http://www.apache.org/licenses/LICENSE-2.0
26   *
27   * Unless required by applicable law or agreed to in writing, software
28   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
29   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
30   * License for the specific language governing permissions and limitations under
31   * the License.
32   */
33  package net.sf.flatpack;
34  
35  import java.io.File;
36  import java.io.FileReader;
37  import java.io.InputStream;
38  import java.io.InputStreamReader;
39  import java.io.Reader;
40  
41  import net.sf.flatpack.xml.MapParser;
42  
43  /**
44   * @author xhensevb
45   * @author zepernick
46   *
47   */
48  public class FixedLengthParser extends AbstractFixedLengthParser {
49      private InputStream pzmapXMLStream;
50  
51      private File pzmapXML;
52  
53      private Reader pzmapReader;
54  
55      // this InputStream and file can be removed after support for
56      // file and inputstream is removed from the parserfactory. The
57      // methods have been deprecated..pz
58      private InputStream dataSourceStream = null;
59  
60      private File dataSource = null;
61  
62      public FixedLengthParser(final InputStream pzmapXMLStream, final InputStream dataSourceStream) {
63          super(null);
64          this.pzmapXMLStream = pzmapXMLStream;
65          this.dataSourceStream = dataSourceStream;
66      }
67  
68      public FixedLengthParser(final File pzmapXML, final File dataSource) {
69          super(null);
70          this.pzmapXML = pzmapXML;
71          this.dataSource = dataSource;
72      }
73  
74      public FixedLengthParser(final Reader pzmapReader, final Reader dataSourceReader) {
75          super(dataSourceReader);
76          this.pzmapReader = pzmapReader;
77      }
78  
79      protected FixedLengthParser(final Reader dataSourceReader, final String dataDefinition) {
80          super(dataSourceReader, dataDefinition);
81      }
82  
83      @Override
84      protected void init() {
85          try {
86              // check to see if the user is using a File or InputStream. This is
87              // here for backwards compatability
88              initStreamOrSource(dataSourceStream, dataSource);
89  
90              boolean closeMapReader = false;
91              if (pzmapXML != null) {
92                  this.pzmapReader = new FileReader(pzmapXML);
93                  closeMapReader = true;
94              } else if (pzmapXMLStream != null) {
95                  this.pzmapReader = new InputStreamReader(pzmapXMLStream);
96                  closeMapReader = true;
97              }
98  
99              try {
100                 setPzMetaData(MapParser.parseMap(this.pzmapReader, this));
101             } finally {
102                 if (closeMapReader) {
103                     // only close the reader if it is one we created
104                     // otherwise we will let the user handle it
105                     this.pzmapReader.close();
106                 }
107             }
108         } catch (final Exception e) {
109             throw new InitialisationException(e);
110         }
111     }
112 }