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::Sequence< uno::Any > aSeq(1);
133  aSeq[0] <<= xDocBuilder->getDocument();
134  uno::Reference< lang::XInitialization > const xInit(xDocProps,
135  uno::UNO_QUERY_THROW);
136  try {
137  xInit->initialize(aSeq);
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 
157 static 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() );
166  xPath->registerNS(GetXMLToken(XML_NP_OFFICE),GetXMLToken(XML_N_OFFICE));
167  xPath->registerNS(GetXMLToken(XML_NP_META), GetXMLToken(XML_N_META));
168 
169  uno::Reference< xml::xpath::XXPathObject > const xObj(
170  xPath->eval(xDoc.get(), "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  const uno::Reference<document::XDocumentProperties>& xDocProps) :
186  SvXMLImportContext( rImport ),
187  mxDocProps(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 
200 void 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 
208 void SAL_CALL SvXMLMetaDocumentContext::endFastElement(sal_Int32 /*nElement*/)
209 {
210 }
211 
213 {
214  // hardcode office:document-meta (necessary in case of flat file ODF)
216  mxDocBuilder->endDocument();
217  if (mxDocProps.is())
218  {
220  }
221  else
222  {
224  }
225 }
226 
227 void SAL_CALL SvXMLMetaDocumentContext::characters( const OUString& /*rChars*/ )
228 {
229 }
230 
231 uno::Reference< xml::sax::XFastContextHandler > SAL_CALL SvXMLMetaDocumentContext::createFastChildContext(
232  sal_Int32 nElement, const uno::Reference< xml::sax::XFastAttributeList >& xAttrList )
233 {
234  if ( nElement == XML_ELEMENT(OFFICE, XML_META) )
235  return new XMLDocumentBuilderContext(
236  GetImport(), nElement, xAttrList, mxDocBuilder, this);
237  return nullptr;
238 }
239 
240 void SvXMLMetaDocumentContext::setBuildId(OUString const& i_rBuildId, const uno::Reference<beans::XPropertySet>& xImportInfo )
241 {
242  OUString sBuildId;
243  // skip to second product
244  sal_Int32 nBegin = i_rBuildId.indexOf( ' ' );
245  if ( nBegin != -1 )
246  {
247  // skip to build information
248  nBegin = i_rBuildId.indexOf( '/', nBegin );
249  if ( nBegin != -1 )
250  {
251  sal_Int32 nEnd = i_rBuildId.indexOf( 'm', nBegin );
252  if ( nEnd != -1 )
253  {
254  OUStringBuffer sBuffer(
255  i_rBuildId.copy( nBegin+1, nEnd-nBegin-1 ) );
256  const OUString sBuildCompare(
257  "$Build-" );
258  nBegin = i_rBuildId.indexOf( sBuildCompare, nEnd );
259  if ( nBegin != -1 )
260  {
261  sBuffer.append( '$' );
262  sBuffer.append( std::u16string_view(i_rBuildId).substr(
263  nBegin + sBuildCompare.getLength()) );
264  sBuildId = sBuffer.makeStringAndClear();
265  }
266  }
267  }
268  }
269 
270  if ( sBuildId.isEmpty() )
271  {
272  if ( i_rBuildId.startsWith("StarOffice 7")
273  || i_rBuildId.startsWith("StarSuite 7")
274  || i_rBuildId.startsWith("StarOffice 6")
275  || i_rBuildId.startsWith("StarSuite 6")
276  || i_rBuildId.startsWith("OpenOffice.org 1"))
277  {
278  sBuildId = "645$8687";
279  }
280  else if (i_rBuildId.startsWith("NeoOffice/2"))
281  {
282  sBuildId = "680$9134"; // fake NeoOffice as OpenOffice.org 2.2 release
283  }
284  }
285 
286  // "LibreOffice_project" was hard-coded since LO 3.3.0
287  // see utl::DocInfoHelper::GetGeneratorString()
288  if (i_rBuildId.indexOf("LibreOffice_project/") != -1)
289  {
290  OUStringBuffer sNumber;
291  auto const firstSlash = i_rBuildId.indexOf("/");
292  assert(firstSlash != -1);
293  for (sal_Int32 i = firstSlash + 1; i < i_rBuildId.getLength(); ++i)
294  {
295  if (rtl::isAsciiDigit(i_rBuildId[i]))
296  {
297  sNumber.append(i_rBuildId[i]);
298  }
299  else if ('.' != i_rBuildId[i])
300  {
301  break;
302  }
303  }
304  if (!sNumber.isEmpty())
305  {
306  sBuildId += ";" + sNumber.makeStringAndClear();
307  }
308  }
309 
310  if ( sBuildId.isEmpty() )
311  return;
312 
313  try
314  {
315  if( xImportInfo.is() )
316  {
317  const OUString aPropName("BuildId");
318  uno::Reference< beans::XPropertySetInfo > xSetInfo(
319  xImportInfo->getPropertySetInfo());
320  if( xSetInfo.is() && xSetInfo->hasPropertyByName( aPropName ) )
321  xImportInfo->setPropertyValue( aPropName, uno::makeAny( sBuildId ) );
322  }
323  }
324  catch(const uno::Exception&)
325  {
326  }
327 }
328 
329 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
Definition: xmlmetai.cxx:208
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:71
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:62
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()
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
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:231
int i
virtual void SAL_CALL startFastElement(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
Definition: xmlmetai.cxx:200
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:240
SvXMLMetaDocumentContext(SvXMLImport &rImport, const css::uno::Reference< css::document::XDocumentProperties > &xDocProps)
Definition: xmlmetai.cxx:184
css::uno::Reference< css::uno::XComponentContext > const & GetComponentContext() const
Definition: xmlimp.cxx:1883
OUString GetAbsoluteReference(const OUString &rValue) const
Definition: xmlimp.cxx:1690
const OUString & GetXMLToken(enum XMLTokenEnum eToken)
return the OUString representation for eToken
Definition: xmltoken.cxx:3378
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:983
#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:158
Any value
const css::uno::Reference< css::beans::XPropertySet > & getImportInfo() const
Definition: xmlimp.hxx:390
handles the top-level office:document-meta element of meta.xml documents
Definition: xmlmetai.hxx:35
virtual void SAL_CALL characters(const OUString &aChars) override
Definition: xmlmetai.cxx:227
virtual ~SvXMLMetaDocumentContext() override
Definition: xmlmetai.cxx:196