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  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include "XMLAutoTextEventExport.hxx"
21 #include <facreg.hxx>
22 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
23 #include <com/sun/star/util/MeasureUnit.hpp>
24 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
25 #include <com/sun/star/document/XEventsSupplier.hpp>
26 #include <com/sun/star/container/XNameReplace.hpp>
27 #include <com/sun/star/container/XNameAccess.hpp>
28 #include <com/sun/star/uno/Reference.hxx>
29 #include <com/sun/star/uno/Sequence.hxx>
30 #include <com/sun/star/uno/Exception.hpp>
31 #include <com/sun/star/uno/XComponentContext.hpp>
32 #include <osl/diagnose.h>
33 #include <xmloff/xmlnmspe.hxx>
34 #include <xmloff/nmspmap.hxx>
35 #include <xmloff/xmltoken.hxx>
36 #include <xmloff/XMLEventExport.hxx>
37 #include <tools/debug.hxx>
38 #include <comphelper/processfactory.hxx>
39 
40 
41 using namespace ::com::sun::star;
42 using namespace ::xmloff::token;
43 
44 using ::std::set;
45 using ::com::sun::star::container::XNameAccess;
46 using ::com::sun::star::container::XNameReplace;
47 using ::com::sun::star::document::XEventsSupplier;
48 using ::com::sun::star::lang::XMultiServiceFactory;
49 using ::com::sun::star::uno::Any;
50 using ::com::sun::star::uno::Exception;
51 using ::com::sun::star::uno::Reference;
52 using ::com::sun::star::uno::Sequence;
53 using ::com::sun::star::uno::XInterface;
54 using ::com::sun::star::uno::UNO_QUERY;
55 using ::com::sun::star::xml::sax::XDocumentHandler;
56 
57 
XMLAutoTextEventExport(const css::uno::Reference<css::uno::XComponentContext> & xContext,OUString const & implementationName,SvXMLExportFlags nFlags)58 XMLAutoTextEventExport::XMLAutoTextEventExport(
59     const css::uno::Reference< css::uno::XComponentContext >& xContext,
60     OUString const & implementationName, SvXMLExportFlags nFlags
61     )
62 :   SvXMLExport(util::MeasureUnit::INCH, xContext, implementationName, XML_AUTO_TEXT, nFlags)
63 {
64 }
65 
~XMLAutoTextEventExport()66 XMLAutoTextEventExport::~XMLAutoTextEventExport()
67 {
68 }
69 
initialize(const Sequence<Any> & rArguments)70 void XMLAutoTextEventExport::initialize(
71     const Sequence<Any> & rArguments )
72 {
73     if (rArguments.getLength() > 1)
74     {
75         Reference<XEventsSupplier> xSupplier;
76         rArguments[1] >>= xSupplier;
77         if (xSupplier.is())
78         {
79             xEvents = xSupplier->getEvents();
80         }
81         else
82         {
83             Reference<XNameReplace> xReplace;
84             rArguments[1] >>= xReplace;
85             if (xReplace.is())
86             {
87                 xEvents = xReplace;
88             }
89             else
90             {
91                 rArguments[1] >>= xEvents;
92             }
93         }
94     }
95 
96     // call super class (for XHandler)
97     SvXMLExport::initialize(rArguments);
98 }
99 
100 
exportDoc(enum XMLTokenEnum)101 ErrCode XMLAutoTextEventExport::exportDoc( enum XMLTokenEnum )
102 {
103     if( !(getExportFlags() & SvXMLExportFlags::OASIS) )
104     {
105         Reference< uno::XComponentContext> xContext = getComponentContext();
106         try
107         {
108 
109             Sequence<Any> aArgs( 1 );
110             aArgs[0] <<= GetDocHandler();
111 
112             // get filter component
113             Reference< xml::sax::XDocumentHandler > xTmpDocHandler(
114                 xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
115                     "com.sun.star.comp.Oasis2OOoTransformer",
116                     aArgs,
117                     xContext),
118                 UNO_QUERY);
119             OSL_ENSURE( xTmpDocHandler.is(),
120                 "can't instantiate OASIS transformer component" );
121             if( xTmpDocHandler.is() )
122             {
123                 SetDocHandler( xTmpDocHandler );
124             }
125         }
126         catch( css::uno::Exception& )
127         {
128         }
129     }
130     if (hasEvents())
131     {
132         GetDocHandler()->startDocument();
133 
134         addChaffWhenEncryptedStorage();
135 
136         addNamespaces();
137 
138         {
139             // container element
140             SvXMLElementExport aContainerElement(
141                 *this, XML_NAMESPACE_OOO, XML_AUTO_TEXT_EVENTS,
142                 true, true);
143 
144             exportEvents();
145         }
146 
147         // and close document again
148         GetDocHandler()->endDocument();
149     }
150 
151     return ERRCODE_NONE;
152 }
153 
hasEvents() const154 bool XMLAutoTextEventExport::hasEvents() const
155 {
156     // TODO: provide full implementation that check for presence of events
157     return xEvents.is();
158 }
159 
addNamespaces()160 void XMLAutoTextEventExport::addNamespaces()
161 {
162     // namespaces for office:, text: and script:
163     GetAttrList().AddAttribute(
164         GetNamespaceMap().GetAttrNameByIndex( XML_NAMESPACE_OFFICE ),
165         GetNamespaceMap().GetNameByIndex( XML_NAMESPACE_OFFICE ) );
166     GetAttrList().AddAttribute(
167         GetNamespaceMap().GetAttrNameByIndex( XML_NAMESPACE_TEXT ),
168         GetNamespaceMap().GetNameByIndex( XML_NAMESPACE_TEXT ) );
169     GetAttrList().AddAttribute(
170         GetNamespaceMap().GetAttrNameByIndex( XML_NAMESPACE_SCRIPT ),
171         GetNamespaceMap().GetNameByIndex( XML_NAMESPACE_SCRIPT ) );
172     GetAttrList().AddAttribute(
173         GetNamespaceMap().GetAttrNameByIndex( XML_NAMESPACE_DOM ),
174         GetNamespaceMap().GetNameByIndex( XML_NAMESPACE_DOM ) );
175     GetAttrList().AddAttribute(
176         GetNamespaceMap().GetAttrNameByIndex( XML_NAMESPACE_OOO ),
177         GetNamespaceMap().GetNameByIndex( XML_NAMESPACE_OOO ) );
178     GetAttrList().AddAttribute(
179         GetNamespaceMap().GetAttrNameByIndex( XML_NAMESPACE_XLINK ),
180         GetNamespaceMap().GetNameByIndex( XML_NAMESPACE_XLINK ) );
181 }
182 
exportEvents()183 void XMLAutoTextEventExport::exportEvents()
184 {
185     DBG_ASSERT(hasEvents(), "no events to export!");
186 
187     GetEventExport().Export(xEvents);
188 }
189 
190 
191 // methods without content:
192 
ExportMeta_()193 void XMLAutoTextEventExport::ExportMeta_() {}
ExportScripts_()194 void XMLAutoTextEventExport::ExportScripts_() {}
ExportFontDecls_()195 void XMLAutoTextEventExport::ExportFontDecls_() {}
ExportStyles_(bool)196 void XMLAutoTextEventExport::ExportStyles_( bool ) {}
ExportAutoStyles_()197 void XMLAutoTextEventExport::ExportAutoStyles_() {}
ExportMasterStyles_()198 void XMLAutoTextEventExport::ExportMasterStyles_() {}
ExportContent_()199 void XMLAutoTextEventExport::ExportContent_() {}
200 
201 
202 // methods to support the component registration
203 
XMLAutoTextEventExport_getSupportedServiceNames()204 Sequence< OUString > XMLAutoTextEventExport_getSupportedServiceNames()
205     throw()
206 {
207     Sequence<OUString> aSeq { XMLAutoTextEventExport_getImplementationName() };
208     return aSeq;
209 }
210 
XMLAutoTextEventExport_getImplementationName()211 OUString XMLAutoTextEventExport_getImplementationName() throw()
212 {
213     return "com.sun.star.comp.Writer.XMLOasisAutotextEventsExporter";
214 }
215 
XMLAutoTextEventExport_createInstance(const Reference<XMultiServiceFactory> & rSMgr)216 Reference< XInterface > XMLAutoTextEventExport_createInstance(
217         const Reference< XMultiServiceFactory > & rSMgr)
218 {
219     return static_cast<cppu::OWeakObject*>(new XMLAutoTextEventExport( comphelper::getComponentContext(rSMgr), XMLAutoTextEventExport_getImplementationName(), SvXMLExportFlags::ALL|SvXMLExportFlags::OASIS));
220 }
221 
222 // methods to support the component registration
223 
XMLAutoTextEventExportOOO_getSupportedServiceNames()224 Sequence< OUString > XMLAutoTextEventExportOOO_getSupportedServiceNames()
225     throw()
226 {
227     Sequence<OUString> aSeq { XMLAutoTextEventExportOOO_getImplementationName() };
228     return aSeq;
229 }
230 
XMLAutoTextEventExportOOO_getImplementationName()231 OUString XMLAutoTextEventExportOOO_getImplementationName() throw()
232 {
233     return "com.sun.star.comp.Writer.XMLAutotextEventsExporter";
234 }
235 
XMLAutoTextEventExportOOO_createInstance(const Reference<XMultiServiceFactory> & rSMgr)236 Reference< XInterface > XMLAutoTextEventExportOOO_createInstance(
237         const Reference< XMultiServiceFactory > & rSMgr)
238 {
239     return static_cast<cppu::OWeakObject*>(new XMLAutoTextEventExport( comphelper::getComponentContext(rSMgr), XMLAutoTextEventExportOOO_getImplementationName(), SvXMLExportFlags::ALL));
240 }
241 
242 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
243