1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "odfemitter.hxx"
23 #include <rtl/ustrbuf.hxx>
24 #include <cppuhelper/exc_hlp.hxx>
25 #include <com/sun/star/io/XInputStream.hpp>
26 #include <com/sun/star/io/XOutputStream.hpp>
27 #include <boost/bind.hpp>
29 using namespace com::sun::star
;
34 class OdfEmitter
: public XmlEmitter
37 uno::Reference
<io::XOutputStream
> m_xOutput
;
38 uno::Sequence
<sal_Int8
> m_aLineFeed
;
39 uno::Sequence
<sal_Int8
> m_aBuf
;
42 explicit OdfEmitter( const uno::Reference
<io::XOutputStream
>& xOutput
);
44 virtual void beginTag( const char* pTag
, const PropertyMap
& rProperties
);
45 virtual void write( const OUString
& rString
);
46 virtual void endTag( const char* pTag
);
49 OdfEmitter::OdfEmitter( const uno::Reference
<io::XOutputStream
>& xOutput
) :
54 OSL_PRECOND(m_xOutput
.is(), "OdfEmitter(): invalid output stream");
55 m_aLineFeed
[0] = '\n';
57 OUStringBuffer aElement
;
58 aElement
.appendAscii("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
59 write(aElement
.makeStringAndClear());
62 void OdfEmitter::beginTag( const char* pTag
, const PropertyMap
& rProperties
)
64 OSL_PRECOND(pTag
,"Invalid tag string");
66 OUStringBuffer aElement
;
67 aElement
.appendAscii("<");
68 aElement
.appendAscii(pTag
);
69 aElement
.appendAscii(" ");
71 std::vector
<OUString
> aAttributes
;
72 PropertyMap::const_iterator
aCurr(rProperties
.begin());
73 const PropertyMap::const_iterator
aEnd(rProperties
.end());
74 while( aCurr
!= aEnd
)
76 OUStringBuffer aAttribute
;
77 aAttribute
.append(aCurr
->first
);
78 aAttribute
.appendAscii("=\"");
79 aAttribute
.append(aCurr
->second
);
80 aAttribute
.appendAscii("\" ");
81 aAttributes
.push_back(aAttribute
.makeStringAndClear());
85 // since the hash map's sorting is undefined (and varies across
86 // platforms, and even between different compile-time settings),
87 // sort the attributes.
88 std::sort(aAttributes
.begin(), aAttributes
.end());
89 std::for_each(aAttributes
.begin(),
91 boost::bind( (OUStringBuffer
& (OUStringBuffer::*)(const OUString
&))
92 (&OUStringBuffer::append
),
95 aElement
.appendAscii(">");
97 write(aElement
.makeStringAndClear());
100 void OdfEmitter::write( const OUString
& rText
)
102 const OString aStr
= OUStringToOString(rText
,RTL_TEXTENCODING_UTF8
);
103 const sal_Int32
nLen( aStr
.getLength() );
104 m_aBuf
.realloc( nLen
);
105 const sal_Char
* pStr
= aStr
.getStr();
106 std::copy(pStr
,pStr
+nLen
,m_aBuf
.getArray());
108 m_xOutput
->writeBytes(m_aBuf
);
109 m_xOutput
->writeBytes(m_aLineFeed
);
112 void OdfEmitter::endTag( const char* pTag
)
114 OUStringBuffer aElement
;
115 aElement
.appendAscii("</");
116 aElement
.appendAscii(pTag
);
117 aElement
.appendAscii(">");
118 write(aElement
.makeStringAndClear());
121 XmlEmitterSharedPtr
createOdfEmitter( const uno::Reference
<io::XOutputStream
>& xOut
)
123 return XmlEmitterSharedPtr(new OdfEmitter(xOut
));
128 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */