📄 csvtupleparser.java
字号:
/* * @(#)$Id: CSVTupleParser.java,v 1.5 2004/07/02 23:59:21 huebsch Exp $ * * Copyright (c) 2001-2004 Regents of the University of California. * All rights reserved. * * This file is distributed under the terms in the attached BERKELEY-LICENSE * file. If you do not find these files, copies can be found by writing to: * Computer Science Division, Database Group, Universite of California, * 617 Soda Hall #1776, Berkeley, CA 94720-1776. Attention: Berkeley License * * Copyright (c) 2003-2004 Intel Corporation. All rights reserved. * * This file is distributed under the terms in the attached INTEL-LICENSE file. * If you do not find these files, copies can be found by writing to: * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, * Berkeley, CA, 94704. Attention: Intel License Inquiry. */package pier.helpers.tupleparsers;import pier.data.Field;import pier.data.Tuple;import pier.exceptions.ReaderException;import util.EasyParser;/** * Parser a PlanetLab-style Comma-Separated-Value (CSV) format stream * from (for example) a PlanetLab sensor. This class also handles the * HTTP headers, and indeed assumes that they will be present at the * head of the input. If they're not, clients should feed a blank * line (e.g. "CRLFCRLF") before feeding the input. */public class CSVTupleParser implements TupleParser { private static final String DATA_DELIMITER = ","; private static final String COMMENT_DELIMITER = "#"; protected final static String NEWLINE = "(\\r)?\\n"; private static final String NULL_VALUE = "\\N"; private TupleParserClient client; private String formatString; private int expectedNumFields; private String tableName; private String columnPrefix; private Tuple tuple; private EasyParser parser; /** * Constructor CSVReader * * @param client * @param formatString * @param tableName * @param columnPrefix */ public CSVTupleParser(TupleParserClient client, String formatString, String tableName, String columnPrefix) { this.client = client; this.formatString = formatString; this.expectedNumFields = formatString.length(); this.tableName = tableName; this.columnPrefix = columnPrefix; this.tuple = Tuple.allocate(tableName); this.parser = new EasyParser(); } /** * Method read * * @param theString */ public void read(String theString) { String splitResults[]; parser.addToBuffer(theString); boolean continueLoop = true; while (continueLoop) { continueLoop = false; splitResults = parser.match(NEWLINE); if (splitResults != null) { continueLoop = true; String dataLine = splitResults[EasyParser.PRE_PATTERN]; if (dataLine.startsWith(COMMENT_DELIMITER)) { // Disregard line } else { String fields[] = dataLine.split(DATA_DELIMITER, 0); int numFields = fields.length; if (numFields < expectedNumFields) { client.handleTupleParserError( new ReaderException( "Too few fields found " + numFields + "/" + expectedNumFields)); } else { for (int i = 0; i < numFields; i++) { createField(unescapeString(fields[i]), i); } finishTuple(); } } } } } /** * Method escapeString * * @param string * @return */ protected String unescapeString(String string) { // Convert: \N -> null string if (string.equals(NULL_VALUE)) { return null; } // Convert: \\ -> \ string = string.replaceAll("\\\\\\\\", "\\\\"); // Convert: \DELIMITER -> DELIMITER string = string.replaceAll("\\\\" + DATA_DELIMITER, DATA_DELIMITER); // Convert: \b -> [backspace ASCII 8] string = string.replaceAll("\\\\b", "\\u0008"); // Convert: \f -> [form feed ASCII 12] string = string.replaceAll("\\\\f", "\\f"); // Convert: \n -> [newline ASCII 10] string = string.replaceAll("\\\\n", "\\n"); // Convert: \r -> [carriage return ASCII 13] string = string.replaceAll("\\\\r", "\\r"); // Convert: \t -> [tab ASCII 9] string = string.replaceAll("\\\\t", "\\t"); // Convert: \v -> [vertical tab ASCII 11] string = string.replaceAll("\\u000B", "\\\\v"); return string; } /** * Method createField * * @param value * @param fieldNum */ protected void createField(String value, int fieldNum) { if ((formatString == null) || (fieldNum >= formatString.length()) || (formatString.charAt(fieldNum) == 's')) { try { addField(value, fieldNum); } catch (Exception exception) { client.handleTupleParserError(exception); return; } } else if (formatString.charAt(fieldNum) == 'i') { try { addField(Integer.valueOf(value.trim()), fieldNum); } catch (Exception exception) { client.handleTupleParserError(exception); return; } } else if (formatString.charAt(fieldNum) == 'f') { try { addField(Float.valueOf(value.trim()), fieldNum); } catch (Exception exception) { client.handleTupleParserError(exception); return; } } else { try { addField(value, fieldNum); } catch (Exception exception) { client.handleTupleParserError(exception); return; } } } /** * Method addField * * @param value * @param fieldNum */ protected void addField(Object value, int fieldNum) { int fieldNumForTuple = fieldNum + 1; Field field = Field.allocate(columnPrefix + fieldNumForTuple, value); tuple.addField(field); } /** * Method finishTuple */ protected void finishTuple() { client.handleTuple(tuple); tuple = Tuple.allocate(tableName); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -