|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
SVTokenizer.java | 0% | 0% | 0% | 0% |
|
1 |
package org.asyrinx.brownie.core.csv;
|
|
2 |
|
|
3 |
/**
|
|
4 |
* CSVやTSVなど区切られた文字列を解析するクラス
|
|
5 |
*
|
|
6 |
* @author kadowaki
|
|
7 |
*/
|
|
8 |
public class SVTokenizer { |
|
9 |
|
|
10 |
/** 解析する文字列 */
|
|
11 |
private String line;
|
|
12 |
|
|
13 |
/** 区切り文字 */
|
|
14 |
private char delim; |
|
15 |
|
|
16 |
/** 引用符 */
|
|
17 |
private char quote; |
|
18 |
|
|
19 |
/** 文字列内での現在の位置 */
|
|
20 |
private int current; |
|
21 |
|
|
22 |
/** 文字列の最後の位置 */
|
|
23 |
private int max; |
|
24 |
|
|
25 |
/**
|
|
26 |
* 区切り文字・引用符を選択するときのコンストラクタ
|
|
27 |
*
|
|
28 |
* @param line
|
|
29 |
* 解析する文字列
|
|
30 |
* @param delim
|
|
31 |
* 区切り文字
|
|
32 |
* @param quote
|
|
33 |
* 引用符
|
|
34 |
*/
|
|
35 | 0 |
public SVTokenizer(String line, char delim, char quote) { |
36 | 0 |
this.line = line;
|
37 | 0 |
this.delim = delim;
|
38 | 0 |
this.quote = quote;
|
39 | 0 |
current = 0; |
40 | 0 |
max = line.length(); |
41 |
} |
|
42 |
|
|
43 |
/**
|
|
44 |
* 次のデータがあるかどうかを判定する
|
|
45 |
*
|
|
46 |
* @return boolean 次のデータの有無
|
|
47 |
*/
|
|
48 | 0 |
public boolean hasMoreTokens() { |
49 | 0 |
return (nextDelim(current) <= max);
|
50 |
} |
|
51 |
|
|
52 |
/**
|
|
53 |
* 次のデータを返す
|
|
54 |
*
|
|
55 |
* @return String 次のデータ
|
|
56 |
*/
|
|
57 | 0 |
public String nextToken() {
|
58 | 0 |
int work = current;
|
59 | 0 |
current = nextDelim(current); |
60 | 0 |
StringBuffer buffer = new StringBuffer();
|
61 | 0 |
while (work < current) {
|
62 | 0 |
char ch = line.charAt(work++);
|
63 | 0 |
if (ch != quote) {
|
64 | 0 |
buffer.append(ch); |
65 |
} |
|
66 |
} |
|
67 | 0 |
current++; |
68 | 0 |
return new String(buffer); |
69 |
} |
|
70 |
|
|
71 |
/**
|
|
72 |
* 次の区切り文字の位置を返す
|
|
73 |
*
|
|
74 |
* @param position
|
|
75 |
* 検索を開始する位置
|
|
76 |
* @return int 次の区切り文字の位置
|
|
77 |
*/
|
|
78 | 0 |
private int nextDelim(int position) { |
79 | 0 |
boolean inquote = false; |
80 | 0 |
while (position < max) {
|
81 | 0 |
char ch = line.charAt(position);
|
82 | 0 |
if (ch == quote) {
|
83 | 0 |
inquote = !inquote; |
84 | 0 |
} else if (!inquote && (ch == delim)) { |
85 | 0 |
break;
|
86 |
} |
|
87 | 0 |
position++; |
88 |
} |
|
89 | 0 |
return position;
|
90 |
} |
|
91 |
} |
|