1*c142477cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*c142477cSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*c142477cSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*c142477cSAndrew Rist * distributed with this work for additional information
6*c142477cSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*c142477cSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*c142477cSAndrew Rist * "License"); you may not use this file except in compliance
9*c142477cSAndrew Rist * with the License. You may obtain a copy of the License at
10*c142477cSAndrew Rist *
11*c142477cSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*c142477cSAndrew Rist *
13*c142477cSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*c142477cSAndrew Rist * software distributed under the License is distributed on an
15*c142477cSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*c142477cSAndrew Rist * KIND, either express or implied. See the License for the
17*c142477cSAndrew Rist * specific language governing permissions and limitations
18*c142477cSAndrew Rist * under the License.
19*c142477cSAndrew Rist *
20*c142477cSAndrew Rist *************************************************************/
21*c142477cSAndrew Rist
22*c142477cSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sdext.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include "odfemitter.hxx"
28cdf0e10cSrcweir
29cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
30cdf0e10cSrcweir #include <cppuhelper/exc_hlp.hxx>
31cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp>
32cdf0e10cSrcweir #include <com/sun/star/io/XOutputStream.hpp>
33cdf0e10cSrcweir #include <boost/bind.hpp>
34cdf0e10cSrcweir
35cdf0e10cSrcweir using namespace com::sun::star;
36cdf0e10cSrcweir
37cdf0e10cSrcweir namespace pdfi
38cdf0e10cSrcweir {
39cdf0e10cSrcweir
40cdf0e10cSrcweir class OdfEmitter : public XmlEmitter
41cdf0e10cSrcweir {
42cdf0e10cSrcweir private:
43cdf0e10cSrcweir uno::Reference<io::XOutputStream> m_xOutput;
44cdf0e10cSrcweir uno::Sequence<sal_Int8> m_aLineFeed;
45cdf0e10cSrcweir uno::Sequence<sal_Int8> m_aBuf;
46cdf0e10cSrcweir
47cdf0e10cSrcweir public:
48cdf0e10cSrcweir explicit OdfEmitter( const uno::Reference<io::XOutputStream>& xOutput );
49cdf0e10cSrcweir
50cdf0e10cSrcweir virtual void beginTag( const char* pTag, const PropertyMap& rProperties );
51cdf0e10cSrcweir virtual void write( const rtl::OUString& rString );
52cdf0e10cSrcweir virtual void endTag( const char* pTag );
53cdf0e10cSrcweir };
54cdf0e10cSrcweir
OdfEmitter(const uno::Reference<io::XOutputStream> & xOutput)55cdf0e10cSrcweir OdfEmitter::OdfEmitter( const uno::Reference<io::XOutputStream>& xOutput ) :
56cdf0e10cSrcweir m_xOutput( xOutput ),
57cdf0e10cSrcweir m_aLineFeed(1),
58cdf0e10cSrcweir m_aBuf()
59cdf0e10cSrcweir {
60cdf0e10cSrcweir OSL_PRECOND(m_xOutput.is(), "OdfEmitter(): invalid output stream");
61cdf0e10cSrcweir m_aLineFeed[0] = '\n';
62cdf0e10cSrcweir
63cdf0e10cSrcweir rtl::OUStringBuffer aElement;
64cdf0e10cSrcweir aElement.appendAscii("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
65cdf0e10cSrcweir write(aElement.makeStringAndClear());
66cdf0e10cSrcweir }
67cdf0e10cSrcweir
beginTag(const char * pTag,const PropertyMap & rProperties)68cdf0e10cSrcweir void OdfEmitter::beginTag( const char* pTag, const PropertyMap& rProperties )
69cdf0e10cSrcweir {
70cdf0e10cSrcweir OSL_PRECOND(pTag,"Invalid tag string");
71cdf0e10cSrcweir
72cdf0e10cSrcweir rtl::OUStringBuffer aElement;
73cdf0e10cSrcweir aElement.appendAscii("<");
74cdf0e10cSrcweir aElement.appendAscii(pTag);
75cdf0e10cSrcweir aElement.appendAscii(" ");
76cdf0e10cSrcweir
77cdf0e10cSrcweir std::vector<rtl::OUString> aAttributes;
78cdf0e10cSrcweir PropertyMap::const_iterator aCurr(rProperties.begin());
79cdf0e10cSrcweir const PropertyMap::const_iterator aEnd(rProperties.end());
80cdf0e10cSrcweir while( aCurr != aEnd )
81cdf0e10cSrcweir {
82cdf0e10cSrcweir rtl::OUStringBuffer aAttribute;
83cdf0e10cSrcweir aAttribute.append(aCurr->first);
84cdf0e10cSrcweir aAttribute.appendAscii("=\"");
85cdf0e10cSrcweir aAttribute.append(aCurr->second);
86cdf0e10cSrcweir aAttribute.appendAscii("\" ");
87cdf0e10cSrcweir aAttributes.push_back(aAttribute.makeStringAndClear());
88cdf0e10cSrcweir ++aCurr;
89cdf0e10cSrcweir }
90cdf0e10cSrcweir
91cdf0e10cSrcweir // since the hash map's sorting is undefined (and varies across
92cdf0e10cSrcweir // platforms, and even between different compile-time settings),
93cdf0e10cSrcweir // sort the attributes.
94cdf0e10cSrcweir std::sort(aAttributes.begin(), aAttributes.end());
95cdf0e10cSrcweir std::for_each(aAttributes.begin(),
96cdf0e10cSrcweir aAttributes.end(),
97cdf0e10cSrcweir boost::bind( (rtl::OUStringBuffer& (rtl::OUStringBuffer::*)(const rtl::OUString&))
98cdf0e10cSrcweir (&rtl::OUStringBuffer::append),
99cdf0e10cSrcweir boost::ref(aElement),
100cdf0e10cSrcweir _1 ));
101cdf0e10cSrcweir aElement.appendAscii(">");
102cdf0e10cSrcweir
103cdf0e10cSrcweir write(aElement.makeStringAndClear());
104cdf0e10cSrcweir }
105cdf0e10cSrcweir
write(const rtl::OUString & rText)106cdf0e10cSrcweir void OdfEmitter::write( const rtl::OUString& rText )
107cdf0e10cSrcweir {
108cdf0e10cSrcweir const rtl::OString aStr = rtl::OUStringToOString(rText,RTL_TEXTENCODING_UTF8);
109cdf0e10cSrcweir const sal_Int32 nLen( aStr.getLength() );
110cdf0e10cSrcweir m_aBuf.realloc( nLen );
111cdf0e10cSrcweir const sal_Char* pStr = aStr.getStr();
112cdf0e10cSrcweir std::copy(pStr,pStr+nLen,m_aBuf.getArray());
113cdf0e10cSrcweir
114cdf0e10cSrcweir m_xOutput->writeBytes(m_aBuf);
115cdf0e10cSrcweir m_xOutput->writeBytes(m_aLineFeed);
116cdf0e10cSrcweir }
117cdf0e10cSrcweir
endTag(const char * pTag)118cdf0e10cSrcweir void OdfEmitter::endTag( const char* pTag )
119cdf0e10cSrcweir {
120cdf0e10cSrcweir rtl::OUStringBuffer aElement;
121cdf0e10cSrcweir aElement.appendAscii("</");
122cdf0e10cSrcweir aElement.appendAscii(pTag);
123cdf0e10cSrcweir aElement.appendAscii(">");
124cdf0e10cSrcweir write(aElement.makeStringAndClear());
125cdf0e10cSrcweir }
126cdf0e10cSrcweir
createOdfEmitter(const uno::Reference<io::XOutputStream> & xOut)127cdf0e10cSrcweir XmlEmitterSharedPtr createOdfEmitter( const uno::Reference<io::XOutputStream>& xOut )
128cdf0e10cSrcweir {
129cdf0e10cSrcweir return XmlEmitterSharedPtr(new OdfEmitter(xOut));
130cdf0e10cSrcweir }
131cdf0e10cSrcweir
132cdf0e10cSrcweir }
133