File indexing completed on 2024-05-12 16:29:20

0001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
0002 /* writerperfect
0003  * Version: MPL 2.0 / LGPLv2.1+
0004  *
0005  * This Source Code Form is subject to the terms of the Mozilla Public
0006  * License, v. 2.0. If a copy of the MPL was not distributed with this
0007  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
0008  *
0009  * Major Contributor(s):
0010  * Copyright (C) 2002-2004 William Lachance (wrlach@gmail.com)
0011  * Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch)
0012  *
0013  * For minor contributions see the git repository.
0014  *
0015  * Alternatively, the contents of this file may be used under the terms
0016  * of the GNU Lesser General Public License Version 2.1 or later
0017  * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
0018  * applicable instead of those above.
0019  *
0020  * For further information visit http://libwpd.sourceforge.net
0021  */
0022 
0023 #include <string.h>
0024 
0025 #include "StringDocumentHandler.hxx"
0026 
0027 StringDocumentHandler::StringDocumentHandler() : m_data(""), m_isTagOpened(false), m_openedTagName("")
0028 {
0029     m_data.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
0030 }
0031 
0032 void StringDocumentHandler::endDocument()
0033 {
0034     if (!m_isTagOpened) return;
0035     m_data.append(">");
0036     m_isTagOpened = false;
0037 }
0038 
0039 void StringDocumentHandler::startElement(const char *psName, const librevenge::RVNGPropertyList &xPropList)
0040 {
0041     if (m_isTagOpened)
0042     {
0043         m_data.append(">");
0044         m_isTagOpened = false;
0045     }
0046     m_data.append("<");
0047     m_data.append(psName);
0048     librevenge::RVNGPropertyList::Iter i(xPropList);
0049     for (i.rewind(); i.next();)
0050     {
0051         // filter out librevenge elements
0052         if (strncmp(i.key(), "librevenge", 10) == 0) continue;
0053 
0054         m_data.append(" ");
0055         m_data.append(i.key());
0056         m_data.append("=\"");
0057         if (i()->getStr().len()>0)
0058             m_data.append(i()->getStr().cstr());
0059         m_data.append("\"");
0060     }
0061     m_isTagOpened = true;
0062     m_openedTagName.sprintf("%s", psName);
0063 }
0064 void StringDocumentHandler::endElement(const char *psName)
0065 {
0066     if (m_isTagOpened)
0067     {
0068         if (m_openedTagName == psName)
0069         {
0070             m_data.append("/>");
0071             m_isTagOpened = false;
0072         }
0073         else // should not happen, but handle it
0074         {
0075             m_data.append(">");
0076             m_data.append("</");
0077             m_data.append(psName);
0078             m_data.append(">");
0079             m_isTagOpened = false;
0080         }
0081     }
0082     else
0083     {
0084         m_data.append("</");
0085         m_data.append(psName);
0086         m_data.append(">");
0087         m_isTagOpened = false;
0088     }
0089 }
0090 
0091 void StringDocumentHandler::characters(const librevenge::RVNGString &sCharacters)
0092 {
0093     if (m_isTagOpened)
0094     {
0095         m_data.append(">");
0096         m_isTagOpened = false;
0097     }
0098     librevenge::RVNGString sEscapedCharacters;
0099     sEscapedCharacters.appendEscapedXML(sCharacters);
0100     if (sEscapedCharacters.len() > 0)
0101         m_data.append(sEscapedCharacters.cstr());
0102 }
0103 
0104 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */