Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Tables |
|
| 0.0;0 |
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 | 0 | public class Tables { |
45 | ||
46 | 0 | private final List<String> names = new ArrayList<String>(); |
47 | private Pattern pattern; | |
48 | ||
49 | public boolean contains(String name) { | |
50 | 0 | if (pattern != null) { |
51 | 0 | return pattern.matcher(name).matches(); |
52 | } else { | |
53 | 0 | return names.contains(name); |
54 | } | |
55 | } | |
56 | ||
57 | /** | |
58 | * a comma separated list of the names of tables | |
59 | * <code><tablelist file="foo.csv"/></code> | |
60 | * | |
61 | * @param tableNames the file to read from | |
62 | */ | |
63 | public void setNames(String tableNames) { | |
64 | 0 | checkInputs(); |
65 | 0 | for (String name : tableNames.split(",")) { |
66 | 0 | names.add(name.trim()); |
67 | } | |
68 | 0 | } |
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><tablelist file="foo.csv"/></code> | |
74 | * | |
75 | * @param pattern a regular expression to compare against | |
76 | */ | |
77 | public void setPattern(String pattern) { | |
78 | 0 | checkInputs(); |
79 | 0 | this.pattern = Pattern.compile(pattern); |
80 | 0 | } |
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><tablelist file="foo.csv"/></code> | |
86 | * | |
87 | * @param file the file to read from | |
88 | */ | |
89 | public void setFile(File file) { | |
90 | 0 | checkInputs(); |
91 | try { | |
92 | 0 | BufferedReader reader = new BufferedReader(new FileReader(file)); |
93 | 0 | for (String line = reader.readLine(); line != null; line = reader.readLine()) { |
94 | 0 | line = line.trim(); |
95 | 0 | if (line.length() > 0 && !line.startsWith("--")) { |
96 | 0 | names.add(line); |
97 | } | |
98 | } | |
99 | 0 | } catch (IOException e) { |
100 | 0 | throw new BuildException("e"); |
101 | 0 | } |
102 | 0 | } |
103 | ||
104 | private void checkInputs() { | |
105 | 0 | if (!names.isEmpty() || pattern != null) { |
106 | 0 | throw new IllegalArgumentException("must specify exactly one of file, pattern, or names"); |
107 | } | |
108 | 0 | } |
109 | ||
110 | public static List<String> narrow(List<String> allTableNames, List<Tables> includes, List<Tables> excludes) { | |
111 | 0 | List<String> tableNames = new ArrayList<String>(); |
112 | 0 | if (includes.isEmpty()) { |
113 | 0 | tableNames.addAll(allTableNames); |
114 | 0 | } else { |
115 | 0 | for (Tables tables : includes) { |
116 | 0 | for (String name : allTableNames) { |
117 | 0 | if (tables.contains(name)) tableNames.add(name); |
118 | 0 | } |
119 | 0 | } |
120 | } | |
121 | ||
122 | 0 | for (Tables tables : excludes) { |
123 | 0 | Iterator<String> namesIterator = tableNames.iterator(); |
124 | 0 | while (namesIterator.hasNext()) { |
125 | 0 | if (tables.contains(namesIterator.next())) { |
126 | 0 | namesIterator.remove(); |
127 | 0 | } |
128 | } | |
129 | ||
130 | 0 | } |
131 | 0 | return tableNames; |
132 | } | |
133 | } |