MATSIM
TabularFileParser.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * TabularFileParser.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.io.BufferedReader;
24 import java.io.IOException;
25 
27 import org.matsim.core.utils.io.IOUtils;
28 
34 public class TabularFileParser implements MatsimSomeReader {
35 
37 
38  private boolean isStart(String line) {
39  final String regex = this.config.getStartRegex();
40  if (regex == null)
41  return true;
42  return line.matches(regex);
43  }
44 
45  private boolean isEnd(String line) {
46  final String regex = this.config.getEndRegex();
47  if (regex == null)
48  return false;
49  return line.matches(regex);
50  }
51 
52  private boolean isComment(String line) {
53  final String regex = this.config.getCommentRegex();
54  if (regex == null)
55  return false;
56  return line.matches(regex);
57  }
58 
59  private String[] split(String line) {
60  final String regex = this.config.getDelimiterRegex();
61  if (regex == null)
62  return new String[] { line };
63  return line.split(regex);
64  }
65 
85  public void parse(TabularFileParserConfig config,
86  TabularFileHandler handler) {
87  if (config == null)
88  throw new NullPointerException(
89  "TabularFileParser requires a non-null configuration.");
90  if (handler == null)
91  throw new NullPointerException(
92  "TabularFileParser requires a non-null handler.");
93 
94  this.config = config;
95 
96  boolean started = (config.getStartRegex() == null);
97  boolean ended = false;
98 
99  try ( BufferedReader reader = config.getUrl() == null ? IOUtils.getBufferedReader(IOUtils.resolveFileOrResource(config.getFile()), config.getCharset()) : IOUtils.getBufferedReader(config.getUrl(), config.getCharset()) ) {
100  String line;
101  while ((line = reader.readLine()) != null && !ended) {
102  if (started) {
103  ended = isEnd(line);
104  if (!ended && !isComment(line)) {
105  handler.startRow(split(line));
106  }
107  } else {
108  started = isStart(line);
109  }
110  }
111  } catch (IOException e) {
112  throw new RuntimeException(e);
113  }
114 
115  // try-with-resources does not need the following any more. kai, may'15
116 // finally {
117 // try {
118 // reader.close();
119 // } catch (IOException e) {
120 // throw new RuntimeException(e);
121 // }
122 // }
123  }
124 
125 }
void parse(TabularFileParserConfig config, TabularFileHandler handler)
static BufferedReader getBufferedReader(URL url, Charset charset)
Definition: IOUtils.java:321
static URL resolveFileOrResource(String filename)
Definition: IOUtils.java:207