⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlwriter.h

📁 This software aims to create an applet and panel tools to manage a wireless interface card, such as
💻 H
字号:
//
// XMLWriter.h
//
// $Id: //poco/Main/XML/include/XML/XMLWriter.h#5 $
//
// Definition of the XMLWriter class.
//
// Copyright (c) 2004, Guenter Obiltschnig/Applied Informatics.
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
// 
// 3. Redistributions in any form must be accompanied by information on
//    how to obtain complete source code for this software and any
//    accompanying software that uses this software.  The source code
//    must either be included in the distribution or be available for no
//    more than the cost of distribution plus a nominal fee, and must be
//    freely redistributable under reasonable conditions.  For an
//    executable file, complete source code means the source code for all
//    modules it contains.  It does not include source code for modules or
//    files that typically accompany the major components of the operating
//    system on which the executable file runs.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//


#ifndef XML_XMLWriter_INCLUDED
#define XML_XMLWriter_INCLUDED


#ifndef XML_XML_INCLUDED
#include "XML/XML.h"
#endif
#ifndef SAX_ContentHandler_INCLUDED
#include "SAX/ContentHandler.h"
#endif
#ifndef SAX_LexicalHandler_INCLUDED
#include "SAX/LexicalHandler.h"
#endif
#ifndef SAX_DTDHandler_INCLUDED
#include "SAX/DTDHandler.h"
#endif
#ifndef SAX_NamespaceSupport_INCLUDED
#include "SAX/NamespaceSupport.h"
#endif
#ifndef XML_XMLString_INCLUDED
#include "XML/XMLString.h"
#endif
#ifndef XML_XMLStream_INCLUDED
#include "XML/XMLStream.h"
#endif
#ifndef XML_Name_INCLUDED
#include "XML/Name.h"
#endif
#ifndef Foundation_TextEncoding_INCLUDED
#include "Foundation/TextEncoding.h"
#endif
#ifndef Foundation_StreamConverter_INCLUDED
#include "Foundation/StreamConverter.h"
#endif
#ifndef STD_VECTOR_INCLUDED
#include <vector>
#define STD_VECTOR_INCLUDED
#endif
#ifndef STD_MAP_INCLUDED
#include <map>
#define STD_MAP_INCLUDED
#endif


XML_BEGIN


class Locator;


