- added Poco::XML::XMLWriter::depth() member function.

- added Poco::XML::XMLWriter::uniquePrefix() and Poco::XML::XMLWriter::isNamespaceMapped().
- Poco::FileChannel now supports a new rotateOnOpen property (true/false) which can be used
  to force rotation of the log file when it's opened.
- fixed a bug in Poco::XML::XMLWriter::emptyElement(): need to pop namespace context
This commit is contained in:
Guenter Obiltschnig
2012-11-10 11:49:39 +01:00
parent a628c56a01
commit 80c45c859d
2 changed files with 39 additions and 6 deletions

View File

@@ -1,7 +1,7 @@
// //
// XMLWriter.h // XMLWriter.h
// //
// $Id: //poco/1.4/XML/include/Poco/XML/XMLWriter.h#1 $ // $Id: //poco/1.4/XML/include/Poco/XML/XMLWriter.h#3 $
// //
// Library: XML // Library: XML
// Package: XML // Package: XML
@@ -279,6 +279,24 @@ public:
static const std::string NEWLINE_CRLF; static const std::string NEWLINE_CRLF;
static const std::string NEWLINE_LF; static const std::string NEWLINE_LF;
// Namespace support.
XMLString uniquePrefix();
/// Creates and returns a unique namespace prefix that
/// can be used with startPrefixMapping().
bool isNamespaceMapped(const std::string& namespc) const;
/// Returns true if the given namespace has been mapped
/// to a prefix in the current element or its ancestors.
// Misc.
int depth() const;
/// Return the number of nested XML elements.
///
/// Will be -1 if no document or fragment has been started,
/// 0 if the document or fragment has been started,
/// 1 if the document element has been written and
/// > 1 for every element nested within the document element.
protected: protected:
typedef std::map<XMLString, XMLString> AttributeMap; typedef std::map<XMLString, XMLString> AttributeMap;
@@ -298,7 +316,6 @@ protected:
void addAttributes(AttributeMap& attributeMap, const Attributes& attributes, const XMLString& elementNamespaceURI); void addAttributes(AttributeMap& attributeMap, const Attributes& attributes, const XMLString& elementNamespaceURI);
void writeAttributes(const AttributeMap& attributeMap); void writeAttributes(const AttributeMap& attributeMap);
void prettyPrint() const; void prettyPrint() const;
XMLString newPrefix();
static std::string nameToString(const XMLString& localName, const XMLString& qname); static std::string nameToString(const XMLString& localName, const XMLString& qname);
private: private:
@@ -357,6 +374,15 @@ private:
}; };
//
// inlines
//
inline int XMLWriter::depth() const
{
return _depth;
}
} } // namespace Poco::XML } } // namespace Poco::XML

View File

@@ -1,7 +1,7 @@
// //
// XMLWriter.cpp // XMLWriter.cpp
// //
// $Id: //poco/1.4/XML/src/XMLWriter.cpp#3 $ // $Id: //poco/1.4/XML/src/XMLWriter.cpp#5 $
// //
// Library: XML // Library: XML
// Package: XML // Package: XML
@@ -321,6 +321,7 @@ void XMLWriter::emptyElement(const XMLString& namespaceURI, const XMLString& loc
_contentWritten = false; _contentWritten = false;
writeMarkup("/"); writeMarkup("/");
closeStartTag(); closeStartTag();
_namespaces.popContext();
} }
@@ -617,7 +618,7 @@ void XMLWriter::writeStartElement(const XMLString& namespaceURI, const XMLString
XMLString prefix; XMLString prefix;
if (!namespaceURI.empty() && !_namespaces.isMapped(namespaceURI)) if (!namespaceURI.empty() && !_namespaces.isMapped(namespaceURI))
{ {
prefix = newPrefix(); prefix = uniquePrefix();
_namespaces.declarePrefix(prefix, namespaceURI); _namespaces.declarePrefix(prefix, namespaceURI);
} }
else prefix = _namespaces.getPrefix(namespaceURI); else prefix = _namespaces.getPrefix(namespaceURI);
@@ -702,7 +703,7 @@ void XMLWriter::declareAttributeNamespaces(const Attributes& attributes)
if (prefix.empty()) prefix = _namespaces.getPrefix(namespaceURI); if (prefix.empty()) prefix = _namespaces.getPrefix(namespaceURI);
if (prefix.empty() && !namespaceURI.empty() && !_namespaces.isMapped(namespaceURI)) if (prefix.empty() && !namespaceURI.empty() && !_namespaces.isMapped(namespaceURI))
{ {
prefix = newPrefix(); prefix = uniquePrefix();
_namespaces.declarePrefix(prefix, namespaceURI); _namespaces.declarePrefix(prefix, namespaceURI);
} }
@@ -882,7 +883,7 @@ std::string XMLWriter::nameToString(const XMLString& localName, const XMLString&
} }
XMLString XMLWriter::newPrefix() XMLString XMLWriter::uniquePrefix()
{ {
std::ostringstream str; std::ostringstream str;
str << "ns" << ++_prefix; str << "ns" << ++_prefix;
@@ -890,4 +891,10 @@ XMLString XMLWriter::newPrefix()
} }
bool XMLWriter::isNamespaceMapped(const std::string& namespc) const
{
return _namespaces.isMapped(namespc);
}
} } // namespace Poco::XML } } // namespace Poco::XML