私が公開しているGWTのライブラリーを紹介します。 GWT-CSV ParserOpenCSVのCSVParserとCSVReaderをGWTで動くようにしてみました。 使い方CSVReaderの方は、StringReaderしか読み込めません。 このStringReaderもsuper-sourceの仕組みで作った偽物なので、コンストラクト以外使えません。 GWTにReaderクラスがないのが原因です。 なるべく、OpenCSVのライブラリーをそのまま使えるようにと思って作成したため、こうなりました。 使用例package com.akjava.lib.csv; import java.io.IOException; import java.io.StringReader; import java.util.List; import au.com.bytecode.opencsv.CSVParser; import au.com.bytecode.opencsv.CSVReader; import com.google.gwt.junit.client.GWTTestCase; public class CSVTest extends GWTTestCase { @Override public String getModuleName() { return "com.akjava.lib.csv.CSV"; } public void test_parse1(){ CSVParser parser=new CSVParser(); try { String[] collect={"text"}; String[] result=parser.parseLine("text"); assertArrayEquals(collect,result); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void test_parse2(){ try { String[] collect={"hello","world"}; CSVParser parser=new CSVParser(); String[] result=parser.parseLine("\"hello\",\"world\""); assertArrayEquals(collect,result); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void test_parse3(){ try { String[] collect={"hello","world"}; CSVParser parser=new CSVParser('\t','\''); String[] result=parser.parseLine("'hello'\tworld"); assertArrayEquals(collect,result); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void test_parse4(){ try { String[] collect={"hello\nhello2","world"}; CSVParser parser=new CSVParser('\t','\''); String[] result=parser.parseLine("'hello\nhello2'\t'world'"); assertArrayEquals(collect,result); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void test_read1(){ try { String[] collect={"hello","world"}; CSVReader reader=new CSVReader(new StringReader("hello,world")); String[] result=reader.readNext(); assertArrayEquals(collect,result); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void test_read2(){ try { String[] collect={"hello","world"}; CSVReader reader=new CSVReader(new StringReader("\"hello\",\"world\"")); String[] result=reader.readNext(); assertArrayEquals(collect,result); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void test_read3(){ try { String[] collect={"hello","world"}; CSVReader reader=new CSVReader(new StringReader("'hello'\tworld"),'\t','\''); String[] result=reader.readNext(); assertArrayEquals(collect,result); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void test_read4(){ try { String[] collect={"hello\nhello2","world"}; CSVReader reader=new CSVReader(new StringReader("'hello\nhello2'\t'world'"),'\t','\''); String[] result=reader.readNext(); assertArrayEquals(collect,result); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void test_readAll(){ try { String[] collect={"hello\nhello2","line2"}; CSVReader reader=new CSVReader(new StringReader( "'hello\nhello2'\n" + "line2"),'\t','\''); List<String[]> all=reader.readAll(); String[] result=new String[all.size()]; for(int i=0;i<all.size();i++){ result[i]=all.get(i)[0]; } assertArrayEquals(collect,result); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void assertArrayEquals(String[] array1,String[] array2){ /* if(array1.length!=array2.length){ assertEquals("differenct length", array1.length,array2.length); }*/ String a1=""; for(int i=0;i<array1.length;i++){ a1+=array1[i].toString(); if(i!=array1.length-1){ a1+=","; } } String a2=""; for(int i=0;i<array2.length;i++){ a2+=array2[i].toString(); if(i!=array2.length-1){ a2+=","; } } assertEquals(a1, a2); } } スーパーソース BufferedReader/* * Copyright (C) 2010 aki * www.akjava.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* * just change package name for GWTcompatible * Original Copyright is below */ package java.io; /** * only for CSVReader */ import java.io.IOException; public class BufferedReader { private final Reader reader; private String[] lines=null; private int lineIndex=0; public BufferedReader(Reader reader){ this.reader=reader; if(reader instanceof StringReader){ String text=((StringReader)reader).getString(); text=text.replace("\r\n", "\n"); text=text.replace("\r", "\n"); lines=text.split("\n"); }else{ throw new UnsupportedOperationException("only accept StringReader.class:"+reader.getClass().getName()); } } public String readLine() throws IOException{ if(lines==null){ throw new IOException("reader not ready"); } if(lineIndex<lines.length){ String tmp=lines[lineIndex]; lineIndex++; return tmp; }else{ return null; } } public void close() throws IOException{ //do nothing } } StringReader/* * Copyright (C) 2010 aki * www.akjava.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* * just change package name for GWTcompatible * Original Copyright is below */ package java.io; /** * only for CSVReader * @author aki * */ public class StringReader extends Reader{ private String string; public StringReader(String string){ this.string=string; } String getString(){ return string; } } |
GWT >
akjava-GWTライブラリー
私が公開しているGWTのライブラリーを紹介します。 GWT-CSV ParserOpenCSVのCSVParserとCSVReaderをGWTで動くようにしてみました。 使い方CSVReaderの方は、StringReaderしか読み込めません。 このStringReaderもsuper-sourceの仕組みで作った偽物なので、コンストラクト以外使えません。 GWTにReaderクラスがないのが原因です。 なるべく、OpenCSVのライブラリーをそのまま使えるようにと思って作成したため、こうなりました。 スーパーソース |