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