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   *
46   */
47  public class DelimiterParser extends AbstractDelimiterParser {
48      private InputStream pzmapXMLStream = null;
49      private File pzmapXML = null;
50      private Reader pzmapReader;
51  
52      // this InputStream and file can be removed after support for
53      // file and inputstream is removed from the parserfactory. The
54      // methods have been deprecated..pz
55      private InputStream dataSourceStream = null;
56      private File dataSource = null;
57  
58      public DelimiterParser(final File pzmapXML, final File dataSource, final char delimiter, final char qualifier, final boolean ignoreFirstRecord) {
59          super(null, delimiter, qualifier, ignoreFirstRecord);
60          this.pzmapXML = pzmapXML;
61          this.dataSource = dataSource;
62      }
63  
64      public DelimiterParser(final InputStream pzmapXMLStream, final InputStream dataSourceStream, final char delimiter, final char qualifier,
65              final boolean ignoreFirstRecord) {
66          super(null, delimiter, qualifier, ignoreFirstRecord);
67          this.pzmapXMLStream = pzmapXMLStream;
68          this.dataSourceStream = dataSourceStream;
69      }
70  
71      public DelimiterParser(final File dataSource, final char delimiter, final char qualifier, final boolean ignoreFirstRecord) {
72          super(null, delimiter, qualifier, ignoreFirstRecord);
73          this.dataSource = dataSource;
74      }
75  
76      public DelimiterParser(final InputStream dataSourceStream, final char delimiter, final char qualifier, final boolean ignoreFirstRecord) {
77          super(null, delimiter, qualifier, ignoreFirstRecord);
78          this.dataSourceStream = dataSourceStream;
79      }
80  
81      public DelimiterParser(final Reader dataSourceReader, final char delimiter, final char qualifier, final boolean ignoreFirstRecord) {
82          super(dataSourceReader, delimiter, qualifier, ignoreFirstRecord);
83      }
84  
85      public DelimiterParser(final Reader dataSourceReader, final Reader pzmapReader, final char delimiter, final char qualifier,
86              final boolean ignoreFirstRecord) {
87          super(dataSourceReader, delimiter, qualifier, ignoreFirstRecord);
88          this.pzmapReader = pzmapReader;
89      }
90  
91      @Override
92      protected void init() {
93          try {
94              // check to see if the user is using a File or InputStream. This is
95              // here for backwards compatibility
96              initStreamOrSource(dataSourceStream, dataSource);
97  
98              boolean closeMapReader = false;
99              if (pzmapXML != null) {
100                 this.pzmapReader = new FileReader(pzmapXML);
101                 closeMapReader = true;
102             } else if (pzmapXMLStream != null) {
103                 this.pzmapReader = new InputStreamReader(pzmapXMLStream);
104                 closeMapReader = true;
105             }
106 
107             if (this.pzmapReader != null) {
108                 try {
109                     setPzMetaData(MapParser.parseMap(this.pzmapReader, this));
110                 } finally {
111                     if (closeMapReader) {
112                         // only close the reader if it is one we created
113                         // otherwise we will let the user handle it
114                         this.pzmapReader.close();
115                     }
116                 }
117             }
118 
119             setInitialised(true);
120         } catch (final Exception e) {
121             throw new InitialisationException(e);
122         }
123     }
124 
125     @Override
126     protected boolean shouldCreateMDFromFile() {
127         return pzmapReader == null;
128     }
129 }