1 | // |
---|
2 | // XMLWriter.cpp |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/XML/src/XMLWriter.cpp#3 $ |
---|
5 | // |
---|
6 | // Library: XML |
---|
7 | // Package: XML |
---|
8 | // Module: XMLWriter |
---|
9 | // |
---|
10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
11 | // and Contributors. |
---|
12 | // |
---|
13 | // Permission is hereby granted, free of charge, to any person or organization |
---|
14 | // obtaining a copy of the software and accompanying documentation covered by |
---|
15 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
16 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
17 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
18 | // do so, all subject to the following: |
---|
19 | // |
---|
20 | // The copyright notices in the Software and this entire statement, including |
---|
21 | // the above license grant, this restriction and the following disclaimer, |
---|
22 | // must be included in all copies of the Software, in whole or in part, and |
---|
23 | // all derivative works of the Software, unless such copies or derivative |
---|
24 | // works are solely in the form of machine-executable object code generated by |
---|
25 | // a source language processor. |
---|
26 | // |
---|
27 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
28 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
29 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
30 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
31 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
32 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
33 | // DEALINGS IN THE SOFTWARE. |
---|
34 | // |
---|
35 | |
---|
36 | |
---|
37 | #include <Poco/XML/XMLWriter.h> |
---|
38 | #include <Poco/XML/XMLString.h> |
---|
39 | #include <Poco/XML/XMLException.h> |
---|
40 | #include <Poco/SAX/AttributesImpl.h> |
---|
41 | #include <Poco/UTF8Encoding.h> |
---|
42 | #include <Poco/UTF16Encoding.h> |
---|
43 | #include <sstream> |
---|
44 | |
---|
45 | |
---|
46 | namespace Poco { |
---|
47 | namespace XML { |
---|
48 | |
---|
49 | |
---|
50 | const std::string XMLWriter::NEWLINE_DEFAULT; |
---|
51 | const std::string XMLWriter::NEWLINE_CR = "\r"; |
---|
52 | const std::string XMLWriter::NEWLINE_CRLF = "\r\n"; |
---|
53 | const std::string XMLWriter::NEWLINE_LF = "\n"; |
---|
54 | const std::string XMLWriter::MARKUP_QUOTENC = """; |
---|
55 | const std::string XMLWriter::MARKUP_APOSENC = "'"; |
---|
56 | const std::string XMLWriter::MARKUP_AMPENC = "&"; |
---|
57 | const std::string XMLWriter::MARKUP_LTENC = "<"; |
---|
58 | const std::string XMLWriter::MARKUP_GTENC = ">"; |
---|
59 | const std::string XMLWriter::MARKUP_LT = "<"; |
---|
60 | const std::string XMLWriter::MARKUP_GT = ">"; |
---|
61 | const std::string XMLWriter::MARKUP_SLASHGT = "/>"; |
---|
62 | const std::string XMLWriter::MARKUP_LTSLASH = "</"; |
---|
63 | const std::string XMLWriter::MARKUP_COLON = ":"; |
---|
64 | const std::string XMLWriter::MARKUP_EQQUOT = "=\""; |
---|
65 | const std::string XMLWriter::MARKUP_QUOT = "\""; |
---|
66 | const std::string XMLWriter::MARKUP_SPACE = " "; |
---|
67 | const std::string XMLWriter::MARKUP_TAB = "\t"; |
---|
68 | const std::string XMLWriter::MARKUP_BEGIN_CDATA = "<![CDATA["; |
---|
69 | const std::string XMLWriter::MARKUP_END_CDATA = "]]>"; |
---|
70 | |
---|
71 | |
---|
72 | #if defined(XML_UNICODE_WCHAR_T) |
---|
73 | #define NATIVE_ENCODING Poco::UTF16Encoding |
---|
74 | #else |
---|
75 | #define NATIVE_ENCODING Poco::UTF8Encoding |
---|
76 | #endif |
---|
77 | |
---|
78 | |
---|
79 | XMLWriter::XMLWriter(XMLByteOutputStream& str, int options): |
---|
80 | _pTextConverter(0), |
---|
81 | _pInEncoding(new NATIVE_ENCODING), |
---|
82 | _pOutEncoding(new Poco::UTF8Encoding), |
---|
83 | _options(options), |
---|
84 | _encoding("UTF-8"), |
---|
85 | _depth(-1), |
---|
86 | _elementCount(0), |
---|
87 | _inFragment(false), |
---|
88 | _inCDATA(false), |
---|
89 | _inDTD(false), |
---|
90 | _inInternalDTD(false), |
---|
91 | _contentWritten(false), |
---|
92 | _unclosedStartTag(false), |
---|
93 | _prefix(0) |
---|
94 | { |
---|
95 | _pTextConverter = new Poco::OutputStreamConverter(str, *_pInEncoding, *_pOutEncoding); |
---|
96 | setNewLine(NEWLINE_DEFAULT); |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | XMLWriter::XMLWriter(XMLByteOutputStream& str, int options, const std::string& encodingName, Poco::TextEncoding& textEncoding): |
---|
101 | _pTextConverter(0), |
---|
102 | _pInEncoding(new NATIVE_ENCODING), |
---|
103 | _pOutEncoding(0), |
---|
104 | _options(options), |
---|
105 | _encoding(encodingName), |
---|
106 | _depth(-1), |
---|
107 | _elementCount(0), |
---|
108 | _inFragment(false), |
---|
109 | _inCDATA(false), |
---|
110 | _inDTD(false), |
---|
111 | _inInternalDTD(false), |
---|
112 | _contentWritten(false), |
---|
113 | _unclosedStartTag(false), |
---|
114 | _prefix(0) |
---|
115 | { |
---|
116 | _pTextConverter = new Poco::OutputStreamConverter(str, *_pInEncoding, textEncoding); |
---|
117 | setNewLine(NEWLINE_DEFAULT); |
---|
118 | } |
---|
119 | |
---|
120 | |
---|
121 | XMLWriter::XMLWriter(XMLByteOutputStream& str, int options, const std::string& encodingName, Poco::TextEncoding* pTextEncoding): |
---|
122 | _pTextConverter(0), |
---|
123 | _pInEncoding(new NATIVE_ENCODING), |
---|
124 | _pOutEncoding(0), |
---|
125 | _options(options), |
---|
126 | _encoding(encodingName), |
---|
127 | _depth(-1), |
---|
128 | _elementCount(0), |
---|
129 | _inFragment(false), |
---|
130 | _inCDATA(false), |
---|
131 | _inDTD(false), |
---|
132 | _inInternalDTD(false), |
---|
133 | _contentWritten(false), |
---|
134 | _unclosedStartTag(false), |
---|
135 | _prefix(0) |
---|
136 | { |
---|
137 | if (pTextEncoding) |
---|
138 | { |
---|
139 | _pTextConverter = new Poco::OutputStreamConverter(str, *_pInEncoding, *pTextEncoding); |
---|
140 | } |
---|
141 | else |
---|
142 | { |
---|
143 | _encoding = "UTF-8"; |
---|
144 | _pOutEncoding = new Poco::UTF8Encoding; |
---|
145 | _pTextConverter = new Poco::OutputStreamConverter(str, *_pInEncoding, *_pOutEncoding); |
---|
146 | } |
---|
147 | setNewLine(NEWLINE_DEFAULT); |
---|
148 | } |
---|
149 | |
---|
150 | |
---|
151 | XMLWriter::~XMLWriter() |
---|
152 | { |
---|
153 | delete _pTextConverter; |
---|
154 | delete _pInEncoding; |
---|
155 | delete _pOutEncoding; |
---|
156 | } |
---|
157 | |
---|
158 | |
---|
159 | void XMLWriter::setDocumentLocator(const Locator* loc) |
---|
160 | { |
---|
161 | } |
---|
162 | |
---|
163 | |
---|
164 | void XMLWriter::setNewLine(const std::string& newLineCharacters) |
---|
165 | { |
---|
166 | if (newLineCharacters.empty()) |
---|
167 | { |
---|
168 | #if defined(_WIN32) |
---|
169 | _newLine = NEWLINE_CRLF; |
---|
170 | #else |
---|
171 | _newLine = NEWLINE_LF; |
---|
172 | #endif |
---|
173 | } |
---|
174 | else _newLine = newLineCharacters; |
---|
175 | } |
---|
176 | |
---|
177 | |
---|
178 | const std::string& XMLWriter::getNewLine() const |
---|
179 | { |
---|
180 | return _newLine; |
---|
181 | } |
---|
182 | |
---|
183 | |
---|
184 | void XMLWriter::startDocument() |
---|
185 | { |
---|
186 | if (_depth != -1) |
---|
187 | throw XMLException("Cannot start a document in another document"); |
---|
188 | |
---|
189 | _inFragment = false; |
---|
190 | _depth = 0; |
---|
191 | _elementCount = 0; |
---|
192 | _inDTD = false; |
---|
193 | _inInternalDTD = false; |
---|
194 | _prefix = 0; |
---|
195 | |
---|
196 | if (_options & WRITE_XML_DECLARATION) |
---|
197 | writeXMLDeclaration(); |
---|
198 | |
---|
199 | _contentWritten = true; |
---|
200 | _namespaces.reset(); |
---|
201 | _namespaces.pushContext(); |
---|
202 | } |
---|
203 | |
---|
204 | |
---|
205 | void XMLWriter::endDocument() |
---|
206 | { |
---|
207 | if (_depth > 0) |
---|
208 | throw XMLException("Not well-formed (at least one tag has no matching end tag)"); |
---|
209 | if (_elementCount == 0) |
---|
210 | throw XMLException("No document element"); |
---|
211 | |
---|
212 | poco_assert_dbg (!_unclosedStartTag); |
---|
213 | |
---|
214 | _elementCount = 0; |
---|
215 | _depth = -1; |
---|
216 | } |
---|
217 | |
---|
218 | |
---|
219 | void XMLWriter::startFragment() |
---|
220 | { |
---|
221 | if (_depth != -1) |
---|
222 | throw XMLException("Cannot start a fragment in another fragment or document"); |
---|
223 | |
---|
224 | _inFragment = true; |
---|
225 | _depth = 0; |
---|
226 | _elementCount = 0; |
---|
227 | _prefix = 0; |
---|
228 | |
---|
229 | _contentWritten = true; |
---|
230 | _namespaces.reset(); |
---|
231 | _namespaces.pushContext(); |
---|
232 | } |
---|
233 | |
---|
234 | |
---|
235 | void XMLWriter::endFragment() |
---|
236 | { |
---|
237 | if (_depth > 1) |
---|
238 | throw XMLException("Not well-formed (at least one tag has no matching end tag)"); |
---|
239 | |
---|
240 | _inFragment = false; |
---|
241 | _elementCount = 0; |
---|
242 | _depth = -1; |
---|
243 | } |
---|
244 | |
---|
245 | |
---|
246 | void XMLWriter::startElement(const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname) |
---|
247 | { |
---|
248 | static const AttributesImpl attributes; |
---|
249 | startElement(namespaceURI, localName, qname, attributes); |
---|
250 | } |
---|
251 | |
---|
252 | |
---|
253 | void XMLWriter::startElement(const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname, const Attributes& attributes) |
---|
254 | { |
---|
255 | if (_depth == 0 && !_inFragment && _elementCount > 1) |
---|
256 | throw XMLException("Not well-formed. Second root element found", nameToString(localName, qname)); |
---|
257 | |
---|
258 | if (_unclosedStartTag) closeStartTag(); |
---|
259 | prettyPrint(); |
---|
260 | writeStartElement(namespaceURI, localName, qname, attributes); |
---|
261 | _elementStack.push_back(Name(qname, namespaceURI, localName)); |
---|
262 | _contentWritten = false; |
---|
263 | ++_depth; |
---|
264 | } |
---|
265 | |
---|
266 | |
---|
267 | void XMLWriter::endElement(const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname) |
---|
268 | { |
---|
269 | if (_depth < 1) |
---|
270 | throw XMLException("No unclosed tag"); |
---|
271 | |
---|
272 | if (!_elementStack.back().equalsWeakly(qname, namespaceURI, localName)) |
---|
273 | throw XMLException("End tag does not match start tag", nameToString(localName, qname)); |
---|
274 | |
---|
275 | _elementStack.pop_back(); |
---|
276 | --_depth; |
---|
277 | if (!_unclosedStartTag) prettyPrint(); |
---|
278 | writeEndElement(namespaceURI, localName, qname); |
---|
279 | _contentWritten = false; |
---|
280 | if (_depth == 0) |
---|
281 | writeNewLine(); |
---|
282 | } |
---|
283 | |
---|
284 | |
---|
285 | void XMLWriter::emptyElement(const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname) |
---|
286 | { |
---|
287 | static const AttributesImpl attributes; |
---|
288 | emptyElement(namespaceURI, localName, qname, attributes); |
---|
289 | } |
---|
290 | |
---|
291 | |
---|
292 | void XMLWriter::emptyElement(const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname, const Attributes& attributes) |
---|
293 | { |
---|
294 | if (_depth == 0 && _elementCount > 1) |
---|
295 | throw XMLException("Not well-formed. Second root element found."); |
---|
296 | |
---|
297 | if (_unclosedStartTag) closeStartTag(); |
---|
298 | prettyPrint(); |
---|
299 | writeStartElement(namespaceURI, localName, qname, attributes); |
---|
300 | _contentWritten = false; |
---|
301 | writeMarkup("/"); |
---|
302 | closeStartTag(); |
---|
303 | } |
---|
304 | |
---|
305 | |
---|
306 | void XMLWriter::characters(const XMLChar ch[], int start, int length) |
---|
307 | { |
---|
308 | if (length == 0) return; |
---|
309 | |
---|
310 | if (_unclosedStartTag) closeStartTag(); |
---|
311 | _contentWritten = _contentWritten || length > 0; |
---|
312 | if (_inCDATA) |
---|
313 | { |
---|
314 | while (length-- > 0) writeXML(ch[start++]); |
---|
315 | } |
---|
316 | else |
---|
317 | { |
---|
318 | while (length-- > 0) |
---|
319 | { |
---|
320 | XMLChar c = ch[start++]; |
---|
321 | switch (c) |
---|
322 | { |
---|
323 | case '"': writeMarkup(MARKUP_QUOTENC); break; |
---|
324 | case '\'': writeMarkup(MARKUP_APOSENC); break; |
---|
325 | case '&': writeMarkup(MARKUP_AMPENC); break; |
---|
326 | case '<': writeMarkup(MARKUP_LTENC); break; |
---|
327 | case '>': writeMarkup(MARKUP_GTENC); break; |
---|
328 | default: |
---|
329 | if (c >= 0 && c < 32) |
---|
330 | { |
---|
331 | if (c == '\t' || c == '\r' || c == '\n') |
---|
332 | writeXML(c); |
---|
333 | else |
---|
334 | throw XMLException("Invalid character token."); |
---|
335 | } |
---|
336 | else writeXML(c); |
---|
337 | } |
---|
338 | } |
---|
339 | } |
---|
340 | } |
---|
341 | |
---|
342 | |
---|
343 | void XMLWriter::characters(const XMLString& str) |
---|
344 | { |
---|
345 | characters(str.data(), 0, (int) str.length()); |
---|
346 | } |
---|
347 | |
---|
348 | |
---|
349 | void XMLWriter::rawCharacters(const XMLString& str) |
---|
350 | { |
---|
351 | if (_unclosedStartTag) closeStartTag(); |
---|
352 | _contentWritten = _contentWritten || !str.empty(); |
---|
353 | writeXML(str); |
---|
354 | } |
---|
355 | |
---|
356 | |
---|
357 | void XMLWriter::ignorableWhitespace(const XMLChar ch[], int start, int length) |
---|
358 | { |
---|
359 | characters(ch, start, length); |
---|
360 | } |
---|
361 | |
---|
362 | |
---|
363 | void XMLWriter::processingInstruction(const XMLString& target, const XMLString& data) |
---|
364 | { |
---|
365 | if (_unclosedStartTag) closeStartTag(); |
---|
366 | prettyPrint(); |
---|
367 | writeMarkup("<?"); |
---|
368 | writeXML(target); |
---|
369 | if (!data.empty()) |
---|
370 | { |
---|
371 | writeMarkup(MARKUP_SPACE); |
---|
372 | writeXML(data); |
---|
373 | } |
---|
374 | writeMarkup("?>"); |
---|
375 | if (_depth == 0) |
---|
376 | writeNewLine(); |
---|
377 | } |
---|
378 | |
---|
379 | |
---|
380 | void XMLWriter::dataElement(const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname, |
---|
381 | const XMLString& data, |
---|
382 | const XMLString& attr1, const XMLString& value1, |
---|
383 | const XMLString& attr2, const XMLString& value2, |
---|
384 | const XMLString& attr3, const XMLString& value3) |
---|
385 | { |
---|
386 | static const XMLString CDATA = toXMLString("CDATA"); |
---|
387 | |
---|
388 | AttributesImpl attributes; |
---|
389 | if (!attr1.empty()) attributes.addAttribute(XMLString(), XMLString(), attr1, CDATA, value1); |
---|
390 | if (!attr2.empty()) attributes.addAttribute(XMLString(), XMLString(), attr2, CDATA, value2); |
---|
391 | if (!attr3.empty()) attributes.addAttribute(XMLString(), XMLString(), attr3, CDATA, value3); |
---|
392 | if (data.empty()) |
---|
393 | { |
---|
394 | emptyElement(namespaceURI, localName, qname, attributes); |
---|
395 | } |
---|
396 | else |
---|
397 | { |
---|
398 | startElement(namespaceURI, localName, qname, attributes); |
---|
399 | characters(data); |
---|
400 | endElement(namespaceURI, localName, qname); |
---|
401 | } |
---|
402 | } |
---|
403 | |
---|
404 | |
---|
405 | void XMLWriter::startPrefixMapping(const XMLString& prefix, const XMLString& namespaceURI) |
---|
406 | { |
---|
407 | if (prefix != NamespaceSupport::XML_NAMESPACE_PREFIX) |
---|
408 | _namespaces.declarePrefix(prefix, namespaceURI); |
---|
409 | } |
---|
410 | |
---|
411 | |
---|
412 | void XMLWriter::endPrefixMapping(const XMLString& prefix) |
---|
413 | { |
---|
414 | if (prefix != NamespaceSupport::XML_NAMESPACE_PREFIX) |
---|
415 | _namespaces.undeclarePrefix(prefix); |
---|
416 | } |
---|
417 | |
---|
418 | |
---|
419 | void XMLWriter::skippedEntity(const XMLString& name) |
---|
420 | { |
---|
421 | } |
---|
422 | |
---|
423 | |
---|
424 | void XMLWriter::startCDATA() |
---|
425 | { |
---|
426 | if (_inCDATA) throw XMLException("Cannot nest CDATA sections"); |
---|
427 | if (_unclosedStartTag) closeStartTag(); |
---|
428 | _inCDATA = true; |
---|
429 | writeMarkup(MARKUP_BEGIN_CDATA); |
---|
430 | } |
---|
431 | |
---|
432 | |
---|
433 | void XMLWriter::endCDATA() |
---|
434 | { |
---|
435 | poco_assert (_inCDATA); |
---|
436 | _inCDATA = false; |
---|
437 | writeMarkup(MARKUP_END_CDATA); |
---|
438 | } |
---|
439 | |
---|
440 | |
---|
441 | void XMLWriter::comment(const XMLChar ch[], int start, int length) |
---|
442 | { |
---|
443 | if (_unclosedStartTag) closeStartTag(); |
---|
444 | prettyPrint(); |
---|
445 | writeMarkup("<!--"); |
---|
446 | while (length-- > 0) writeXML(ch[start++]); |
---|
447 | writeMarkup("-->"); |
---|
448 | _contentWritten = false; |
---|
449 | } |
---|
450 | |
---|
451 | |
---|
452 | void XMLWriter::startDTD(const XMLString& name, const XMLString& publicId, const XMLString& systemId) |
---|
453 | { |
---|
454 | writeMarkup("<!DOCTYPE "); |
---|
455 | writeXML(name); |
---|
456 | if (!publicId.empty()) |
---|
457 | { |
---|
458 | writeMarkup(" PUBLIC \""); |
---|
459 | writeXML(publicId); |
---|
460 | writeMarkup("\""); |
---|
461 | } |
---|
462 | if (!systemId.empty()) |
---|
463 | { |
---|
464 | writeMarkup(" SYSTEM \""); |
---|
465 | writeXML(systemId); |
---|
466 | writeMarkup("\""); |
---|
467 | } |
---|
468 | _inDTD = true; |
---|
469 | } |
---|
470 | |
---|
471 | |
---|
472 | void XMLWriter::endDTD() |
---|
473 | { |
---|
474 | poco_assert (_inDTD); |
---|
475 | if (_inInternalDTD) |
---|
476 | { |
---|
477 | writeNewLine(); |
---|
478 | writeMarkup("]"); |
---|
479 | _inInternalDTD = false; |
---|
480 | } |
---|
481 | writeMarkup(">"); |
---|
482 | writeNewLine(); |
---|
483 | _inDTD = false; |
---|
484 | } |
---|
485 | |
---|
486 | |
---|
487 | void XMLWriter::startEntity(const XMLString& name) |
---|
488 | { |
---|
489 | } |
---|
490 | |
---|
491 | |
---|
492 | void XMLWriter::endEntity(const XMLString& name) |
---|
493 | { |
---|
494 | } |
---|
495 | |
---|
496 | |
---|
497 | void XMLWriter::notationDecl(const XMLString& name, const XMLString* publicId, const XMLString* systemId) |
---|
498 | { |
---|
499 | if (!_inDTD) throw XMLException("Notation declaration not within DTD"); |
---|
500 | if (!_inInternalDTD) |
---|
501 | { |
---|
502 | writeMarkup(" ["); |
---|
503 | _inInternalDTD = true; |
---|
504 | } |
---|
505 | if (_options & PRETTY_PRINT) |
---|
506 | { |
---|
507 | writeNewLine(); |
---|
508 | writeMarkup(MARKUP_TAB); |
---|
509 | } |
---|
510 | writeMarkup("<!NOTATION "); |
---|
511 | writeXML(name); |
---|
512 | if (systemId && !systemId->empty()) |
---|
513 | { |
---|
514 | writeMarkup(" SYSTEM \""); |
---|
515 | writeXML(*systemId); |
---|
516 | writeMarkup("\""); |
---|
517 | } |
---|
518 | if (publicId && !publicId->empty()) |
---|
519 | { |
---|
520 | writeMarkup(" PUBLIC \""); |
---|
521 | writeXML(*publicId); |
---|
522 | writeMarkup("\""); |
---|
523 | } |
---|
524 | writeMarkup(">"); |
---|
525 | } |
---|
526 | |
---|
527 | |
---|
528 | void XMLWriter::unparsedEntityDecl(const XMLString& name, const XMLString* publicId, const XMLString& systemId, const XMLString& notationName) |
---|
529 | { |
---|
530 | if (!_inDTD) throw XMLException("Entity declaration not within DTD"); |
---|
531 | if (!_inInternalDTD) |
---|
532 | { |
---|
533 | writeMarkup(" ["); |
---|
534 | _inInternalDTD = true; |
---|
535 | } |
---|
536 | if (_options & PRETTY_PRINT) |
---|
537 | { |
---|
538 | writeNewLine(); |
---|
539 | writeMarkup(MARKUP_TAB); |
---|
540 | } |
---|
541 | writeMarkup("<!ENTITY "); |
---|
542 | writeXML(name); |
---|
543 | if (!systemId.empty()) |
---|
544 | { |
---|
545 | writeMarkup(" SYSTEM \""); |
---|
546 | writeXML(systemId); |
---|
547 | writeMarkup("\""); |
---|
548 | } |
---|
549 | if (publicId && !publicId->empty()) |
---|
550 | { |
---|
551 | writeMarkup(" PUBLIC \""); |
---|
552 | writeXML(*publicId); |
---|
553 | writeMarkup("\""); |
---|
554 | } |
---|
555 | if (!notationName.empty()) |
---|
556 | { |
---|
557 | writeMarkup(" NDATA "); |
---|
558 | writeXML(notationName); |
---|
559 | } |
---|
560 | writeMarkup(">"); |
---|
561 | } |
---|
562 | |
---|
563 | |
---|
564 | void XMLWriter::prettyPrint() const |
---|
565 | { |
---|
566 | if ((_options & PRETTY_PRINT) && !_contentWritten) |
---|
567 | { |
---|
568 | writeNewLine(); |
---|
569 | writeIndent(); |
---|
570 | } |
---|
571 | } |
---|
572 | |
---|
573 | |
---|
574 | void XMLWriter::writeStartElement(const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname, const Attributes& attributes) |
---|
575 | { |
---|
576 | ++_elementCount; |
---|
577 | writeMarkup(MARKUP_LT); |
---|
578 | if (!localName.empty() && (qname.empty() || localName == qname)) |
---|
579 | { |
---|
580 | XMLString prefix; |
---|
581 | if (!namespaceURI.empty() && !_namespaces.isMapped(namespaceURI)) |
---|
582 | { |
---|
583 | prefix = newPrefix(); |
---|
584 | _namespaces.declarePrefix(prefix, namespaceURI); |
---|
585 | } |
---|
586 | else prefix = _namespaces.getPrefix(namespaceURI); |
---|
587 | writeName(prefix, localName); |
---|
588 | } |
---|
589 | else if (namespaceURI.empty() && localName.empty() && !qname.empty()) |
---|
590 | { |
---|
591 | writeXML(qname); |
---|
592 | } |
---|
593 | else if (!localName.empty() && !qname.empty()) |
---|
594 | { |
---|
595 | XMLString local; |
---|
596 | XMLString prefix; |
---|
597 | Name::split(qname, prefix, local); |
---|
598 | if (prefix.empty()) prefix = _namespaces.getPrefix(namespaceURI); |
---|
599 | const XMLString& uri = _namespaces.getURI(prefix); |
---|
600 | if ((uri.empty() || uri != namespaceURI) && !namespaceURI.empty()) |
---|
601 | { |
---|
602 | _namespaces.declarePrefix(prefix, namespaceURI); |
---|
603 | } |
---|
604 | writeName(prefix, localName); |
---|
605 | } |
---|
606 | else throw XMLException("Tag mismatch", nameToString(localName, qname)); |
---|
607 | |
---|
608 | declareAttributeNamespaces(attributes); |
---|
609 | AttributeMap attributeMap; |
---|
610 | addNamespaceAttributes(attributeMap); |
---|
611 | addAttributes(attributeMap, attributes, namespaceURI); |
---|
612 | writeAttributes(attributeMap); |
---|
613 | _unclosedStartTag = true; |
---|
614 | _namespaces.pushContext(); |
---|
615 | } |
---|
616 | |
---|
617 | |
---|
618 | void XMLWriter::writeEndElement(const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname) |
---|
619 | { |
---|
620 | if (_unclosedStartTag) |
---|
621 | { |
---|
622 | writeMarkup(MARKUP_SLASHGT); |
---|
623 | _unclosedStartTag = false; |
---|
624 | } |
---|
625 | else |
---|
626 | { |
---|
627 | writeMarkup(MARKUP_LTSLASH); |
---|
628 | if (!localName.empty()) |
---|
629 | { |
---|
630 | XMLString prefix = _namespaces.getPrefix(namespaceURI); |
---|
631 | writeName(prefix, localName); |
---|
632 | } |
---|
633 | else |
---|
634 | { |
---|
635 | writeXML(qname); |
---|
636 | } |
---|
637 | writeMarkup(MARKUP_GT); |
---|
638 | } |
---|
639 | _namespaces.popContext(); |
---|
640 | } |
---|
641 | |
---|
642 | |
---|
643 | void XMLWriter::closeStartTag() |
---|
644 | { |
---|
645 | _unclosedStartTag = false; |
---|
646 | writeMarkup(MARKUP_GT); |
---|
647 | } |
---|
648 | |
---|
649 | |
---|
650 | void XMLWriter::declareAttributeNamespaces(const Attributes& attributes) |
---|
651 | { |
---|
652 | for (int i = 0; i < attributes.getLength(); i++) |
---|
653 | { |
---|
654 | XMLString namespaceURI = attributes.getURI(i); |
---|
655 | XMLString localName = attributes.getLocalName(i); |
---|
656 | XMLString qname = attributes.getQName(i); |
---|
657 | if (!localName.empty()) |
---|
658 | { |
---|
659 | XMLString prefix; |
---|
660 | XMLString splitLocalName; |
---|
661 | Name::split(qname, prefix, splitLocalName); |
---|
662 | if (prefix.empty()) prefix = _namespaces.getPrefix(namespaceURI); |
---|
663 | if (prefix.empty() && !namespaceURI.empty() && !_namespaces.isMapped(namespaceURI)) |
---|
664 | { |
---|
665 | prefix = newPrefix(); |
---|
666 | _namespaces.declarePrefix(prefix, namespaceURI); |
---|
667 | } |
---|
668 | |
---|
669 | |
---|
670 | const XMLString& uri = _namespaces.getURI(prefix); |
---|
671 | if ((uri.empty() || uri != namespaceURI) && !namespaceURI.empty()) |
---|
672 | { |
---|
673 | _namespaces.declarePrefix(prefix, namespaceURI); |
---|
674 | } |
---|
675 | } |
---|
676 | } |
---|
677 | } |
---|
678 | |
---|
679 | |
---|
680 | void XMLWriter::addNamespaceAttributes(AttributeMap& attributeMap) |
---|
681 | { |
---|
682 | NamespaceSupport::PrefixSet prefixes; |
---|
683 | _namespaces.getDeclaredPrefixes(prefixes); |
---|
684 | for (NamespaceSupport::PrefixSet::const_iterator it = prefixes.begin(); it != prefixes.end(); ++it) |
---|
685 | { |
---|
686 | XMLString prefix = *it; |
---|
687 | XMLString uri = _namespaces.getURI(prefix); |
---|
688 | XMLString qname = NamespaceSupport::XMLNS_NAMESPACE_PREFIX; |
---|
689 | |
---|
690 | if (!prefix.empty()) |
---|
691 | { |
---|
692 | qname.append(toXMLString(MARKUP_COLON)); |
---|
693 | qname.append(prefix); |
---|
694 | } |
---|
695 | attributeMap[qname] = uri; |
---|
696 | } |
---|
697 | } |
---|
698 | |
---|
699 | |
---|
700 | void XMLWriter::addAttributes(AttributeMap& attributeMap, const Attributes& attributes, const XMLString& elementNamespaceURI) |
---|
701 | { |
---|
702 | for (int i = 0; i < attributes.getLength(); i++) |
---|
703 | { |
---|
704 | XMLString namespaceURI = attributes.getURI(i); |
---|
705 | XMLString localName = attributes.getLocalName(i); |
---|
706 | XMLString qname = attributes.getQName(i); |
---|
707 | if (!localName.empty()) |
---|
708 | { |
---|
709 | XMLString prefix; |
---|
710 | if (namespaceURI != elementNamespaceURI) |
---|
711 | prefix = _namespaces.getPrefix(namespaceURI); |
---|
712 | if (!prefix.empty()) |
---|
713 | { |
---|
714 | qname = prefix; |
---|
715 | qname.append(toXMLString(MARKUP_COLON)); |
---|
716 | } |
---|
717 | else qname.clear(); |
---|
718 | qname.append(localName); |
---|
719 | } |
---|
720 | attributeMap[qname] = attributes.getValue(i); |
---|
721 | } |
---|
722 | } |
---|
723 | |
---|
724 | |
---|
725 | void XMLWriter::writeAttributes(const AttributeMap& attributeMap) |
---|
726 | { |
---|
727 | for (AttributeMap::const_iterator it = attributeMap.begin(); it != attributeMap.end(); ++it) |
---|
728 | { |
---|
729 | writeMarkup(MARKUP_SPACE); |
---|
730 | writeXML(it->first); |
---|
731 | writeMarkup(MARKUP_EQQUOT); |
---|
732 | characters(it->second); |
---|
733 | writeMarkup(MARKUP_QUOT); |
---|
734 | } |
---|
735 | } |
---|
736 | |
---|
737 | |
---|
738 | void XMLWriter::writeMarkup(const std::string& str) const |
---|
739 | { |
---|
740 | #if defined(XML_UNICODE_WCHAR_T) |
---|
741 | const XMLString xmlString = toXMLString(str); |
---|
742 | writeXML(xmlString); |
---|
743 | #else |
---|
744 | _pTextConverter->write(str.data(), (int) str.size()); |
---|
745 | #endif |
---|
746 | } |
---|
747 | |
---|
748 | |
---|
749 | void XMLWriter::writeXML(const XMLString& str) const |
---|
750 | { |
---|
751 | _pTextConverter->write((const char*) str.data(), (int) str.size()*sizeof(XMLChar)); |
---|
752 | } |
---|
753 | |
---|
754 | |
---|
755 | void XMLWriter::writeXML(XMLChar ch) const |
---|
756 | { |
---|
757 | _pTextConverter->write((const char*) &ch, sizeof(ch)); |
---|
758 | } |
---|
759 | |
---|
760 | |
---|
761 | void XMLWriter::writeName(const XMLString& prefix, const XMLString& localName) |
---|
762 | { |
---|
763 | if (prefix.empty()) |
---|
764 | { |
---|
765 | writeXML(localName); |
---|
766 | } |
---|
767 | else |
---|
768 | { |
---|
769 | writeXML(prefix); |
---|
770 | writeMarkup(MARKUP_COLON); |
---|
771 | writeXML(localName); |
---|
772 | } |
---|
773 | } |
---|
774 | |
---|
775 | |
---|
776 | void XMLWriter::writeNewLine() const |
---|
777 | { |
---|
778 | if (_options & PRETTY_PRINT) |
---|
779 | writeMarkup(_newLine); |
---|
780 | } |
---|
781 | |
---|
782 | |
---|
783 | void XMLWriter::writeIndent() const |
---|
784 | { |
---|
785 | for (int i = 0; i < _depth; ++i) |
---|
786 | writeMarkup(MARKUP_TAB); |
---|
787 | } |
---|
788 | |
---|
789 | |
---|
790 | void XMLWriter::writeXMLDeclaration() |
---|
791 | { |
---|
792 | writeMarkup("<?xml version=\"1.0\""); |
---|
793 | if (!_encoding.empty()) |
---|
794 | { |
---|
795 | writeMarkup(" encoding=\""); |
---|
796 | writeMarkup(_encoding); |
---|
797 | writeMarkup("\""); |
---|
798 | } |
---|
799 | writeMarkup("?>"); |
---|
800 | writeNewLine(); |
---|
801 | } |
---|
802 | |
---|
803 | |
---|
804 | std::string XMLWriter::nameToString(const XMLString& localName, const XMLString& qname) |
---|
805 | { |
---|
806 | if (qname.empty()) |
---|
807 | return fromXMLString(localName); |
---|
808 | else |
---|
809 | return fromXMLString(qname); |
---|
810 | } |
---|
811 | |
---|
812 | |
---|
813 | XMLString XMLWriter::newPrefix() |
---|
814 | { |
---|
815 | std::ostringstream str; |
---|
816 | str << "ns" << ++_prefix; |
---|
817 | return toXMLString(str.str()); |
---|
818 | } |
---|
819 | |
---|
820 | |
---|
821 | } } // namespace Poco::XML |
---|