class XML_API XMLWriter: public ContentHandler, public LexicalHandler, public DTDHandler
	/// This class serializes SAX2 ContentHandler, LexicalHandler and
	/// DTDHandler events back into a stream.
	/// 
	/// Various consistency checks are performed on the written data
	/// (i.e. there must be exactly one root element and every startElement() 
	/// must have a matching endElement()).
	///
	/// The XMLWriter supports optional pretty-printing of the serialized XML.
	/// Note, however, that pretty-printing XML data alters the
	/// information set of the document being written, since in
	/// XML all whitespace is potentially relevant to an application.
	///
	/// The writer contains extensive support for XML Namespaces, so that a client  
	/// application does not have to keep track of prefixes and supply xmlns attributes.
	///
	/// If the client does not provide namespace prefixes (either by specifying them
	/// as part of the qualified name given to startElement(), or by calling
	/// startPrefixMapping()), the XMLWriter automatically generates namespace
	/// 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
	};

	XMLWriter(XMLByteOutputStream& str, int options);
		/// Creates the XMLWriter and sets the specified options.
		///
		/// The resulting stream will be UTF-8 encoded.

	XMLWriter(XMLByteOutputStream& str, int options, const std::string& encodingName, Foundation::TextEncoding& textEncoding);
		/// Creates the XMLWriter and sets the specified options.
		///
		/// The encoding is reflected in the XML declaration.
		/// The caller is responsible for that the given encodingName matches with
		/// the given textEncoding.

	XMLWriter(XMLByteOutputStream& str, int options, const std::string& encodingName, Foundation::TextEncoding* pTextEncoding);
		/// Creates the XMLWriter and sets the specified options.
		///
		/// The encoding is reflected in the XML declaration.
		/// The caller is responsible for that the given encodingName matches with
		/// the given textEncoding.
		/// If pTextEncoding is null, the given encodingName is ignored and the
		/// default UTF-8 encoding is used.

	~XMLWriter();
		/// Destroys the XMLWriter.

	void setNewLine(const std::string& newLineCharacters);
		/// Sets the line ending for the resulting XML file.
		///
		/// Possible values are:
		///   * NEWLINE_DEFAULT (whatever is appropriate for the current platform)
		///   * NEWLINE_CRLF    (Windows),
		///   * NEWLINE_LF      (Unix),
		///   * NEWLINE_CR      (Macintosh)

	const std::string& getNewLine() const;
		/// Returns the line ending currently in use.

	// ContentHandler
	void setDocumentLocator(const Locator* loc);
		/// Currently unused.

	void startDocument();
		/// Writes a generic XML declaration to the stream.
		/// If a document type has been set (see SetDocumentType),
		/// a DOCTYPE declaration is also written.

	void endDocument();
		/// Checks that all elements are closed and prints a final newline.

	void startFragment();
		/// Use this instead of StartDocument() if you want to write
		/// a fragment rather than a document (no XML declaration and
		/// more than one "root" element allowed).

	void endFragment();
		/// Checks that all elements are closed and prints a final newline.

	void startElement(const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname, const Attributes& attributes);
		/// Writes an XML start element tag.
		///
		/// Namespaces are handled as follows.
		///
		///   1. If a qname, but no namespaceURI and localName are given, the qname is taken as element name.
		///
		///   2. If a namespaceURI and a localName, but no qname is given, and the given namespaceURI has been
		///      declared earlier, the namespace prefix for the given namespaceURI together with the localName
		///      is taken as element name. If the namespace has not been declared, a prefix in the form
		///      "ns1", "ns2", etc. is generated and the namespace is declared with the generated prefix.
		///
		///   3. If all three are given, and the namespace given in namespaceURI has not been declared, it is declared now.
		///      Otherwise, see 2.

	void startElement(const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname);
		/// Writes an XML start element tag with no attributes.
		/// See the other startElement() method for more information.
		
	void endElement(const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname);
		/// Writes an XML end element tag.
		///
		/// Throws an exception if the name of doesn't match the
		/// one of the most recent startElement().

	void emptyElement(const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname);
		/// Writes an empty XML element tag (<elem/>).

	void emptyElement(const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname, const Attributes& attributes);
		/// Writes an empty XML element tag with the given attributes (<elem attr1="value1"... />).

	void characters(const XMLChar ch[], int start, int length);
		/// Writes XML character data. Quotes, ampersand's, less-than and
		/// greater-than signs are escaped, unless a CDATA section
		/// has been opened by calling startCDATA().
		///
		/// The characters must be encoded in UTF-8 (if XMLChar is char) or 
		/// UTF-16 (if XMLChar is wchar_t).

	void characters(const XMLString& str);
		/// Writes XML character data. Quotes, ampersand's, less-than and
		/// greater-than signs are escaped, unless a CDATA section
		/// has been opened by calling startCDATA().
		///
		/// The characters must be encoded in UTF-8 (if XMLChar is char) or 
		/// UTF-16 (if XMLChar is wchar_t).

	void rawCharacters(const XMLString& str);
		/// Writes the characters in the given string as they are.
		/// The caller is responsible for escaping characters as
		/// necessary to produce valid XML.
		///
		/// The characters must be encoded in UTF-8 (if XMLChar is char) or 
		/// UTF-16 (if XMLChar is wchar_t).

	void ignorableWhitespace(const XMLChar ch[], int start, int length);
		/// Writes whitespace characters by simply passing them to
		/// characters().

	void processingInstruction(const XMLString& target, const XMLString& data);
		/// Writes a processing instruction.

	void startPrefixMapping(const XMLString& prefix, const XMLString& namespaceURI);
		/// Begin the scope of a prefix-URI Namespace mapping.
		/// A namespace declaration is written with the next element.

	void endPrefixMapping(const XMLString& prefix);
		/// End the scope of a prefix-URI mapping.

	void skippedEntity(const XMLString& name);
		/// Does nothing.

	void dataElement(const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname, const XMLString& data, 
	                         const XMLString& attr1 = XMLString(), const XMLString& value1 = XMLString(),
							 const XMLString& attr2 = XMLString(), const XMLString& value2 = XMLString(),
							 const XMLString& attr3 = XMLString(), const XMLString& value3 = XMLString());
		/// Writes a data element in the form <name attr1="value1"...>data</name>.

	// LexicalHandler
	void startCDATA();
		/// Writes the <![CDATA[ string that begins a CDATA section.
		/// Use characters() to write the actual character data.

	void endCDATA();
		/// Writes the ]]> string that ends a CDATA section.

	void comment(const XMLChar ch[], int start, int length);
		/// Writes a comment.

	void startDTD(const XMLString& name, const XMLString& publicId, const XMLString& systemId);
		/// Writes a DTD declaration.

	void endDTD();
		/// Writes the closing characters of a DTD declaration.

	void startEntity(const XMLString& name);
		/// Does nothing.

	void endEntity(const XMLString& name);
		/// Does nothing.
	
	// DTDHandler
	void notationDecl(const XMLString& name, const XMLString* publicId, const XMLString* systemId);
	void unparsedEntityDecl(const XMLString& name, const XMLString* publicId, const XMLString& systemId, const XMLString& notationName);

	static const std::string NEWLINE_DEFAULT;
	static const std::string NEWLINE_CR;
	static const std::string NEWLINE_CRLF;
	static const std::string NEWLINE_LF;

