xref: /AOO41X/main/sdext/source/pdfimport/odf/odfemitter.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_sdext.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "odfemitter.hxx"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
34*cdf0e10cSrcweir #include <cppuhelper/exc_hlp.hxx>
35*cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/io/XOutputStream.hpp>
37*cdf0e10cSrcweir #include <boost/bind.hpp>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir using namespace com::sun::star;
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir namespace pdfi
42*cdf0e10cSrcweir {
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir class OdfEmitter : public XmlEmitter
45*cdf0e10cSrcweir {
46*cdf0e10cSrcweir private:
47*cdf0e10cSrcweir     uno::Reference<io::XOutputStream> m_xOutput;
48*cdf0e10cSrcweir     uno::Sequence<sal_Int8>           m_aLineFeed;
49*cdf0e10cSrcweir     uno::Sequence<sal_Int8>           m_aBuf;
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir public:
52*cdf0e10cSrcweir     explicit OdfEmitter( const uno::Reference<io::XOutputStream>& xOutput );
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir     virtual void beginTag( const char* pTag, const PropertyMap& rProperties );
55*cdf0e10cSrcweir     virtual void write( const rtl::OUString& rString );
56*cdf0e10cSrcweir     virtual void endTag( const char* pTag );
57*cdf0e10cSrcweir };
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir OdfEmitter::OdfEmitter( const uno::Reference<io::XOutputStream>& xOutput ) :
60*cdf0e10cSrcweir     m_xOutput( xOutput ),
61*cdf0e10cSrcweir     m_aLineFeed(1),
62*cdf0e10cSrcweir     m_aBuf()
63*cdf0e10cSrcweir {
64*cdf0e10cSrcweir     OSL_PRECOND(m_xOutput.is(), "OdfEmitter(): invalid output stream");
65*cdf0e10cSrcweir     m_aLineFeed[0] = '\n';
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir     rtl::OUStringBuffer aElement;
68*cdf0e10cSrcweir     aElement.appendAscii("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
69*cdf0e10cSrcweir     write(aElement.makeStringAndClear());
70*cdf0e10cSrcweir }
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir void OdfEmitter::beginTag( const char* pTag, const PropertyMap& rProperties )
73*cdf0e10cSrcweir {
74*cdf0e10cSrcweir     OSL_PRECOND(pTag,"Invalid tag string");
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir     rtl::OUStringBuffer aElement;
77*cdf0e10cSrcweir     aElement.appendAscii("<");
78*cdf0e10cSrcweir     aElement.appendAscii(pTag);
79*cdf0e10cSrcweir     aElement.appendAscii(" ");
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir     std::vector<rtl::OUString>        aAttributes;
82*cdf0e10cSrcweir     PropertyMap::const_iterator       aCurr(rProperties.begin());
83*cdf0e10cSrcweir     const PropertyMap::const_iterator aEnd(rProperties.end());
84*cdf0e10cSrcweir     while( aCurr != aEnd )
85*cdf0e10cSrcweir     {
86*cdf0e10cSrcweir         rtl::OUStringBuffer aAttribute;
87*cdf0e10cSrcweir         aAttribute.append(aCurr->first);
88*cdf0e10cSrcweir         aAttribute.appendAscii("=\"");
89*cdf0e10cSrcweir         aAttribute.append(aCurr->second);
90*cdf0e10cSrcweir         aAttribute.appendAscii("\" ");
91*cdf0e10cSrcweir         aAttributes.push_back(aAttribute.makeStringAndClear());
92*cdf0e10cSrcweir         ++aCurr;
93*cdf0e10cSrcweir     }
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir     // since the hash map's sorting is undefined (and varies across
96*cdf0e10cSrcweir     // platforms, and even between different compile-time settings),
97*cdf0e10cSrcweir     // sort the attributes.
98*cdf0e10cSrcweir     std::sort(aAttributes.begin(), aAttributes.end());
99*cdf0e10cSrcweir     std::for_each(aAttributes.begin(),
100*cdf0e10cSrcweir                   aAttributes.end(),
101*cdf0e10cSrcweir                   boost::bind( (rtl::OUStringBuffer& (rtl::OUStringBuffer::*)(const rtl::OUString&))
102*cdf0e10cSrcweir                                (&rtl::OUStringBuffer::append),
103*cdf0e10cSrcweir                                boost::ref(aElement),
104*cdf0e10cSrcweir                                _1 ));
105*cdf0e10cSrcweir     aElement.appendAscii(">");
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir     write(aElement.makeStringAndClear());
108*cdf0e10cSrcweir }
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir void OdfEmitter::write( const rtl::OUString& rText )
111*cdf0e10cSrcweir {
112*cdf0e10cSrcweir     const rtl::OString aStr = rtl::OUStringToOString(rText,RTL_TEXTENCODING_UTF8);
113*cdf0e10cSrcweir     const sal_Int32 nLen( aStr.getLength() );
114*cdf0e10cSrcweir     m_aBuf.realloc( nLen );
115*cdf0e10cSrcweir     const sal_Char* pStr = aStr.getStr();
116*cdf0e10cSrcweir     std::copy(pStr,pStr+nLen,m_aBuf.getArray());
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir     m_xOutput->writeBytes(m_aBuf);
119*cdf0e10cSrcweir     m_xOutput->writeBytes(m_aLineFeed);
120*cdf0e10cSrcweir }
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir void OdfEmitter::endTag( const char* pTag )
123*cdf0e10cSrcweir {
124*cdf0e10cSrcweir     rtl::OUStringBuffer aElement;
125*cdf0e10cSrcweir     aElement.appendAscii("</");
126*cdf0e10cSrcweir     aElement.appendAscii(pTag);
127*cdf0e10cSrcweir     aElement.appendAscii(">");
128*cdf0e10cSrcweir     write(aElement.makeStringAndClear());
129*cdf0e10cSrcweir }
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir XmlEmitterSharedPtr createOdfEmitter( const uno::Reference<io::XOutputStream>& xOut )
132*cdf0e10cSrcweir {
133*cdf0e10cSrcweir     return XmlEmitterSharedPtr(new OdfEmitter(xOut));
134*cdf0e10cSrcweir }
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir }
137