1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: odfemitter.cxx,v $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_sdext.hxx"
35 #include "odfemitter.hxx"
37 #include <rtl/ustrbuf.hxx>
38 #include <comphelper/anytostring.hxx>
39 #include <cppuhelper/exc_hlp.hxx>
40 #include <com/sun/star/io/XInputStream.hpp>
41 #include <com/sun/star/io/XOutputStream.hpp>
42 #include <boost/bind.hpp>
44 using namespace com::sun::star
;
49 class OdfEmitter
: public XmlEmitter
52 uno::Reference
<io::XOutputStream
> m_xOutput
;
53 uno::Sequence
<sal_Int8
> m_aLineFeed
;
54 uno::Sequence
<sal_Int8
> m_aBuf
;
57 explicit OdfEmitter( const uno::Reference
<io::XOutputStream
>& xOutput
);
59 virtual void beginTag( const char* pTag
, const PropertyMap
& rProperties
);
60 virtual void write( const rtl::OUString
& rString
);
61 virtual void endTag( const char* pTag
);
64 OdfEmitter::OdfEmitter( const uno::Reference
<io::XOutputStream
>& xOutput
) :
69 OSL_PRECOND(m_xOutput
.is(), "OdfEmitter(): invalid output stream");
70 m_aLineFeed
[0] = '\n';
72 rtl::OUStringBuffer aElement
;
73 aElement
.appendAscii("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
74 write(aElement
.makeStringAndClear());
77 void OdfEmitter::beginTag( const char* pTag
, const PropertyMap
& rProperties
)
79 OSL_PRECOND(pTag
,"Invalid tag string");
81 rtl::OUStringBuffer aElement
;
82 aElement
.appendAscii("<");
83 aElement
.appendAscii(pTag
);
84 aElement
.appendAscii(" ");
86 std::vector
<rtl::OUString
> aAttributes
;
87 PropertyMap::const_iterator
aCurr(rProperties
.begin());
88 const PropertyMap::const_iterator
aEnd(rProperties
.end());
89 while( aCurr
!= aEnd
)
91 rtl::OUStringBuffer aAttribute
;
92 aAttribute
.append(aCurr
->first
);
93 aAttribute
.appendAscii("=\"");
94 aAttribute
.append(aCurr
->second
);
95 aAttribute
.appendAscii("\" ");
96 aAttributes
.push_back(aAttribute
.makeStringAndClear());
100 // since the hash map's sorting is undefined (and varies across
101 // platforms, and even between different compile-time settings),
102 // sort the attributes.
103 std::sort(aAttributes
.begin(), aAttributes
.end());
104 std::for_each(aAttributes
.begin(),
106 boost::bind( (rtl::OUStringBuffer
& (rtl::OUStringBuffer::*)(const rtl::OUString
&))
107 (&rtl::OUStringBuffer::append
),
108 boost::ref(aElement
),
110 aElement
.appendAscii(">");
112 write(aElement
.makeStringAndClear());
115 void OdfEmitter::write( const rtl::OUString
& rText
)
117 const rtl::OString aStr
= rtl::OUStringToOString(rText
,RTL_TEXTENCODING_UTF8
);
118 const sal_Int32
nLen( aStr
.getLength() );
119 m_aBuf
.realloc( nLen
);
120 const sal_Char
* pStr
= aStr
.getStr();
121 std::copy(pStr
,pStr
+nLen
,m_aBuf
.getArray());
123 m_xOutput
->writeBytes(m_aBuf
);
124 m_xOutput
->writeBytes(m_aLineFeed
);
127 void OdfEmitter::endTag( const char* pTag
)
129 rtl::OUStringBuffer aElement
;
130 aElement
.appendAscii("</");
131 aElement
.appendAscii(pTag
);
132 aElement
.appendAscii(">");
133 write(aElement
.makeStringAndClear());
136 XmlEmitterSharedPtr
createOdfEmitter( const uno::Reference
<io::XOutputStream
>& xOut
)
138 return XmlEmitterSharedPtr(new OdfEmitter(xOut
));