protected:
	typedef std::map<XMLString, XMLString> AttributeMap;

	void writeStartElement(const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname, const Attributes& attributes);
	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 declareAttributeNamespaces(const Attributes& attributes);
	void addNamespaceAttributes(AttributeMap& attributeMap);
	void addAttributes(AttributeMap& attributeMap, const Attributes& attributes, const XMLString& elementNamespaceURI);
	void writeAttributes(const AttributeMap& attributeMap);
	void prettyPrint() const;
	XMLString newPrefix();
	static std::string nameToString(const XMLString& localName, const XMLString& qname);

private:
	struct Namespace
	{
		Namespace(const XMLString& thePrefix, const XMLString& theNamespaceURI):
			prefix(thePrefix),
			namespaceURI(theNamespaceURI)
		{
		}

		XMLString prefix;
		XMLString namespaceURI;
	};
	typedef std::vector<Name> ElementStack;
	
	Foundation::OutputStreamConverter* _pTextConverter;
	Foundation::TextEncoding*          _pInEncoding;
	Foundation::TextEncoding*          _pOutEncoding;
	int              _options;
	std::string      _encoding;
	std::string      _newLine;
	int              _depth;
	int              _elementCount;
	bool             _inFragment;
	bool             _inCDATA;
	bool             _inDTD;
	bool             _inInternalDTD;
	bool             _contentWritten;
	bool             _unclosedStartTag;
	ElementStack     _elementStack;
	NamespaceSupport _namespaces;
	int              _prefix;

	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_LTSLASH;
	static const std::string MARKUP_COLON;
	static const std::string MARKUP_EQQUOT;
	static const std::string MARKUP_QUOT;
	static const std::string MARKUP_SPACE;
	static const std::string MARKUP_TAB;
	static const std::string MARKUP_BEGIN_CDATA;
	static const std::string MARKUP_END_CDATA;
};


XML_END


#endif // XML_XMLWriter_INCLUDED

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -