| 32 | using ElementStack = vector<std::pair<ElementPtr, xml_node>>; |
| 33 | |
| 34 | void documentToXml(DocumentPtr doc, xml_node& xmlRoot, const XmlWriteOptions* writeOptions) |
| 35 | { |
| 36 | ElementStack elemStack; |
| 37 | elemStack.emplace_back(doc, xmlRoot); |
| 38 | while (!elemStack.empty()) |
| 39 | { |
| 40 | ElementPtr elem = elemStack.back().first; |
| 41 | xml_node xmlNode = elemStack.back().second; |
| 42 | elemStack.pop_back(); |
| 43 | |
| 44 | bool writeXIncludeEnable = writeOptions ? writeOptions->writeXIncludeEnable : true; |
| 45 | ElementPredicate elementPredicate = writeOptions ? writeOptions->elementPredicate : nullptr; |
| 46 | |
| 47 | // Store attributes in XML. |
| 48 | if (!elem->getName().empty()) |
| 49 | { |
| 50 | xmlNode.append_attribute(Element::NAME_ATTRIBUTE.c_str()) = elem->getName().c_str(); |
| 51 | } |
| 52 | for (const string& attrName : elem->getAttributeNames()) |
| 53 | { |
| 54 | xml_attribute xmlAttr = xmlNode.append_attribute(attrName.c_str()); |
| 55 | xmlAttr.set_value(elem->getAttribute(attrName).c_str()); |
| 56 | } |
| 57 | |
| 58 | // Create child elements and recurse. |
| 59 | StringSet writtenSourceFiles; |
| 60 | for (auto child : elem->getChildren()) |
| 61 | { |
| 62 | if (elementPredicate && !elementPredicate(child)) |
| 63 | { |
| 64 | continue; |
| 65 | } |
| 66 | |
| 67 | // Write XInclude references if requested. |
| 68 | if (writeXIncludeEnable && child->hasSourceUri()) |
| 69 | { |
| 70 | string sourceUri = child->getSourceUri(); |
| 71 | if (sourceUri != elem->getDocument()->getSourceUri()) |
| 72 | { |
| 73 | if (!writtenSourceFiles.count(sourceUri)) |
| 74 | { |
| 75 | if (!xmlNode.attribute(XINCLUDE_NAMESPACE.c_str())) |
| 76 | { |
| 77 | xmlNode.append_attribute(XINCLUDE_NAMESPACE.c_str()) = XINCLUDE_URL.c_str(); |
| 78 | } |
| 79 | xml_node includeNode = xmlNode.append_child(XINCLUDE_TAG.c_str()); |
| 80 | xml_attribute includeAttr = includeNode.append_attribute("href"); |
| 81 | FilePath includePath(sourceUri); |
| 82 | |
| 83 | // Write relative include paths in Posix format, and absolute |
| 84 | // include paths in native format. |
| 85 | FilePath::Format includeFormat = includePath.isAbsolute() ? FilePath::FormatNative : FilePath::FormatPosix; |
| 86 | includeAttr.set_value(includePath.asString(includeFormat).c_str()); |
| 87 | |
| 88 | writtenSourceFiles.insert(sourceUri); |
| 89 | } |
| 90 | continue; |
| 91 | } |
no test coverage detected