Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*************************************************************************
3 : *
4 : * OpenOffice.org - a multi-platform office productivity suite
5 : *
6 : * Author:
7 : * Fridrich Strba <fridrich.strba@bluewin.ch>
8 : * Thorsten Behrens <tbehrens@novell.com>
9 : *
10 : * Copyright (C) 2008, Novell Inc.
11 : * Parts copyright 2005 by Sun Microsystems, Inc.
12 : *
13 : * The Contents of this file are made available subject to
14 : * the terms of GNU Lesser General Public License Version 3.
15 : *
16 : ************************************************************************/
17 :
18 :
19 : #include "odfserializer.hxx"
20 : #include <osl/diagnose.h>
21 : #include <rtl/ustrbuf.hxx>
22 : #include <cppuhelper/compbase1.hxx>
23 : #include <cppuhelper/basemutex.hxx>
24 : #include <com/sun/star/uno/Sequence.hxx>
25 : #include <boost/noncopyable.hpp>
26 :
27 : using namespace ::com::sun::star;
28 :
29 : namespace svgi
30 : {
31 :
32 : typedef ::cppu::WeakComponentImplHelper1<
33 : com::sun::star::xml::sax::XDocumentHandler> ODFSerializerBase;
34 :
35 0 : class ODFSerializer : private cppu::BaseMutex,
36 : public ODFSerializerBase,
37 : boost::noncopyable
38 : {
39 : public:
40 0 : explicit ODFSerializer(const uno::Reference<io::XOutputStream>& xOut) :
41 : ODFSerializerBase(m_aMutex),
42 : m_xOutStream(xOut),
43 : m_aLineFeed(1),
44 0 : m_aBuf()
45 : {
46 0 : m_aLineFeed[0] = '\n';
47 0 : }
48 :
49 : virtual void SAL_CALL startDocument( ) throw (xml::sax::SAXException, uno::RuntimeException);
50 : virtual void SAL_CALL endDocument( ) throw (xml::sax::SAXException, uno::RuntimeException);
51 : virtual void SAL_CALL startElement( const ::rtl::OUString& aName, const uno::Reference< xml::sax::XAttributeList >& xAttribs ) throw (xml::sax::SAXException, uno::RuntimeException);
52 : virtual void SAL_CALL endElement( const ::rtl::OUString& aName ) throw (xml::sax::SAXException, uno::RuntimeException);
53 : virtual void SAL_CALL characters( const ::rtl::OUString& aChars ) throw (xml::sax::SAXException, uno::RuntimeException);
54 : virtual void SAL_CALL ignorableWhitespace( const ::rtl::OUString& aWhitespaces ) throw (xml::sax::SAXException, uno::RuntimeException);
55 : virtual void SAL_CALL processingInstruction( const ::rtl::OUString& aTarget, const ::rtl::OUString& aData ) throw (xml::sax::SAXException, uno::RuntimeException);
56 : virtual void SAL_CALL setDocumentLocator( const uno::Reference< xml::sax::XLocator >& xLocator ) throw (xml::sax::SAXException, uno::RuntimeException);
57 :
58 : private:
59 : uno::Reference<io::XOutputStream> m_xOutStream;
60 : uno::Sequence<sal_Int8> m_aLineFeed;
61 : uno::Sequence<sal_Int8> m_aBuf;
62 : };
63 :
64 0 : void SAL_CALL ODFSerializer::startDocument( ) throw (xml::sax::SAXException, uno::RuntimeException)
65 : {
66 : OSL_PRECOND(m_xOutStream.is(), "ODFSerializer(): invalid output stream");
67 :
68 0 : rtl::OUStringBuffer aElement;
69 0 : aElement.appendAscii("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
70 0 : characters(aElement.makeStringAndClear());
71 0 : }
72 :
73 0 : void SAL_CALL ODFSerializer::endDocument() throw (xml::sax::SAXException, uno::RuntimeException)
74 0 : {}
75 :
76 0 : void SAL_CALL ODFSerializer::startElement( const ::rtl::OUString& aName,
77 : const uno::Reference< xml::sax::XAttributeList >& xAttribs ) throw (xml::sax::SAXException, uno::RuntimeException)
78 : {
79 0 : rtl::OUStringBuffer aElement;
80 0 : aElement.appendAscii("<");
81 0 : aElement.append(aName);
82 0 : aElement.appendAscii(" ");
83 :
84 0 : const sal_Int16 nLen=xAttribs->getLength();
85 0 : for( sal_Int16 i=0; i<nLen; ++i )
86 : {
87 0 : rtl::OUStringBuffer aAttribute;
88 0 : aElement.append(xAttribs->getNameByIndex(i));
89 0 : aElement.appendAscii("=\"");
90 0 : aElement.append(xAttribs->getValueByIndex(i));
91 0 : aElement.appendAscii("\" ");
92 0 : }
93 :
94 0 : aElement.appendAscii(">");
95 0 : characters(aElement.makeStringAndClear());
96 0 : }
97 :
98 0 : void SAL_CALL ODFSerializer::endElement( const ::rtl::OUString& aName ) throw (xml::sax::SAXException, uno::RuntimeException)
99 : {
100 0 : rtl::OUStringBuffer aElement;
101 0 : aElement.appendAscii("</");
102 0 : aElement.append(aName);
103 0 : aElement.appendAscii(">");
104 0 : characters(aElement.makeStringAndClear());
105 0 : }
106 :
107 0 : void SAL_CALL ODFSerializer::characters( const ::rtl::OUString& aChars ) throw (xml::sax::SAXException, uno::RuntimeException)
108 : {
109 : const rtl::OString aStr = rtl::OUStringToOString(aChars,
110 0 : RTL_TEXTENCODING_UTF8);
111 0 : const sal_Int32 nLen( aStr.getLength() );
112 0 : m_aBuf.realloc( nLen );
113 0 : const sal_Char* pStr = aStr.getStr();
114 0 : std::copy(pStr,pStr+nLen,m_aBuf.getArray());
115 :
116 0 : m_xOutStream->writeBytes(m_aBuf);
117 : // TODO(F1): Make pretty printing configurable
118 0 : m_xOutStream->writeBytes(m_aLineFeed);
119 0 : }
120 :
121 0 : void SAL_CALL ODFSerializer::ignorableWhitespace( const ::rtl::OUString& aWhitespaces ) throw (xml::sax::SAXException, uno::RuntimeException)
122 : {
123 : // TODO(F1): Make pretty printing configurable
124 0 : characters(aWhitespaces);
125 0 : }
126 :
127 0 : void SAL_CALL ODFSerializer::processingInstruction( const ::rtl::OUString&,
128 : const ::rtl::OUString& ) throw (xml::sax::SAXException, uno::RuntimeException)
129 0 : {}
130 :
131 0 : void SAL_CALL ODFSerializer::setDocumentLocator( const uno::Reference< xml::sax::XLocator >& ) throw (xml::sax::SAXException, uno::RuntimeException)
132 0 : {}
133 :
134 0 : uno::Reference< xml::sax::XDocumentHandler> createSerializer(const uno::Reference<io::XOutputStream>& xOut )
135 : {
136 0 : return uno::Reference<xml::sax::XDocumentHandler>(new ODFSerializer(xOut));
137 : }
138 :
139 : }
140 :
141 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|