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