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