View Javadoc

1   //
2   // Copyright (c) 2004, International Decision Systems
3   // all rights reserved
4   /*
5    * Copyright (c) 2004 International Decision Systems, Inc.  All Rights Reserved.
6    *
7    * By using this Software, You acknowledge that the Software is a valuable asset
8    * and trade secret of either International Decision Systems, Inc. ("IDSI") or a
9    * third party supplier of IDSI and constitutes confidential and proprietary
10   * information.
11   *
12   * NEITHER IDSI NOR ANY AGENT OR PERSON ACTING FOR OR WITH IDSI HAS MADE OR DOES
13   * MAKE ANY STATEMENTS, AFFIRMATIONS, REPRESENTATIONS OR WARRANTIES WHATSOEVER
14   * TO YOU, WHETHER EXPRESS OR IMPLIED, AS TO THE SOFTWARE, THE QUALITY OR
15   * CONDITION OF THE SOFTWARE, OR THE OPERATING CHARACTERISTICS OR RELIABILITY OF
16   * THE SOFTWARE, OR ITS SUITABILITY FOR ANY GENERAL OR PARTICULAR PURPOSE, OR AS
17   * TO ANY OTHER MATTER WHATSOEVER; ANY AND ALL OTHER WARRANTIES INCLUDING
18   * WITHOUT LIMITATION ANY WARRANTIES IMPLIED BY LAW, SUCH AS THE IMPLIED
19   * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND TITLE,
20   * USE AND NON-INFRINGEMENT; ARE HEREBY EXPRESSLY DISCLAIMED AND EXCLUDED.
21  */
22  package net.sourceforge.addam.impexp;
23  
24  import net.sourceforge.addam.util.TableUtil;
25  
26  import org.apache.tools.ant.BuildException;
27  
28  import java.io.BufferedReader;
29  import java.io.File;
30  import java.io.FileReader;
31  import java.io.IOException;
32  import java.util.ArrayList;
33  import java.util.List;
34  import java.util.Iterator;
35  import java.util.regex.Pattern;
36  
37  
38  /**
39   * Provides a list of tables and associated csv files for import or export.
40   *
41   * @author TIM3
42   * @since Aug 2, 2004
43   */
44  public class Tables {
45  
46      private final List<String> names = new ArrayList<String>();
47      private Pattern pattern;
48  
49      public boolean contains(String name) {
50          if (pattern != null) {
51              return pattern.matcher(name).matches();
52          } else {
53              return names.contains(name);
54          }
55      }
56  
57      /**
58       * a comma separated list of the names of tables
59       * <code>&lt;tablelist file="foo.csv"/&gt;</code>
60       *
61       * @param tableNames the file to read from
62       */
63      public void setNames(String tableNames) {
64          checkInputs();
65          for (String name : tableNames.split(",")) {
66              names.add(name.trim());
67          }
68      }
69  
70      /**
71       * sets the file to read table names from; this is used when the tablelist is
72       * provided as an element, i.e.
73       * <code>&lt;tablelist file="foo.csv"/&gt;</code>
74       *
75       * @param pattern a regular expression to compare against
76       */
77      public void setPattern(String pattern) {
78          checkInputs();
79          this.pattern = Pattern.compile(pattern);
80      }
81  
82      /**
83       * sets the file to read table names from; this is used when the tablelist is
84       * provided as an element, i.e.
85       * <code>&lt;tablelist file="foo.csv"/&gt;</code>
86       *
87       * @param file the file to read from
88       */
89      public void setFile(File file) {
90          checkInputs();
91          try {
92              BufferedReader reader = new BufferedReader(new FileReader(file));
93              for (String line = reader.readLine(); line != null; line = reader.readLine()) {
94                  line = line.trim();
95                  if (line.length() > 0 && !line.startsWith("--")) {
96                      names.add(line);
97                  }
98              }
99          } catch (IOException e) {
100             throw new BuildException("e");
101         }
102     }
103 
104     private void checkInputs() {
105         if (!names.isEmpty() || pattern != null) {
106             throw new IllegalArgumentException("must specify exactly one of file, pattern, or names");
107         }
108     }
109 
110     public static List<String> narrow(List<String> allTableNames, List<Tables> includes, List<Tables> excludes) {
111         List<String> tableNames = new ArrayList<String>();
112         if (includes.isEmpty()) {
113             tableNames.addAll(allTableNames);
114         } else {
115             for (Tables tables : includes) {
116                 for (String name : allTableNames) {
117                     if (tables.contains(name)) tableNames.add(name);
118                 }
119             }
120         }
121 
122         for (Tables tables : excludes) {
123             Iterator<String> namesIterator = tableNames.iterator();
124             while (namesIterator.hasNext()) {
125                 if (tables.contains(namesIterator.next())) {
126                     namesIterator.remove();
127                 }
128             }
129 
130         }
131         return tableNames;
132     }
133 }