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