Avoid potential negative array index access to cached text.
[LibreOffice.git] / xmloff / source / text / XMLAutoTextEventExport.cxx
blobb6a089e8a038e3fbb06b9373513f42cd6a7f4476
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "XMLAutoTextEventExport.hxx"
21 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
22 #include <com/sun/star/util/MeasureUnit.hpp>
23 #include <com/sun/star/document/XEventsSupplier.hpp>
24 #include <com/sun/star/container/XNameReplace.hpp>
25 #include <com/sun/star/container/XNameAccess.hpp>
26 #include <com/sun/star/uno/Reference.hxx>
27 #include <com/sun/star/uno/Sequence.hxx>
28 #include <com/sun/star/uno/Exception.hpp>
29 #include <com/sun/star/uno/XComponentContext.hpp>
30 #include <osl/diagnose.h>
31 #include <xmloff/xmlnamespace.hxx>
32 #include <xmloff/namespacemap.hxx>
33 #include <xmloff/xmltoken.hxx>
34 #include <xmloff/XMLEventExport.hxx>
35 #include <tools/debug.hxx>
36 #include <comphelper/processfactory.hxx>
39 using namespace ::com::sun::star;
40 using namespace ::xmloff::token;
42 using ::com::sun::star::container::XNameReplace;
43 using ::com::sun::star::document::XEventsSupplier;
44 using ::com::sun::star::uno::Any;
45 using ::com::sun::star::uno::Exception;
46 using ::com::sun::star::uno::Reference;
47 using ::com::sun::star::uno::Sequence;
48 using ::com::sun::star::uno::XInterface;
49 using ::com::sun::star::uno::UNO_QUERY;
50 using ::com::sun::star::xml::sax::XDocumentHandler;
53 XMLAutoTextEventExport::XMLAutoTextEventExport(
54 const css::uno::Reference< css::uno::XComponentContext >& xContext,
55 OUString const & implementationName, SvXMLExportFlags nFlags
57 : SvXMLExport(xContext, implementationName, util::MeasureUnit::INCH, XML_AUTO_TEXT, nFlags)
61 XMLAutoTextEventExport::~XMLAutoTextEventExport()
65 void XMLAutoTextEventExport::initialize(
66 const Sequence<Any> & rArguments )
68 if (rArguments.getLength() > 1)
70 Reference<XEventsSupplier> xSupplier;
71 rArguments[1] >>= xSupplier;
72 if (xSupplier.is())
74 xEvents = xSupplier->getEvents();
76 else
78 Reference<XNameReplace> xReplace;
79 rArguments[1] >>= xReplace;
80 if (xReplace.is())
82 xEvents = xReplace;
84 else
86 rArguments[1] >>= xEvents;
91 // call super class (for XHandler)
92 SvXMLExport::initialize(rArguments);
96 ErrCode XMLAutoTextEventExport::exportDoc( enum XMLTokenEnum )
98 if( !(getExportFlags() & SvXMLExportFlags::OASIS) )
100 Reference< uno::XComponentContext> xContext = getComponentContext();
104 Sequence<Any> aArgs{ Any(GetDocHandler()) };
106 // get filter component
107 Reference< xml::sax::XDocumentHandler > xTmpDocHandler(
108 xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
109 "com.sun.star.comp.Oasis2OOoTransformer",
110 aArgs,
111 xContext),
112 UNO_QUERY);
113 OSL_ENSURE( xTmpDocHandler.is(),
114 "can't instantiate OASIS transformer component" );
115 if( xTmpDocHandler.is() )
117 SetDocHandler( xTmpDocHandler );
120 catch( css::uno::Exception& )
124 if (hasEvents())
126 GetDocHandler()->startDocument();
128 addChaffWhenEncryptedStorage();
130 addNamespaces();
133 // container element
134 SvXMLElementExport aContainerElement(
135 *this, XML_NAMESPACE_OOO, XML_AUTO_TEXT_EVENTS,
136 true, true);
138 exportEvents();
141 // and close document again
142 GetDocHandler()->endDocument();
145 return ERRCODE_NONE;
148 bool XMLAutoTextEventExport::hasEvents() const
150 // TODO: provide full implementation that check for presence of events
151 return xEvents.is();
154 void XMLAutoTextEventExport::addNamespaces()
156 // namespaces for office:, text: and script:
157 GetAttrList().AddAttribute(
158 GetNamespaceMap().GetAttrNameByIndex( XML_NAMESPACE_OFFICE ),
159 GetNamespaceMap().GetNameByIndex( XML_NAMESPACE_OFFICE ) );
160 GetAttrList().AddAttribute(
161 GetNamespaceMap().GetAttrNameByIndex( XML_NAMESPACE_TEXT ),
162 GetNamespaceMap().GetNameByIndex( XML_NAMESPACE_TEXT ) );
163 GetAttrList().AddAttribute(
164 GetNamespaceMap().GetAttrNameByIndex( XML_NAMESPACE_SCRIPT ),
165 GetNamespaceMap().GetNameByIndex( XML_NAMESPACE_SCRIPT ) );
166 GetAttrList().AddAttribute(
167 GetNamespaceMap().GetAttrNameByIndex( XML_NAMESPACE_DOM ),
168 GetNamespaceMap().GetNameByIndex( XML_NAMESPACE_DOM ) );
169 GetAttrList().AddAttribute(
170 GetNamespaceMap().GetAttrNameByIndex( XML_NAMESPACE_OOO ),
171 GetNamespaceMap().GetNameByIndex( XML_NAMESPACE_OOO ) );
172 GetAttrList().AddAttribute(
173 GetNamespaceMap().GetAttrNameByIndex( XML_NAMESPACE_XLINK ),
174 GetNamespaceMap().GetNameByIndex( XML_NAMESPACE_XLINK ) );
177 void XMLAutoTextEventExport::exportEvents()
179 DBG_ASSERT(hasEvents(), "no events to export!");
181 GetEventExport().Export(xEvents);
185 // methods without content:
187 void XMLAutoTextEventExport::ExportMeta_() {}
188 void XMLAutoTextEventExport::ExportScripts_() {}
189 void XMLAutoTextEventExport::ExportFontDecls_() {}
190 void XMLAutoTextEventExport::ExportStyles_( bool ) {}
191 void XMLAutoTextEventExport::ExportAutoStyles_() {}
192 void XMLAutoTextEventExport::ExportMasterStyles_() {}
193 void XMLAutoTextEventExport::ExportContent_() {}
196 // methods to support the component registration
198 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
199 com_sun_star_comp_Writer_XMLOasisAutotextEventsExporter_get_implementation(
200 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
202 return cppu::acquire(new XMLAutoTextEventExport(
203 context, "com.sun.star.comp.Writer.XMLOasisAutotextEventsExporter",
204 SvXMLExportFlags::ALL | SvXMLExportFlags::OASIS));
207 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
208 com_sun_star_comp_Writer_XMLAutotextEventsExporter_get_implementation(
209 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
211 return cppu::acquire(new XMLAutoTextEventExport(
212 context, "com.sun.star.comp.Writer.XMLAutotextEventsExporter",
213 SvXMLExportFlags::ALL));
216 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */