MATSIM
TabularFileParserConfig.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * TabularFileParserConfig.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.core.utils.io.tabularFileParser;
22 
23 import java.net.URL;
24 import java.nio.charset.Charset;
25 import java.nio.charset.StandardCharsets;
26 
34 
35  // -------------------- CLASS VARIABLES --------------------
36 
37  private static final String ANYTHING = ".*";
38 
39  private static final String ANY_SPACE = "\\s*";
40 
41  // -------------------- INSTANCE VARIABLES --------------------
42 
43  private String file = null;
44 
45  private URL url = null;
46 
47  private String startRegex = null;
48 
49  private String endRegex = null;
50 
51  private String commentRegex = null;
52 
53  private String delimiterRegex = null;
54 
55  private Charset charset = StandardCharsets.UTF_8;
56 
57  // -------------------- CONSTRUCTION --------------------
58 
63  }
64 
65  // -------------------- SETTERS --------------------
66 
73  public void setFileName(String file) {
74  this.file = file;
75  }
76 
83  public void setUrl(URL url) {
84  this.url = url;
85  }
86 
92  public void setCharset(Charset charset) {
93  this.charset = charset;
94  }
95 
96  // ---------- DIRECT SETTING OF REGULAR EXPRESSIONS ----------
97 
106  public void setStartRegex(String regex) {
107  this.startRegex = regex;
108  }
109 
118  public void setEndRegex(String regex) {
119  this.endRegex = regex;
120  }
121 
130  public void setCommentRegex(String regex) {
131  this.commentRegex = regex;
132  }
133 
142  public void setDelimiterRegex(String regex) {
143  this.delimiterRegex = regex;
144  }
145 
146  // ---------- CREATION OF REGULAR EXPRESSIONS FROM TAGS ----------
147 
156  public void setStartTag(String tag) {
157  if (tag != null)
158  startRegex = quote(tag) + ANYTHING;
159  }
160 
169  public void setEndTag(String tag) {
170  if (tag != null)
171  endRegex = quote(tag) + ANYTHING;
172  }
173 
181  public void setCommentTags(String[] tags) {
182  commentRegex = alternativeExpr(tags) + ANYTHING;
183  }
184 
193  public void setDelimiterTags(String[] tags) {
194  if (tags == null || tags.length == 0)
195  delimiterRegex = null;
196  else
197  delimiterRegex = ANY_SPACE + alternativeExpr(tags) + ANY_SPACE;
198  }
199 
200  // ---------- GENERATION OF REGULAR EXPRESSIONS ----------
201 
202  private String alternativeExpr(String[] alternatives) {
203  StringBuilder result = new StringBuilder();
204 
205  if (alternatives != null)
206  for (int i = 0; i < alternatives.length; i++) {
207  result.append(quote(alternatives[i]));
208  if (i < alternatives.length - 1)
209  result.append('|');
210  }
211  return result.toString();
212  }
213 
214  private String quote(String expr) {
215  return "\\Q" + expr + "\\E";
216  }
217 
218  // -------------------- DATA ACCESS --------------------
219 
220  public String getFile() {
221  return file;
222  }
223 
224  public URL getUrl() {
225  return this.url;
226  }
227 
228  public String getStartRegex() {
229  return startRegex;
230  }
231 
232  public String getEndRegex() {
233  return endRegex;
234  }
235 
236  public String getCommentRegex() {
237  return commentRegex;
238  }
239 
240  public String getDelimiterRegex() {
241  return delimiterRegex;
242  }
243 
244  public Charset getCharset() {
245  return this.charset;
246  }
247 
248  // MISC
249 
250  @Override
251  public String toString() {
252  StringBuilder result = new StringBuilder(100);
253 
254  result.append("TabularFileParserConfig:\n\tfile=");
255  result.append(file);
256  result.append("\n\tstartRegex=");
257  result.append(startRegex);
258  result.append("\n\tendRegex=");
259  result.append(endRegex);
260  result.append("\n\tcommentRegex=");
261  result.append(commentRegex);
262  result.append("\n\tdelimiterRegex=");
263  result.append(delimiterRegex);
264 
265  return result.toString();
266  }
267 
268 }