Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : */
9 :
10 : #include "dumpfilter.hxx"
11 : #include "unofreg.hxx"
12 :
13 : #include <wrtsh.hxx>
14 : #include <cppuhelper/supportsservice.hxx>
15 : #include <docsh.hxx>
16 : #include <rootfrm.hxx>
17 : #include <unotxdoc.hxx>
18 :
19 : #include <unotools/mediadescriptor.hxx>
20 :
21 : #include <libxml/xmlwriter.h>
22 :
23 : using namespace ::com::sun::star;
24 :
25 4 : OUString SAL_CALL LayoutDumpFilter_getImplementationName() throw()
26 : {
27 4 : return OUString( "com.sun.star.comp.Writer.LayoutDump" );
28 : }
29 :
30 0 : uno::Sequence< OUString > SAL_CALL LayoutDumpFilter_getSupportedServiceNames() throw()
31 : {
32 0 : uno::Sequence< OUString > aSeq( 1 );
33 0 : aSeq[0] = "com.sun.star.document.ExportFilter";
34 0 : return aSeq;
35 : }
36 :
37 0 : uno::Reference< uno::XInterface > SAL_CALL LayoutDumpFilter_createInstance(
38 : const uno::Reference< lang::XMultiServiceFactory > & )
39 : throw (css::uno::Exception)
40 : {
41 0 : return static_cast< cppu::OWeakObject* >( new sw::LayoutDumpFilter( ) );
42 : }
43 :
44 : namespace
45 : {
46 0 : int writeCallback( void* pContext, const char* sBuffer, int nLen )
47 : {
48 0 : int written = nLen;
49 :
50 : // Actually write bytes to XOutputSream
51 : try
52 : {
53 0 : uno::XInterface* pObj = ( uno::XInterface* )pContext;
54 0 : uno::Reference< io::XOutputStream > xOut( pObj, uno::UNO_QUERY_THROW );
55 :
56 : // Don't output the terminating \0 to the xml or the file will be invalid
57 0 : uno::Sequence< sal_Int8 > seq( nLen );
58 0 : strncpy( ( char * ) seq.getArray() , sBuffer, nLen );
59 0 : xOut->writeBytes( seq );
60 : }
61 0 : catch (const uno::Exception&)
62 : {
63 0 : written = -1;
64 : }
65 :
66 0 : return written;
67 : }
68 :
69 0 : int closeCallback( void* pContext )
70 : {
71 0 : int result = 0;
72 : try
73 : {
74 0 : uno::XInterface* pObj = ( uno::XInterface* )pContext;
75 0 : uno::Reference< io::XOutputStream > xOut( pObj, uno::UNO_QUERY_THROW );
76 0 : xOut->closeOutput( );
77 : }
78 0 : catch (const uno::Exception&)
79 : {
80 0 : result = -1;
81 : }
82 0 : return result;
83 : }
84 : }
85 :
86 : namespace sw
87 : {
88 :
89 0 : LayoutDumpFilter::LayoutDumpFilter( )
90 : {
91 0 : }
92 :
93 0 : LayoutDumpFilter::~LayoutDumpFilter( )
94 : {
95 0 : }
96 :
97 : // XFilter
98 0 : sal_Bool LayoutDumpFilter::filter( const uno::Sequence< beans::PropertyValue >& aDescriptor )
99 : throw (uno::RuntimeException, std::exception)
100 : {
101 0 : bool bRet = false;
102 :
103 0 : utl::MediaDescriptor aMediaDesc = aDescriptor;
104 :
105 : // Get the output stream
106 : uno::Reference< io::XOutputStream > xOut = aMediaDesc.getUnpackedValueOrDefault(
107 0 : utl::MediaDescriptor::PROP_OUTPUTSTREAM(),
108 0 : uno::Reference< io::XOutputStream >() );
109 :
110 : // Actually get the SwRootFrm to call dumpAsXml
111 0 : uno::Reference< lang::XUnoTunnel > xDocTunnel( m_xSrcDoc, uno::UNO_QUERY );
112 0 : SwXTextDocument* pXDoc = UnoTunnelGetImplementation< SwXTextDocument >( xDocTunnel );
113 0 : if ( pXDoc )
114 : {
115 0 : SwRootFrm* pLayout = pXDoc->GetDocShell()->GetWrtShell()->GetLayout();
116 :
117 : // Get sure that the whole layout is processed: set a visible area
118 : // even though there isn't any need of it
119 0 : pXDoc->GetDocShell()->GetWrtShell()->StartAction();
120 0 : Rectangle aRect( 0, 0, 26000, 21000 );
121 0 : pXDoc->GetDocShell()->SetVisArea( aRect );
122 0 : pLayout->InvalidateAllCntnt( );
123 0 : pXDoc->GetDocShell()->GetWrtShell()->EndAction();
124 :
125 : // Dump the layout XML into the XOutputStream
126 : xmlOutputBufferPtr outBuffer = xmlOutputBufferCreateIO(
127 0 : writeCallback, closeCallback, ( void* ) xOut.get(), NULL );
128 :
129 0 : xmlTextWriterPtr writer = xmlNewTextWriter( outBuffer );
130 0 : xmlTextWriterSetIndent(writer, 1);
131 0 : xmlTextWriterStartDocument( writer, NULL, NULL, NULL );
132 :
133 : // TODO This doesn't export the whole XML file, whereas dumpAsXML() does it nicely
134 0 : pLayout->dumpAsXml( writer );
135 :
136 0 : xmlTextWriterEndDocument( writer );
137 0 : xmlFreeTextWriter( writer );
138 :
139 0 : bRet = true;
140 : }
141 :
142 0 : return bRet;
143 : }
144 :
145 0 : void LayoutDumpFilter::cancel( ) throw (uno::RuntimeException, std::exception)
146 : {
147 0 : }
148 :
149 : // XExporter
150 0 : void LayoutDumpFilter::setSourceDocument( const uno::Reference< lang::XComponent >& xDoc )
151 : throw (lang::IllegalArgumentException, uno::RuntimeException, std::exception)
152 : {
153 0 : m_xSrcDoc = xDoc;
154 0 : }
155 :
156 : // XInitialization
157 0 : void LayoutDumpFilter::initialize( const uno::Sequence< uno::Any >& )
158 : throw (uno::Exception, uno::RuntimeException, std::exception)
159 : {
160 0 : }
161 :
162 : // XServiceInfo
163 0 : OUString LayoutDumpFilter::getImplementationName( )
164 : throw (uno::RuntimeException, std::exception)
165 : {
166 0 : return LayoutDumpFilter_getImplementationName();
167 : }
168 :
169 0 : sal_Bool LayoutDumpFilter::supportsService( const OUString& rServiceName )
170 : throw (uno::RuntimeException, std::exception)
171 : {
172 0 : return cppu::supportsService(this, rServiceName);
173 : }
174 :
175 0 : uno::Sequence< OUString > LayoutDumpFilter::getSupportedServiceNames()
176 : throw (uno::RuntimeException, std::exception)
177 : {
178 0 : return LayoutDumpFilter_getSupportedServiceNames();
179 : }
180 :
181 270 : } // Namespace sw
182 :
183 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|