[26] | 1 | #include "GSEXmlParser.h" |
---|
| 2 | |
---|
| 3 | void Data::Dump() |
---|
| 4 | { |
---|
| 5 | LOG("Sample iid: " << iid); |
---|
| 6 | LOG("Source: " << source); |
---|
| 7 | LOG("Supplementary-Data Type: " << supp_type); |
---|
| 8 | LOG("Supplementary-Data: " << supp_data); |
---|
| 9 | LOG("\n-------- Description --------"); |
---|
| 10 | for(int i = 0; i < desc.GetCount(); i++) |
---|
| 11 | LOG(" " << desc.GetKey(i) << " ..... " << desc[i]); |
---|
| 12 | LOG("-------- Description --------\n"); |
---|
| 13 | } |
---|
| 14 | |
---|
| 15 | String Data::ToXML() |
---|
| 16 | { |
---|
| 17 | String descs; //descriptions |
---|
| 18 | for(int i = 0; i < desc.GetCount(); i++) |
---|
| 19 | descs << XmlTag("Description")("name", desc.GetKey(i)).Text(desc[i]); |
---|
| 20 | |
---|
| 21 | return XmlTag("Sample")("iid", iid)( |
---|
| 22 | XmlTag("Source").Text(source) + |
---|
| 23 | XmlTag("Supplementary-Data")("type", supp_type).Text(supp_data) + |
---|
| 24 | XmlTag("Descriptions") (descs) |
---|
| 25 | ); |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | void GSEParser::Dump() |
---|
| 29 | { |
---|
| 30 | LOG("\n======== GSE XML ========\n"); |
---|
| 31 | for (int i=0; i < data.GetCount(); i++) { |
---|
| 32 | data[i].Dump(); |
---|
| 33 | } |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | void GSEParser::LoadXML(String filename) { |
---|
| 37 | //Load XML File |
---|
| 38 | //LOG("\n---- GSE 2109 XML ----"); |
---|
| 39 | String gse; |
---|
| 40 | gse = LoadFile(GetDataFile(filename)); |
---|
| 41 | XmlNode rnode = ParseXML(gse); |
---|
| 42 | |
---|
| 43 | //Clear Data Array |
---|
| 44 | data.Clear(); |
---|
| 45 | |
---|
| 46 | //Get Root Node |
---|
| 47 | const XmlNode &xnode = rnode["MINiML"]; |
---|
| 48 | |
---|
| 49 | //Save Data Array, one by one |
---|
| 50 | Vector<String> lines, des; |
---|
| 51 | String str, lstr, rstr; |
---|
| 52 | for (int i=0; i<xnode.GetCount(); i++) { |
---|
| 53 | if (xnode[i].GetTag()=="Sample") { |
---|
| 54 | //LOG("Sample(iid):" << xnode[i].Attr("iid")); |
---|
| 55 | //LOG("Source:" << xnode[i]["Channel"]["Source"].GatherText()); |
---|
| 56 | //LOG("\n-------- Description --------"); |
---|
| 57 | Data d; |
---|
| 58 | d.iid = xnode[i].Attr("iid"); |
---|
| 59 | d.source = xnode[i]["Channel"]["Source"].GatherText(); |
---|
| 60 | d.supp_type = xnode[i]["Supplementary-Data"].Attr("type"); |
---|
| 61 | d.supp_data = xnode[i]["Supplementary-Data"].GatherText(); |
---|
| 62 | lines = Split(xnode[i]["Description"].GatherText(),'\n'); |
---|
| 63 | for (int j=0; j<lines.GetCount(); j++) { |
---|
| 64 | if (lines[j].Find("=") > -1) { |
---|
| 65 | des = Split(lines[j], '='); |
---|
| 66 | } else if (lines[j].Find(":") > -1) { |
---|
| 67 | des = Split(lines[j], ':'); |
---|
| 68 | } |
---|
| 69 | if (des.GetCount()>1) { |
---|
| 70 | lstr = TrimSpace(des[0]); |
---|
| 71 | rstr = TrimSpace(des[1]); |
---|
| 72 | //LOG(lstr << ":" << rstr); |
---|
| 73 | String& v = d.desc.GetAdd(lstr); |
---|
| 74 | v = rstr; |
---|
| 75 | } |
---|
| 76 | } |
---|
| 77 | data.Add(d); |
---|
| 78 | //LOG("-------- Description --------\n"); |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | // |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | String GSEParser::ToXML() |
---|
| 85 | { |
---|
| 86 | String datas; // Datas |
---|
| 87 | for (int i = 0; i < data.GetCount(); i++) { |
---|
| 88 | datas << data[i].ToXML(); |
---|
| 89 | } |
---|
| 90 | return XmlHeader() + XmlTag("GSEFAMILY")(datas); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | String TrimSpace(String str) { |
---|
| 94 | while (str.StartsWith(" ") && str.GetLength()) { |
---|
| 95 | str = str.Mid(1); |
---|
| 96 | } |
---|
| 97 | while (str.EndsWith(" ") && str.GetLength()) { |
---|
| 98 | str = str.Left(str.GetLength()-1); |
---|
| 99 | } |
---|
| 100 | return str; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | CONSOLE_APP_MAIN |
---|
| 104 | { |
---|
| 105 | String in, out, arg_flag; |
---|
| 106 | in = "GSE2109_family.xml"; |
---|
| 107 | out = "GSEFamily.xml"; |
---|
| 108 | |
---|
| 109 | const Vector<String>& cmdline = CommandLine(); |
---|
| 110 | for(int i = 0; i < cmdline.GetCount(); i++) { |
---|
| 111 | if (cmdline[i].StartsWith("-")){ |
---|
| 112 | arg_flag = cmdline[i]; |
---|
| 113 | } else if (arg_flag.IsEqual("-i")) { |
---|
| 114 | in = cmdline[i]; |
---|
| 115 | //Cout() << "in: " << in << "\n"; |
---|
| 116 | } else if (arg_flag.IsEqual("-o")) { |
---|
| 117 | out = cmdline[i]; |
---|
| 118 | //Cout() << "out: " << out << "\n"; |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | GSEParser parser; |
---|
| 123 | parser.LoadXML(in); |
---|
| 124 | SaveFile(out,parser.ToXML()); |
---|
| 125 | |
---|
| 126 | } |
---|
| 127 | |
---|