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.FileNotFoundException;
37  import java.io.InputStream;
38  import java.io.Reader;
39  import java.sql.Connection;
40  import java.sql.SQLException;
41  import java.util.List;
42  
43  import net.sf.flatpack.structure.ColumnMetaData;
44  import net.sf.flatpack.util.ParserUtils;
45  
46  /**
47   * @author xhensevb
48   * @author zepernick
49   *
50   */
51  public class DBFixedLengthParser extends AbstractFixedLengthParser {
52      private final Connection con;
53  
54      // this InputStream and file can be removed after support for
55      // file and inputstream is removed from the parserfactory. The
56      // methods have been deprecated..pz
57      private InputStream dataSourceStream = null;
58  
59      private File dataSource = null;
60  
61      public DBFixedLengthParser(final Connection con, final InputStream dataSourceStream, final String dataDefinition) {
62          // Reader will be setup in the init(), passing null for now.
63          // this constructor will eventually be deleted
64          super(null, dataDefinition);
65          this.con = con;
66          this.dataSourceStream = dataSourceStream;
67      }
68  
69      public DBFixedLengthParser(final Connection con, final File dataSource, final String dataDefinition) {
70          super(null, dataDefinition);
71          this.con = con;
72          this.dataSource = dataSource;
73      }
74  
75      public DBFixedLengthParser(final Connection con, final Reader dataSourceReader, final String dataDefinition) {
76          super(dataSourceReader, dataDefinition);
77          this.con = con;
78      }
79  
80      @Override
81      protected void init() {
82          try {
83              // check to see if the user is using a File or InputStream. This is
84              // here for backwards compatability
85              initStreamOrSource(dataSourceStream, dataSource);
86  
87              final List<ColumnMetaData> cmds = ParserUtils.buildMDFromSQLTable(con, getDataDefinition(), this);
88              addToMetaData(cmds);
89  
90              if (cmds.isEmpty()) {
91                  throw new FileNotFoundException("DATA DEFINITION CAN NOT BE FOUND IN THE DATABASE " + getDataDefinition());
92              }
93  
94              setInitialised(true);
95          } catch (final SQLException | FileNotFoundException e) {
96              throw new InitialisationException(e);
97          }
98      }
99  }