// dataset_extract.cxx // Insert the dataset described by an XML file into a dataset // database. #include #include #include #include "dataset_util/FileStatus.h" #include "dataset_util/Environment.h" #include "dataset_util/DtdRegistry.h" #include "dataset_catalog/DatasetSelectionCatalog.h" #include "dataset_base/Dataset.h" #include "dataset_base/DatasetCreator.h" #include "dataset_base/DatasetRepository.h" #include "dataset_xml/XmlParser.h" using std::string; using std::cout; using std::cerr; using std::endl; using dset::DatasetSelectionCatalog; using dset::Dataset; using dset::DatasetCreator; using dset::DatasetRepository; using dset::DatasetList; // Register TestDataset and TestCompoundDataset DTD for testing. #include "dataset_base/DatasetCreator_t.h" //********************************************************************** // Helpers. //********************************************************************** // Return XML parser. XmlParser& parser() { static XmlParser pr; return pr; } //********************************************************************** int main(int argc, char* argv[]) { bool help = false; bool error = 0; string conn = ""; string xfile = "dataset.xml"; string sid = ""; string dsname = ""; int iarg = 0; string exename = argv[iarg++]; while ( iarg < argc ) { // Fetch flag and its argument. string flag = argv[iarg++]; string arg = ""; if ( iarg < argc ) { bool has_arg = false; arg = argv[iarg]; if ( arg.size() && arg[0] == '-' ) { arg = ""; } else { has_arg = true; ++iarg; } } if ( flag == "-c" ) { if ( arg == "" ) { error = 12; break; } conn = arg; } else if ( flag == "-i" ) { if ( arg == "" ) { error = 15; break; } sid = arg; } else if ( flag == "-n" ) { if ( arg == "" ) { error = 16; break; } dsname = arg; } else if ( flag == "-f" ) { if ( arg == "" ) { xfile = "dataset.xml"; } xfile = arg; } else if ( flag == "-h" ) { help = true; break; } else { error = 19; break; } } if ( error || help ) { cout << "Usage: "; cout << " " << exename << " [-c CONN -i ID [-f XFILE]" << endl; cout << " " << exename << " [-c CONN] -n DSNAME [-f XFILE]" << endl; cout << " -h - help: this message" << endl; cout << " -c CONN - repository connection string [\"\"]" << endl; cout << " -i ID - ID of the dataset in format iii-jjj" << endl; cout << " -n DSNAME - Name of the dataset (in the DSC)" << endl; cout << " One of -i and -n must be provided" << endl; cout << " -f XFILE - name of output XML file" << endl; cout << " If -f or XFILE is absent, use dataset.xml" << endl; if ( error ) { cerr << "Error reading command line" << endl; } return error; } // Open database. DatasetRepository::set_default_instance(conn); DatasetRepository& rep = DatasetRepository::default_instance(); if ( ! rep.is_valid() ) { cerr << "Unable to open repository with connection " << endl; cerr << " " << conn << endl; } // Open the dataset. const Dataset* pdst = 0; DatasetId did; if ( dsname.size() != 0 ) { did = DatasetSelectionCatalog::default_instance().id(dsname); if ( ! did.is_valid() ) { cerr << "Unable to retrieve ID from DSC" << endl; return 33; } } else { did = DatasetId(sid); } if ( ! did.is_valid() ) { cerr << "Unable to parse id" << endl; return 34; } pdst = rep.extract(did); if ( pdst == 0 ) { int ecode = rep.error(); string msg = rep.error_message(); cerr << "Error retrieving input dataset with ID: " << ecode << endl; cerr << " \"" << msg << "\"" << endl; return 35; } if ( ! pdst->is_valid() ) { cerr << "Invalid dataset" << endl; return 36; } // Write to file. const XmlElement* pele = pdst->xml(); if ( pele == 0 ) { cerr << "Error extracting XML from dataset" << endl; return 37; } int wstat = parser().write(xfile, *pele); if ( wstat != 0 ) { cerr << "Erroring writing XML" << endl; return 38; } cout << "Dataset " << sid << " written to " << xfile << endl; return 0; }