diff options
author | Stef Walter <stef@memberwebs.com> | 2004-07-29 22:52:30 +0000 |
---|---|---|
committer | Stef Walter <stef@memberwebs.com> | 2004-07-29 22:52:30 +0000 |
commit | 740d8a6bcb6521e188361befa7f5636c880bf63b (patch) | |
tree | f269a5d492d712be73d840fdefeb6ad247ee03db /src | |
parent | 0105af34f6977c51619bf1060e74e3c249932c68 (diff) |
- Moved to DOMC
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 8 | ||||
-rw-r--r-- | src/domcxx.h | 1659 | ||||
-rw-r--r-- | src/domhelpers.cpp | 4 | ||||
-rw-r--r-- | src/domhelpers.h | 8 | ||||
-rw-r--r-- | src/internal.h | 47 | ||||
-rw-r--r-- | src/rtfparser.cpp | 11 | ||||
-rw-r--r-- | src/rtfparser.h | 8 | ||||
-rw-r--r-- | src/rtfx.cpp | 5 | ||||
-rw-r--r-- | src/sablo.h | 2173 | ||||
-rw-r--r-- | src/tags.h | 28 | ||||
-rw-r--r-- | src/utf8.cpp (renamed from src/sablotr.cpp) | 38 | ||||
-rw-r--r-- | src/xmlcomposehelpers.h | 4 | ||||
-rw-r--r-- | src/xmlcomposer.cpp | 79 | ||||
-rw-r--r-- | src/xmlcomposer.h | 6 | ||||
-rw-r--r-- | src/xmlfixups.cpp | 29 | ||||
-rw-r--r-- | src/xmlfixups.h | 2 |
16 files changed, 1807 insertions, 2302 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 317aa41..f561270 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -3,11 +3,11 @@ bin_PROGRAMS = rtfx rtfx_SOURCES = rtfx.cpp levelhandler.cpp levelhandler.h \ reference.h xmlcomposer.cpp xmlcomposer.h xmlcomposehelpers.cpp rtfformatting.h \ - xmlcomposehelpers.h rtfparser.cpp rtfparser.h sablo.h sablotr.cpp usuals.h \ + xmlcomposehelpers.h rtfparser.cpp rtfparser.h domcxx.h usuals.h utf8.cpp internal.h \ xmlfixups.h xmlfixups.cpp domhelpers.h domhelpers.cpp tags.h -rtfx_LDADD = -lsablot -lexpat $(LIB_ICONV) -rtfx_CFLAGS = -O0 -I${top_srcdir} -I/usr/local/include -rtfx_LDFLAGS = -L/usr/local/lib +rtfx_LDADD = -ldomc -lmba +rtfx_CXXFLAGS = -I$(srcdir)/../libs/domc/src/ -I$(srcdir)/../libs/libmba/src/ +rtfx_LDFLAGS = -L/usr/local/lib -L$(srcdir)/../libs/libmba/ -L$(srcdir)/../libs/domc/ man_MANS = rtfx.1 EXTRA_DIST = $(man_MANS) diff --git a/src/domcxx.h b/src/domcxx.h new file mode 100644 index 0000000..bd96078 --- /dev/null +++ b/src/domcxx.h @@ -0,0 +1,1659 @@ +/* + * Copyright (c) 2004, Nate Nielsen + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * * 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. + * * The names of contributors to this software may not be + * used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 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. + * + * + * CONTRIBUTORS + * Nate Nielsen <nielsen@memberwebs.com> + * + */ + +// TODO: UPDATE these comments once done +// DOMC C++ WRAPPER +// +// Everything is in the DOM namespace. +// + +#ifndef __DOMCXX_H__ +#define __DOMCXX_H__ + +#include <stdio.h> +#include <exception> +#include <string> + +namespace DOM +{ + extern "C" + { + #define this _this + #include <domc.h> + #include <dom.h> + #undef this + } + +#ifdef ASSERT + #define DOM_ASSERT ASSERT +#else + #include <assert.h> + #define ASSERT assert +#endif + + typedef std::basic_string<char> data_str; + typedef std::basic_string<char> dom_str; + + #define ASSERT_VALID() \ + ASSERT(isValid()); + #define ASSERT_VALID_NODE(node) \ + ASSERT(node.isValid()); + #define ASSERT_TYPE(t) \ + ASSERT(getNodeType() == t) + #define ASSERT_NODE_TYPE(n, t) \ + ASSERT(n.getNodeType() == t) + #define RESET_ERR() \ + DOM_Exception = 0; + #define CHECK_ERR() \ + if(DOM_Exception != 0) throw DOMException(DOM_Exception); + #define NO_REFCNT 0xFFFFFFFF + #define FROM_D(s) \ + ((DOM_String*)((s).c_str())) + #define FROM_V(s) \ + ((DOM_String*)((s).c_str())) + + class DOMException + { + public: + DOMException(int e) + { + ASSERT(e != 0); + code = (short)e; + } + + typedef enum + { + INDEX_SIZE_ERR = 1, + DOMSTRING_SIZE_ERR = 2, + HIERARCHY_REQUEST_ERR = 3, + WRONG_DOCUMENT_ERR = 4, + INVALID_CHARACTER_ERR = 5, + NO_DATA_ALLOWED_ERR = 6, + NO_MODIFICATION_ALLOWED_ERR = 7, + NOT_FOUND_ERR = 8, + NOT_SUPPORTED_ERR = 9, + INUSE_ATTRIBUTE_ERR = 10 + } CODES; + + int getCode() + { return code; } + char* getMessage() + { /* TODO: Get from the domc library */ return NULL; } + void getDetails(int* cde, char** message, + char** documentUri, int* fileLine) + { + *cde = code; + *message = NULL; // TODO: Get from domc library + *documentUri = NULL; + *fileLine = 0; + } + + short code; + }; + + class NodeList; + class NamedNodeMap; + class Element; + class Document; + class DOMImplementation; + + /* We only perform ref counting on nodes outside the tree */ + #define INC_NODE_REF(n) \ + if((n).m_node != NULL && ((n).m_node)->parentNode == NULL) incref((n).m_node) + #define DEC_NODE_REF(n) \ + if((n).m_node != NULL && ((n).m_node)->parentNode == NULL) decref((n).m_node) + + /** + * Thin wrapper class for a DOM Node + */ + class Node + { + public: + enum TYPES + { + ELEMENT_NODE = DOM_ELEMENT_NODE, + ATTRIBUTE_NODE = DOM_ATTRIBUTE_NODE, + TEXT_NODE = DOM_TEXT_NODE, + CDATA_SECTION_NODE = DOM_CDATA_SECTION_NODE, + ENTITY_REFERENCE_NODE = DOM_ENTITY_REFERENCE_NODE, + ENTITY_NODE = DOM_ENTITY_NODE, + PROCESSING_INSTRUCTION_NODE = DOM_PROCESSING_INSTRUCTION_NODE, + COMMENT_NODE = DOM_COMMENT_NODE, + DOCUMENT_NODE = DOM_DOCUMENT_NODE, + DOCUMENT_TYPE_NODE = DOM_DOCUMENT_TYPE_NODE, + DOCUMENT_FRAGMENT_NODE = DOM_DOCUMENT_FRAGMENT_NODE, + NOTATION_NODE = DOM_NOTATION_NODE + }; + + Node() + { m_node = NULL; } + + Node(const Node& node) + { + INC_NODE_REF(node); + m_node = node.m_node; + } + + Node& operator=(const Node& other) + { + INC_NODE_REF(other); + DEC_NODE_REF(*this); + m_node = other.m_node; + return *this; + } + + Node& operator=(const void* null) + { + ASSERT(null == NULL); + DEC_NODE_REF(*this); + m_node = NULL; + return *this; + } + + ~Node() + { DEC_NODE_REF(*this); } + + bool operator==(const Node& other) const + { return m_node == other.m_node; } + bool operator==(const void* null) const + { ASSERT(null == NULL); return m_node == NULL; } + bool operator!=(const Node& other) const + { return !operator==(other); } + bool operator!=(const void* null) const + { return !operator==(null); } + + const Node* operator->() const + { return (const Node*)this; } + Node* operator->() + { return this; } + + dom_str getNodeName() const + throw(DOMException) + { + ASSERT_VALID(); + DOM_String* name = m_node->nodeName; + return dom_str(name); + } + + data_str getNodeValue() const + throw(DOMException) + { + ASSERT_VALID(); + // TODO: Check on memory management + DOM_String* value = DOM_Node_getNodeValue(m_node); + return data_str(value); + } + + void setNodeValue(const data_str& value) + throw(DOMException) + { + ASSERT_VALID(); + RESET_ERR(); + DOM_Node_setNodeValue(m_node, FROM_V(value)); + CHECK_ERR(); + } + + short getNodeType() const + throw(DOMException) + { + ASSERT_VALID(); + return (short)(m_node->nodeType); + } + + Node getParentNode() const + throw(DOMException) + { + ASSERT_VALID(); + return Node(m_node->parentNode); + } + + NodeList getChildNodes() const + throw(DOMException); + + Node getFirstChild() const + throw(DOMException) + { + ASSERT_VALID(); + return Node(m_node->firstChild); + } + + Node getLastChild() const + throw(DOMException) + { + ASSERT_VALID(); + return Node(m_node->lastChild); + } + + Node getPreviousSibling() const + throw(DOMException) + { + ASSERT_VALID(); + return Node(m_node->previousSibling); + } + + Node getNextSibling() const + throw(DOMException) + { + ASSERT_VALID(); + return Node(m_node->nextSibling); + } + + NamedNodeMap getAttributes() const + throw(DOMException); + + Document getOwnerDocument() const + throw(DOMException); + + Node insertBefore(const Node& newChild, const Node& refChild) + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_VALID_NODE(newChild); + ASSERT_VALID_NODE(refChild); + RESET_ERR(); + Node node(DOM_Node_insertBefore(m_node, newChild.m_node, refChild.m_node)); + CHECK_ERR(); + return node; + } + + Node replaceChild(const Node& newChild, const Node& refChild) + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_VALID_NODE(newChild); + ASSERT_VALID_NODE(refChild); + RESET_ERR(); + Node node(DOM_Node_replaceChild(m_node, newChild.m_node, refChild.m_node)); + CHECK_ERR(); + return node; + } + + Node removeChild(const Node& oldChild) + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_VALID_NODE(oldChild); + RESET_ERR(); + Node node(DOM_Node_removeChild(m_node, oldChild.m_node)); + CHECK_ERR(); + return node; + } + + Node appendChild(const Node& newChild) + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_VALID_NODE(newChild); + RESET_ERR(); + Node node(DOM_Node_appendChild(m_node, newChild.m_node)); + CHECK_ERR(); + return node; + } + + bool hasChildNodes() const + throw(DOMException) + { + ASSERT_VALID(); + return DOM_Node_hasChildNodes(m_node) ? true : false; + } + + Node cloneNode(bool deep) const + throw(DOMException) + { + ASSERT_VALID(); + RESET_ERR(); + Node node(DOM_Node_cloneNode(m_node, deep ? 1 : 0)); + CHECK_ERR(); + return node; + } + + void normalize() + throw(DOMException) + { + ASSERT_VALID(); + if(m_node->nodeType == DOM_ELEMENT_NODE) + { + RESET_ERR(); + DOM_Element_normalize(m_node); + CHECK_ERR(); + } + } + + bool isSupported(const dom_str& feature, + const dom_str& version) const + { + ASSERT_VALID(); + return false; + } + +#if INCOMPLETE + dom_str getNamespaceURI() const + throw(DOMException) + { + } + + dom_str getPrefix() const + throw(DOMException) + { + } + + void setPrefix(const dom_str& prefix) + throw(DOMException) + { + } + + dom_str getLocalName() const + throw(DOMException) + { + } +#endif + + bool hasAttributes() const + throw (DOMException) + { + ASSERT_VALID(); + + if(getNodeType() != ELEMENT_NODE) + return false; + + return m_node->attributes != NULL && + m_node->attributes->length > 0; + } + + void* setUserData(void* data) + throw(DOMException) + { + ASSERT_VALID(); + m_node->userData = data; + } + + void* getUserData() const + throw(DOMException) + { + ASSERT_VALID(); + return m_node->userData; + } + + bool isValid() const + { return m_node != NULL; } + + protected: + Node(DOM_Node* node) + { + m_node = node; + INC_NODE_REF(*this); + } + + protected: + DOM_Node* m_node; + + friend class Document; + friend class NodeList; + friend class NamedNodeMap; + + private: + inline void incref(DOM_Node* node) + { ASSERT(node); ++(node->rtfxRefCount); } + + inline void decref(DOM_Node* node) + { + ASSERT(node); + ASSERT(node->parentNode == NULL); + --(node->rtfxRefCount); + if(node->rtfxRefCount <= 0) + { + ASSERT(node->ownerDocument); + DOM_Document_destroyNode(node->ownerDocument, node); + } + } + }; + + class Attr : + public Node + { + public: + Attr() { } + Attr(const Attr& node) : + Node(node) { } + + Attr& operator=(const Attr& other) + { Node::operator=(other); return *this; } + Attr& operator=(const void* null) + { Node::operator=(null); return *this; } + const Attr* operator->() const + { return (const Attr*)this; } + Attr* operator->() + { return this; } + + dom_str getName() const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(ATTRIBUTE_NODE); + return getNodeName(); + } + + Element getOwnerElement() const + throw(DOMException); + + bool getSpecified() const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(ATTRIBUTE_NODE); + return true; + } + + data_str getValue() const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(ATTRIBUTE_NODE); + return getNodeValue(); + } + + void setValue(const data_str& value) + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(ATTRIBUTE_NODE); + setNodeValue(value); + } + + protected: + Attr(DOM_Node* node) : + Node(node) { } + + friend class Element; + friend class Document; + }; + + /** + * This wrapper class for an element + */ + class Element : + public Node + { + public: + Element() { } + Element(const Element& node) : + Node(node) {} + + Element& operator=(const Node& other) + { Node::operator=(other); return *this; } + Element& operator=(const Element& other) + { Node::operator=(other); return *this; } + Element& operator=(const void* null) + { Node::operator=(null); return *this; } + const Element* operator->() const + { return (const Element*)this; } + Element* operator->() + { return this; } + + dom_str getTagName() const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(ELEMENT_NODE); + return getNodeName(); + } + + data_str getAttribute(const dom_str& name) const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(ELEMENT_NODE); + RESET_ERR(); + // TODO: Check on memory management + DOM_String* value = DOM_Element_getAttribute(m_node, FROM_D(name)); + CHECK_ERR(); + return data_str(value); + } + + void setAttribute(const dom_str& name, const data_str& value) + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(ELEMENT_NODE); + RESET_ERR(); + DOM_Element_setAttribute(m_node, FROM_D(name), FROM_V(value)); + CHECK_ERR(); + } + + void removeAttribute(const dom_str& name) + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(ELEMENT_NODE); + RESET_ERR(); + DOM_Element_removeAttribute(m_node, FROM_D(name)); + CHECK_ERR(); + } + + Attr getAttributeNode(const dom_str& name) const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(ELEMENT_NODE); + RESET_ERR(); + Attr attr(DOM_Element_getAttributeNode(m_node, FROM_D(name))); + CHECK_ERR(); + return attr; + } + + Attr setAttributeNode(const Attr& attr) + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_VALID_NODE(attr); + ASSERT_TYPE(ELEMENT_NODE); + RESET_ERR(); + Attr att(DOM_Element_setAttributeNode(m_node, attr.m_node)); + CHECK_ERR(); + return att; + } + + Attr removeAttributeNode(const Attr& attr) + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_VALID_NODE(attr); + ASSERT_TYPE(ELEMENT_NODE); + RESET_ERR(); + Attr att(DOM_Element_removeAttributeNode(m_node, attr.m_node)); + CHECK_ERR(); + return att; + } + + NodeList getElementsByTagName(const dom_str& name) const + throw(DOMException); + +#ifdef INCOMPLETE + data_str getAttributeNS(const dom_str& uri, const dom_str& name) const + throw(DOMException) + { + } + + void setAttributeNS(const dom_str& uri, const dom_str& name, + const data_str& value) + throw(DOMException) + { + } + + void removeAttributeNS(const dom_str& uri, const dom_str& name) + throw(DOMException) + { + } + + Attr getAttributeNodeNS(const dom_str& uri, const dom_str& name) const + throw(DOMException) + { + } + + Attr setAttributeNodeNS(const Attr& attr) + throw(DOMException) + { + } + + NodeList getElementsByTagNameNS(const dom_str& uri, + const dom_str& name) const + throw(DOMException); +#endif + + bool hasAttribute(const dom_str& name) const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(ELEMENT_NODE); + RESET_ERR(); + bool ret = (DOM_Element_getAttributeNode(m_node, FROM_D(name)) != NULL); + CHECK_ERR(); + return ret; + } + +#ifdef INCOMPLETE + bool hasAttributeNS(const dom_str& uri, const dom_str& name) const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(ELEMENT_NODE); + return getAttributeNodeNS(uri, name) != NULL; + } +#endif + + protected: + Element(DOM_Node* node) : + Node(node) { } + + friend class Attr; + friend class Document; + }; + + + class CharacterData : + public Node + { + public: + CharacterData() { } + CharacterData(const Node& node) : + Node(node) { } + + CharacterData& operator=(const Node& other) + { Node::operator=(other); return *this; } + CharacterData& operator=(const CharacterData& other) + { Node::operator=(other); return *this; } + CharacterData& operator=(const void* null) + { Node::operator=(null); return *this; } + const CharacterData* operator->() const + { return (const CharacterData*)this; } + CharacterData* operator->() + { return this; } + + void appendData(const data_str& data) + throw(DOMException) + { + ASSERT_VALID(); + ASSERT(getNodeType() == TEXT_NODE || + getNodeType() == CDATA_SECTION_NODE || + getNodeType() == COMMENT_NODE); + RESET_ERR(); + DOM_CharacterData_appendData(m_node, FROM_D(data)); + CHECK_ERR(); + } + + void deleteData(int offset, int count) + throw(DOMException) + { + ASSERT_VALID(); + ASSERT(getNodeType() == TEXT_NODE || + getNodeType() == CDATA_SECTION_NODE || + getNodeType() == COMMENT_NODE); + RESET_ERR(); + DOM_CharacterData_deleteData(m_node, offset, count); + CHECK_ERR(); + } + + data_str getData() const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT(getNodeType() == TEXT_NODE || + getNodeType() == CDATA_SECTION_NODE || + getNodeType() == COMMENT_NODE); + return getNodeValue(); + } + + int getLength() const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT(getNodeType() == TEXT_NODE || + getNodeType() == CDATA_SECTION_NODE || + getNodeType() == COMMENT_NODE); + RESET_ERR(); + int len = DOM_CharacterData_getLength(m_node); + CHECK_ERR(); + return len; + } + + void insertData(int offset, const data_str& data) + throw(DOMException) + { + ASSERT_VALID(); + ASSERT(getNodeType() == TEXT_NODE || + getNodeType() == CDATA_SECTION_NODE || + getNodeType() == COMMENT_NODE); + RESET_ERR(); + DOM_CharacterData_insertData(m_node, offset, FROM_V(data)); + CHECK_ERR(); + } + + void replaceData(int offset, int count, const data_str& data) + throw(DOMException) + { + ASSERT_VALID(); + ASSERT(getNodeType() == TEXT_NODE || + getNodeType() == CDATA_SECTION_NODE || + getNodeType() == COMMENT_NODE); + RESET_ERR(); + DOM_CharacterData_replaceData(m_node, offset, count, FROM_V(data)); + CHECK_ERR(); + } + + void setData(const data_str& data) + throw(DOMException) + { + ASSERT_VALID(); + ASSERT(getNodeType() == TEXT_NODE || + getNodeType() == CDATA_SECTION_NODE || + getNodeType() == COMMENT_NODE); + setNodeValue(data); + } + + data_str substringData(int offset, int count) const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT(getNodeType() == TEXT_NODE || + getNodeType() == CDATA_SECTION_NODE || + getNodeType() == COMMENT_NODE); + RESET_ERR(); + DOM_String* value = DOM_CharacterData_substringData(m_node, offset, count); + CHECK_ERR(); + return data_str(value); + } + + protected: + CharacterData(DOM_Node* node) : + Node(node) { } + }; + + class Text : + public CharacterData + { + public: + Text() { } + Text(const Node& node) : + CharacterData(node) { } + + Text& operator=(const Text& other) + { CharacterData::operator=(other); return *this; } + Text& operator=(const void* null) + { CharacterData::operator=(null); return *this; } + const Text* operator->() const + { return (const Text*)this; } + Text* operator->() + { return this; } + + Text splitText(int offset) + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(TEXT_NODE); + RESET_ERR(); + Text text(DOM_Text_splitText(m_node, offset)); + CHECK_ERR(); + return text; + } + + protected: + Text(DOM_Node* node) : + CharacterData(node) { } + + friend class Document; + }; + + class CDATASection : + public Text + { + public: + CDATASection() { } + CDATASection(const CDATASection& node) : + Text(node) { } + + CDATASection& operator=(const CDATASection& other) + { Text::operator=(other); return *this; } + CDATASection& operator=(void* null) + { Text::operator=(null); return *this; } + const CDATASection* operator->() const + { return (const CDATASection*)this; } + CDATASection* operator->() + { return this; } + + protected: + CDATASection(DOM_Node* node) : + Text(node) { } + + friend class Document; + }; + + class Comment : + public CharacterData + { + public: + Comment() { } + Comment(const Comment& node) : + CharacterData(node) { } + + Comment& operator=(const Node& other) + { Node::operator=(other); return *this; } + Comment& operator=(const Comment& other) + { CharacterData::operator=(other); return *this; } + Comment& operator=(void* null) + { CharacterData::operator=(null); return *this; } + const Comment* operator->() const + { return (const Comment*)this; } + Comment* operator->() + { return this; } + + protected: + Comment(DOM_Node* node) : + CharacterData(node) { } + + friend class Document; + }; + + class ProcessingInstruction : + public Node + { + public: + ProcessingInstruction() { } + ProcessingInstruction(const ProcessingInstruction& node) : + Node(node) { } + + ProcessingInstruction& operator=(const Node& other) + { Node::operator=(other); return *this; } + ProcessingInstruction& operator=(const ProcessingInstruction& other) + { Node::operator=(other); return *this; } + ProcessingInstruction& operator=(void* null) + { Node::operator=(null); return *this; } + const ProcessingInstruction* operator->() const + { return (const ProcessingInstruction*)this; } + ProcessingInstruction* operator->() + { return this; } + + data_str getData() const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(PROCESSING_INSTRUCTION_NODE); + return getNodeValue(); + } + + dom_str getTarget() const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(PROCESSING_INSTRUCTION_NODE); + return getNodeName(); + } + + void setData(const data_str& data) + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(PROCESSING_INSTRUCTION_NODE); + setNodeValue(data); + } + + protected: + ProcessingInstruction(DOM_Node* node) : + Node(node) { } + + friend class Document; + }; + + class DocumentFragment : + public Node + { + public: + DocumentFragment() { } + DocumentFragment(const DocumentFragment& node) : + Node(node) { } + + DocumentFragment& operator=(const Node& other) + { Node::operator=(other); return *this; } + DocumentFragment& operator=(const DocumentFragment& other) + { Node::operator=(other); return *this; } + DocumentFragment& operator=(void* null) + { Node::operator=(null); return *this; } + const DocumentFragment* operator->() const + { return (const DocumentFragment*)this; } + DocumentFragment* operator->() + { return this; } + + protected: + DocumentFragment(DOM_Node* node) : + Node(node) { } + + friend class Document; + }; + + class Entity : + public Node + { + public: + Entity() { } + Entity(const Entity& node) : + Node(node) { } + + Entity& operator=(const Node& other) + { Node::operator=(other); return *this; } + Entity& operator=(const Entity& other) + { Node::operator=(other); return *this; } + Entity& operator=(void* null) + { Node::operator=(null); return *this; } + const Entity* operator->() const + { return (const Entity*)this; } + Entity* operator->() + { return this; } + + dom_str getNotationName() const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(ENTITY_NODE); + return getNodeName(); + } + + dom_str getPublicId() const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(ENTITY_NODE); + return dom_str(m_node->u.Entity.publicId); + } + + dom_str getSystemId() const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(ENTITY_NODE); + return dom_str(m_node->u.Entity.systemId); + } + + protected: + Entity(DOM_Node* node) : + Node(node) { } + + friend class DocumentType; + }; + + class EntityReference : + public Node + { + public: + EntityReference() { } + EntityReference(const EntityReference& node) : + Node(node) { } + + EntityReference& operator=(const Node& other) + { Node::operator=(other); return *this; } + EntityReference& operator=(const EntityReference& other) + { Node::operator=(other); return *this; } + EntityReference& operator=(void* null) + { Node::operator=(null); return *this; } + const EntityReference* operator->() const + { return (const EntityReference*)this; } + EntityReference* operator->() + { return this; } + + protected: + EntityReference(DOM_Node* node) : + Node(node) { } + + friend class DocumentType; + friend class Document; + }; + + class Notation : + public Node + { + public: + Notation() { } + Notation(const Notation& node) : + Node(node) { } + + Notation& operator=(const Node& other) + { Node::operator=(other); return *this; } + Notation& operator=(const Notation& other) + { Node::operator=(other); return *this; } + Notation& operator=(void* null) + { Node::operator=(null); return *this; } + const Notation* operator->() const + { return (const Notation*)this; } + Notation* operator->() + { return this; } + + dom_str getPublicId() const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(NOTATION_NODE); + return dom_str(m_node->u.Notation.publicId); + } + + dom_str getSystemId() const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(NOTATION_NODE); + return dom_str(m_node->u.Notation.systemId); + } + + protected: + Notation(DOM_Node* node) : + Node(node) { } + + friend class DocumentType; + }; + + class DocumentType : + public Node + { + public: + DocumentType() { } + DocumentType(const DocumentType& node) : + Node(node) { } + + DocumentType& operator=(const Node& other) + { Node::operator=(other); return *this; } + DocumentType& operator=(const DocumentType& other) + { Node::operator=(other); return *this; } + DocumentType& operator=(void* null) + { Node::operator=(null); return *this; } + const DocumentType* operator->() const + { return (const DocumentType*)this; } + DocumentType* operator->() + { return this; } + + NamedNodeMap getEntities() const + throw(DOMException); + + NamedNodeMap getNotations() const + throw(DOMException); + + dom_str getInternalSubset() const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(DOCUMENT_TYPE_NODE); + return dom_str(m_node->u.DocumentType.internalSubset); + } + + dom_str getName() const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(DOCUMENT_TYPE_NODE); + return getNodeName(); + } + + protected: + DocumentType(DOM_Node* node) : + Node(node) { } + + friend class Document; + friend class DOMImplementation; + }; + + + class Document : + public Node + { + public: + Document() { } + Document(const Document& doc) : + Node(doc) { } + + Document& operator=(const Document& other) + { Node::operator=(other); return *this; } + Document& operator=(void* null) + { Node::operator=(null); return *this; } + const Document* operator->() const + { return (const Document*)this; } + Document* operator->() + { return this; } + + DocumentType getDocType() const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(DOCUMENT_NODE); + RESET_ERR(); + DocumentType dt(DOM_Document_getDoctype(m_node)); + CHECK_ERR(); + return dt; + } + + DOMImplementation getImplementation() const + throw(DOMException); + + Element getDocumentElement() const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(DOCUMENT_NODE); + RESET_ERR(); + Element el(DOM_Document_getDocumentElement(m_node)); + CHECK_ERR(); + return el; + } + + Element createElement(const dom_str& tag) const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(DOCUMENT_NODE); + RESET_ERR(); + Element el(DOM_Document_createElement(m_node, FROM_D(tag))); + CHECK_ERR(); + return el; + } + + DocumentFragment createDocumentFragment() const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(DOCUMENT_NODE); + RESET_ERR(); + DocumentFragment df(DOM_Document_createDocumentFragment(m_node)); + CHECK_ERR(); + return df; + } + + Text createTextNode(const data_str& data) const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(DOCUMENT_NODE); + RESET_ERR(); + Text text(DOM_Document_createTextNode(m_node, FROM_V(data))); + CHECK_ERR(); + return text; + } + + Comment createComment(const data_str& data) const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(DOCUMENT_NODE); + RESET_ERR(); + Comment comment(DOM_Document_createComment(m_node, FROM_V(data))); + CHECK_ERR(); + return comment; + } + + CDATASection createCDATASection(const data_str& data) const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(DOCUMENT_NODE); + RESET_ERR(); + CDATASection cdata(DOM_Document_createCDATASection(m_node, FROM_V(data))); + CHECK_ERR(); + return cdata; + } + + ProcessingInstruction createProcessingInstruction(const dom_str& targ, + const data_str& data) const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(DOCUMENT_NODE); + RESET_ERR(); + ProcessingInstruction pi(DOM_Document_createProcessingInstruction + (m_node, FROM_D(targ), FROM_V(data))); + CHECK_ERR(); + return pi; + } + + Attr createAttribute(const dom_str& name) const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(DOCUMENT_NODE); + RESET_ERR(); + Attr attr(DOM_Document_createAttribute(m_node, FROM_D(name))); + CHECK_ERR(); + return attr; + } + + EntityReference createEntityReference(const dom_str& name) + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(DOCUMENT_NODE); + RESET_ERR(); + EntityReference er(DOM_Document_createEntityReference(m_node, FROM_D(name))); + CHECK_ERR(); + return er; + } + + NodeList getElementsByTagName(const dom_str& name) const + throw(DOMException); + +#ifdef INCOMPLETE + Node importNode(const Node& import, bool deep) const + throw(DOMException) + { + } + + Element createElementNS(const dom_str& uri, const dom_str& tag) const + throw(DOMException) + { + } + + Attr createAttributeNS(const dom_str& uri, const dom_str& name) const + throw(DOMException) + { + } + + NodeList getElementsByTagNameNS(const dom_str& uri, + const dom_str& name) const + throw(DOMException); + + Element getElementById(const dom_str& id) const + throw(DOMException) + { + } +#endif + void serialize(FILE* f) + throw(DOMException) + { + ASSERT_VALID(); + RESET_ERR(); + DOM_DocumentLS_fwrite(m_node, f); + CHECK_ERR(); + } + + protected: + Document(DOM_Document* doc) : + Node(doc) { } + + friend class Node; + friend class DOMImplementation; + }; + + class DOMImplementation + { + public: + DOMImplementation() { } + DOMImplementation(const DOMImplementation& impl) { } + + DOMImplementation& operator=(const DOMImplementation& other) + { return *this; } + DOMImplementation& operator=(void* null) + { return *this; } + const DOMImplementation* operator->() const + { return (const DOMImplementation*)this; } + DOMImplementation* operator->() + { return this; } + + Document createDocument(const dom_str& uri, const dom_str& qname, + const DocumentType& type) const + throw(DOMException) + { + RESET_ERR(); + Document doc(DOM_Implementation_createDocument + (FROM_D(uri), FROM_D(qname), type.m_node)); + CHECK_ERR(); + return doc; + } + + DocumentType createDocumentType(const dom_str& qname, + const dom_str& publicId, + const dom_str& systemId) const + throw(DOMException) + { + RESET_ERR(); + DocumentType doctype(DOM_Implementation_createDocumentType + (FROM_D(qname), FROM_D(publicId), FROM_D(systemId))); + CHECK_ERR(); + return doctype; + } + + bool hasFeature(const dom_str& feature, const dom_str& version) const + throw(DOMException) + { + RESET_ERR(); + int ret = DOM_Implementation_hasFeature(FROM_D(feature), FROM_D(version)); + CHECK_ERR(); + return ret == 0 ? false : true; + } + }; + + /* We only perform ref counting on lists marked that way */ + #define INC_NODELIST_REF(l) \ + if((l).m_list != NULL && ((l).m_list->rtfxRefCount) != NO_REFCNT) incref((l).m_list) + #define DEC_NODELIST_REF(l) \ + if((l).m_list != NULL && ((l).m_list->rtfxRefCount) != NO_REFCNT) decref((l).m_list) + + class NodeList + { + public: + NodeList() + { m_list = NULL; } + + NodeList(const NodeList& list) + { + INC_NODELIST_REF(list); + m_list = list.m_list; + } + + NodeList& operator=(const NodeList& other) + { + INC_NODELIST_REF(other); + DEC_NODELIST_REF(*this); + m_list = other.m_list; + return *this; + } + + NodeList& operator=(const void* null) + { + ASSERT(null == NULL); + DEC_NODELIST_REF(*this); + m_list = NULL; + return *this; + } + + ~NodeList() + { DEC_NODELIST_REF(*this); } + + bool operator==(const NodeList& other) const + { return m_list == other.m_list; } + bool operator==(const void* null) const + { ASSERT(null == NULL); return m_list == NULL; } + bool operator!=(const NodeList& other) const + { return !operator==(other); } + bool operator!=(const void* null) const + { return !operator==(null); } + + const NodeList* operator->() const + { return (const NodeList*)this; } + NodeList* operator->() + { return this; } + +#ifdef COMPARE_REF + bool operator==(const NodeList& other) const + { return m_list == ((NodeList&)other).m_list; } +#endif + + virtual int getLength() const + throw(DOMException) + { + ASSERT_VALID(); + return m_list->length; + } + + virtual Node item(int index) const + throw(DOMException) + { + ASSERT_VALID(); + RESET_ERR(); + Node node(DOM_NodeList_item(m_list, index)); + CHECK_ERR(); + return node; + } + + protected: + NodeList(DOM_NodeList* list, bool refcnt) + { + m_list = list; + + if(m_list != NULL) + { + if(refcnt) + INC_NODELIST_REF(*this); + else + m_list->rtfxRefCount = NO_REFCNT; + } + } + + bool isValid() const + { + return m_list != NULL; + } + + protected: + DOM_NodeList* m_list; + + friend class Element; + friend class Document; + + private: + inline void incref(DOM_NodeList* list) + { ASSERT(list && (list->rtfxRefCount) != NO_REFCNT); ++(list->rtfxRefCount); } + + inline void decref(DOM_NodeList* list) + { + ASSERT(list->rtfxRefCount != NO_REFCNT); + --(list->rtfxRefCount); + if(list->rtfxRefCount <= 0) + { + ASSERT(list->_ownerDocument != NULL); + DOM_Document_destroyNodeList(list->_ownerDocument, list, 0); + } + } + + friend class Node; + }; + + + class NamedNodeMap : + public NodeList + { + public: + NamedNodeMap() + { } + + NamedNodeMap(const NamedNodeMap& list) : + NodeList(list) { } + + NamedNodeMap& operator=(const NamedNodeMap& other) + { NodeList::operator=(other); return *this; } + NamedNodeMap& operator=(void* null) + { NodeList::operator=(null); return *this; } + const NamedNodeMap* operator->() const + { return (const NamedNodeMap*)this; } + NamedNodeMap* operator->() + { return this; } + + virtual int getLength() const + throw(DOMException) + { + ASSERT_VALID(); + return NodeList::getLength(); + } + + virtual Node getNamedItem(const dom_str& name) const + throw(DOMException) + { + ASSERT_VALID(); + RESET_ERR(); + Node node(DOM_NamedNodeMap_getNamedItem(m_list, FROM_D(name))); + CHECK_ERR(); + return node; + } + +#ifdef INCOMPLETE + virtual Node getNamedItemNS(const dom_str& uri, const dom_str& name) const + throw(DOMException) + { + } +#endif + + virtual Node item(int index) const + throw(DOMException) + { + ASSERT_VALID(); + RESET_ERR(); + Node node(DOM_NodeList_item(m_list, index)); + CHECK_ERR(); + return node; + } + + virtual Node removeNamedItem(const dom_str& name) + throw(DOMException) + { + ASSERT_VALID(); + RESET_ERR(); + Node node(DOM_NamedNodeMap_removeNamedItem(m_list, FROM_D(name))); + CHECK_ERR(); + return node; + } + +#ifdef INCOMPLETE + virtual Node removeNamedItemNS(const dom_str& uri, const dom_str& name) + throw(DOMException) + { + ASSERT_VALID(); + _try_(S::SDOM_NOT_SUPPORTED_ERR); + return Node(); + } +#endif + + virtual Node setNamedItem(const Node& arg) + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_VALID_NODE(arg); + RESET_ERR(); + Node node(DOM_NamedNodeMap_setNamedItem(m_list, arg.m_node)); + CHECK_ERR(); + return node; + } + +#ifdef INCOMPLETE + virtual Node setNamedItemNS(const Node& arg) + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_VALID_NODE(arg); + _try_(S::SDOM_NOT_SUPPORTED_ERR); + return Node(); + } +#endif + + protected: + NamedNodeMap(DOM_NodeList* list, bool refcnt) : + NodeList(list, refcnt) { } + + friend class Node; + friend class DocumentType; + }; + + + inline NodeList Node::getChildNodes() const + throw(DOMException) + { + ASSERT_VALID(); + return NodeList(m_node->childNodes, false); + } + + inline NamedNodeMap Node::getAttributes() const + throw(DOMException) + { + ASSERT_VALID(); + if(getNodeType() != ELEMENT_NODE) + return NamedNodeMap(); + + return NamedNodeMap(m_node->attributes, false); + } + + inline Document Node::getOwnerDocument() const + throw(DOMException) + { + ASSERT_VALID(); + return Document(m_node->ownerDocument); + } + + inline Element Attr::getOwnerElement() const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(ATTRIBUTE_NODE); + return Element(m_node->u.Attr.ownerElement); + } + + inline NodeList Element::getElementsByTagName(const dom_str& name) const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(ELEMENT_NODE); + RESET_ERR(); + NodeList list(DOM_Element_getElementsByTagName(m_node, FROM_D(name)), true); + CHECK_ERR(); + return list; + } + +#ifdef INCOMPLETE + inline NodeList Element::getElementsByTagNameNS(const dom_str& uri, + const dom_str& name) const + throw(DOMException) + { + } +#endif + + inline NodeList Document::getElementsByTagName(const dom_str& name) const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(DOCUMENT_NODE); + RESET_ERR(); + NodeList list(DOM_Document_getElementsByTagName(m_node, FROM_D(name)), true); + CHECK_ERR(); + return list; + } + +#ifdef INCOMPLETE + inline NodeList Document::getElementsByTagNameNS(const dom_str& uri, + const dom_str& name) const + throw(DOMException) + { + } +#endif + + inline DOMImplementation Document::getImplementation() const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(DOCUMENT_NODE); + return DOMImplementation(); + } + + inline NamedNodeMap DocumentType::getEntities() const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(DOCUMENT_TYPE_NODE); + return NamedNodeMap(m_node->u.DocumentType.entities, false); + } + + inline NamedNodeMap DocumentType::getNotations() const + throw(DOMException) + { + ASSERT_VALID(); + ASSERT_TYPE(DOCUMENT_TYPE_NODE); + return NamedNodeMap(m_node->u.DocumentType.notations, false); + } + +}; // namespace DOM + + +#endif //__DOMCXX_H__ diff --git a/src/domhelpers.cpp b/src/domhelpers.cpp index 9a9cfd2..d2b3b8b 100644 --- a/src/domhelpers.cpp +++ b/src/domhelpers.cpp @@ -221,7 +221,7 @@ void ElementTable::load(const DOM::Node& parent, const string& name) if(DOMHelpers::isElement(child, name)) { DOM::Element& el = (DOM::Element&)child; - wstring id = el.getAttribute(kAtId); + string id = el.getAttribute(kAtId); if(!id.empty()) insert(make_pair(id, el)); @@ -231,7 +231,7 @@ void ElementTable::load(const DOM::Node& parent, const string& name) } } -DOM::Element ElementTable::get(const wstring& id) const +DOM::Element ElementTable::get(const string& id) const { const_iterator it = find(id); return it == end() ? DOM::Element() : it->second; diff --git a/src/domhelpers.h b/src/domhelpers.h index 8b14e2b..2c29da0 100644 --- a/src/domhelpers.h +++ b/src/domhelpers.h @@ -39,7 +39,7 @@ #ifndef __DOMHELPERS_H__ #define __DOMHELPERS_H__ -#include "sablo.h" +#include "domcxx.h" #include <map> #include <stack> #include <set> @@ -86,14 +86,14 @@ public: * things like fonts, styles, lists from their definitions. */ class ElementTable : - public std::map<wstring, DOM::Element> + public std::map<string, DOM::Element> { public: void load(const DOM::Node& parent, const string& name); - DOM::Element get(const wstring& id) const; + DOM::Element get(const string& id) const; - bool has(const wstring& id) const + bool has(const string& id) const { return find(id) != end(); } void removeIds(); diff --git a/src/internal.h b/src/internal.h new file mode 100644 index 0000000..b6a0808 --- /dev/null +++ b/src/internal.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2004, Nate Nielsen + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * * 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. + * * The names of contributors to this software may not be + * used to endorse or promote products derived from this + * software without specific prior written permission. + * + * 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. + * + * + * CONTRIBUTORS + * Nate Nielsen <nielsen@memberwebs.com> + * + */ + +#ifndef __INTERNAL_H__ +#define __INTERNAL_H__ + +// NOTE: These append to their output buffer +bool transcode16to8(wchar_t ch, string& ret); +bool transcode16to8(const wstring& data, string& ret); +bool transcode8to16(const string& data, wstring& ret); + +#endif // __INTERNAL_H__ diff --git a/src/rtfparser.cpp b/src/rtfparser.cpp index c136e95..cf61496 100644 --- a/src/rtfparser.cpp +++ b/src/rtfparser.cpp @@ -41,6 +41,7 @@ #include <stdlib.h> #include <stdio.h> #include "rtfparser.h" +#include "internal.h" /* ---------------------------------------------------------------------------------- @@ -197,10 +198,10 @@ void RtfParser::sendData(wchar_t ch) m_uniEat--; else - m_dataBuffer.append(1, ch); + transcode16to8(ch, m_dataBuffer); } -void RtfParser::sendData(wstring data) +void RtfParser::sendData(const wstring& data) { // Skip any unicode chars we've been asked to if(m_uniEat > 0) @@ -209,16 +210,16 @@ void RtfParser::sendData(wstring data) if(len > m_uniEat) len = m_uniEat; - m_dataBuffer.append(data.substr(len)); + transcode16to8(data.substr(len), m_dataBuffer); m_uniEat -= len; } else { - m_dataBuffer.append(data); + transcode16to8(data, m_dataBuffer); } } -void RtfParser::sendControlWord(string cw, int flags, int param) +void RtfParser::sendControlWord(const string& cw, int flags, int param) { flushData(); diff --git a/src/rtfparser.h b/src/rtfparser.h index 9509a35..0090648 100644 --- a/src/rtfparser.h +++ b/src/rtfparser.h @@ -87,7 +87,7 @@ protected: int m_uniEat; FILE* m_file; // The file we're currently parsing - wstring m_dataBuffer; // The block of data we're caching to send + string m_dataBuffer; // The block of data we're caching to send private: @@ -96,9 +96,9 @@ private: bool parseHexChar(int num); // Convenience functions for calling the handler - void sendControlWord(string cw, int flags, int param); + void sendControlWord(const string& cw, int flags, int param); void sendData(wchar_t ch); - void sendData(wstring data); + void sendData(const wstring& data); void flushData(); }; @@ -129,7 +129,7 @@ public: virtual void groupEnd() = 0; // A block of character data encountered - virtual void charData(wstring data) = 0; + virtual void charData(const string& data) = 0; // Flags for controlWord enum diff --git a/src/rtfx.cpp b/src/rtfx.cpp index 541432e..2a9f3fc 100644 --- a/src/rtfx.cpp +++ b/src/rtfx.cpp @@ -115,10 +115,7 @@ int main(int argc, char* argv[]) return 1; } - // TODO: This is disgusting. We need to bug the sablotron guys - // for a better way to serialize a document. DOM::Document doc = composer.getDocument(); - string xml = doc.serialize(); FILE* out = fopen(argv[1], "wb"); if(!out) @@ -127,7 +124,7 @@ int main(int argc, char* argv[]) return 1; } - fwrite(xml.c_str(), 1, xml.length(), out); + doc.serialize(out); fclose(out); return 0; } diff --git a/src/sablo.h b/src/sablo.h deleted file mode 100644 index 196b70b..0000000 --- a/src/sablo.h +++ /dev/null @@ -1,2173 +0,0 @@ -/* - * Copyright (c) 2004, Nate Nielsen - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * * Redistributions of source code must retain the above - * copyright notice, this list of conditions and the - * following disclaimer. - * * 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. - * * The names of contributors to this software may not be - * used to endorse or promote products derived from this - * software without specific prior written permission. - * - * 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. - * - * - * CONTRIBUTORS - * Nate Nielsen <nielsen@memberwebs.com> - * - */ - -// SABLOTRON C++ WRAPPER -// -// This wrapper tries to emulate the W3C DOM as much as possible. -// Objects returned can be copied liberally. When copied they still -// refer to the original Sablotron classes. -// Almost all classes are "light" wrappers, and shouldn't be more -// than 4 to 8 bytes a piece, with no additional memory allocation. -// Certain objects (NodeList, NamedNodeMap implementations) are -// heavier, and are reference counted. -// -// Salbotron uses UTF8 exclusively internally. This wrapper -// supports on the fly conversions from UTF16. Define the following -// constants to enable conversions: -// -// USE_UTF16 -// Convert all data values from UTF16 to UTF8. -// -// DOM_UTF16 -// Convert all DOM names from UTF16 to UTF8. -// -// If either of the above two constants are enabled you must include -// salbotr.cpp in your build. -// -// Everything is in the DOM namespace. -// -// Objects of type Document and DOMImplementation must be manually -// freed with their 'release' member function. -// -// - -#ifndef __SABLO_H__ -#define __SABLO_H__ - -#define USE_UTF16 -// #define DOM_UTF16 - -#include <stdio.h> -#include <wchar.h> -#include <exception> -#include <string> - -namespace DOM -{ - namespace S - { - extern "C" - { - #include <sablot.h> - #include <sdom.h> - } - }; - - bool transcode16to8(const std::basic_string<wchar_t>& data, - std::basic_string<char>& ret); - bool transcode8to16(const std::basic_string<char>& data, - std::basic_string<wchar_t>& ret); - - namespace INT - { - typedef std::basic_string<char> _8str; - typedef std::basic_string<wchar_t> _16str; - } - -#ifdef ASSERT - #define DOM_ASSERT ASSERT -#else - #include <assert.h> - #define ASSERT assert -#endif - -#ifdef USE_UTF16 - #ifdef DOM_UTF16 - typedef INT::_16str data_str; - typedef INT::_16str dom_str; - #define FROM_V(v) _16to8(v) - #define FROM_D(v) _16to8(v) - #define TO_V(v) _8to16(v) - #define TO_D(v) _8to16(v) - #else - typedef INT::_16str data_str; - typedef INT::_8str dom_str; - #define FROM_V(v) _16to8(v) - #define FROM_D(v) v - #define TO_V(v) _8to16(v) - #define TO_D(v) v - #endif -#else - typedef INT::_8str data_str; - typedef INT::_8str dom_str; - #define FROM_V(v) v - #define FROM_D(v) v - #define TO_V(v) v - #define TO_D(v) v -#endif - - namespace INT - { - template<typename C> - class Ref - { - public: - Ref() - { m_ptr = NULL; } - - Ref(C* ptr) - { - m_ptr = ptr; - addref(); - } - - Ref(C* ptr, bool addref) - { - m_ptr = ptr; - if(addref) - addref(); - } - - ~Ref() - { - release(); - } - - Ref(const Ref& orig) - { - m_ptr = orig.m_ptr; - addref(); - } - - Ref& operator=(const C* ptr) - { - C* old = m_ptr; - m_ptr = (C*)ptr; - addref(); - if(old) - old->release(); - return *this; - } - - Ref& operator=(const Ref& orig) - { return operator=(orig.m_ptr); } - -#ifdef COMPARE_REF - bool operator==(const C* ptr) - { - if(m_ptr == NULL && ptr == NULL) - return true; - else if(m_ptr == NULL || ptr == NULL) - return false; - - return *ptr == *m_ptr; - } - - bool operator==(const Ref& orig) - { return operator==(orig.m_ptr); } -#else - bool operator==(const C* ptr) - { - ASSERT(ptr == NULL); - return m_ptr == NULL; - } -#endif - operator C*() const - { return m_ptr; } - - operator C&() const - { return &m_ptr; } - - C* operator->() const - { return m_ptr; } - - protected: - void release() - { - if(m_ptr) - m_ptr->release(); - m_ptr = NULL; - } - - void addref() - { - if(m_ptr) - m_ptr->addref(); - } - - private: - C* m_ptr; - }; - - class Inst - { - public: - Inst() - { m_x = 0; } - virtual ~Inst() - { } - void addref() - { m_x++; } - void release() - { - if((--m_x) <= 0) - delete this; - } - - private: - int m_x; - }; - - class Base; - }; - - #define ASSERT_VALID() \ - ASSERT(isValid()); - #define ASSERT_VALID_NODE(node) \ - ASSERT(node.isValid()); - #define ASSERT_TYPE(t) \ - ASSERT(getNodeType() == t) - #define ASSERT_NODE_TYPE(n, t) \ - ASSERT(n.getNodeType() == t) - - class DOMException - { - public: - typedef enum - { - INDEX_SIZE_ERR = S::SDOM_INDEX_SIZE_ERR, - DOMSTRING_SIZE_ERR = S::SDOM_DOMSTRING_SIZE_ERR, - HIERARCHY_REQUEST_ERR = S::SDOM_HIERARCHY_REQUEST_ERR, - WRONG_DOCUMENT_ERR = S::SDOM_WRONG_DOCUMENT_ERR, - INVALID_CHARACTER_ERR = S::SDOM_INVALID_CHARACTER_ERR, - NO_DATA_ALLOWED_ERR = S::SDOM_NO_DATA_ALLOWED_ERR, - NO_MODIFICATION_ALLOWED_ERR = S::SDOM_NO_MODIFICATION_ALLOWED_ERR, - NOT_FOUND_ERR = S::SDOM_NOT_FOUND_ERR, - NOT_SUPPORTED_ERR = S::SDOM_NOT_SUPPORTED_ERR, - INUSE_ATTRIBUTE_ERR = S::SDOM_INUSE_ATTRIBUTE_ERR, - INVALID_STATE_ERR = S::SDOM_INVALID_STATE_ERR, - SYNTAX_ERR = S::SDOM_SYNTAX_ERR, - INVALID_MODIFICATION_ERR = S::SDOM_INVALID_MODIFICATION_ERR, - NAMESPACE_ERR = S::SDOM_NAMESPACE_ERR, - INVALID_ACCESS_ERR = S::SDOM_INVALID_ACCESS_ERR, - /* not in spec below this point: */ - INVALID_NODE_TYPE = S::SDOM_INVALID_NODE_TYPE, - QUERY_PARSE_ERR = S::SDOM_QUERY_PARSE_ERR, - QUERY_EXECUTION_ERR = S::SDOM_QUERY_EXECUTION_ERR, - NOT_OK = S::SDOM_NOT_OK - } CODES; - - int getCode() - { return code; } - char* getMessage() - { return S::SDOM_getExceptionMessage(m_sit); } - void getDetails(int* code, char** message, - char** documentUri, int* fileLine) - { S::SDOM_getExceptionDetails(m_sit, code, message, documentUri, fileLine); } - - short code; - - protected: - DOMException(S::SDOM_Exception e, S::SablotSituation s) - { - code = e; - m_sit = s; - } - - S::SablotSituation m_sit; - - friend class INT::Base; - }; - - namespace INT - { - /** - * The base class that keeps references to sablo - */ - class Base - { - public: - bool operator==(const Base& other) const - { return m_sit == other.m_sit; } - bool operator==(const void* null) const - { ASSERT(null == NULL); return m_sit == NULL; }; - bool operator!=(const Base& other) const - { return !operator==(other); } - bool operator!=(const void* null) const - { return !operator==(null); } - - protected: - Base(S::SablotSituation sit) - { m_sit = sit; } - Base(const Base& base) - { m_sit = base.m_sit; } - Base& operator=(const Base& other) - { m_sit = other.m_sit; return *this; } - Base& operator=(const void* null) - { ASSERT(null == NULL); m_sit = NULL; return *this; } - inline bool isValid() const - { return m_sit != NULL; } - - inline S::SDOM_Exception _try_(S::SDOM_Exception e) const - throw(DOMException) - { - if(e != S::SDOM_OK) - throw DOMException(e, m_sit); - return e; - } - -#ifdef USE_UTF16 - inline _16str _8to16(const _8str& d) const - throw(DOMException) - { - _16str s; - if(!transcode8to16(d, s)) - throw DOMException(S::SDOM_INVALID_CHARACTER_ERR, m_sit); - return s; - } - - inline _8str _16to8(const _16str& d) const - throw(DOMException) - { - _8str s; - if(!transcode16to8(d, s)) - throw DOMException(S::SDOM_INVALID_CHARACTER_ERR, m_sit); - return s; - } -#endif - - S::SablotSituation m_sit; - }; - - class NamedNodeMap; - class NodeList; - class ChildNodeList; - class AttrNodeList; - class DOMNodeList; - class AttrNamedNodeMap; - } - - class Element; - class Document; - class DOMImplementation; - - typedef INT::Ref<INT::NamedNodeMap> NamedNodeMap; - typedef INT::Ref<INT::NodeList> NodeList; - - /** - * Thin wrapper class for a DOM Node - */ - class Node : - public INT::Base - { - public: - enum TYPES - { - ELEMENT_NODE = S::SDOM_ELEMENT_NODE, - ATTRIBUTE_NODE = S::SDOM_ATTRIBUTE_NODE, - TEXT_NODE = S::SDOM_TEXT_NODE, - CDATA_SECTION_NODE = S::SDOM_CDATA_SECTION_NODE, - ENTITY_REFERENCE_NODE = S::SDOM_ENTITY_REFERENCE_NODE, - ENTITY_NODE = S::SDOM_ENTITY_NODE, - PROCESSING_INSTRUCTION_NODE = S::SDOM_PROCESSING_INSTRUCTION_NODE, - COMMENT_NODE = S::SDOM_COMMENT_NODE, - DOCUMENT_NODE = S::SDOM_DOCUMENT_NODE, - DOCUMENT_TYPE_NODE = S::SDOM_DOCUMENT_TYPE_NODE, - DOCUMENT_FRAGMENT_NODE = S::SDOM_DOCUMENT_FRAGMENT_NODE, - NOTATION_NODE = S::SDOM_NOTATION_NODE - }; - - Node() : INT::Base(NULL) - { - m_node = NULL; - } - - Node(const Node& node) - : INT::Base(node) - { - m_node = node.m_node; - } - - Node& operator=(const Node& other) - { - Base::operator=(other); - m_node = other.m_node; - return *this; - } - - Node& operator=(const void* null) - { - ASSERT(null == NULL); - Base::operator=(null); - m_node = NULL; - return *this; - } - - bool operator==(const Node& other) const - { - return Base::operator==(other) && - m_node == other.m_node; - } - - bool operator==(const void* null) const - { - ASSERT(null == NULL); - return Base::operator==(null) || - m_node == NULL; - } - - bool operator!=(const Node& other) const - { return !operator==(other); } - - bool operator!=(const void* null) const - { return !operator==(null); } - - const Node* operator->() const - { return (const Node*)this; } - Node* operator->() - { return this; } - - dom_str getNodeName() const - throw(DOMException) - { - ASSERT_VALID(); - S::SDOM_char* name; - _try_(S::SDOM_getNodeName(m_sit, m_node, &name)); - return TO_D(INT::_8str(name)); - } - - data_str getNodeValue() const - throw(DOMException) - { - ASSERT_VALID(); - S::SDOM_char* value; - _try_(S::SDOM_getNodeValue(m_sit, m_node, &value)); - return TO_V(INT::_8str(value)); - } - - void setNodeValue(const data_str& value) - throw(DOMException) - { - ASSERT_VALID(); - _try_(S::SDOM_setNodeValue(m_sit, m_node, FROM_V(value).c_str())); - } - - short getNodeType() const - throw(DOMException) - { - ASSERT_VALID(); - S::SDOM_NodeType type; - _try_(S::SDOM_getNodeType(m_sit, m_node, &type)); - return (short)type; - } - - Node getParentNode() const - throw(DOMException) - { - ASSERT_VALID(); - S::SDOM_Node parent; - _try_(S::SDOM_getParentNode(m_sit, m_node, &parent)); - return Node(m_sit, parent); - } - - NodeList getChildNodes() const - throw(DOMException); - - Node getFirstChild() const - throw(DOMException) - { - ASSERT_VALID(); - S::SDOM_Node child; - _try_(S::SDOM_getFirstChild(m_sit, m_node, &child)); - return Node(m_sit, child); - } - - Node getLastChild() const - throw(DOMException) - { - ASSERT_VALID(); - S::SDOM_Node child; - _try_(S::SDOM_getLastChild(m_sit, m_node, &child)); - return Node(m_sit, child); - } - - Node getPreviousSibling() const - throw(DOMException) - { - ASSERT_VALID(); - S::SDOM_Node sib; - _try_(S::SDOM_getPreviousSibling(m_sit, m_node, &sib)); - return Node(m_sit, sib); - } - - Node getNextSibling() const - throw(DOMException) - { - ASSERT_VALID(); - S::SDOM_Node sib; - _try_(S::SDOM_getNextSibling(m_sit, m_node, &sib)); - return Node(m_sit, sib); - } - - NamedNodeMap getAttributes() const - throw(DOMException); - - Document getOwnerDocument() const - throw(DOMException); - - Node insertBefore(const Node& newChild, const Node& refChild) - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_VALID_NODE(newChild); - ASSERT_VALID_NODE(refChild); - _try_(S::SDOM_insertBefore(m_sit, m_node, newChild.m_node, refChild.m_node)); - return newChild; - } - - Node replaceChild(const Node& newChild, const Node& refChild) - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_VALID_NODE(newChild); - ASSERT_VALID_NODE(refChild); - _try_(S::SDOM_replaceChild(m_sit, m_node, newChild.m_node, refChild.m_node)); - return newChild; - } - - Node removeChild(const Node& oldChild) - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_VALID_NODE(oldChild); - _try_(S::SDOM_removeChild(m_sit, m_node, oldChild.m_node)); - return oldChild; - } - - Node appendChild(const Node& newChild) - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_VALID_NODE(newChild); - _try_(S::SDOM_appendChild(m_sit, m_node, newChild.m_node)); - return newChild; - } - - bool hasChildNodes() const - throw(DOMException) - { - ASSERT_VALID(); - int count = 0; - _try_(S::SDOM_getChildNodeCount(m_sit, m_node, &count)); - return count != 0; - } - - Node cloneNode(bool deep) const - throw(DOMException) - { - ASSERT_VALID(); - S::SDOM_Node node; - _try_(S::SDOM_cloneNode(m_sit, m_node, deep ? 1 : 0, &node)); - return Node(m_sit, node); - } - - void normalize() - throw(DOMException) - { - ASSERT_VALID(); - _try_(S::SDOM_NOT_SUPPORTED_ERR); - } - - bool isSupported(const dom_str& feature, - const dom_str& version) const - { - ASSERT_VALID(); - return false; - } - - dom_str getNamespaceURI() const - throw(DOMException) - { - ASSERT_VALID(); - S::SDOM_char* uri; - _try_(S::SDOM_getNodeNSUri(m_sit, m_node, &uri)); - return TO_D(INT::_8str(uri)); - } - - dom_str getPrefix() const - throw(DOMException) - { - ASSERT_VALID(); - S::SDOM_char* prefix; - _try_(S::SDOM_getNodePrefix(m_sit, m_node, &prefix)); - return TO_D(INT::_8str(prefix)); - } - - void setPrefix(const dom_str& prefix) - throw(DOMException) - { - ASSERT_VALID(); - _try_(S::SDOM_NOT_SUPPORTED_ERR); - } - - dom_str getLocalName() const - throw(DOMException) - { - ASSERT_VALID(); - S::SDOM_char* name; - _try_(S::SDOM_getNodeLocalName(m_sit, m_node, &name)); - return TO_D(INT::_8str(name)); - } - - bool hasAttributes() const - throw (DOMException) - { - ASSERT_VALID(); - - if(getNodeType() != ELEMENT_NODE) - return false; - - int count = 0; - _try_(S::SDOM_getAttributeNodeCount(m_sit, m_node, &count)); - return count != 0; - } - - void* setUserData(void* data) - throw(DOMException) - { - ASSERT_VALID(); - void* old = S::SDOM_getNodeInstanceData(m_node); - S::SDOM_setNodeInstanceData(m_node, data); - return old; - } - - void* getUserData() const - throw(DOMException) - { - ASSERT_VALID(); - return S::SDOM_getNodeInstanceData(m_node); - } - - std::string serialize() const - throw(DOMException) - { - ASSERT_VALID(); - S::SDOM_Document doc; - _try_(S::SDOM_getOwnerDocument(m_sit, m_node, &doc)); - S::SDOM_char* serialized; - _try_(S::SDOM_nodeToString(m_sit, doc, m_node, &serialized)); - std::string ret(serialized); - S::SablotFree(serialized); - return ret; - } - - bool isValid() const - { - return Base::isValid() && - m_node != NULL; - } - - protected: - Node(S::SablotSituation sit, S::SDOM_Node node) : - INT::Base(sit) { m_node = node; } - - protected: - S::SDOM_Node m_node; - - friend class Document; - friend class INT::ChildNodeList; - friend class INT::AttrNodeList; - friend class INT::DOMNodeList; - friend class INT::AttrNamedNodeMap; - }; - - class Attr : - public Node - { - public: - Attr() { } - Attr(const Attr& node) : - Node(node) { } - - Attr& operator=(const Attr& other) - { Node::operator=(other); return *this; } - Attr& operator=(const void* null) - { Node::operator=(null); return *this; } - const Attr* operator->() const - { return (const Attr*)this; } - Attr* operator->() - { return this; } - - dom_str getName() const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(ATTRIBUTE_NODE); - return getNodeName(); - } - - Element getOwnerElement() const - throw(DOMException); - - bool getSpecified() const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(ATTRIBUTE_NODE); - return true; - } - - data_str getValue() const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(ATTRIBUTE_NODE); - return getNodeValue(); - } - - void setValue(const data_str& value) - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(ATTRIBUTE_NODE); - setNodeValue(value); - } - - protected: - Attr(S::SablotSituation sit, S::SDOM_Node node) : - Node(sit, node) { } - - friend class Element; - friend class Document; - }; - - /** - * This wrapper class for an element - */ - class Element : - public Node - { - public: - Element() { } - Element(const Element& node) : - Node(node) {} - - Element& operator=(const Node& other) - { Node::operator=(other); return *this; } - Element& operator=(const Element& other) - { Node::operator=(other); return *this; } - Element& operator=(const void* null) - { Node::operator=(null); return *this; } - const Element* operator->() const - { return (const Element*)this; } - Element* operator->() - { return this; } - - dom_str getTagName() const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(ELEMENT_NODE); - return getNodeName(); - } - - data_str getAttribute(const dom_str& name) const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(ELEMENT_NODE); - S::SDOM_char* value; - _try_(S::SDOM_getAttribute(m_sit, m_node, FROM_D(name).c_str(), &value)); - return TO_V(INT::_8str(value)); - } - - void setAttribute(const dom_str& name, const data_str& value) - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(ELEMENT_NODE); - _try_(S::SDOM_setAttribute(m_sit, m_node, FROM_D(name).c_str(), - FROM_V(value).c_str())); - } - - void removeAttribute(const dom_str& name) - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(ELEMENT_NODE); - _try_(S::SDOM_removeAttribute(m_sit, m_node, FROM_D(name).c_str())); - } - - Attr getAttributeNode(const dom_str& name) const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(ELEMENT_NODE); - S::SDOM_Node attr; - _try_(S::SDOM_getAttributeNode(m_sit, m_node, FROM_D(name).c_str(), &attr)); - return Attr(m_sit, attr); - } - - Attr setAttributeNode(const Attr& attr) - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_VALID_NODE(attr); - ASSERT_TYPE(ELEMENT_NODE); - S::SDOM_Node repl; - _try_(S::SDOM_setAttributeNode(m_sit, m_node, attr.m_node, &repl)); - return Attr(m_sit, repl); - } - - Attr removeAttributeNode(const Attr& attr) - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_VALID_NODE(attr); - ASSERT_TYPE(ELEMENT_NODE); - S::SDOM_Node rem; - _try_(S::SDOM_removeAttributeNode(m_sit, m_node, attr.m_node, &rem)); - return Attr(m_sit, rem); - } - - NodeList getElementsByTagName(const dom_str& name) const - throw(DOMException); - - data_str getAttributeNS(const dom_str& uri, const dom_str& name) const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(ELEMENT_NODE); - S::SDOM_char* value; - _try_(S::SDOM_getAttributeNS(m_sit, m_node, (char*)FROM_D(uri).c_str(), - (char*)FROM_D(name).c_str(), &value)); - return TO_V(INT::_8str(value)); - } - - void setAttributeNS(const dom_str& uri, const dom_str& name, - const data_str& value) - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(ELEMENT_NODE); - _try_(S::SDOM_setAttributeNS(m_sit, m_node, FROM_D(uri).c_str(), - FROM_D(name).c_str(), FROM_V(value).c_str())); - } - - void removeAttributeNS(const dom_str& uri, const dom_str& name) - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(ELEMENT_NODE); - DOM::Attr attr = getAttributeNodeNS(uri, name); - if(attr != NULL) - removeAttributeNode(attr); - } - - Attr getAttributeNodeNS(const dom_str& uri, const dom_str& name) const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(ELEMENT_NODE); - S::SDOM_Node attr; - _try_(S::SDOM_getAttributeNodeNS(m_sit, m_node, (char*)FROM_D(uri).c_str(), - (char*)FROM_D(name).c_str(), &attr)); - return Attr(m_sit, attr); - } - - Attr setAttributeNodeNS(const Attr& attr) - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_VALID_NODE(attr); - ASSERT_TYPE(ELEMENT_NODE); - S::SDOM_Node repl; - _try_(S::SDOM_setAttributeNodeNS(m_sit, m_node, attr.m_node, &repl)); - return Attr(m_sit, repl); - } - - NodeList getElementsByTagNameNS(const dom_str& uri, - const dom_str& name) const - throw(DOMException); - - bool hasAttribute(const dom_str& name) const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(ELEMENT_NODE); - return getAttributeNode(name) != NULL; - } - - bool hasAttributeNS(const dom_str& uri, const dom_str& name) const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(ELEMENT_NODE); - return getAttributeNodeNS(uri, name) != NULL; - } - - protected: - Element(S::SablotSituation sit, S::SDOM_Node node) : - Node(sit, node) { } - - friend class Attr; - friend class Document; - }; - - - class CharacterData : - public Node - { - public: - CharacterData() { } - CharacterData(const Node& node) : - Node(node) { } - - CharacterData& operator=(const Node& other) - { Node::operator=(other); return *this; } - CharacterData& operator=(const CharacterData& other) - { Node::operator=(other); return *this; } - CharacterData& operator=(const void* null) - { Node::operator=(null); return *this; } - const CharacterData* operator->() const - { return (const CharacterData*)this; } - CharacterData* operator->() - { return this; } - - void appendData(const data_str& data) - throw(DOMException) - { - ASSERT_VALID(); - ASSERT(getNodeType() == TEXT_NODE || - getNodeType() == CDATA_SECTION_NODE || - getNodeType() == COMMENT_NODE); - data_str val = getNodeValue(); - val.append(data); - setNodeValue(val); - } - - void deleteData(int offset, int count) - throw(DOMException) - { - ASSERT_VALID(); - ASSERT(getNodeType() == TEXT_NODE || - getNodeType() == CDATA_SECTION_NODE || - getNodeType() == COMMENT_NODE); - data_str val = getNodeValue(); - val.erase(offset, count); - setNodeValue(val); - } - - data_str getData() const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT(getNodeType() == TEXT_NODE || - getNodeType() == CDATA_SECTION_NODE || - getNodeType() == COMMENT_NODE); - return getNodeValue(); - } - - int getLength() const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT(getNodeType() == TEXT_NODE || - getNodeType() == CDATA_SECTION_NODE || - getNodeType() == COMMENT_NODE); - return getNodeValue().size(); - } - - void insertData(int offset, const data_str& data) - throw(DOMException) - { - ASSERT_VALID(); - ASSERT(getNodeType() == TEXT_NODE || - getNodeType() == CDATA_SECTION_NODE || - getNodeType() == COMMENT_NODE); - data_str val = getNodeValue(); - val.insert(offset, data); - setNodeValue(val); - } - - void replaceData(int offset, int count, const data_str& arg) - throw(DOMException) - { - ASSERT_VALID(); - ASSERT(getNodeType() == TEXT_NODE || - getNodeType() == CDATA_SECTION_NODE || - getNodeType() == COMMENT_NODE); - data_str val = getNodeValue(); - val.erase(offset, count); - val.insert(offset, arg); - setNodeValue(val); - } - - void setData(const data_str& data) - throw(DOMException) - { - ASSERT_VALID(); - ASSERT(getNodeType() == TEXT_NODE || - getNodeType() == CDATA_SECTION_NODE || - getNodeType() == COMMENT_NODE); - setNodeValue(data); - } - - data_str substringData(int offset, int count) const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT(getNodeType() == TEXT_NODE || - getNodeType() == CDATA_SECTION_NODE || - getNodeType() == COMMENT_NODE); - data_str val = getNodeValue(); - return val.substr(offset, count); - } - - protected: - CharacterData(S::SablotSituation sit, S::SDOM_Node node) : - Node(sit, node) { } - }; - - class Text : - public CharacterData - { - public: - Text() { } - Text(const Node& node) : - CharacterData(node) { } - - Text& operator=(const Text& other) - { CharacterData::operator=(other); return *this; } - Text& operator=(const void* null) - { CharacterData::operator=(null); return *this; } - const Text* operator->() const - { return (const Text*)this; } - Text* operator->() - { return this; } - - Text splitText(int offset) - throw(DOMException); - - protected: - Text(S::SablotSituation sit, S::SDOM_Node node) : - CharacterData(sit, node) { } - - friend class Document; - }; - - class CDATASection : - public Text - { - public: - CDATASection() { } - CDATASection(const CDATASection& node) : - Text(node) { } - - CDATASection& operator=(const CDATASection& other) - { Text::operator=(other); return *this; } - CDATASection& operator=(void* null) - { Text::operator=(null); return *this; } - const CDATASection* operator->() const - { return (const CDATASection*)this; } - CDATASection* operator->() - { return this; } - - protected: - CDATASection(S::SablotSituation sit, S::SDOM_Node node) : - Text(sit, node) { } - - friend class Document; - }; - - class Comment : - public CharacterData - { - public: - Comment() { } - Comment(const Comment& node) : - CharacterData(node) { } - - Comment& operator=(const Node& other) - { Node::operator=(other); return *this; } - Comment& operator=(const Comment& other) - { CharacterData::operator=(other); return *this; } - Comment& operator=(void* null) - { CharacterData::operator=(null); return *this; } - const Comment* operator->() const - { return (const Comment*)this; } - Comment* operator->() - { return this; } - - protected: - Comment(S::SablotSituation sit, S::SDOM_Node node) : - CharacterData(sit, node) { } - - friend class Document; - }; - - class ProcessingInstruction : - public Node - { - public: - ProcessingInstruction() { } - ProcessingInstruction(const ProcessingInstruction& node) : - Node(node) { } - - ProcessingInstruction& operator=(const Node& other) - { Node::operator=(other); return *this; } - ProcessingInstruction& operator=(const ProcessingInstruction& other) - { Node::operator=(other); return *this; } - ProcessingInstruction& operator=(void* null) - { Node::operator=(null); return *this; } - const ProcessingInstruction* operator->() const - { return (const ProcessingInstruction*)this; } - ProcessingInstruction* operator->() - { return this; } - - data_str getData() const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(PROCESSING_INSTRUCTION_NODE); - return getNodeValue(); - } - - dom_str getTarget() const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(PROCESSING_INSTRUCTION_NODE); - _try_(S::SDOM_NOT_SUPPORTED_ERR); - return dom_str(); - } - - void setData(const data_str& data) - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(PROCESSING_INSTRUCTION_NODE); - setNodeValue(data); - } - - protected: - ProcessingInstruction(S::SablotSituation sit, S::SDOM_Node node) : - Node(sit, node) { } - - friend class Document; - }; - - class DocumentFragment : - public Node - { - public: - DocumentFragment() { } - DocumentFragment(const DocumentFragment& node) : - Node(node) { } - - DocumentFragment& operator=(const Node& other) - { Node::operator=(other); return *this; } - DocumentFragment& operator=(const DocumentFragment& other) - { Node::operator=(other); return *this; } - DocumentFragment& operator=(void* null) - { Node::operator=(null); return *this; } - const DocumentFragment* operator->() const - { return (const DocumentFragment*)this; } - DocumentFragment* operator->() - { return this; } - }; - - class Entity : - public Node - { - public: - Entity() { } - Entity(const Entity& node) : - Node(node) { } - - Entity& operator=(const Node& other) - { Node::operator=(other); return *this; } - Entity& operator=(const Entity& other) - { Node::operator=(other); return *this; } - Entity& operator=(void* null) - { Node::operator=(null); return *this; } - const Entity* operator->() const - { return (const Entity*)this; } - Entity* operator->() - { return this; } - - dom_str getNotationName() const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(ENTITY_NODE); - _try_(S::SDOM_NOT_SUPPORTED_ERR); - return dom_str(); - } - - dom_str getPublicId() const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(ENTITY_NODE); - _try_(S::SDOM_NOT_SUPPORTED_ERR); - return dom_str(); - } - - dom_str getSystemId() const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(ENTITY_NODE); - _try_(S::SDOM_NOT_SUPPORTED_ERR); - return dom_str(); - } - }; - - class EntityReference : - public Node - { - public: - EntityReference() { } - EntityReference(const EntityReference& node) : - Node(node) { } - - EntityReference& operator=(const Node& other) - { Node::operator=(other); return *this; } - EntityReference& operator=(const EntityReference& other) - { Node::operator=(other); return *this; } - EntityReference& operator=(void* null) - { Node::operator=(null); return *this; } - const EntityReference* operator->() const - { return (const EntityReference*)this; } - EntityReference* operator->() - { return this; } - }; - - class Notation : - public Node - { - public: - Notation() { } - Notation(const Notation& node) : - Node(node) { } - - Notation& operator=(const Node& other) - { Node::operator=(other); return *this; } - Notation& operator=(const Notation& other) - { Node::operator=(other); return *this; } - Notation& operator=(void* null) - { Node::operator=(null); return *this; } - const Notation* operator->() const - { return (const Notation*)this; } - Notation* operator->() - { return this; } - - dom_str getPublicId() const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(NOTATION_NODE); - _try_(S::SDOM_NOT_SUPPORTED_ERR); - return dom_str(); - } - - dom_str getSystemId() const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(NOTATION_NODE); - _try_(S::SDOM_NOT_SUPPORTED_ERR); - return dom_str(); - } - }; - - class DocumentType : - public Node - { - public: - DocumentType() { } - DocumentType(const DocumentType& node) : - Node(node) { } - - DocumentType& operator=(const Node& other) - { Node::operator=(other); return *this; } - DocumentType& operator=(const DocumentType& other) - { Node::operator=(other); return *this; } - DocumentType& operator=(void* null) - { Node::operator=(null); return *this; } - const DocumentType* operator->() const - { return (const DocumentType*)this; } - DocumentType* operator->() - { return this; } - - NamedNodeMap getEntities() const - throw(DOMException); - - dom_str getInternalSubset() const - throw(DOMException) - { - ASSERT_VALID(); - _try_(S::SDOM_NOT_SUPPORTED_ERR); - return dom_str(); - } - - dom_str getName() const - throw(DOMException) - { - ASSERT_VALID(); - _try_(S::SDOM_NOT_SUPPORTED_ERR); - return dom_str(); - } - - NamedNodeMap getNotations() const - throw(DOMException); - }; - - - class Document : - public Node - { - public: - Document() { } - Document(const Document& doc) : - Node(doc) { } - Document(S::SablotSituation sit, S::SDOM_Document doc) : - Node(sit, NULL) { m_node = doc; } - - Document& operator=(const Document& other) - { Node::operator=(other); return *this; } - Document& operator=(void* null) - { Node::operator=(null); return *this; } - const Document* operator->() const - { return (const Document*)this; } - Document* operator->() - { return this; } - - DocumentType getDocType() const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(DOCUMENT_NODE); - return DocumentType(); - } - - DOMImplementation getImplementation() const - throw(DOMException); - - Element getDocumentElement() const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(DOCUMENT_NODE); - S::SDOM_Node child; - _try_(S::SDOM_getFirstChild(m_sit, m_node, &child)); - return Element(m_sit, child); - } - - Element createElement(const dom_str& tag) const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(DOCUMENT_NODE); - S::SDOM_Node el; - _try_(S::SDOM_createElement(m_sit, (S::SDOM_Document)m_node, - &el, FROM_D(tag).c_str())); - return Element(m_sit, el); - } - - DocumentFragment createDocumentFragment() const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(DOCUMENT_NODE); - _try_(S::SDOM_NOT_SUPPORTED_ERR); - return DocumentFragment(); - } - - Text createTextNode(const data_str& data) const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(DOCUMENT_NODE); - S::SDOM_Node text; - _try_(S::SDOM_createTextNode(m_sit, (S::SDOM_Document)m_node, - &text, FROM_V(data).c_str())); - return Text(m_sit, text); - } - - Comment createComment(const data_str& data) const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(DOCUMENT_NODE); - S::SDOM_Node comment; - _try_(S::SDOM_createComment(m_sit, (S::SDOM_Document)m_node, - &comment, FROM_V(data).c_str())); - return Comment(m_sit, comment); - } - - CDATASection createCDATASection(const data_str& data) const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(DOCUMENT_NODE); - S::SDOM_Node cdata; - _try_(S::SDOM_createCDATASection(m_sit, (S::SDOM_Document)m_node, - &cdata, FROM_V(data).c_str())); - return CDATASection(m_sit, cdata); - } - - ProcessingInstruction createProcessingInstruction(const dom_str& targ, - const data_str& data) const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(DOCUMENT_NODE); - S::SDOM_Node pi; - _try_(S::SDOM_createProcessingInstruction(m_sit, - (S::SDOM_Document)m_node, - &pi, FROM_D(targ).c_str(), - FROM_V(data).c_str())); - return ProcessingInstruction(m_sit, pi); - } - - Attr createAttribute(const dom_str& name) const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(DOCUMENT_NODE); - S::SDOM_Node attr; - _try_(S::SDOM_createAttribute(m_sit, (S::SDOM_Document)m_node, - &attr, FROM_D(name).c_str())); - return Attr(m_sit, attr); - } - - EntityReference createEntityReference() - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(DOCUMENT_NODE); - _try_(S::SDOM_NOT_SUPPORTED_ERR); - return EntityReference(); - } - - NodeList getElementsByTagName(const dom_str& name) const - throw(DOMException); - - Node importNode(const Node& import, bool deep) const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(DOCUMENT_NODE); - ASSERT_VALID_NODE(import); - S::SDOM_Node imported; - _try_(S::SDOM_cloneForeignNode(m_sit, (S::SDOM_Document)m_node, - import.m_node, deep ? 1 : 0, &imported)); - return Node(m_sit, imported); - } - - Element createElementNS(const dom_str& uri, const dom_str& tag) const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(DOCUMENT_NODE); - S::SDOM_Node el; - _try_(S::SDOM_createElementNS(m_sit, (S::SDOM_Document)m_node, - &el, FROM_D(uri).c_str(), FROM_D(tag).c_str())); - return Element(m_sit, el); - } - - Attr createAttributeNS(const dom_str& uri, const dom_str& name) const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(DOCUMENT_NODE); - S::SDOM_Node attr; - _try_(S::SDOM_createAttributeNS(m_sit, (S::SDOM_Document)m_node, - &attr, FROM_D(uri).c_str(), FROM_D(name).c_str())); - return Attr(m_sit, attr); - } - - NodeList getElementsByTagNameNS(const dom_str& uri, - const dom_str& name) const - throw(DOMException); - - Element getElementById(const dom_str& id) const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(DOCUMENT_NODE); - dom_str query = "//*[id('" + id + "')]"; - S::SDOM_NodeList result; - _try_(S::SDOM_xql(m_sit, FROM_D(query).c_str(), m_node, &result)); - - int length; - _try_(S::SDOM_getNodeListLength(m_sit, result, &length)); - - Element ret; - if(length != 1) - { - ret = Element(); - } - else - { - S::SDOM_Node el; - _try_(S::SDOM_getNodeListItem(m_sit, result, 0, &el)); - ret = Element(m_sit, el); - } - - S::SDOM_disposeNodeList(m_sit, result); - return ret; - } - - std::string serialize() const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(DOCUMENT_NODE); - S::SDOM_char* serialized; - _try_(S::SDOM_docToString(m_sit, (S::SDOM_Document)m_node, &serialized)); - std::string ret(serialized); - S::SablotFree(serialized); - return ret; - } - - void release() - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(DOCUMENT_NODE); - S::SDOM_tmpListDump((S::SDOM_Document)m_node, 0); - if(S::SablotDestroyDocument(m_sit, (S::SDOM_Document)m_node)) - _try_(S::SDOM_NOT_OK); - *this = NULL; - } - - protected: - - friend class Node; - friend class DOMImplementation; - }; - - - class DOMImplementation : - public INT::Base - { - public: - DOMImplementation() - throw(DOMException) : INT::Base(NULL) - { - if(S::SablotCreateSituation(&m_sit)) - _try_(S::SDOM_NOT_OK); - } - DOMImplementation(S::SablotSituation sit) - throw(DOMException) : INT::Base(sit) { } - DOMImplementation(const DOMImplementation& impl) : - INT::Base(impl) { } - - DOMImplementation& operator=(const DOMImplementation& other) - { Base::operator=(other); return *this; } - DOMImplementation& operator=(void* null) - { Base::operator=(null); return *this; } - const DOMImplementation* operator->() const - { return (const DOMImplementation*)this; } - DOMImplementation* operator->() - { return this; } - - Document createDocument(const dom_str& uri, const dom_str& qname, - const DocumentType& type) const - throw(DOMException) - { - ASSERT_VALID(); - S::SDOM_Document doc; - if(S::SablotCreateDocument(m_sit, &doc)) - _try_(S::SDOM_NOT_OK); - - Document document(m_sit, doc); - - if(!qname.empty()) - { - if(!uri.empty()) - document.appendChild(document.createElementNS(uri, qname)); - else - document.appendChild(document.createElement(qname)); - } - - return document; - } - - DocumentType createDocumentType(const dom_str& qname, - const dom_str& publicId, - const dom_str& systemId) const - throw(DOMException) - { - ASSERT_VALID(); - _try_(S::SDOM_NOT_SUPPORTED_ERR); - return DocumentType(); - } - - bool hasFeature(const dom_str& feature, const dom_str& version) const - throw(DOMException) - { - ASSERT_VALID(); - return false; - } - - void release() - throw(DOMException) - { - ASSERT_VALID(); - if(S::SablotDestroySituation(m_sit)) - _try_(S::SDOM_NOT_OK); - } - }; - - namespace INT - { - class NodeList : - public INT::Base, - public INT::Inst - { - public: -#ifdef COMPARE_REF - virtual bool operator==(const NodeList& other) const - { return Base::operator==(other); } -#endif - virtual int getLength() const - throw(DOMException) - { - ASSERT_VALID(); - _try_(S::SDOM_NOT_SUPPORTED_ERR); - return 0; - } - - virtual Node item(int index) const - throw(DOMException) - { - ASSERT_VALID(); - _try_(S::SDOM_NOT_SUPPORTED_ERR); - return Node(); - } - - protected: - NodeList(S::SablotSituation sit) : - INT::Base(sit) { } - - virtual bool isValid() const - { - return false; - } - - private: - NodeList(const NodeList& list) : INT::Base(list) { } - }; - - class ChildNodeList : - public NodeList - { - public: -#ifdef COMPARE_REF - virtual bool operator==(const NodeList& other) const - { return m_el == ((ChildNodeList)other).m_el; } -#endif - virtual int getLength() const - throw(DOMException) - { - ASSERT_VALID(); - int length; - _try_(S::SDOM_getChildNodeCount(m_sit, m_el, &length)); - return length; - } - - virtual Node item(int index) const - throw(DOMException) - { - ASSERT_VALID(); - S::SDOM_Node attr; - _try_(S::SDOM_getChildNodeIndex(m_sit, m_el, index, &attr)); - return Node(m_sit, attr); - } - - protected: - ChildNodeList(S::SablotSituation sit, S::SDOM_Node node) : - NodeList(sit) { m_el = node; } - - virtual bool isValid() const - { - return m_el != NULL; - } - - protected: - S::SDOM_Node m_el; - - friend class Node; - }; - - class AttrNodeList : - public NodeList - { - public: -#ifdef COMPARE_REF - virtual bool operator==(const NodeList& other) const - { return m_el == ((AttrNodeList)other).m_el; } -#endif - virtual int getLength() const - throw(DOMException) - { - ASSERT_VALID(); - int length; - _try_(S::SDOM_getAttributeNodeCount(m_sit, m_el, &length)); - return length; - } - - virtual Node item(int index) const - throw(DOMException) - { - ASSERT_VALID(); - S::SDOM_Node attr; - _try_(S::SDOM_getAttributeNodeIndex(m_sit, m_el, index, &attr)); - return Node(m_sit, attr); - } - - protected: - AttrNodeList(S::SablotSituation sit, S::SDOM_Node el) : - NodeList(sit) { m_el = el; } - - virtual bool isValid() const - { - return m_el != NULL; - } - - protected: - S::SDOM_Node m_el; - }; - - - class DOMNodeList : - public NodeList - { - public: -#ifdef COMPARE_REF - virtual bool operator==(const NodeList& other) const - { return m_list == ((DOMNodeList&)other).m_list; } -#endif - virtual int getLength() const - throw(DOMException) - { - ASSERT_VALID(); - int length; - _try_(S::SDOM_getNodeListLength(m_sit, m_list, &length)); - return length; - } - - virtual Node item(int index) const - throw(DOMException) - { - ASSERT_VALID(); - S::SDOM_Node it; - _try_(S::SDOM_getNodeListItem(m_sit, m_list, index, &it)); - return Node(m_sit, it); - } - - protected: - DOMNodeList(S::SablotSituation sit, S::SDOM_NodeList list) : - NodeList(sit) - { - m_list = list; - } - - ~DOMNodeList() - { - if(m_list != NULL) - S::SDOM_disposeNodeList(m_sit, m_list); - m_list = NULL; - } - - virtual bool isValid() const - { - return m_list != NULL; - } - - protected: - S::SDOM_NodeList m_list; - - friend class Element; - friend class Document; - }; - - - class NamedNodeMap : - public INT::Base, - public INT::Inst - { - public: -#ifdef COMPARE_REF - virtual bool operator==(const NamedNodeMap& other) const - { Base::operator==(other); } -#endif - virtual int getLength() const - throw(DOMException) - { - ASSERT_VALID(); - _try_(S::SDOM_NOT_SUPPORTED_ERR); - return 0; - } - - virtual Node getNamedItem(const dom_str& name) const - throw(DOMException) - { - ASSERT_VALID(); - _try_(S::SDOM_NOT_SUPPORTED_ERR); - return Node(); - } - - virtual Node getNamedItemNS(const dom_str& uri, const dom_str& name) const - throw(DOMException) - { - ASSERT_VALID(); - _try_(S::SDOM_NOT_SUPPORTED_ERR); - return Node(); - } - - virtual Node item(int index) const - throw(DOMException) - { - ASSERT_VALID(); - _try_(S::SDOM_NOT_SUPPORTED_ERR); - return Node(); - } - - virtual Node removeNamedItem(const dom_str& name) - throw(DOMException) - { - ASSERT_VALID(); - _try_(S::SDOM_NOT_SUPPORTED_ERR); - return Node(); - } - - virtual Node removeNamedItemNS(const dom_str& uri, const dom_str& name) - throw(DOMException) - { - ASSERT_VALID(); - _try_(S::SDOM_NOT_SUPPORTED_ERR); - return Node(); - } - - virtual Node setNamedItem(const Node& arg) - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_VALID_NODE(arg); - _try_(S::SDOM_NOT_SUPPORTED_ERR); - return Node(); - } - - virtual Node setNamedItemNS(const Node& arg) - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_VALID_NODE(arg); - _try_(S::SDOM_NOT_SUPPORTED_ERR); - return Node(); - } - - protected: - NamedNodeMap(S::SablotSituation sit) : - INT::Base(sit) { } - - virtual bool isValid() const - { - return false; - } - - private: - NamedNodeMap(const NamedNodeMap& map) : INT::Base(map) { } - }; - - class AttrNamedNodeMap : - public NamedNodeMap - { - public: -#ifdef COMPARE_REF - virtual bool operator==(const NamedNodeMap& other) const - { return m_el == ((AttrNamedNodeMap)other).m_el; } -#endif - virtual int getLength() const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_VALID_NODE(m_el); - int length; - _try_(S::SDOM_getAttributeNodeCount(m_sit, m_el.m_node, &length)); - return length; - } - - virtual Node getNamedItem(const dom_str& name) const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_VALID_NODE(m_el); - return m_el.getAttributeNode(name); - } - - virtual Node getNamedItemNS(const dom_str& uri, const dom_str& name) const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_VALID_NODE(m_el); - return m_el.getAttributeNodeNS(uri, name); - } - - virtual Node item(int index) const - throw(DOMException) - { - ASSERT_VALID(); - S::SDOM_Node attr; - _try_(S::SDOM_getAttributeNodeIndex(m_sit, m_el.m_node, index, &attr)); - return Node(m_sit, attr); - } - - virtual Node removeNamedItem(const dom_str& name) - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_VALID_NODE(m_el); - Node node = getNamedItem(name); - if(node != NULL) - m_el.removeAttributeNode((Attr&)node); - return node; - } - - virtual Node removeNamedItemNS(const dom_str& uri, const dom_str& name) - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_VALID_NODE(m_el); - Node node = getNamedItemNS(uri, name); - if(node != NULL) - m_el.removeAttributeNode((Attr&)node); - return node; - } - - virtual Node setNamedItem(const Node& arg) - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_VALID_NODE(m_el); - return m_el.setAttributeNode((Attr&)arg); - } - - virtual Node setNamedItemNS(const Node& arg) - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_VALID_NODE(m_el); - return m_el.setAttributeNodeNS((Attr&)arg); - } - - protected: - AttrNamedNodeMap(S::SablotSituation sit, const Node& el) : - NamedNodeMap(sit) - { - ASSERT_VALID_NODE(el); - ASSERT_NODE_TYPE(el, Node::ELEMENT_NODE); - m_el = (Element&)el; - } - - virtual bool isValid() const - { - return m_el.isValid(); - } - - - protected: - Element m_el; - - friend class Node; - }; - }; - - - inline NodeList Node::getChildNodes() const - throw(DOMException) - { - ASSERT_VALID(); - return NodeList(new INT::ChildNodeList(m_sit, m_node)); - } - - inline NamedNodeMap Node::getAttributes() const - throw(DOMException) - { - ASSERT_VALID(); - if(getNodeType() != ELEMENT_NODE) - return NamedNodeMap(NULL); - - return NamedNodeMap(new INT::AttrNamedNodeMap(m_sit, *this)); - } - - inline Document Node::getOwnerDocument() const - throw(DOMException) - { - ASSERT_VALID(); - S::SDOM_Document doc; - _try_(S::SDOM_getOwnerDocument(m_sit, m_node, &doc)); - return Document(m_sit, doc); - } - - inline Element Attr::getOwnerElement() const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(ATTRIBUTE_NODE); - S::SDOM_Node el; - _try_(S::SDOM_getAttributeElement(m_sit, m_node, &el)); - return Element(m_sit, el); - } - - inline NodeList Element::getElementsByTagName(const dom_str& name) const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(ELEMENT_NODE); - dom_str query = "descendant::" + name; - S::SDOM_NodeList result; - _try_(S::SDOM_xql(m_sit, FROM_D(query).c_str(), m_node, &result)); - return NodeList(new INT::DOMNodeList(m_sit, result)); - } - - inline NodeList Element::getElementsByTagNameNS(const dom_str& uri, - const dom_str& name) const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(ELEMENT_NODE); - dom_str query = "descendant::*[namespace-uri()='" + uri + "'"; - if(name != "*") - query += " and local-name()='" + name + "']"; - S::SDOM_NodeList result; - _try_(S::SDOM_xql(m_sit, FROM_D(query).c_str(), m_node, &result)); - return NodeList(new INT::DOMNodeList(m_sit, result)); - } - - inline NodeList Document::getElementsByTagName(const dom_str& name) const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(DOCUMENT_NODE); - dom_str query = "descendant::" + name; - S::SDOM_NodeList result; - _try_(S::SDOM_xql(m_sit, FROM_D(query).c_str(), m_node, &result)); - return NodeList(new INT::DOMNodeList(m_sit, result)); - } - - inline NodeList Document::getElementsByTagNameNS(const dom_str& uri, - const dom_str& name) const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(DOCUMENT_NODE); - dom_str query = "descendant::*[namespace-uri()='" + uri + "'"; - if(name != "*") - query += " and local-name()='" + name + "']"; - S::SDOM_NodeList result; - _try_(S::SDOM_xql(m_sit, FROM_D(query).c_str(), m_node, &result)); - return NodeList(new INT::DOMNodeList(m_sit, result)); - } - - inline DOMImplementation Document::getImplementation() const - throw(DOMException) - { - ASSERT_VALID(); - ASSERT_TYPE(DOCUMENT_NODE); - return DOMImplementation(m_sit); - } - - inline Text Text::splitText(int offset) - throw(DOMException) - { - ASSERT_VALID(); - ASSERT(getNodeType() == TEXT_NODE || - getNodeType() == CDATA_SECTION_NODE); - - data_str val = getNodeValue(); - setNodeValue(val.substr(0, offset)); - - Document doc = getOwnerDocument(); - ASSERT(doc != NULL); - - Text split(m_sit, NULL); - val = val.substr(0, offset); - - switch(getNodeType()) - { - case TEXT_NODE: - split = doc.createTextNode(val); - break; - - case CDATA_SECTION_NODE: - split = doc.createCDATASection(val); - break; - - default: - ASSERT(false); - }; - - Node parent = getParentNode(); - if(parent != NULL) - { - Node next = getNextSibling(); - if(next != NULL) - parent.insertBefore(split, next); - else - parent.appendChild(split); - } - - return split; - } - - inline NamedNodeMap DocumentType::getEntities() const - throw(DOMException) - { - ASSERT_VALID(); - _try_(S::SDOM_NOT_SUPPORTED_ERR); - return NamedNodeMap(NULL); - } - - inline NamedNodeMap DocumentType::getNotations() const - throw(DOMException) - { - ASSERT_VALID(); - _try_(S::SDOM_NOT_SUPPORTED_ERR); - return NamedNodeMap(NULL); - } - -}; // namespace DOM - - -#endif //__SABLO_H__ @@ -45,7 +45,7 @@ */ static const char* kNSPrefix = "xmlns"; -static const wchar_t* kNSRtfx = L"http://memberwebs.com/ns/rtfx/"; +static const char* kNSRtfx = "http://memberwebs.com/ns/rtfx/"; // Tags used internally static const char* kElDest = "i_dest"; @@ -103,18 +103,18 @@ static const char* kAtTo = "to"; static const char* kAtSize = "size"; // Values -static const wchar_t* kValDisc = L"disc"; -static const wchar_t* kValLowerAlpha = L"lower-alpha"; -static const wchar_t* kValUpperAlpha = L"upper-alpha"; -static const wchar_t* kValLowerRoman = L"lower-roman"; -static const wchar_t* kValUpperRoman = L"upper-roman"; -static const wchar_t* kValArabic = L"arabic"; -static const wchar_t* kValNull = L""; -static const wchar_t* kValFootNote = L"footnote"; -static const wchar_t* kValList = L"list"; -static const wchar_t* kValPara = L"para"; -static const wchar_t* kValTable = L"table"; -static const wchar_t* kValTrue = L"true"; -static const wchar_t* kValZero = L"0"; +static const char* kValDisc = "disc"; +static const char* kValLowerAlpha = "lower-alpha"; +static const char* kValUpperAlpha = "upper-alpha"; +static const char* kValLowerRoman = "lower-roman"; +static const char* kValUpperRoman = "upper-roman"; +static const char* kValArabic = "arabic"; +static const char* kValNull = ""; +static const char* kValFootNote = "footnote"; +static const char* kValList = "list"; +static const char* kValPara = "para"; +static const char* kValTable = "table"; +static const char* kValTrue = "true"; +static const char* kValZero = "0"; #endif // __TAGS_H__ diff --git a/src/sablotr.cpp b/src/utf8.cpp index 47853cc..7c2e3ee 100644 --- a/src/sablotr.cpp +++ b/src/utf8.cpp @@ -36,17 +36,35 @@ * */ -// SABLOTRON C++ WRAPPER CONVERSION FUNCTIONS -// - #include <wchar.h> -#include "sablo.h" +#include "usuals.h" + +bool transcode16to8(wchar_t ch, string& ret) +{ + ret.reserve(ret.length() + 3); + + if(ch <= 0x007F) + { + ret.append(1, (char)ch); + } + else if(ch <= 0x07FF) + { + ret.append(1, (char)(192 | (ch >> 6))); + ret.append(1, (char)(128 | (ch & 63))); + } + else + { + ret.append(1, (char)(224 | (ch >> 12))); + ret.append(1, (char)(128 | ((ch >> 6) & 63))); + ret.append(1, (char)(128 | (ch & 63))); + } + + return true; +} -bool DOM::transcode16to8(const std::basic_string<wchar_t>& data, - std::basic_string<char>& ret) +bool transcode16to8(const wstring& data, string& ret) { - ret.resize(0); - ret.reserve(data.length() + (data.length() / 2)); + ret.reserve(ret.length() + data.length() + (data.length() / 2)); // basic_string.c_str doesn't work properly everywhere // most notably not in the g++ std library @@ -76,10 +94,8 @@ bool DOM::transcode16to8(const std::basic_string<wchar_t>& data, return true; } -bool DOM::transcode8to16(const std::basic_string<char>& data, - std::basic_string<wchar_t>& ret) +bool transcode8to16(const string& data, wstring& ret) { - ret.resize(0); ret.reserve(data.length()); // basic_string.c_str doesn't work properly everywhere diff --git a/src/xmlcomposehelpers.h b/src/xmlcomposehelpers.h index aa8a52d..64e4723 100644 --- a/src/xmlcomposehelpers.h +++ b/src/xmlcomposehelpers.h @@ -41,7 +41,7 @@ #include "usuals.h" #include "reference.h" -#include "sablo.h" +#include "domcxx.h" #include "rtfformatting.h" class XmlComposer; @@ -60,7 +60,7 @@ public: // This is called when the Destination is first used virtual void initialize() {}; // Called when data arrives at destination - virtual void charData(wstring data) {}; + virtual void charData(string data) {}; // Called when the Destination goes out of scope virtual void done() {}; diff --git a/src/xmlcomposer.cpp b/src/xmlcomposer.cpp index c856d2d..e642acb 100644 --- a/src/xmlcomposer.cpp +++ b/src/xmlcomposer.cpp @@ -43,18 +43,12 @@ #include "tags.h" -wstring formatInt(int num) +string formatInt(int num) { - char buff[16]; - // Certain OSs don't support swprintf :( + char buff[16]; sprintf(buff, "%d", num); - - wstring n; - for(char* s = buff; *s; s++) - n.append(1, *s); - - return n; + return string(buff); } /* ---------------------------------------------------------------------------------- @@ -74,23 +68,11 @@ XmlComposer::XmlComposer(const XmlComposerOptions& options) XmlComposer::~XmlComposer() { clear(); - - if(m_impl != NULL) - m_impl.release(); } void XmlComposer::clear() { - if(m_document != NULL) - { - try - { - m_document.release(); - } - catch(...) { } - - m_document = NULL; - } + m_document = NULL; LevelHandler::clear(); } @@ -131,32 +113,17 @@ void XmlComposer::endDocument() XmlFixups fix; - // Pass 0: Cleanup the tree - // XmlFixups::combineDuplicates(m_document); - // XmlFixups::consolidateStartTags(m_document); - // XmlFixups::consolidateEndTags(m_document); - // Pass 1: Block breakout - fix.breakTables(m_document); + /* fix.breakTables(m_document); fix.breakTags(m_document, kElTable, kElRow); fix.breakTags(m_document, kElRow, kElCell); fix.wrapTags(m_document, kElCell, kElDest); fix.breakBlocks(m_document); - fix.breakLists(m_document); + fix.breakLists(m_document); */ // Pass 2: Fixups fix.runPassTwo(m_document); - // XmlFixups::fixLists(m_document); - // XmlFixups::fixStyles(m_document); - // XmlFixups::fixBlocks(m_document); - // XmlFixups::removeTags(m_document); - // XmlFixups::breakBreak(m_document, kElDoc, kElPage); - // XmlFixups::breakBreak(m_document, kElDoc, kElSect); - - // Pass 3: Final cleanup - // XmlFixups::combineDuplicates(m_document); - DOM::Element top = m_document.getDocumentElement(); ASSERT(top != NULL); top.setAttribute(kNSPrefix, kNSRtfx); @@ -164,7 +131,7 @@ void XmlComposer::endDocument() return; } -void XmlComposer::charData(wstring data) +void XmlComposer::charData(const string& data) { ASSERT(m_curLevel != NULL); DestinationPtr destination = m_curLevel->getDestination(); @@ -255,7 +222,7 @@ DOM::Element XmlComposer::popElement() return element; } -void XmlComposer::setAttribute(const string& name, const wstring& value, DOM::Element el) +void XmlComposer::setAttribute(const string& name, const string& value, DOM::Element el) { ASSERT(name.length() > 0); if(el == NULL) @@ -390,7 +357,7 @@ void XmlComposer::BaseAnalyser::applyParaFormatting(RtfFormatting* format, if(format == NULL) format = &(m_composer->getTextFormatting()); - wstring fix = kValPara; + string fix = kValPara; // Is it a list? int list = format->paraList(); @@ -401,7 +368,7 @@ void XmlComposer::BaseAnalyser::applyParaFormatting(RtfFormatting* format, // Is it a cell? if(format->paraInTable()) - el.setAttribute(kAtCell, L"1"); + el.setAttribute(kAtCell, "1"); else el.removeAttribute(kAtCell); @@ -603,7 +570,7 @@ bool XmlComposer::BaseAnalyser::processTextAutoContent(const string& cw, int fla #define ON_CONTROLWORD(cls) \ void XmlComposer::cls::controlWord(const string& cw, int flags, int param) #define ON_CHARDATA(cls) \ - void XmlComposer::cls::charData(wstring data) + void XmlComposer::cls::charData(const string& data) #define ON_GROUPSTART(cls) \ void XmlComposer::cls::groupStart() #define ON_GROUPEND(cls) \ @@ -746,7 +713,7 @@ ON_DONE(FontTable) deffont->setAttribute(kAtId, kValZero); // Default size is always 12 pt - deffont->setAttribute(kAtSize, L"12"); + deffont->setAttribute(kAtSize, "12"); // TODO: Is this correct? deffont->setAttribute(kAtColor, kValZero); @@ -805,7 +772,7 @@ ON_INITIALIZE(List) if(DO_EXTRAS()) AN_ATTRIBUTE(kAtType, kValDisc); - AN_ATTRIBUTE(kAtOrdered, L"0"); + AN_ATTRIBUTE(kAtOrdered, "0"); levelsSeen = 0; } @@ -859,10 +826,10 @@ ON_CONTROLWORD(List) { case 0: case 5: case 6: case 7: case 22: case 1: case 2: case 3: case 4: - AN_ATTRIBUTE(kAtOrdered, L"1"); + AN_ATTRIBUTE(kAtOrdered, "1"); break; default: - AN_ATTRIBUTE(kAtOrdered, L"0"); + AN_ATTRIBUTE(kAtOrdered, "0"); } } @@ -907,7 +874,7 @@ ON_CONTROLWORD(ListOverrideTable) // List id for current listoverride else if(cw == "listid" && HAS_PARAM) { - wstring id = formatInt(param); + string id = formatInt(param); if(lists != NULL) { @@ -1097,9 +1064,9 @@ ON_CHARDATA(Content) if(fontsize != -1) { // Little hack for half point sizes - wstring size = NUM_ATTR(fontsize / 2); + string size = NUM_ATTR(fontsize / 2); if(fontsize % 2) - size.append(L".5"); + size.append(".5"); AN_ATTRIBUTE(kAtSize, size); } @@ -1231,13 +1198,13 @@ ON_INITIALIZE(Attribute) ON_CHARDATA(Attribute) { // Get the current value - wstring cur = element.getAttribute(name); + string cur = element.getAttribute(name); + // Append data if(data.at(data.size() - 1) == L';') - data.resize(data.size() - 1); - - // Append data - cur.append(data); + cur.append(data.substr(data.size() - 1)); + else + cur.append(data); // Write it back element.setAttribute(name, cur); diff --git a/src/xmlcomposer.h b/src/xmlcomposer.h index 41aad0e..357cb58 100644 --- a/src/xmlcomposer.h +++ b/src/xmlcomposer.h @@ -73,7 +73,7 @@ public: virtual void controlWord(const string& cw, int flags, int param); virtual void groupStart(); virtual void groupEnd(); - virtual void charData(wstring data); + virtual void charData(const string& data); // Create an XML element with given name DOM::Element createElement(const string& name); @@ -91,7 +91,7 @@ public: DOM::Element popElement(); // Set attributes on the current XML Element - void setAttribute(const string& name, const wstring& value, DOM::Element el = DOM::Element()); + void setAttribute(const string& name, const string& value, DOM::Element el = DOM::Element()); void setAttribute(const string& name, int value, DOM::Element el = DOM::Element()); // The current analyser in use @@ -152,7 +152,7 @@ protected: #define END_ANALYSER }; #define DATA_PORTION protected: #define INITIALIZE virtual void initialize(); - #define CHARDATA virtual void charData(wstring data); + #define CHARDATA virtual void charData(const string& data); #define CONTROLWORD virtual void controlWord(const string& cw, int flags, int param); #define GROUPSTART virtual void groupStart(); #define GROUPEND virtual void groupEnd(); diff --git a/src/xmlfixups.cpp b/src/xmlfixups.cpp index 3b56f6b..d9bdb0a 100644 --- a/src/xmlfixups.cpp +++ b/src/xmlfixups.cpp @@ -345,7 +345,7 @@ void XmlFixups::breakLists(DOM::Document& doc) DOM::Element list; DOM::Element e; - wstring previd; + string previd; while(child != NULL) { @@ -355,7 +355,7 @@ void XmlFixups::breakLists(DOM::Document& doc) e = (DOM::Element&)child; // ... and has a list attribute - wstring listid = e.getAttribute(kAtList); + string listid = e.getAttribute(kAtList); if(listid.length() > 0) { e.removeAttribute(kAtList); @@ -432,6 +432,7 @@ void XmlFixups::runPassTwo(const DOM::Document& doc) { el = *it; + // Mark each node as we've seen it so we don't // do a given element twice if((int)el.getUserData() == PASS_TWO) @@ -502,8 +503,10 @@ void XmlFixups::runPassTwo(const DOM::Document& doc) continue; // Current element no longer valid } + continue; + // Change id attribute on fonts to name - else if(haveFonts && name == kElFont) + /* else */ if(haveFonts && name == kElFont) { if(el.hasAttribute(kAtId)) { @@ -756,27 +759,14 @@ void XmlFixups::fixBlock(const DOM::Document& doc, DOM::Element& block) { // Okay now change blocks to whatever element they're supposed to be string fix; - wstring val; DOM::Node parent = block.getParentNode(); if(parent != NULL) { // Figure out what kind of element they want block fixed to - val = block.getAttribute(kAtFix); - if(val.length() > 0) - block.removeAttribute(kAtFix); - - // BUG: Sablotron bug work around - if(val.length() == 0) - { - val = block.getAttributeNS("", kAtFix); - if(val.length() > 0) - block.removeAttributeNS("", kAtFix); - } - - if(val.length() > 0) - DOM::transcode16to8(val, fix); + fix = block.getAttribute(kAtFix); + block.removeAttribute(kAtFix); if(fix.length() == 0) fix = kElPara; @@ -789,6 +779,7 @@ void XmlFixups::fixBlock(const DOM::Document& doc, DOM::Element& block) while(block.hasChildNodes()) el.appendChild(block.removeChild(block.getFirstChild())); - parent.replaceChild(el, block); + // parent.replaceChild(el, block); + parent.appendChild(el); } } diff --git a/src/xmlfixups.h b/src/xmlfixups.h index 99fe876..f51d4a8 100644 --- a/src/xmlfixups.h +++ b/src/xmlfixups.h @@ -39,7 +39,7 @@ #ifndef __XMLFIXUPS_H__ #define __XMLFIXUPS_H__ -#include "sablo.h" +#include "domcxx.h" #include "domhelpers.h" /* |