1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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><tablelist file="foo.csv"/></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><tablelist file="foo.csv"/></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><tablelist file="foo.csv"/></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 }