Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * Version: MPL 1.1 / GPLv3+ / LGPLv3+
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Initial Developer of the Original Code is
16 : * Novell Inc.
17 : * Portions created by the Initial Developer are Copyright (C) 2010 the
18 : * Initial Developer. All Rights Reserved.
19 : *
20 : * Contributor(s): Cedric Bosdonnat <cbosdonnat@novell.com>
21 : *
22 : * Alternatively, the contents of this file may be used under the terms of
23 : * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
24 : * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
25 : * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
26 : * instead of those above.
27 : */
28 :
29 : #include "dumpfilter.hxx"
30 :
31 : #include <wrtsh.hxx>
32 : #include <docsh.hxx>
33 : #include <rootfrm.hxx>
34 : #include <unotxdoc.hxx>
35 : #include <unobaseclass.hxx>
36 : #include <cppuhelper/weak.hxx>
37 : #include <vcl/svapp.hxx>
38 :
39 : #include <comphelper/mediadescriptor.hxx>
40 :
41 : #include <libxml/xmlwriter.h>
42 :
43 : using namespace ::com::sun::star;
44 :
45 0 : ::rtl::OUString SAL_CALL LayoutDumpFilter_getImplementationName() throw( uno::RuntimeException )
46 : {
47 0 : return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.Writer.LayoutDump" ) );
48 : }
49 :
50 0 : uno::Sequence< rtl::OUString > SAL_CALL LayoutDumpFilter_getSupportedServiceNames() throw( uno::RuntimeException )
51 : {
52 0 : uno::Sequence< rtl::OUString > aSeq( 1 );
53 0 : aSeq[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.ExportFilter" ) );
54 0 : return aSeq;
55 : }
56 :
57 0 : uno::Reference< uno::XInterface > SAL_CALL LayoutDumpFilter_createInstance(
58 : const uno::Reference< lang::XMultiServiceFactory > & )
59 : {
60 0 : return static_cast< cppu::OWeakObject* >( new sw::LayoutDumpFilter( ) );
61 : }
62 :
63 : namespace
64 : {
65 0 : int writeCallback( void* pContext, const char* sBuffer, int nLen )
66 : {
67 0 : int written = nLen;
68 :
69 : // Actually write bytes to XOutputSream
70 : try
71 : {
72 0 : uno::XInterface* pObj = ( uno::XInterface* )pContext;
73 0 : uno::Reference< io::XOutputStream > xOut( pObj, uno::UNO_QUERY_THROW );
74 :
75 : // Don't output the terminating \0 to the xml or the file will be invalid
76 0 : uno::Sequence< sal_Int8 > seq( nLen );
77 0 : strncpy( ( char * ) seq.getArray() , sBuffer, nLen );
78 0 : xOut->writeBytes( seq );
79 : }
80 0 : catch (const uno::Exception&)
81 : {
82 0 : written = -1;
83 : }
84 :
85 0 : return written;
86 : }
87 :
88 0 : int closeCallback( void* pContext )
89 : {
90 0 : int result = 0;
91 : try
92 : {
93 0 : uno::XInterface* pObj = ( uno::XInterface* )pContext;
94 0 : uno::Reference< io::XOutputStream > xOut( pObj, uno::UNO_QUERY_THROW );
95 0 : xOut->closeOutput( );
96 : }
97 0 : catch (const uno::Exception&)
98 : {
99 0 : result = -1;
100 : }
101 0 : return result;
102 : }
103 : }
104 :
105 : namespace sw
106 : {
107 :
108 0 : LayoutDumpFilter::LayoutDumpFilter( )
109 : {
110 0 : }
111 :
112 0 : LayoutDumpFilter::~LayoutDumpFilter( )
113 : {
114 0 : }
115 :
116 : // XFilter
117 0 : sal_Bool LayoutDumpFilter::filter( const uno::Sequence< beans::PropertyValue >& aDescriptor )
118 : throw (uno::RuntimeException)
119 : {
120 0 : sal_Bool bRet = sal_False;
121 :
122 0 : comphelper::MediaDescriptor aMediaDesc = aDescriptor;
123 :
124 : // Get the output stream
125 : uno::Reference< io::XOutputStream > xOut = aMediaDesc.getUnpackedValueOrDefault(
126 0 : comphelper::MediaDescriptor::PROP_OUTPUTSTREAM(),
127 0 : uno::Reference< io::XOutputStream >() );
128 :
129 : // Actually get the SwRootFrm to call dumpAsXml
130 0 : uno::Reference< lang::XUnoTunnel > xDocTunnel( m_xSrcDoc, uno::UNO_QUERY );
131 0 : SwXTextDocument* pXDoc = UnoTunnelGetImplementation< SwXTextDocument >( xDocTunnel );
132 0 : if ( pXDoc )
133 : {
134 0 : SwRootFrm* pLayout = pXDoc->GetDocShell()->GetWrtShell()->GetLayout();
135 :
136 : // Get sure that the whole layout is processed: set a visible area
137 : // even though there isn't any need of it
138 0 : pXDoc->GetDocShell()->GetWrtShell()->StartAction();
139 0 : Rectangle aRect( 0, 0, 26000, 21000 );
140 0 : pXDoc->GetDocShell()->SetVisArea( aRect );
141 0 : pLayout->InvalidateAllCntnt( );
142 0 : pXDoc->GetDocShell()->GetWrtShell()->EndAction();
143 :
144 : // Dump the layout XML into the XOutputStream
145 : xmlOutputBufferPtr outBuffer = xmlOutputBufferCreateIO(
146 0 : writeCallback, closeCallback, ( void* ) xOut.get(), NULL );
147 :
148 0 : xmlTextWriterPtr writer = xmlNewTextWriter( outBuffer );
149 0 : xmlTextWriterSetIndent(writer, 1);
150 0 : xmlTextWriterStartDocument( writer, NULL, NULL, NULL );
151 :
152 : // TODO This doesn't export the whole XML file, whereas dumpAsXML() does it nicely
153 0 : pLayout->dumpAsXml( writer );
154 :
155 0 : xmlTextWriterEndDocument( writer );
156 0 : xmlFreeTextWriter( writer );
157 :
158 0 : bRet = sal_True;
159 : }
160 :
161 0 : return bRet;
162 : }
163 :
164 0 : void LayoutDumpFilter::cancel( ) throw (uno::RuntimeException)
165 : {
166 0 : }
167 :
168 : // XExporter
169 0 : void LayoutDumpFilter::setSourceDocument( const uno::Reference< lang::XComponent >& xDoc )
170 : throw (lang::IllegalArgumentException, uno::RuntimeException)
171 : {
172 0 : m_xSrcDoc = xDoc;
173 0 : }
174 :
175 : // XInitialization
176 0 : void LayoutDumpFilter::initialize( const uno::Sequence< uno::Any >& )
177 : throw (uno::Exception, uno::RuntimeException)
178 : {
179 0 : }
180 :
181 : // XServiceInfo
182 0 : ::rtl::OUString LayoutDumpFilter::getImplementationName( )
183 : throw (uno::RuntimeException)
184 : {
185 0 : return LayoutDumpFilter_getImplementationName();
186 : }
187 :
188 0 : sal_Bool LayoutDumpFilter::supportsService( const ::rtl::OUString& rServiceName )
189 : throw (uno::RuntimeException)
190 : {
191 0 : uno::Sequence< rtl::OUString > seqServiceNames = getSupportedServiceNames();
192 0 : const rtl::OUString* pArray = seqServiceNames.getConstArray();
193 0 : for ( sal_Int32 nCounter=0; nCounter < seqServiceNames.getLength(); nCounter++ )
194 : {
195 0 : if ( pArray[nCounter] == rServiceName )
196 : {
197 0 : return sal_True ;
198 : }
199 : }
200 0 : return sal_False ;
201 : }
202 :
203 0 : uno::Sequence< ::rtl::OUString > LayoutDumpFilter::getSupportedServiceNames()
204 : throw (uno::RuntimeException)
205 : {
206 0 : return LayoutDumpFilter_getSupportedServiceNames();
207 : }
208 :
209 : } // Namespace sw
210 :
211 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|