1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* writerperfect
3  * Version: MPL 2.0 / LGPLv2.1+
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * Major Contributor(s):
10  * Copyright (C) 2002-2004 William Lachance (wrlach@gmail.com)
11  * Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch)
12  *
13  * For minor contributions see the git repository.
14  *
15  * Alternatively, the contents of this file may be used under the terms
16  * of the GNU Lesser General Public License Version 2.1 or later
17  * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
18  * applicable instead of those above.
19  *
20  * For further information visit http://libwpd.sourceforge.net
21  */
22 
23 #include <string.h>
24 
25 #include "StringDocumentHandler.hxx"
26 
StringDocumentHandler()27 StringDocumentHandler::StringDocumentHandler() : m_data(""), m_isTagOpened(false), m_openedTagName("")
28 {
29 	m_data.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
30 }
31 
endDocument()32 void StringDocumentHandler::endDocument()
33 {
34 	if (!m_isTagOpened) return;
35 	m_data.append(">");
36 	m_isTagOpened = false;
37 }
38 
startElement(const char * psName,const librevenge::RVNGPropertyList & xPropList)39 void StringDocumentHandler::startElement(const char *psName, const librevenge::RVNGPropertyList &xPropList)
40 {
41 	if (m_isTagOpened)
42 	{
43 		m_data.append(">");
44 		m_isTagOpened = false;
45 	}
46 	m_data.append("<");
47 	m_data.append(psName);
48 	librevenge::RVNGPropertyList::Iter i(xPropList);
49 	for (i.rewind(); i.next();)
50 	{
51 		// filter out librevenge elements
52 		if (!strncmp(i.key(), "librevenge:", 11)) continue;
53 
54 		m_data.append(" ");
55 		m_data.append(i.key());
56 		m_data.append("=\"");
57 		if (i()->getStr().len()>0)
58 			m_data.append(i()->getStr().cstr());
59 		m_data.append("\"");
60 	}
61 	m_isTagOpened = true;
62 	m_openedTagName.sprintf("%s", psName);
63 }
endElement(const char * psName)64 void StringDocumentHandler::endElement(const char *psName)
65 {
66 	if (m_isTagOpened)
67 	{
68 		if (m_openedTagName == psName)
69 		{
70 			m_data.append("/>");
71 			m_isTagOpened = false;
72 		}
73 		else // should not happen, but handle it
74 		{
75 			m_data.append(">");
76 			m_data.append("</");
77 			m_data.append(psName);
78 			m_data.append(">");
79 			m_isTagOpened = false;
80 		}
81 	}
82 	else
83 	{
84 		m_data.append("</");
85 		m_data.append(psName);
86 		m_data.append(">");
87 		m_isTagOpened = false;
88 	}
89 }
90 
characters(const librevenge::RVNGString & sCharacters)91 void StringDocumentHandler::characters(const librevenge::RVNGString &sCharacters)
92 {
93 	if (m_isTagOpened)
94 	{
95 		m_data.append(">");
96 		m_isTagOpened = false;
97 	}
98 	librevenge::RVNGString sEscapedCharacters;
99 	sEscapedCharacters.appendEscapedXML(sCharacters);
100 	if (sEscapedCharacters.len() > 0)
101 		m_data.append(sEscapedCharacters.cstr());
102 }
103 
104 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */
105