📄 xmlwriter.cpp
字号:
{ if (prefix != NamespaceSupport::XML_NAMESPACE_PREFIX) _namespaces.undeclarePrefix(prefix);}void XMLWriter::skippedEntity(const XMLString& name){}void XMLWriter::startCDATA(){ if (_inCDATA) throw XMLException("Cannot nest CDATA sections"); if (_unclosedStartTag) closeStartTag(); _inCDATA = true; writeMarkup(MARKUP_BEGIN_CDATA);}void XMLWriter::endCDATA(){ poco_assert (_inCDATA); _inCDATA = false; writeMarkup(MARKUP_END_CDATA);}void XMLWriter::comment(const XMLChar ch[], int start, int length){ if (_unclosedStartTag) closeStartTag(); prettyPrint(); writeMarkup("<!--"); while (length-- > 0) writeXML(ch[start++]); writeMarkup("-->"); _contentWritten = false;}void XMLWriter::startDTD(const XMLString& name, const XMLString& publicId, const XMLString& systemId){ writeMarkup("<!DOCTYPE "); writeXML(name); if (!publicId.empty()) { writeMarkup(" PUBLIC \""); writeXML(publicId); writeMarkup("\""); } if (!systemId.empty()) { writeMarkup(" SYSTEM \""); writeXML(systemId); writeMarkup("\""); } _inDTD = true;}void XMLWriter::endDTD(){ poco_assert (_inDTD); if (_inInternalDTD) { writeNewLine(); writeMarkup("]"); _inInternalDTD = false; } writeMarkup(">"); writeNewLine(); _inDTD = false;}void XMLWriter::startEntity(const XMLString& name){}void XMLWriter::endEntity(const XMLString& name){}void XMLWriter::notationDecl(const XMLString& name, const XMLString* publicId, const XMLString* systemId){ if (!_inDTD) throw XMLException("Notation declaration not within DTD"); if (!_inInternalDTD) { writeMarkup(" ["); _inInternalDTD = true; } if (_options & PRETTY_PRINT) { writeNewLine(); writeMarkup(MARKUP_TAB); } writeMarkup("<!NOTATION "); writeXML(name); if (systemId && !systemId->empty()) { writeMarkup(" SYSTEM \""); writeXML(*systemId); writeMarkup("\""); } if (publicId && !publicId->empty()) { writeMarkup(" PUBLIC \""); writeXML(*publicId); writeMarkup("\""); } writeMarkup(">");}void XMLWriter::unparsedEntityDecl(const XMLString& name, const XMLString* publicId, const XMLString& systemId, const XMLString& notationName){ if (!_inDTD) throw XMLException("Entity declaration not within DTD"); if (!_inInternalDTD) { writeMarkup(" ["); _inInternalDTD = true; } if (_options & PRETTY_PRINT) { writeNewLine(); writeMarkup(MARKUP_TAB); } writeMarkup("<!ENTITY "); writeXML(name); if (!systemId.empty()) { writeMarkup(" SYSTEM \""); writeXML(systemId); writeMarkup("\""); } if (publicId && !publicId->empty()) { writeMarkup(" PUBLIC \""); writeXML(*publicId); writeMarkup("\""); } if (!notationName.empty()) { writeMarkup(" NDATA "); writeXML(notationName); } writeMarkup(">");}void XMLWriter::prettyPrint() const{ if ((_options & PRETTY_PRINT) && !_contentWritten) { writeNewLine(); writeIndent(); }}void XMLWriter::writeStartElement(const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname, const Attributes& attributes){ ++_elementCount; writeMarkup(MARKUP_LT); if (!localName.empty() && (qname.empty() || localName == qname)) { XMLString prefix; if (!namespaceURI.empty() && !_namespaces.isMapped(namespaceURI)) { prefix = newPrefix(); _namespaces.declarePrefix(prefix, namespaceURI); } else prefix = _namespaces.getPrefix(namespaceURI); writeName(prefix, localName); } else if (namespaceURI.empty() && localName.empty() && !qname.empty()) { writeXML(qname); } else if (!localName.empty() && !qname.empty()) { XMLString local; XMLString prefix; Name::split(qname, prefix, local); if (prefix.empty()) prefix = _namespaces.getPrefix(namespaceURI); const XMLString& uri = _namespaces.getURI(prefix); if ((uri.empty() || uri != namespaceURI) && !namespaceURI.empty()) { _namespaces.declarePrefix(prefix, namespaceURI); } writeName(prefix, localName); } else throw XMLException("Tag mismatch", nameToString(localName, qname)); declareAttributeNamespaces(attributes); AttributeMap attributeMap; addNamespaceAttributes(attributeMap); addAttributes(attributeMap, attributes, namespaceURI); writeAttributes(attributeMap); _unclosedStartTag = true; _namespaces.pushContext();}void XMLWriter::writeEndElement(const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname){ if (_unclosedStartTag) { writeMarkup(MARKUP_SLASHGT); _unclosedStartTag = false; } else { writeMarkup(MARKUP_LTSLASH); if (!localName.empty()) { XMLString prefix = _namespaces.getPrefix(namespaceURI); writeName(prefix, localName); } else { writeXML(qname); } writeMarkup(MARKUP_GT); } _namespaces.popContext();}void XMLWriter::closeStartTag(){ _unclosedStartTag = false; writeMarkup(MARKUP_GT);}void XMLWriter::declareAttributeNamespaces(const Attributes& attributes){ for (int i = 0; i < attributes.getLength(); i++) { XMLString namespaceURI = attributes.getURI(i); XMLString localName = attributes.getLocalName(i); XMLString qname = attributes.getQName(i); if (!localName.empty()) { XMLString prefix; XMLString splitLocalName; Name::split(qname, prefix, splitLocalName); if (prefix.empty()) prefix = _namespaces.getPrefix(namespaceURI); if (prefix.empty() && !namespaceURI.empty() && !_namespaces.isMapped(namespaceURI)) { prefix = newPrefix(); _namespaces.declarePrefix(prefix, namespaceURI); } const XMLString& uri = _namespaces.getURI(prefix); if ((uri.empty() || uri != namespaceURI) && !namespaceURI.empty()) { _namespaces.declarePrefix(prefix, namespaceURI); } } }}void XMLWriter::addNamespaceAttributes(AttributeMap& attributeMap){ NamespaceSupport::PrefixSet prefixes; _namespaces.getDeclaredPrefixes(prefixes); for (NamespaceSupport::PrefixSet::const_iterator it = prefixes.begin(); it != prefixes.end(); ++it) { XMLString prefix = *it; XMLString uri = _namespaces.getURI(prefix); XMLString qname = NamespaceSupport::XMLNS_NAMESPACE_PREFIX; if (!prefix.empty()) { qname.append(toXMLString(MARKUP_COLON)); qname.append(prefix); } attributeMap[qname] = uri; }}void XMLWriter::addAttributes(AttributeMap& attributeMap, const Attributes& attributes, const XMLString& elementNamespaceURI){ for (int i = 0; i < attributes.getLength(); i++) { XMLString namespaceURI = attributes.getURI(i); XMLString localName = attributes.getLocalName(i); XMLString qname = attributes.getQName(i); if (!localName.empty()) { XMLString prefix; if (namespaceURI != elementNamespaceURI) prefix = _namespaces.getPrefix(namespaceURI); if (!prefix.empty()) { qname = prefix; qname.append(toXMLString(MARKUP_COLON)); } else qname.clear(); qname.append(localName); } attributeMap[qname] = attributes.getValue(i); }}void XMLWriter::writeAttributes(const AttributeMap& attributeMap){ for (AttributeMap::const_iterator it = attributeMap.begin(); it != attributeMap.end(); ++it) { writeMarkup(MARKUP_SPACE); writeXML(it->first); writeMarkup(MARKUP_EQQUOT); characters(it->second); writeMarkup(MARKUP_QUOT); }}void XMLWriter::writeMarkup(const std::string& str) const{ _pTextConverter->write(str.data(), (int) str.size());}void XMLWriter::writeXML(const XMLString& str) const{ _pTextConverter->write((const char*) str.data(), (int) str.size()*sizeof(XMLChar));}void XMLWriter::writeXML(XMLChar ch) const{ _pTextConverter->write((const char*) &ch, sizeof(ch));}void XMLWriter::writeName(const XMLString& prefix, const XMLString& localName){ if (prefix.empty()) { writeXML(localName); } else { writeXML(prefix); writeMarkup(MARKUP_COLON); writeXML(localName); }}void XMLWriter::writeNewLine() const{ if (_options & PRETTY_PRINT) writeMarkup(_newLine);}void XMLWriter::writeIndent() const{ for (int i = 0; i < _depth; ++i) writeMarkup(MARKUP_TAB);}void XMLWriter::writeXMLDeclaration(){ writeMarkup("<?xml version=\"1.0\""); if (!_encoding.empty()) { writeMarkup(" encoding=\""); writeMarkup(_encoding); writeMarkup("\""); } writeMarkup("?>"); writeNewLine();}std::string XMLWriter::nameToString(const XMLString& localName, const XMLString& qname){ if (qname.empty()) return fromXMLString(localName); else return fromXMLString(qname);}XMLString XMLWriter::newPrefix(){ std::ostringstream str; str << "ns" << ++_prefix; return toXMLString(str.str());}} } // namespace Poco::XML
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -