1 | package hozo.sparql;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.FileOutputStream;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.io.OutputStream;
|
---|
7 | import java.io.OutputStreamWriter;
|
---|
8 | import java.io.PrintWriter;
|
---|
9 | import java.util.List;
|
---|
10 | import java.util.Map;
|
---|
11 |
|
---|
12 | import com.hp.hpl.jena.rdf.model.RDFNode;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * 繧サ繝代Ξ繝シ繧ソ縺ォ繧医▲縺ヲ蛹コ蛻�i繧後◆蠖「縺ァ繝��繧ソ繧偵お繧ッ繧ケ繝昴�繝医☆繧九◆繧√�繧ッ繝ゥ繧ケ
|
---|
16 | * @author kato
|
---|
17 | *
|
---|
18 | */
|
---|
19 | public class SeparatedValuesExporter {
|
---|
20 |
|
---|
21 | private String separator;
|
---|
22 | private List<Map<String, RDFNode>> data;
|
---|
23 |
|
---|
24 | public SeparatedValuesExporter(String separator, List<Map<String, RDFNode>> data){
|
---|
25 | this.separator = separator;
|
---|
26 | this.data = data;
|
---|
27 | }
|
---|
28 |
|
---|
29 | public boolean export(File file) throws IOException{
|
---|
30 | return export(new FileOutputStream(file));
|
---|
31 | }
|
---|
32 |
|
---|
33 | public boolean export(OutputStream stream) throws IOException{
|
---|
34 |
|
---|
35 | PrintWriter pw = new PrintWriter(new OutputStreamWriter(stream));
|
---|
36 |
|
---|
37 | if (!outputHeader(pw, data.get(0))){
|
---|
38 | pw.flush();
|
---|
39 | pw.close();
|
---|
40 | return false;
|
---|
41 | }
|
---|
42 |
|
---|
43 | for (Map<String, RDFNode> datum : data){
|
---|
44 | outputContent(pw, datum);
|
---|
45 | }
|
---|
46 | pw.flush();
|
---|
47 | pw.close();
|
---|
48 |
|
---|
49 | return false;
|
---|
50 | }
|
---|
51 |
|
---|
52 | private boolean outputHeader(PrintWriter stream, Map<String, RDFNode> columns){
|
---|
53 | StringBuilder sb = new StringBuilder();
|
---|
54 |
|
---|
55 | int i = 0;
|
---|
56 | for (String key : columns.keySet()){
|
---|
57 | sb.append(key);
|
---|
58 | if (++i != columns.keySet().size()){
|
---|
59 | sb.append(getSeparator());
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | outputLine(stream, sb.toString());
|
---|
64 |
|
---|
65 | return true;
|
---|
66 | }
|
---|
67 |
|
---|
68 | private boolean outputContent(PrintWriter stream, Map<String, RDFNode> row){
|
---|
69 | StringBuilder sb = new StringBuilder();
|
---|
70 |
|
---|
71 | int i = 0;
|
---|
72 | for (String key : row.keySet()){
|
---|
73 | sb.append(row.get(key));
|
---|
74 | if (++i != row.keySet().size()){
|
---|
75 | sb.append(getSeparator());
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | outputLine(stream, sb.toString());
|
---|
80 |
|
---|
81 | return true;
|
---|
82 | }
|
---|
83 |
|
---|
84 | private String getSeparator(){
|
---|
85 | return this.separator;
|
---|
86 | }
|
---|
87 |
|
---|
88 | private void outputLine(PrintWriter stream, String line){
|
---|
89 | stream.println(line);
|
---|
90 | }
|
---|
91 |
|
---|
92 | }
|
---|