| 1 | package jp.ac.osaka_u.sanken.sparql;
|
|---|
| 2 |
|
|---|
| 3 | import java.io.File;
|
|---|
| 4 | import java.io.FileNotFoundException;
|
|---|
| 5 | import java.io.FileOutputStream;
|
|---|
| 6 | import java.io.IOException;
|
|---|
| 7 | import java.io.OutputStream;
|
|---|
| 8 | import java.util.HashMap;
|
|---|
| 9 | import java.util.List;
|
|---|
| 10 | import java.util.Map;
|
|---|
| 11 |
|
|---|
| 12 | import com.hp.hpl.jena.rdf.model.RDFNode;
|
|---|
| 13 |
|
|---|
| 14 | public class SparqlUtil {
|
|---|
| 15 |
|
|---|
| 16 | /** TSV蠖「蠑丞�蜉�*/
|
|---|
| 17 | public static final int OUTPUT_TYPE_TSV = 0;
|
|---|
| 18 |
|
|---|
| 19 | public static final int OUTPUT_TYPE_CSV = 1;
|
|---|
| 20 |
|
|---|
| 21 | public static final int OUTPUT_TYPE_XLS = 2;
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 | private static HashMap<Integer, String> separatorMap;
|
|---|
| 25 |
|
|---|
| 26 | static {
|
|---|
| 27 | separatorMap = new HashMap<Integer, String>();
|
|---|
| 28 | separatorMap.put(SparqlUtil.OUTPUT_TYPE_TSV, "\t");
|
|---|
| 29 | separatorMap.put(SparqlUtil.OUTPUT_TYPE_CSV, ",");
|
|---|
| 30 |
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * 謖�ョ壹@縺溘せ繝医Μ繝シ繝縺ォ邨先棡繧呈枚蟄怜�縺ィ縺励※蜃コ蜉帙☆繧� |
|---|
| 35 | * @param results
|
|---|
| 36 | * @param type
|
|---|
| 37 | * @param stream
|
|---|
| 38 | * @return
|
|---|
| 39 | */
|
|---|
| 40 | public static boolean saveResult(List<Map<String, RDFNode>> results, int type, OutputStream stream){
|
|---|
| 41 | /**
|
|---|
| 42 | * type縺ォ繧医▲縺ヲresult format繧貞、峨∴縺ヲ蜀恒OST縺吶k縺薙→繧り�∴繧峨l繧九′縲∵勸驕肴ァ縺後↑縺��縺ァ菫晉蕗縺ィ縺励� |
|---|
| 43 | * 螟画鋤縺ッ閾ェ蜑阪〒螳溯」�☆繧九� |
|---|
| 44 | */
|
|---|
| 45 | if (type == SparqlUtil.OUTPUT_TYPE_TSV || type == SparqlUtil.OUTPUT_TYPE_CSV){
|
|---|
| 46 | String separator = separatorMap.get(type);
|
|---|
| 47 | if (separator == null){
|
|---|
| 48 | return false;
|
|---|
| 49 | }
|
|---|
| 50 | SeparatedValuesExporter expo = new SeparatedValuesExporter(separator, results);
|
|---|
| 51 | try {
|
|---|
| 52 | expo.export(stream);
|
|---|
| 53 | return true;
|
|---|
| 54 | } catch (IOException e) {
|
|---|
| 55 | e.printStackTrace();
|
|---|
| 56 | }
|
|---|
| 57 | return true;
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | return false;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | public static boolean saveResult(List<Map<String, RDFNode>> results, int type, File file) throws FileNotFoundException{
|
|---|
| 64 | return saveResult(results, type, new FileOutputStream(file));
|
|---|
| 65 | }
|
|---|
| 66 | }
|
|---|