From 0aaa097fa6a8c08577360699d44665c84d0d9d9c Mon Sep 17 00:00:00 2001 From: jsteemann Date: Tue, 14 Feb 2017 22:57:58 +0100 Subject: [PATCH] added generic XML export --- arangosh/Export/ExportFeature.cpp | 78 +++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 25 deletions(-) diff --git a/arangosh/Export/ExportFeature.cpp b/arangosh/Export/ExportFeature.cpp index 552327d297..889522d1fa 100644 --- a/arangosh/Export/ExportFeature.cpp +++ b/arangosh/Export/ExportFeature.cpp @@ -92,7 +92,7 @@ void ExportFeature::collectOptions( options->addOption("--progress", "show progress", new BooleanParameter(&_progress)); - std::unordered_set exports = {"json", "jsonl", "xgmml"}; + std::unordered_set exports = {"json", "jsonl", "xgmml", "xml"}; options->addOption( "--type", "type of export", new DiscreteValuesParameter(&_typeExport, exports)); } @@ -123,8 +123,6 @@ void ExportFeature::validateOptions( FATAL_ERROR_EXIT(); } - std::transform(_typeExport.begin(), _typeExport.end(), _typeExport.begin(), ::tolower); - if (_typeExport == "xgmml" && _graphName.empty() ) { LOG_TOPIC(FATAL, Logger::CONFIG) << "expecting a graph name to dump a graph"; FATAL_ERROR_EXIT(); @@ -211,11 +209,11 @@ void ExportFeature::start() { uint64_t exportedSize = 0; - if (_typeExport == "json" || _typeExport == "jsonl") { + if (_typeExport == "json" || _typeExport == "jsonl" || _typeExport == "xml") { if (_collections.size()) { collectionExport(httpClient.get()); - for(auto const& collection : _collections) { + for (auto const& collection : _collections) { std::string filePath = _outputDirectory + TRI_DIR_SEPARATOR_STR + collection + "." + _typeExport; int64_t fileSize = TRI_SizeFile(filePath.c_str()); @@ -258,7 +256,6 @@ void ExportFeature::collectionExport(SimpleHttpClient* httpClient) { } int fd = -1; - TRI_DEFER(TRI_CLOSE(fd)); std::string const url = "_api/cursor"; @@ -280,25 +277,37 @@ void ExportFeature::collectionExport(SimpleHttpClient* httpClient) { errorMsg = "cannot write to file '" + fileName + "'"; THROW_ARANGO_EXCEPTION_MESSAGE(TRI_ERROR_CANNOT_WRITE_FILE, errorMsg); } + + TRI_DEFER(TRI_CLOSE(fd)); _firstLine = true; if (_typeExport == "json") { - std::string openingBracket = "[\n"; + std::string openingBracket = "["; writeToFile(fd, openingBracket, fileName); + } else if (_typeExport == "xml") { + std::string xmlHeader = "\n" + "\n"); + writeToFile(fd, xmlHeader, fileName); } writeCollectionBatch(fd, VPackArrayIterator(body.get("result")), fileName); while (body.hasKey("id")) { - std::string const url = "/_api/cursor/"+body.get("id").copyString(); + std::string const url = "/_api/cursor/" + body.get("id").copyString(); parsedBody = httpCall(httpClient, url, rest::RequestType::PUT); body = parsedBody->slice(); writeCollectionBatch(fd, VPackArrayIterator(body.get("result")), fileName); } + if (_typeExport == "json") { - std::string closingBracket = "]\n"; - writeToFile(fd, closingBracket , fileName); + std::string closingBracket = "\n]"; + writeToFile(fd, closingBracket, fileName); + } else if (_typeExport == "xml") { + std::string xmlFooter = ""; + writeToFile(fd, xmlFooter, fileName); } } } @@ -307,18 +316,39 @@ void ExportFeature::writeCollectionBatch(int fd, VPackArrayIterator it, std::str std::string line; line.reserve(1024); - for (auto const& doc : it) { - line.clear(); - - if (_firstLine && _typeExport == "json") { - _firstLine = false; - } else if (!_firstLine && _typeExport == "json") { - line.push_back(','); + if (_typeExport == "jsonl") { + for (auto const& doc : it) { + line.clear(); + line += doc.toJson(); + line.push_back('\n'); + writeToFile(fd, line, fileName); + } + } else if (_typeExport == "json") { + for (auto const& doc : it) { + line.clear(); + if (!_firstLine) { + line.append(",\n ", 4); + } else { + line.append("\n ", 3); + _firstLine = false; + } + line += doc.toJson(); + writeToFile(fd, line, fileName); + } + } else if (_typeExport == "xml") { + for (auto const& doc : it) { + line.clear(); + line.append("\n"); + writeToFile(fd, line, fileName); + for (auto const& att : VPackObjectIterator(doc)) { + xgmmlWriteOneAtt(fd, fileName, att.value, att.key.copyString(), 2); + } + line.clear(); + line.append("\n"); + writeToFile(fd, line, fileName); } - - line += doc.toJson(); - line.push_back('\n'); - writeToFile(fd, line, fileName); } } @@ -492,8 +522,7 @@ void ExportFeature::writeGraphBatch(int fd, VPackArrayIterator it, std::string c writeToFile(fd, xmlTag, fileName); for (auto const& it : VPackObjectIterator(doc)) { - xmlTag = encode_char_entities(it.key.copyString()); - xgmmlWriteOneAtt(fd, fileName, it.value, xmlTag); + xgmmlWriteOneAtt(fd, fileName, it.value, it.key.copyString()); } xmlTag = "\n"; @@ -513,8 +542,7 @@ void ExportFeature::writeGraphBatch(int fd, VPackArrayIterator it, std::string c writeToFile(fd, xmlTag, fileName); for (auto const& it : VPackObjectIterator(doc)) { - xmlTag = encode_char_entities(it.key.copyString()); - xgmmlWriteOneAtt(fd, fileName, it.value, xmlTag); + xgmmlWriteOneAtt(fd, fileName, it.value, it.key.copyString()); } xmlTag = "\n";