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