1 /*************************************************************************
3 * OpenOffice.org - a multi-platform office productivity suite
6 * Fridrich Strba <fridrich.strba@bluewin.ch>
7 * Thorsten Behrens <tbehrens@novell.com>
9 * Copyright (C) 2008, Novell Inc.
10 * Parts copyright 2005 by Sun Microsystems, Inc.
12 * The Contents of this file are made available subject to
13 * the terms of GNU Lesser General Public License Version 2.1.
15 ************************************************************************/
17 // MARKER(update_precomp.py): autogen include statement, do not remove
18 #include "precompiled_filter.hxx"
20 #include "odfserializer.hxx"
21 #include <osl/diagnose.h>
22 #include <rtl/ustrbuf.hxx>
23 #include <cppuhelper/compbase1.hxx>
24 #include <cppuhelper/basemutex.hxx>
25 #include <com/sun/star/uno/Sequence.hxx>
26 #include <boost/noncopyable.hpp>
28 using namespace ::com::sun::star
;
33 typedef ::cppu::WeakComponentImplHelper1
<
34 com::sun::star::xml::sax::XDocumentHandler
> ODFSerializerBase
;
36 class ODFSerializer
: private cppu::BaseMutex
,
37 public ODFSerializerBase
,
41 explicit ODFSerializer(const uno::Reference
<io::XOutputStream
>& xOut
) :
42 ODFSerializerBase(m_aMutex
),
47 m_aLineFeed
[0] = '\n';
50 virtual void SAL_CALL
startDocument( ) throw (xml::sax::SAXException
, uno::RuntimeException
);
51 virtual void SAL_CALL
endDocument( ) throw (xml::sax::SAXException
, uno::RuntimeException
);
52 virtual void SAL_CALL
startElement( const ::rtl::OUString
& aName
, const uno::Reference
< xml::sax::XAttributeList
>& xAttribs
) throw (xml::sax::SAXException
, uno::RuntimeException
);
53 virtual void SAL_CALL
endElement( const ::rtl::OUString
& aName
) throw (xml::sax::SAXException
, uno::RuntimeException
);
54 virtual void SAL_CALL
characters( const ::rtl::OUString
& aChars
) throw (xml::sax::SAXException
, uno::RuntimeException
);
55 virtual void SAL_CALL
ignorableWhitespace( const ::rtl::OUString
& aWhitespaces
) throw (xml::sax::SAXException
, uno::RuntimeException
);
56 virtual void SAL_CALL
processingInstruction( const ::rtl::OUString
& aTarget
, const ::rtl::OUString
& aData
) throw (xml::sax::SAXException
, uno::RuntimeException
);
57 virtual void SAL_CALL
setDocumentLocator( const uno::Reference
< xml::sax::XLocator
>& xLocator
) throw (xml::sax::SAXException
, uno::RuntimeException
);
60 uno::Reference
<io::XOutputStream
> m_xOutStream
;
61 uno::Sequence
<sal_Int8
> m_aLineFeed
;
62 uno::Sequence
<sal_Int8
> m_aBuf
;
65 void SAL_CALL
ODFSerializer::startDocument( ) throw (xml::sax::SAXException
, uno::RuntimeException
)
67 OSL_PRECOND(m_xOutStream
.is(), "ODFSerializer(): invalid output stream");
69 rtl::OUStringBuffer aElement
;
70 aElement
.appendAscii("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
71 characters(aElement
.makeStringAndClear());
74 void SAL_CALL
ODFSerializer::endDocument() throw (xml::sax::SAXException
, uno::RuntimeException
)
77 void SAL_CALL
ODFSerializer::startElement( const ::rtl::OUString
& aName
,
78 const uno::Reference
< xml::sax::XAttributeList
>& xAttribs
) throw (xml::sax::SAXException
, uno::RuntimeException
)
80 rtl::OUStringBuffer aElement
;
81 aElement
.appendAscii("<");
82 aElement
.append(aName
);
83 aElement
.appendAscii(" ");
85 const sal_Int16 nLen
=xAttribs
->getLength();
86 for( sal_Int16 i
=0; i
<nLen
; ++i
)
88 rtl::OUStringBuffer aAttribute
;
89 aElement
.append(xAttribs
->getNameByIndex(i
));
90 aElement
.appendAscii("=\"");
91 aElement
.append(xAttribs
->getValueByIndex(i
));
92 aElement
.appendAscii("\" ");
95 aElement
.appendAscii(">");
96 characters(aElement
.makeStringAndClear());
99 void SAL_CALL
ODFSerializer::endElement( const ::rtl::OUString
& aName
) throw (xml::sax::SAXException
, uno::RuntimeException
)
101 rtl::OUStringBuffer aElement
;
102 aElement
.appendAscii("</");
103 aElement
.append(aName
);
104 aElement
.appendAscii(">");
105 characters(aElement
.makeStringAndClear());
108 void SAL_CALL
ODFSerializer::characters( const ::rtl::OUString
& aChars
) throw (xml::sax::SAXException
, uno::RuntimeException
)
110 const rtl::OString aStr
= rtl::OUStringToOString(aChars
,
111 RTL_TEXTENCODING_UTF8
);
112 const sal_Int32
nLen( aStr
.getLength() );
113 m_aBuf
.realloc( nLen
);
114 const sal_Char
* pStr
= aStr
.getStr();
115 std::copy(pStr
,pStr
+nLen
,m_aBuf
.getArray());
117 m_xOutStream
->writeBytes(m_aBuf
);
118 // TODO(F1): Make pretty printing configurable
119 m_xOutStream
->writeBytes(m_aLineFeed
);
122 void SAL_CALL
ODFSerializer::ignorableWhitespace( const ::rtl::OUString
& aWhitespaces
) throw (xml::sax::SAXException
, uno::RuntimeException
)
124 // TODO(F1): Make pretty printing configurable
125 characters(aWhitespaces
);
128 void SAL_CALL
ODFSerializer::processingInstruction( const ::rtl::OUString
&,
129 const ::rtl::OUString
& ) throw (xml::sax::SAXException
, uno::RuntimeException
)
132 void SAL_CALL
ODFSerializer::setDocumentLocator( const uno::Reference
< xml::sax::XLocator
>& ) throw (xml::sax::SAXException
, uno::RuntimeException
)
135 uno::Reference
< xml::sax::XDocumentHandler
> createSerializer(const uno::Reference
<io::XOutputStream
>& xOut
)
137 return uno::Reference
<xml::sax::XDocumentHandler
>(new ODFSerializer(xOut
));