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 
19 
20 #include <libxml/xmlwriter.h>
21 
22 using namespace ::com::sun::star;
23 
24 namespace
25 {
26  int writeCallback( void* pContext, const char* sBuffer, int nLen )
27  {
28  int written = nLen;
29 
30  // Actually write bytes to XOutputSream
31  try
32  {
33  uno::XInterface* pObj = static_cast<uno::XInterface*>(pContext);
34  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  uno::Sequence< sal_Int8 > seq( nLen );
38  strncpy( reinterpret_cast<char *>(seq.getArray()), sBuffer, nLen );
39  xOut->writeBytes( seq );
40  }
41  catch (const uno::Exception&)
42  {
43  written = -1;
44  }
45 
46  return written;
47  }
48 
49  int closeCallback( void* pContext )
50  {
51  int result = 0;
52  try
53  {
54  uno::XInterface* pObj = static_cast<uno::XInterface*>(pContext);
55  uno::Reference< io::XOutputStream > xOut( pObj, uno::UNO_QUERY_THROW );
56  xOut->closeOutput( );
57  }
58  catch (const uno::Exception&)
59  {
60  result = -1;
61  }
62  return result;
63  }
64 }
65 
66 namespace sw
67 {
68 
70  {
71  }
72 
74  {
75  }
76 
77  // XFilter
78  sal_Bool LayoutDumpFilter::filter( const uno::Sequence< beans::PropertyValue >& aDescriptor )
79  {
80  bool bRet = false;
81 
82  utl::MediaDescriptor aMediaDesc = aDescriptor;
83 
84  // Get the output stream
85  uno::Reference< io::XOutputStream > xOut = aMediaDesc.getUnpackedValueOrDefault(
87  uno::Reference< io::XOutputStream >() );
88 
89  // Actually get the SwRootFrame to call dumpAsXml
90  uno::Reference< lang::XUnoTunnel > xDocTunnel( m_xSrcDoc, uno::UNO_QUERY );
91  SwXTextDocument* pXDoc = UnoTunnelGetImplementation< SwXTextDocument >( xDocTunnel );
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  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  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 
150  uno::Sequence< OUString > LayoutDumpFilter::getSupportedServiceNames()
151  {
152  uno::Sequence<OUString> aSeq { "com.sun.star.document.ExportFilter" };
153  return aSeq;
154  }
155 
156 } // Namespace sw
157 
158 
159 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
161  css::uno::Sequence<css::uno::Any> const &)
162 {
163  return cppu::acquire(new sw::LayoutDumpFilter());
164 }
165 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void InvalidateAllContent(SwInvalidateFlags nInvalidate)
Invalidate all Content, Size or PrtArea.
Definition: wsfrm.cxx:4098
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: dumpfilter.cxx:150
static const OUString & PROP_OUTPUTSTREAM()
virtual void dumpAsXml(xmlTextWriterPtr writer=nullptr) const
Definition: xmldump.cxx:256
Dialog to specify the properties of date form field.
Definition: accfrmobj.cxx:38
The root element of a Writer document layout.
Definition: rootfrm.hxx:79
virtual ~LayoutDumpFilter() override
Definition: dumpfilter.cxx:73
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
struct _xmlTextWriter * xmlTextWriterPtr
css::uno::Reference< css::lang::XComponent > m_xSrcDoc
Definition: dumpfilter.hxx:33
virtual void SAL_CALL initialize(const css::uno::Sequence< css::uno::Any > &aArguments) override
Definition: dumpfilter.cxx:135
void EndAction(const bool bIdleEnd=false, const bool DoSetPosX=false)
Definition: crsrsh.cxx:228
MetadataImporterPluginType * result
unsigned char sal_Bool
virtual void SAL_CALL cancel() override
Definition: dumpfilter.cxx:124
SwDocShell * GetDocShell()
Definition: unotxdoc.hxx:464
SwWrtShell * GetWrtShell()
Access to the SwWrtShell belonging to SwView.
Definition: docsh.hxx:224
virtual void SAL_CALL setSourceDocument(const css::uno::Reference< css::lang::XComponent > &xDoc) override
Definition: dumpfilter.cxx:129
void StartAction()
Definition: crsrsh.cxx:211
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
Definition: dumpfilter.cxx:145
virtual sal_Bool SAL_CALL filter(const css::uno::Sequence< css::beans::PropertyValue > &aDescriptor) override
Definition: dumpfilter.cxx:78
virtual void SetVisArea(const tools::Rectangle &rRect) override
OLE-stuff.
Definition: docsh.cxx:905
Implementation of UNO export service to dump the layout of the document as XML.
Definition: dumpfilter.hxx:25
SwRootFrame * GetLayout() const
Definition: viewsh.cxx:2071
virtual OUString SAL_CALL getImplementationName() override
Definition: dumpfilter.cxx:140
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:160