| 1 | // | 
|---|
| 2 | // DOMWriter.cpp | 
|---|
| 3 | // | 
|---|
| 4 | // Library: XML | 
|---|
| 5 | // Package: DOM | 
|---|
| 6 | // Module:  DOMWriter | 
|---|
| 7 | // | 
|---|
| 8 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. | 
|---|
| 9 | // and Contributors. | 
|---|
| 10 | // | 
|---|
| 11 | // SPDX-License-Identifier:	BSL-1.0 | 
|---|
| 12 | // | 
|---|
| 13 |  | 
|---|
| 14 |  | 
|---|
| 15 |  | 
|---|
| 16 | #include "Poco/DOM/DOMWriter.h" | 
|---|
| 17 | #include "Poco/XML/XMLWriter.h" | 
|---|
| 18 | #include "Poco/DOM/Document.h" | 
|---|
| 19 | #include "Poco/DOM/DocumentFragment.h" | 
|---|
| 20 | #include "Poco/DOM/DocumentType.h" | 
|---|
| 21 | #include "Poco/DOM/DOMException.h" | 
|---|
| 22 | #include "Poco/DOM/DOMSerializer.h" | 
|---|
| 23 | #include "Poco/SAX/LexicalHandler.h" | 
|---|
| 24 | #include "Poco/XML/XMLException.h" | 
|---|
| 25 | #include "Poco/Path.h" | 
|---|
| 26 | #include "Poco/FileStream.h" | 
|---|
| 27 |  | 
|---|
| 28 |  | 
|---|
| 29 | namespace Poco { | 
|---|
| 30 | namespace XML { | 
|---|
| 31 |  | 
|---|
| 32 |  | 
|---|
| 33 | DOMWriter::DOMWriter(): | 
|---|
| 34 | _pTextEncoding(0), | 
|---|
| 35 | _options(0), | 
|---|
| 36 | _indent( "\t") | 
|---|
| 37 | { | 
|---|
| 38 | } | 
|---|
| 39 |  | 
|---|
| 40 |  | 
|---|
| 41 | DOMWriter::~DOMWriter() | 
|---|
| 42 | { | 
|---|
| 43 | } | 
|---|
| 44 |  | 
|---|
| 45 |  | 
|---|
| 46 | void DOMWriter::setEncoding(const std::string& encodingName, Poco::TextEncoding& textEncoding) | 
|---|
| 47 | { | 
|---|
| 48 | _encodingName  = encodingName; | 
|---|
| 49 | _pTextEncoding = &textEncoding; | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 |  | 
|---|
| 53 | void DOMWriter::setOptions(int options) | 
|---|
| 54 | { | 
|---|
| 55 | _options = options; | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 |  | 
|---|
| 59 | void DOMWriter::setNewLine(const std::string& newLine) | 
|---|
| 60 | { | 
|---|
| 61 | _newLine = newLine; | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 |  | 
|---|
| 65 | void DOMWriter::setIndent(const std::string& indent) | 
|---|
| 66 | { | 
|---|
| 67 | _indent = indent; | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 |  | 
|---|
| 71 | void DOMWriter::writeNode(XMLByteOutputStream& ostr, const Node* pNode) | 
|---|
| 72 | { | 
|---|
| 73 | poco_check_ptr (pNode); | 
|---|
| 74 |  | 
|---|
| 75 | bool isFragment = pNode->nodeType() != Node::DOCUMENT_NODE; | 
|---|
| 76 |  | 
|---|
| 77 | XMLWriter writer(ostr, _options, _encodingName, _pTextEncoding); | 
|---|
| 78 | writer.setNewLine(_newLine); | 
|---|
| 79 | writer.setIndent(_indent); | 
|---|
| 80 |  | 
|---|
| 81 | DOMSerializer serializer; | 
|---|
| 82 | serializer.setContentHandler(&writer); | 
|---|
| 83 | serializer.setDTDHandler(&writer); | 
|---|
| 84 | serializer.setProperty(XMLReader::PROPERTY_LEXICAL_HANDLER, static_cast<LexicalHandler*>(&writer)); | 
|---|
| 85 | if (isFragment) writer.startFragment(); | 
|---|
| 86 | serializer.serialize(pNode); | 
|---|
| 87 | if (isFragment) writer.endFragment(); | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 |  | 
|---|
| 91 | void DOMWriter::writeNode(const std::string& systemId, const Node* pNode) | 
|---|
| 92 | { | 
|---|
| 93 | Poco::FileOutputStream ostr(systemId); | 
|---|
| 94 | if (ostr.good()) | 
|---|
| 95 | writeNode(ostr, pNode); | 
|---|
| 96 | else | 
|---|
| 97 | throw Poco::CreateFileException(systemId); | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 |  | 
|---|
| 101 | } } // namespace Poco::XML | 
|---|
| 102 |  | 
|---|
| 103 |  | 
|---|