summaryrefslogtreecommitdiff
path: root/src/de/lmu/ifi/dbs/elki/datasource/parser/AbstractParser.java
blob: 3c294ca46e4b4a68b856c96fa0912ed376ae135f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package de.lmu.ifi.dbs.elki.datasource.parser;

/*
 This file is part of ELKI:
 Environment for Developing KDD-Applications Supported by Index-Structures

 Copyright (C) 2012
 Ludwig-Maximilians-Universität München
 Lehr- und Forschungseinheit für Datenbanksysteme
 ELKI Development Team

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import de.lmu.ifi.dbs.elki.logging.Logging;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.AbstractParameterizer;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.OptionID;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.constraints.StringLengthConstraint;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.Parameterization;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.PatternParameter;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.StringParameter;

/**
 * Abstract superclass for all parsers providing the option handler for handling
 * options.
 * 
 * @author Arthur Zimek
 */
public abstract class AbstractParser {
  /**
   * A pattern defining whitespace.
   */
  public static final String DEFAULT_SEPARATOR = "(\\s+|\\s*[,;]\\s*)";

  /**
   * A quote pattern
   */
  public static final char QUOTE_CHAR = '\"';

  /**
   * A pattern catching most numbers that can be parsed using Double.parseDouble:
   * 
   * Some examples: <code>1</code> <code>1.</code> <code>1.2</code>
   * <code>.2</code> <code>-.2e-03</code>
   */
  public static final String NUMBER_PATTERN = "[+-]?(?:\\d+\\.?|\\d*\\.\\d+)?(?:[eE][-]?\\d+)?";

  /**
   * OptionID for the column separator parameter (defaults to whitespace as in
   * {@link #DEFAULT_SEPARATOR}.
   */
  public static final OptionID COLUMN_SEPARATOR_ID = OptionID.getOrCreateOptionID("parser.colsep", "Column separator pattern. The default assumes whitespace separated data.");

  /**
   * OptionID for the quote character parameter (defaults to a double quotation
   * mark as in {@link #QUOTE_CHAR}.
   */
  public static final OptionID QUOTE_ID = OptionID.getOrCreateOptionID("parser.quote", "Quotation character. The default is to use a double quote.");

  /**
   * Stores the column separator pattern
   */
  private Pattern colSep = null;

  /**
   * Stores the quotation character
   */
  protected char quoteChar = QUOTE_CHAR;

  /**
   * The comment character.
   */
  public static final String COMMENT = "#";

  /**
   * A sign to separate attributes.
   */
  public static final String ATTRIBUTE_CONCATENATION = " ";

  /**
   * Constructor.
   * 
   * @param colSep Column separator
   * @param quoteChar Quote character
   */
  public AbstractParser(Pattern colSep, char quoteChar) {
    super();
    this.colSep = colSep;
    this.quoteChar = quoteChar;
  }

  /**
   * Tokenize a string. Works much like colSep.split() except it honors
   * quotation characters.
   * 
   * @param input Input string
   * @return Tokenized string
   */
  protected List<String> tokenize(String input) {
    ArrayList<String> matchList = new ArrayList<String>();
    Matcher m = colSep.matcher(input);

    int index = 0;
    boolean inquote = (input.length() > 0) && (input.charAt(0) == quoteChar);
    while(m.find()) {
      // Quoted code path vs. regular code path
      if(inquote && m.start() > 0) {
        // Closing quote found?
        if(m.start() > index + 1 && input.charAt(m.start() - 1) == quoteChar) {
          // Strip quote characters
          if (index + 1 < m.start() - 1) {
            matchList.add(input.substring(index + 1, m.start() - 1));
          }
          // Seek past
          index = m.end();
          // new quote?
          inquote = (index < input.length()) && (input.charAt(index) == quoteChar);
        }
      }
      else {
        // Add match before separator
        if (index < m.start()) {
          matchList.add(input.substring(index, m.start()));
        }
        // Seek past separator
        index = m.end();
        // new quote?
        inquote = (index < input.length()) && (input.charAt(index) == quoteChar);
      }
    }
    // Nothing found - return original string.
    if(index == 0) {
      matchList.add(input);
      return matchList;
    }
    // Add tail after last separator.
    if(inquote) {
      if(input.charAt(input.length() - 1) == quoteChar) {
        if (index + 1 < input.length() - 1) {
          matchList.add(input.substring(index + 1, input.length() - 1));
        }
      }
      else {
        getLogger().warning("Invalid quoted line in input.");
        if (index < input.length()) {
          matchList.add(input.substring(index, input.length()));
        }
      }
    }
    else {
      if (index < input.length()) {
        matchList.add(input.substring(index, input.length()));
      }
    }
    // Return
    return matchList;
  }

  /**
   * Get the logger for this class.
   * 
   * @return Logger.
   */
  protected abstract Logging getLogger();

  /**
   * Returns a string representation of the object.
   * 
   * @return a string representation of the object.
   */
  @Override
  public String toString() {
    return getClass().getName();
  }

  /**
   * Parameterization class.
   * 
   * @author Erich Schubert
   * 
   * @apiviz.exclude
   */
  public static abstract class Parameterizer extends AbstractParameterizer {
    /**
     * Stores the column separator pattern
     */
    protected Pattern colSep = null;

    /**
     * Stores the quotation character
     */
    protected char quoteChar = QUOTE_CHAR;

    @Override
    protected void makeOptions(Parameterization config) {
      super.makeOptions(config);
      PatternParameter colParam = new PatternParameter(COLUMN_SEPARATOR_ID, DEFAULT_SEPARATOR);
      if(config.grab(colParam)) {
        colSep = colParam.getValue();
      }
      StringParameter quoteParam = new StringParameter(QUOTE_ID, new StringLengthConstraint(1, 1), ""+QUOTE_CHAR);
      if(config.grab(quoteParam)) {
        quoteChar = quoteParam.getValue().charAt(0);
      }
    }

    @Override
    protected abstract AbstractParser makeInstance();
  }
}