LibreOffice Module xmloff (master) 1
xmlmetai.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 * 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 <sal/config.h>
21
22#include <string_view>
23
24#include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
25#include <com/sun/star/xml/dom/SAXDocumentBuilder.hpp>
26#include <com/sun/star/xml/dom/XSAXDocumentBuilder2.hpp>
27#include <com/sun/star/xml/xpath/XPathAPI.hpp>
28#include <com/sun/star/beans/XPropertySet.hpp>
29#include <com/sun/star/beans/XPropertySetInfo.hpp>
30#include <com/sun/star/document/XDocumentProperties.hpp>
33#include <o3tl/string_view.hxx>
34#include <rtl/character.hxx>
35#include <rtl/ustrbuf.hxx>
36#include <utility>
37#include <xmloff/xmlmetai.hxx>
38#include <xmloff/xmlimp.hxx>
39#include <xmloff/xmltoken.hxx>
41
42using namespace com::sun::star;
43using namespace ::xmloff::token;
44
45namespace {
46
48class XMLDocumentBuilderContext : public SvXMLImportContext
49{
50private:
51 css::uno::Reference< css::xml::dom::XSAXDocumentBuilder2> mxDocBuilder;
52 SvXMLMetaDocumentContext *const m_pTopLevel;
53
54public:
55 XMLDocumentBuilderContext(SvXMLImport& rImport, sal_Int32 nElement,
56 const css::uno::Reference< css::xml::sax::XFastAttributeList>& xAttrList,
57 css::uno::Reference<css::xml::dom::XSAXDocumentBuilder2> xDocBuilder,
58 SvXMLMetaDocumentContext * pTopLevel);
59
60 virtual void SAL_CALL characters( const OUString& aChars ) override;
61
62 virtual void SAL_CALL startFastElement( sal_Int32 nElement,
63 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList ) override;
64
65 virtual void SAL_CALL endFastElement( sal_Int32 nElement ) override;
66
67 virtual void SAL_CALL startUnknownElement( const OUString& Namespace, const OUString& Name,
68 const css::uno::Reference< css::xml::sax::XFastAttributeList >& Attribs ) override;
69
70 virtual void SAL_CALL endUnknownElement( const OUString& Namespace, const OUString& Name ) override;
71
72 virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(
73 sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList ) override;
74
75};
76
77}
78
79XMLDocumentBuilderContext::XMLDocumentBuilderContext(SvXMLImport& rImport,
80 sal_Int32 /*nElement*/, const uno::Reference<xml::sax::XFastAttributeList>&,
81 uno::Reference<xml::dom::XSAXDocumentBuilder2> xDocBuilder,
82 SvXMLMetaDocumentContext *const pTopLevel)
83 : SvXMLImportContext(rImport)
84 , mxDocBuilder(std::move(xDocBuilder))
85 , m_pTopLevel(pTopLevel)
86{
87}
88
89void SAL_CALL XMLDocumentBuilderContext::startFastElement( sal_Int32 nElement,
90 const uno::Reference< xml::sax::XFastAttributeList >& xAttribs )
91{
92 mxDocBuilder->startFastElement(nElement, xAttribs);
93}
94
95void SAL_CALL XMLDocumentBuilderContext::endFastElement( sal_Int32 nElement )
96{
97 mxDocBuilder->endFastElement(nElement);
98 if (m_pTopLevel)
99 {
100 // call this here because in the flat ODF case the top-level
101 // endFastElement is called only at the very end of the document,
102 // which is too late to init BuildId
103 m_pTopLevel->FinishMetaElement();
104 }
105}
106
107void SAL_CALL XMLDocumentBuilderContext::startUnknownElement( const OUString& rNamespace,
108 const OUString& rName, const uno::Reference< xml::sax::XFastAttributeList >& xAttrList )
109{
110 mxDocBuilder->startUnknownElement(rNamespace, rName, xAttrList);
111}
112
113void SAL_CALL XMLDocumentBuilderContext::endUnknownElement( const OUString& rNamespace, const OUString& rName )
114{
115 mxDocBuilder->endUnknownElement(rNamespace, rName);
116}
117
118void SAL_CALL XMLDocumentBuilderContext::characters( const OUString& rChars )
119{
120 mxDocBuilder->characters(rChars);
121}
122
123uno::Reference< xml::sax::XFastContextHandler > SAL_CALL XMLDocumentBuilderContext::createFastChildContext(
124 sal_Int32 nElement, const uno::Reference< xml::sax::XFastAttributeList >& xAttrList )
125{
126 return new XMLDocumentBuilderContext(GetImport(), nElement, xAttrList, mxDocBuilder, nullptr);
127}
128
129static void
131 uno::Reference<xml::dom::XSAXDocumentBuilder2> const& xDocBuilder,
132 uno::Reference<document::XDocumentProperties> const& xDocProps)
133{
134 uno::Reference< lang::XInitialization > const xInit(xDocProps,
135 uno::UNO_QUERY_THROW);
136 try {
137 xInit->initialize({ uno::Any(xDocBuilder->getDocument()) });
138 rImport.SetStatistics(xDocProps->getDocumentStatistics());
139 // convert all URLs from relative to absolute
140 xDocProps->setTemplateURL(rImport.GetAbsoluteReference(
141 xDocProps->getTemplateURL()));
142 xDocProps->setAutoloadURL(rImport.GetAbsoluteReference(
143 xDocProps->getAutoloadURL()));
145 xDocProps->getGenerator(), rImport.getImportInfo());
146 } catch (const uno::RuntimeException&) {
147 throw;
148 } catch (const uno::Exception&) {
149 css::uno::Any anyEx = cppu::getCaughtException();
150 throw lang::WrappedTargetRuntimeException(
151 "SvXMLMetaDocumentContext::initDocumentProperties: "
152 "properties init exception",
153 rImport, anyEx);
154 }
155}
156
157static void
159 uno::Reference<xml::dom::XSAXDocumentBuilder2> const& xDocBuilder)
160{
161 uno::Reference< xml::dom::XDocument > const xDoc(xDocBuilder->getDocument(),
162 uno::UNO_SET_THROW);
163 try {
164 uno::Reference< xml::xpath::XXPathAPI > const xPath = xml::xpath::XPathAPI::create(
165 rImport.GetComponentContext() );
167 xPath->registerNS(GetXMLToken(XML_NP_META), GetXMLToken(XML_N_META));
168
169 uno::Reference< xml::xpath::XXPathObject > const xObj(
170 xPath->eval(xDoc, "string(/office:document-meta/office:meta/meta:generator)"),
171 uno::UNO_SET_THROW);
172 OUString const value(xObj->getString());
174 } catch (const uno::RuntimeException&) {
175 throw;
176 } catch (const uno::Exception&) {
177 css::uno::Any anyEx = cppu::getCaughtException();
178 throw lang::WrappedTargetRuntimeException(
179 "SvXMLMetaDocumentContext::initGenerator: exception",
180 rImport, anyEx);
181 }
182}
183
185 uno::Reference<document::XDocumentProperties> xDocProps) :
186 SvXMLImportContext( rImport ),
187 mxDocProps(std::move(xDocProps)),
188 mxDocBuilder(
189 xml::dom::SAXDocumentBuilder::create(
191{
192// #i103539#: must always read meta.xml for generator, xDocProps unwanted then
193// OSL_ENSURE(xDocProps.is(), "SvXMLMetaDocumentContext: no document props");
194}
195
197{
198}
199
200void SAL_CALL SvXMLMetaDocumentContext::startFastElement(sal_Int32 /*nElement*/,
201 const uno::Reference< xml::sax::XFastAttributeList >& xAttrList )
202{
203 mxDocBuilder->startDocument();
204 // hardcode office:document-meta (necessary in case of flat file ODF)
205 mxDocBuilder->startFastElement(XML_ELEMENT(OFFICE, XML_DOCUMENT_META), xAttrList);
206}
207
209{
210 // hardcode office:document-meta (necessary in case of flat file ODF)
212 mxDocBuilder->endDocument();
213 if (mxDocProps.is())
214 {
216 }
217 else
218 {
220 }
221}
222
223uno::Reference< xml::sax::XFastContextHandler > SAL_CALL SvXMLMetaDocumentContext::createFastChildContext(
224 sal_Int32 nElement, const uno::Reference< xml::sax::XFastAttributeList >& xAttrList )
225{
226 if ( nElement == XML_ELEMENT(OFFICE, XML_META) )
227 return new XMLDocumentBuilderContext(
228 GetImport(), nElement, xAttrList, mxDocBuilder, this);
229 return nullptr;
230}
231
232void SvXMLMetaDocumentContext::setBuildId(std::u16string_view i_rBuildId, const uno::Reference<beans::XPropertySet>& xImportInfo )
233{
234 OUString sBuildId;
235 // skip to second product
236 size_t nBegin = i_rBuildId.find( ' ' );
237 if ( nBegin != std::u16string_view::npos )
238 {
239 // skip to build information
240 nBegin = i_rBuildId.find( '/', nBegin );
241 if ( nBegin != std::u16string_view::npos )
242 {
243 size_t nEnd = i_rBuildId.find( 'm', nBegin );
244 if ( nEnd != std::u16string_view::npos )
245 {
246 OUStringBuffer sBuffer(
247 i_rBuildId.substr( nBegin+1, nEnd-nBegin-1 ) );
248 static const OUStringLiteral sBuildCompare(
249 u"$Build-" );
250 nBegin = i_rBuildId.find( sBuildCompare, nEnd );
251 if ( nBegin != std::u16string_view::npos )
252 {
253 sBuffer.append( '$' );
254 sBuffer.append( i_rBuildId.substr(nBegin + sBuildCompare.getLength()) );
255 sBuildId = sBuffer.makeStringAndClear();
256 }
257 }
258 }
259 }
260
261 if ( sBuildId.isEmpty() )
262 {
263 if ( o3tl::starts_with(i_rBuildId, u"StarOffice 7")
264 || o3tl::starts_with(i_rBuildId, u"StarSuite 7")
265 || o3tl::starts_with(i_rBuildId, u"StarOffice 6")
266 || o3tl::starts_with(i_rBuildId, u"StarSuite 6")
267 || o3tl::starts_with(i_rBuildId, u"OpenOffice.org 1"))
268 {
269 sBuildId = "645$8687";
270 }
271 else if (o3tl::starts_with(i_rBuildId, u"NeoOffice/2"))
272 {
273 sBuildId = "680$9134"; // fake NeoOffice as OpenOffice.org 2.2 release
274 }
275 }
276
277 // "LibreOffice_project" was hard-coded since LO 3.3.0
278 // see utl::DocInfoHelper::GetGeneratorString()
279 if (i_rBuildId.find(u"LibreOffice_project/") != std::u16string_view::npos)
280 {
281 OUStringBuffer sNumber;
282 size_t const firstSlash = i_rBuildId.find('/');
283 assert(firstSlash != std::u16string_view::npos);
284 for (size_t i = firstSlash + 1; i < i_rBuildId.size(); ++i)
285 {
286 if (rtl::isAsciiDigit(i_rBuildId[i]))
287 {
288 sNumber.append(i_rBuildId[i]);
289 }
290 else if ('.' != i_rBuildId[i])
291 {
292 break;
293 }
294 }
295 if (!sNumber.isEmpty())
296 {
297 sBuildId += ";" + sNumber;
298 }
299 }
300
301 if ( sBuildId.isEmpty() )
302 return;
303
304 try
305 {
306 if( xImportInfo.is() )
307 {
308 static const OUStringLiteral aPropName(u"BuildId");
309 uno::Reference< beans::XPropertySetInfo > xSetInfo(
310 xImportInfo->getPropertySetInfo());
311 if( xSetInfo.is() && xSetInfo->hasPropertyByName( aPropName ) )
312 xImportInfo->setPropertyValue( aPropName, uno::Any( sBuildId ) );
313 }
314 }
315 catch(const uno::Exception&)
316 {
317 }
318}
319
320/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
This class deliberately does not support XWeak, to improve performance when loading large documents.
Definition: xmlictxt.hxx:48
virtual void SAL_CALL startFastElement(sal_Int32 Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > &Attribs) override
Definition: xmlictxt.cxx:45
virtual void SAL_CALL endFastElement(sal_Int32 Element) override
endFastElement is called before a context will be destructed, but after an elements context has been ...
Definition: xmlictxt.cxx:40
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:60
virtual css::uno::Reference< XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > &Attribs) override
Definition: xmlictxt.cxx:59
virtual void SAL_CALL endUnknownElement(const OUString &Namespace, const OUString &Name) override
Definition: xmlictxt.cxx:54
virtual void SAL_CALL startUnknownElement(const OUString &Namespace, const OUString &Name, const css::uno::Reference< css::xml::sax::XFastAttributeList > &Attribs) override
Definition: xmlictxt.cxx:49
virtual void SAL_CALL characters(const OUString &aChars) override
This method is called for all characters that are contained in the current element.
Definition: xmlictxt.cxx:70
virtual void SetStatistics(const css::uno::Sequence< css::beans::NamedValue > &i_rStats)
Definition: xmlimp.cxx:904
const css::uno::Reference< css::beans::XPropertySet > & getImportInfo() const
Definition: xmlimp.hxx:387
OUString GetAbsoluteReference(const OUString &rValue) const
Definition: xmlimp.cxx:1620
css::uno::Reference< css::uno::XComponentContext > const & GetComponentContext() const
Definition: xmlimp.cxx:1796
handles the top-level office:document-meta element of meta.xml documents
Definition: xmlmetai.hxx:37
css::uno::Reference< css::document::XDocumentProperties > mxDocProps
Definition: xmlmetai.hxx:39
static void setBuildId(std::u16string_view rGenerator, const css::uno::Reference< css::beans::XPropertySet > &xImportInfo)
Definition: xmlmetai.cxx:232
css::uno::Reference< css::xml::dom::XSAXDocumentBuilder2 > mxDocBuilder
Definition: xmlmetai.hxx:40
SvXMLMetaDocumentContext(SvXMLImport &rImport, css::uno::Reference< css::document::XDocumentProperties > xDocProps)
Definition: xmlmetai.cxx:184
virtual ~SvXMLMetaDocumentContext() override
Definition: xmlmetai.cxx:196
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
Definition: xmlmetai.cxx:223
virtual void SAL_CALL startFastElement(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
Definition: xmlmetai.cxx:200
Any value
float u
Reference< XComponentContext > getProcessComponentContext()
Any SAL_CALL getCaughtException()
css::uno::Reference< css::deployment::XPackageRegistry > create(css::uno::Reference< css::deployment::XPackageRegistry > const &xRootRegistry, OUString const &context, OUString const &cachePath, css::uno::Reference< css::uno::XComponentContext > const &xComponentContext)
int i
OUString aPropName
constexpr bool starts_with(std::basic_string_view< charT, traits > sv, std::basic_string_view< charT, traits > x) noexcept
Handling of tokens in XML:
const OUString & GetXMLToken(enum XMLTokenEnum eToken)
return the OUString representation for eToken
Definition: xmltoken.cxx:3525
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:98
static void lcl_initGenerator(SvXMLImport &rImport, uno::Reference< xml::dom::XSAXDocumentBuilder2 > const &xDocBuilder)
Definition: xmlmetai.cxx:158
static void lcl_initDocumentProperties(SvXMLImport &rImport, uno::Reference< xml::dom::XSAXDocumentBuilder2 > const &xDocBuilder, uno::Reference< document::XDocumentProperties > const &xDocProps)
Definition: xmlmetai.cxx:130