Template of Windows TypePerf cmd and Impl a CSVParser class+test
This commit is contained in:
parent
216f6dcb9b
commit
315932070a
11 changed files with 349 additions and 18 deletions
110
src/zutil/parser/CSVParser.java
Normal file
110
src/zutil/parser/CSVParser.java
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
/*
|
||||
* Copyright (c) 2015 ezivkoc
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package zutil.parser;
|
||||
|
||||
import zutil.struct.MutableInt;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
|
||||
/**
|
||||
* Created by Ziver
|
||||
*/
|
||||
public class CSVParser extends Parser{
|
||||
|
||||
private Reader in;
|
||||
private char delimiter;
|
||||
private boolean parseHeader;
|
||||
|
||||
private DataNode headers;
|
||||
|
||||
|
||||
public CSVParser(Reader in){
|
||||
this(in, false, ',');
|
||||
}
|
||||
public CSVParser(Reader in, boolean inclusedHeader){
|
||||
this(in, inclusedHeader, ',');
|
||||
}
|
||||
public CSVParser(Reader in, boolean includesHeader, char delimiter){
|
||||
this.in = in;
|
||||
this.delimiter = delimiter;
|
||||
this.parseHeader = includesHeader;
|
||||
}
|
||||
|
||||
public DataNode getHeaders() {
|
||||
return headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts parsing from a string
|
||||
*
|
||||
* @param csv is the JSON String to parse
|
||||
* @return a DataNode object representing the JSON in the input String
|
||||
*/
|
||||
public static DataNode read(String csv){
|
||||
try{
|
||||
return new CSVParser(new StringReader(csv)).read();
|
||||
}catch (IOException e){
|
||||
e.printStackTrace();
|
||||
}catch (NullPointerException e){}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Starts parsing from the input.
|
||||
* This method will block until one row has been parsed.
|
||||
*
|
||||
* @return a DataNode object representing a row in the CSV
|
||||
*/
|
||||
@Override
|
||||
public DataNode read() throws IOException {
|
||||
if(parseHeader) {
|
||||
parseHeader = false;
|
||||
headers = read();
|
||||
}
|
||||
|
||||
DataNode data = new DataNode(DataNode.DataType.List);
|
||||
StringBuilder value = new StringBuilder();
|
||||
boolean quoteStarted = false;
|
||||
int c;
|
||||
while((c=in.read()) >= 0 && c != '\n'){
|
||||
if(c == delimiter && !quoteStarted){
|
||||
data.add(value.toString());
|
||||
value.delete(0, value.length()); // Reset StringBuilder
|
||||
}
|
||||
else if(c == '\"' && // Ignored quotes
|
||||
(value.length() == 0 || quoteStarted)){
|
||||
quoteStarted = !quoteStarted;
|
||||
}
|
||||
else
|
||||
value.append((char)c);
|
||||
}
|
||||
if(value.length() > 0)
|
||||
data.add(value.toString());
|
||||
if(data.size() == 0)
|
||||
return null;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
44
src/zutil/parser/Parser.java
Normal file
44
src/zutil/parser/Parser.java
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* Copyright (c) 2015 ezivkoc
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package zutil.parser;
|
||||
|
||||
import zutil.struct.MutableInt;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
|
||||
/**
|
||||
* Created by Ziver
|
||||
*/
|
||||
public abstract class Parser {
|
||||
|
||||
/**
|
||||
* Starts parsing data from the input.
|
||||
* This method will block until one {@link DataNode} has been parsed.
|
||||
*
|
||||
* @return a DataNode object representing one item from the input data
|
||||
* or null it it is the end of the stream
|
||||
*/
|
||||
public abstract DataNode read() throws IOException;
|
||||
|
||||
}
|
||||
|
|
@ -24,6 +24,7 @@ package zutil.parser.json;
|
|||
|
||||
import zutil.parser.DataNode;
|
||||
import zutil.parser.DataNode.DataType;
|
||||
import zutil.parser.Parser;
|
||||
import zutil.struct.MutableInt;
|
||||
|
||||
import java.io.IOException;
|
||||
|
|
@ -36,7 +37,7 @@ import java.util.regex.Pattern;
|
|||
*
|
||||
* @author Ziver
|
||||
*/
|
||||
public class JSONParser{
|
||||
public class JSONParser extends Parser {
|
||||
public static final Pattern NUMBER_PATTERN = Pattern.compile("^[0-9.]++$");
|
||||
public static final Pattern BOOLEAN_PATTERN = Pattern.compile("^(true|false)$", Pattern.CASE_INSENSITIVE);
|
||||
|
||||
|
|
@ -47,11 +48,12 @@ public class JSONParser{
|
|||
}
|
||||
|
||||
/**
|
||||
* Starts parsing from the InputStream.
|
||||
* Starts parsing from the input.
|
||||
* This method will block until one root tree has been parsed.
|
||||
*
|
||||
* @return a DataNode object representing the input JSON
|
||||
*/
|
||||
@Override
|
||||
public DataNode read() throws IOException {
|
||||
return parse(in, new MutableInt());
|
||||
}
|
||||
|
|
@ -80,11 +82,13 @@ public class JSONParser{
|
|||
DataNode node = null;
|
||||
end.i = 0;
|
||||
|
||||
char c = '_';
|
||||
while((c=(char)in.read()) < 0 || Character.isWhitespace(c) ||
|
||||
c == ',' || c == ':');
|
||||
int c = '_';
|
||||
while((c=in.read()) >= 0 &&
|
||||
(Character.isWhitespace(c) || c == ',' || c == ':'));
|
||||
|
||||
switch( c ){
|
||||
// End of stream
|
||||
case -1: break;
|
||||
// This is the end of an Map or List
|
||||
case ']':
|
||||
case '}':
|
||||
|
|
@ -114,19 +118,19 @@ public class JSONParser{
|
|||
case '\"':
|
||||
root = new DataNode(DataType.String);
|
||||
StringBuilder str = new StringBuilder();
|
||||
while((c=(char)in.read()) >= 0 && c != '\"')
|
||||
str.append(c);
|
||||
while((c=in.read()) >= 0 && c != '\"')
|
||||
str.append((char)c);
|
||||
root.set(str.toString());
|
||||
break;
|
||||
// Parse unknown type
|
||||
default:
|
||||
StringBuilder tmp = new StringBuilder().append(c);
|
||||
while((c=(char)in.read()) >= 0 && c != ',' && c != '='){
|
||||
StringBuilder tmp = new StringBuilder().append((char)c);
|
||||
while((c=in.read()) >= 0 && c != ',' && c != '='){
|
||||
if(c == ']' || c == '}'){
|
||||
end.i = 1;
|
||||
break;
|
||||
}
|
||||
tmp.append(c);
|
||||
tmp.append((char)c);
|
||||
}
|
||||
// Check what type of type the data is
|
||||
String data = tmp.toString().trim();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue