XML: complete merge from 1.4.2

This commit is contained in:
Marian Krivos
2011-09-14 14:34:31 +00:00
parent 189695f9c3
commit 16ff8caf6b
59 changed files with 5219 additions and 474 deletions

View File

@@ -61,20 +61,31 @@ public:
Node* insertBefore(Node* newChild, Node* refChild);
Node* replaceChild(Node* newChild, Node* oldChild);
Node* removeChild(Node* oldChild);
Node* appendChild(Node* newChild);
bool hasChildNodes() const;
bool hasAttributes() const;
Node* appendChild(Node* newChild);
bool hasChildNodes() const;
bool hasAttributes() const;
Node* getNodeByPath(const XMLString& path) const;
Node* getNodeByPathNS(const XMLString& path, const NSMap& nsMap) const;
protected:
AbstractContainerNode(Document* pOwnerDocument);
AbstractContainerNode(Document* pOwnerDocument);
AbstractContainerNode(Document* pOwnerDocument, const AbstractContainerNode& node);
~AbstractContainerNode();
void dispatchNodeRemovedFromDocument();
void dispatchNodeInsertedIntoDocument();
void dispatchNodeRemovedFromDocument();
void dispatchNodeInsertedIntoDocument();
static const Node* findNode(XMLString::const_iterator& it, const XMLString::const_iterator& end, const Node* pNode, const NSMap* pNSMap);
static const Node* findElement(const XMLString& name, const Node* pNode, const NSMap* pNSMap);
static const Node* findElement(int index, const Node* pNode, const NSMap* pNSMap);
static const Node* findElement(const XMLString& attr, const XMLString& value, const Node* pNode, const NSMap* pNSMap);
static const Attr* findAttribute(const XMLString& name, const Node* pNode, const NSMap* pNSMap);
bool hasAttributeValue(const XMLString& name, const XMLString& value, const NSMap* pNSMap) const;
static bool namesAreEqual(const Node* pNode1, const Node* pNode2, const NSMap* pNSMap);
static bool namesAreEqual(const Node* pNode, const XMLString& name, const NSMap* pNSMap);
private:
AbstractNode* _pFirstChild;
AbstractNode* _pFirstChild;
friend class AbstractNode;
friend class NodeAppender;

View File

@@ -91,10 +91,12 @@ public:
void removeEventListener(const XMLString& type, EventListener* listener, bool useCapture);
bool dispatchEvent(Event* evt);
// Extensions
XMLString innerText() const;
// Extensions
XMLString innerText() const;
Node* getNodeByPath(const XMLString& path) const;
Node* getNodeByPathNS(const XMLString& path, const NSMap& nsMap) const;
virtual void autoRelease();
virtual void autoRelease();
protected:
AbstractNode(Document* pOwnerDocument);

View File

@@ -111,15 +111,15 @@ public:
EntityResolver* getEntityResolver() const;
/// Returns the entity resolver used by the underlying SAXParser.
void setEntityResolver(EntityResolver* pEntityResolver);
/// Sets the entity resolver on the underlying SAXParser.
void setEntityResolver(EntityResolver* pEntityResolver);
/// Sets the entity resolver on the underlying SAXParser.
static const XMLString FEATURE_WHITESPACE;
static const XMLString FEATURE_FILTER_WHITESPACE;
private:
SAXParser _saxParser;
NamePool* _pNamePool;
bool _whitespace;
SAXParser _saxParser;
NamePool* _pNamePool;
bool _filterWhitespace;
};

View File

@@ -86,11 +86,22 @@ public:
/// of supported values.
const std::string& getNewLine() const;
/// Returns the line ending characters used by the
/// internal XMLWriter.
/// Returns the line ending characters used by the
/// internal XMLWriter.
void writeNode(XMLByteOutputStream& ostr, const Node* pNode);
/// Writes the XML for the given node to the specified stream.
void setIndent(const std::string& indent);
/// Sets the string used for one indentation step.
///
/// The default is a single TAB character.
/// The given string should only contain TAB or SPACE
/// characters (e.g., a single TAB character, or
/// two to four SPACE characters).
const std::string& getIndent() const;
/// Returns the string used for one indentation step.
void writeNode(XMLByteOutputStream& ostr, const Node* pNode);
/// Writes the XML for the given node to the specified stream.
void writeNode(const std::string& systemId, const Node* pNode);
/// Writes the XML for the given node to the file specified in systemId,
@@ -98,9 +109,10 @@ public:
private:
std::string _encodingName;
Poco::TextEncoding* _pTextEncoding;
int _options;
std::string _newLine;
Poco::TextEncoding* _pTextEncoding;
int _options;
std::string _newLine;
std::string _indent;
};
@@ -125,6 +137,12 @@ inline const std::string& DOMWriter::getNewLine() const
}
inline const std::string& DOMWriter::getIndent() const
{
return _indent;
}
} } // namespace Poco::XML

View File

@@ -43,6 +43,7 @@
#include "Poco/XML/XML.h"
#include "Poco/DOM/AbstractContainerNode.h"
#include "Poco/DOM/DocumentEvent.h"
#include "Poco/DOM/Element.h"
#include "Poco/XML/XMLString.h"
#include "Poco/XML/NamePool.h"
#include "Poco/AutoReleasePool.h"

View File

@@ -66,11 +66,12 @@ protected:
Node* find(const Node* pParent, unsigned long index) const;
const Node* _pParent;
XMLString _name;
mutable unsigned long _count;
friend class Element;
friend class Document;
XMLString _name;
mutable unsigned long _count;
friend class AbstractContainerNode;
friend class Element;
friend class Document;
};
@@ -92,11 +93,12 @@ protected:
const Node* _pParent;
XMLString _localName;
XMLString _namespaceURI;
mutable unsigned long _count;
friend class Element;
friend class Document;
XMLString _namespaceURI;
mutable unsigned long _count;
friend class AbstractContainerNode;
friend class Element;
friend class Document;
};

View File

@@ -43,6 +43,7 @@
#include "Poco/XML/XML.h"
#include "Poco/DOM/EventTarget.h"
#include "Poco/XML/XMLString.h"
#include "Poco/SAX/NamespaceSupport.h"
namespace Poco {
@@ -226,17 +227,68 @@ public:
/// Returns the local name of the node.
virtual bool hasAttributes() const = 0;
/// Returns whether this node (if it is an element) has any attributes.
// Extensions
virtual XMLString innerText() const = 0;
/// Returns a string containing the concatenated values of the node
/// and all its child nodes.
///
/// This method is not part of the W3C Document Object Model.
/// Returns whether this node (if it is an element) has any attributes.
// Extensions
typedef Poco::XML::NamespaceSupport NSMap;
virtual XMLString innerText() const = 0;
/// Returns a string containing the concatenated values of the node
/// and all its child nodes.
///
/// This method is not part of the W3C Document Object Model.
virtual Node* getNodeByPath(const XMLString& path) const = 0;
/// Searches a node (element or attribute) based on a simplified XPath
/// expression.
///
/// Only simple XPath expressions are supported. These are the slash
/// notation for specifying paths to elements, and the square bracket
/// expression for finding elements by their index, by attribute value,
/// or finding attributes by names.
///
/// The slash at the beginning is optional, the evaluation always starts
/// at this element. A double-slash at the beginning recursively searches
/// the entire subtree for the first element.
///
/// Examples:
/// elem1/elem2/elem3
/// /elem1/elem2/elem3
/// /elem1/elem2[1]
/// /elem1/elem2[@attr1]
/// /elem1/elem2[@attr1='value']
/// //elem2[@attr1='value']
/// //[@attr1='value']
///
/// This method is an extension to the W3C Document Object Model.
virtual Node* getNodeByPathNS(const XMLString& path, const NSMap& nsMap) const = 0;
/// Searches a node (element or attribute) based on a simplified XPath
/// expression. The given NSMap must contain mappings from namespace
/// prefixes to namespace URIs for all namespace prefixes used in
/// the path expression.
///
/// Only simple XPath expressions are supported. These are the slash
/// notation for specifying paths to elements, and the square bracket
/// expression for finding elements by their index, by attribute value,
/// or finding attributes by names.
///
/// The slash at the beginning is optional, the evaluation always starts
/// at this element. A double-slash at the beginning recursively searches
/// the entire subtree for the first element.
///
/// Examples:
/// /ns1:elem1/ns2:elem2/ns2:elem3
/// /ns1:elem1/ns2:elem2[1]
/// /ns1:elem1/ns2:elem2[@attr1]
/// /ns1:elem1/ns2:elem2[@attr1='value']
/// //ns2:elem2[@ns1:attr1='value']
/// //[@ns1:attr1='value']
///
/// This method is an extension to the W3C Document Object Model.
protected:
virtual ~Node();
virtual ~Node();
};

View File

@@ -77,13 +77,13 @@ class XML_API TreeWalker
{
public:
TreeWalker(Node* root, unsigned long whatToShow, NodeFilter* pFilter = 0);
/// Creates a TreeWalker over the subtree rooted at the specified node.
TreeWalker(const TreeWalker& walker);
/// Creates a TreeWalker by copying another NodeIterator.
TreeWalker& operator = (const TreeWalker& walker);
/// Assignment operator.
/// Creates a TreeWalker over the subtree rooted at the specified node.
TreeWalker(const TreeWalker& walker);
/// Creates a TreeWalker by copying another TreeWalker.
TreeWalker& operator = (const TreeWalker& walker);
/// Assignment operator.
~TreeWalker();
/// Destroys the TreeWalker.

View File

@@ -212,22 +212,22 @@ inline int AttributesImpl::getLength() const
inline const XMLString& AttributesImpl::getLocalName(int i) const
{
poco_assert (i < _attributes.size());
return _attributes[i].localName;
poco_assert (0 <= i && i < static_cast<int>(_attributes.size()));
return _attributes[i].localName;
}
inline const XMLString& AttributesImpl::getQName(int i) const
{
poco_assert (i < _attributes.size());
return _attributes[i].qname;
poco_assert (0 <= i && i < static_cast<int>(_attributes.size()));
return _attributes[i].qname;
}
inline const XMLString& AttributesImpl::getType(int i) const
{
poco_assert (i < _attributes.size());
return _attributes[i].type;
poco_assert (0 <= i && i < static_cast<int>(_attributes.size()));
return _attributes[i].type;
}
@@ -253,8 +253,8 @@ inline const XMLString& AttributesImpl::getType(const XMLString& namespaceURI, c
inline const XMLString& AttributesImpl::getValue(int i) const
{
poco_assert (i < _attributes.size());
return _attributes[i].value;
poco_assert (0 <= i && i < static_cast<int>(_attributes.size()));
return _attributes[i].value;
}
@@ -280,15 +280,15 @@ inline const XMLString& AttributesImpl::getValue(const XMLString& namespaceURI,
inline const XMLString& AttributesImpl::getURI(int i) const
{
poco_assert (i < _attributes.size());
return _attributes[i].namespaceURI;
poco_assert (0 <= i && i < static_cast<int>(_attributes.size()));
return _attributes[i].namespaceURI;
}
inline bool AttributesImpl::isSpecified(int i) const
{
poco_assert (i < _attributes.size());
return _attributes[i].specified;
poco_assert (0 <= i && i < static_cast<int>(_attributes.size()));
return _attributes[i].specified;
}

View File

@@ -92,13 +92,13 @@ public:
/// Remove the given namespace prefix.
void getDeclaredPrefixes(PrefixSet& prefixes) const;
/// Return an enumeration of all prefixes declared in this context.
///
/// The empty (default) prefix will be included in this enumeration; note that
/// this behaviour differs from that of getPrefix(java.lang.String) and getPrefixes().
/// Return an enumeration of all prefixes declared in this context.
///
/// The empty (default) prefix will be included in this enumeration; note that
/// this behaviour differs from that of getPrefix() and getPrefixes().
const XMLString& getPrefix(const XMLString& namespaceURI) const;
/// Return one of the prefixes mapped to a Namespace URI.
const XMLString& getPrefix(const XMLString& namespaceURI) const;
/// Return one of the prefixes mapped to a Namespace URI.
///
/// If more than one prefix is currently mapped to the same URI, this method
/// will make an arbitrary selection; if you want all of the prefixes, use the
@@ -179,14 +179,14 @@ public:
/// prefixes currently declared. The result will be returned in
/// namespaceURI and localName.
/// If the raw name has a prefix that has not been declared, then the return
/// value will be false, otherwise true.
///
/// Note that attribute names are processed differently than element names:
/// an unprefixed element name will received the
/// default Namespace (if any), while an unprefixed element name will not.
/// value will be false, otherwise true.
///
/// Note that attribute names are processed differently than element names:
/// an unprefixed element name will receive the
/// default Namespace (if any), while an unprefixed attribute name will not.
void reset();
/// Reset this Namespace support object for reuse.
void reset();
/// Reset this Namespace support object for reuse.
///
/// It is necessary to invoke this method before reusing the Namespace support
/// object for a new session. If namespace declaration URIs are to be supported,

View File

@@ -56,12 +56,16 @@ class XML_API SAXParser: public XMLReader
/// * http://xml.org/sax/features/external-general-entities
/// * http://xml.org/sax/features/external-parameter-entities
/// * http://xml.org/sax/features/namespaces
/// * http://xml.org/sax/features/namespace-prefixes
/// * http://xml.org/sax/properties/lexical-handler
/// * http://xml.org/sax/properties/declaration-handler
/// * http://xml.org/sax/features/namespace-prefixes
/// * http://xml.org/sax/properties/lexical-handler
/// * http://xml.org/sax/properties/declaration-handler
///
/// The following proprietary extensions are supported:
/// * http://www.appinf.com/features/enable-partial-reads --
/// see ParserEngine::setEnablePartialReads()
{
public:
SAXParser();
SAXParser();
/// Creates an SAXParser.
SAXParser(const XMLString& encoding);
@@ -100,11 +104,13 @@ public:
void parse(const XMLString& systemId);
void parseMemoryNP(const char* xml, std::size_t size);
/// Extensions
void parseString(const std::string& xml);
/// Extensions
void parseString(const std::string& xml);
static const XMLString FEATURE_PARTIAL_READS;
protected:
void setupParse();
void setupParse();
private:
ParserEngine _engine;

View File

@@ -40,7 +40,11 @@
#include "Poco/XML/XML.h"
#if defined(POCO_UNBUNDLED)
#include <expat.h>
#else
#include "Poco/XML/expat.h"
#endif
#include "Poco/XML/XMLString.h"
#include "Poco/XML/XMLStream.h"
#include "Poco/SAX/Locator.h"
@@ -158,12 +162,32 @@ public:
void setErrorHandler(ErrorHandler* pErrorHandler);
/// Allow an application to register an error event handler.
ErrorHandler* getErrorHandler() const;
/// Return the current error handler.
void parse(InputSource* pInputSource);
/// Parse an XML document from the given InputSource.
ErrorHandler* getErrorHandler() const;
/// Return the current error handler.
void setEnablePartialReads(bool flag = true);
/// Enable or disable partial reads from the input source.
///
/// This is useful for parsing XML from a socket stream for
/// a protocol like XMPP, where basically single elements
/// are read one at a time from the input source's stream, and
/// following elements depend upon responses sent back to
/// the peer.
///
/// Normally, the parser always reads blocks of PARSE_BUFFER_SIZE
/// at a time, and blocks until a complete block has been read (or
/// the end of the stream has been reached).
/// This allows for efficient parsing of "complete" XML documents,
/// but fails in a case such as XMPP, where only XML fragments
/// are sent at a time.
bool getEnablePartialReads() const;
/// Returns true if partial reads are enabled (see
/// setEnablePartialReads()), false otherwise.
void parse(InputSource* pInputSource);
/// Parse an XML document from the given InputSource.
void parse(const char* pBuffer, std::size_t size);
/// Parses an XML document from the given buffer.
@@ -187,12 +211,18 @@ protected:
void parseByteInputStream(XMLByteInputStream& istr);
/// Parses an entity from the given stream.
void parseCharInputStream(XMLCharInputStream& istr);
/// Parses an entity from the given stream.
void parseCharInputStream(XMLCharInputStream& istr);
/// Parses an entity from the given stream.
std::streamsize readBytes(XMLByteInputStream& istr, char* pBuffer, std::streamsize bufferSize);
/// Reads at most bufferSize bytes from the given stream into the given buffer.
void handleError(int errorNo);
/// Throws an XMLException with a message corresponding
/// to the given Expat error code.
std::streamsize readChars(XMLCharInputStream& istr, XMLChar* pBuffer, std::streamsize bufferSize);
/// Reads at most bufferSize chars from the given stream into the given buffer.
void handleError(int errorNo);
/// Throws an XMLException with a message corresponding
/// to the given Expat error code.
void parseExternal(XML_Parser extParser, InputSource* pInputSource);
/// Parse an XML document from the given InputSource.
@@ -249,12 +279,13 @@ private:
char* _pBuffer;
bool _encodingSpecified;
XMLString _encoding;
bool _expandInternalEntities;
bool _externalGeneralEntities;
bool _externalParameterEntities;
NamespaceStrategy* _pNamespaceStrategy;
EncodingMap _encodings;
ContextStack _context;
bool _expandInternalEntities;
bool _externalGeneralEntities;
bool _externalParameterEntities;
bool _enablePartialReads;
NamespaceStrategy* _pNamespaceStrategy;
EncodingMap _encodings;
ContextStack _context;
EntityResolver* _pEntityResolver;
DTDHandler* _pDTDHandler;
@@ -337,6 +368,12 @@ inline ErrorHandler* ParserEngine::getErrorHandler() const
}
inline bool ParserEngine::getEnablePartialReads() const
{
return _enablePartialReads;
}
} } // namespace Poco::XML

View File

@@ -71,21 +71,9 @@
// Automatically link XML library.
//
#if defined(_MSC_VER)
#if !defined(POCO_NO_AUTOMATIC_LIBS) && !defined(XML_EXPORTS)
#if defined(POCO_DLL)
#if defined(_DEBUG)
#pragma comment(lib, "PocoXMLd.lib")
#else
#pragma comment(lib, "PocoXML.lib")
#endif
#else
#if defined(_DEBUG)
#pragma comment(lib, "PocoXMLmtd.lib")
#else
#pragma comment(lib, "PocoXMLmt.lib")
#endif
#endif
#endif
#if !defined(POCO_NO_AUTOMATIC_LIBS) && !defined(XML_EXPORTS)
#pragma comment(lib, "PocoXML" POCO_LIB_SUFFIX)
#endif
#endif

View File

@@ -83,14 +83,29 @@ class XML_API XMLWriter: public ContentHandler, public LexicalHandler, public DT
/// prefixes in the form ns1, ns2, etc.
{
public:
enum Options
{
CANONICAL = 0x00, /// do not write an XML declaration
WRITE_XML_DECLARATION = 0x01, /// write an XML declaration
PRETTY_PRINT = 0x02 /// pretty-print XML markup
};
enum Options
{
CANONICAL = 0x00,
/// Do not write an XML declaration (default).
XMLWriter(XMLByteOutputStream& str, int options);
CANONICAL_XML = 0x01,
/// Enables basic support for Canonical XML:
/// - do not write an XML declaration
/// - do not use special empty element syntax
/// - set the New Line character to NEWLINE_LF
WRITE_XML_DECLARATION = 0x02,
/// Write an XML declaration.
PRETTY_PRINT = 0x04,
/// Pretty-print XML markup.
PRETTY_PRINT_ATTRIBUTES = 0x08
/// Write each attribute on a separate line.
/// PRETTY_PRINT must be specified as well.
};
XMLWriter(XMLByteOutputStream& str, int options);
/// Creates the XMLWriter and sets the specified options.
///
/// The resulting stream will be UTF-8 encoded.
@@ -123,12 +138,23 @@ public:
/// * NEWLINE_LF (Unix),
/// * NEWLINE_CR (Macintosh)
const std::string& getNewLine() const;
/// Returns the line ending currently in use.
const std::string& getNewLine() const;
/// Returns the line ending currently in use.
// ContentHandler
void setDocumentLocator(const Locator* loc);
/// Currently unused.
void setIndent(const std::string& indent);
/// Sets the string used for one indentation step.
///
/// The default is a single TAB character.
/// The given string should only contain TAB or SPACE
/// characters (e.g., a single TAB character, or
/// two to four SPACE characters).
const std::string& getIndent() const;
/// Returns the string used for one indentation step.
// ContentHandler
void setDocumentLocator(const Locator* loc);
/// Currently unused.
void startDocument();
/// Writes a generic XML declaration to the stream.
@@ -260,12 +286,13 @@ protected:
void writeEndElement(const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname);
void writeMarkup(const std::string& str) const;
void writeXML(const XMLString& str) const;
void writeXML(XMLChar ch) const;
void writeNewLine() const;
void writeIndent() const;
void writeName(const XMLString& prefix, const XMLString& localName);
void writeXMLDeclaration();
void closeStartTag();
void writeXML(XMLChar ch) const;
void writeNewLine() const;
void writeIndent() const;
void writeIndent(int indent) const;
void writeName(const XMLString& prefix, const XMLString& localName);
void writeXMLDeclaration();
void closeStartTag();
void declareAttributeNamespaces(const Attributes& attributes);
void addNamespaceAttributes(AttributeMap& attributeMap);
void addAttributes(AttributeMap& attributeMap, const Attributes& attributes, const XMLString& elementNamespaceURI);
@@ -302,18 +329,23 @@ private:
bool _inInternalDTD;
bool _contentWritten;
bool _unclosedStartTag;
ElementStack _elementStack;
NamespaceSupport _namespaces;
int _prefix;
ElementStack _elementStack;
NamespaceSupport _namespaces;
int _prefix;
bool _nsContextPushed;
std::string _indent;
static const std::string MARKUP_QUOTENC;
static const std::string MARKUP_APOSENC;
static const std::string MARKUP_AMPENC;
static const std::string MARKUP_LTENC;
static const std::string MARKUP_GTENC;
static const std::string MARKUP_LT;
static const std::string MARKUP_GT;
static const std::string MARKUP_SLASHGT;
static const std::string MARKUP_QUOTENC;
static const std::string MARKUP_APOSENC;
static const std::string MARKUP_AMPENC;
static const std::string MARKUP_LTENC;
static const std::string MARKUP_GTENC;
static const std::string MARKUP_TABENC;
static const std::string MARKUP_CRENC;
static const std::string MARKUP_LFENC;
static const std::string MARKUP_LT;
static const std::string MARKUP_GT;
static const std::string MARKUP_SLASHGT;
static const std::string MARKUP_LTSLASH;
static const std::string MARKUP_COLON;
static const std::string MARKUP_EQQUOT;