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, "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 
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 
223 uno::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 
232 void SvXMLMetaDocumentContext::setBuildId(OUString const& i_rBuildId, const uno::Reference<beans::XPropertySet>& xImportInfo )
233 {
234  OUString sBuildId;
235  // skip to second product
236  sal_Int32 nBegin = i_rBuildId.indexOf( ' ' );
237  if ( nBegin != -1 )
238  {
239  // skip to build information
240  nBegin = i_rBuildId.indexOf( '/', nBegin );
241  if ( nBegin != -1 )
242  {
243  sal_Int32 nEnd = i_rBuildId.indexOf( 'm', nBegin );
244  if ( nEnd != -1 )
245  {
246  OUStringBuffer sBuffer(
247  i_rBuildId.subView( nBegin+1, nEnd-nBegin-1 ) );
248  const OUString sBuildCompare(
249  "$Build-" );
250  nBegin = i_rBuildId.indexOf( sBuildCompare, nEnd );
251  if ( nBegin != -1 )
252  {
253  sBuffer.append( '$' );
254  sBuffer.append( i_rBuildId.subView(nBegin + sBuildCompare.getLength()) );
255  sBuildId = sBuffer.makeStringAndClear();
256  }
257  }
258  }
259  }
260 
261  if ( sBuildId.isEmpty() )
262  {
263  if ( i_rBuildId.startsWith("StarOffice 7")
264  || i_rBuildId.startsWith("StarSuite 7")
265  || i_rBuildId.startsWith("StarOffice 6")
266  || i_rBuildId.startsWith("StarSuite 6")
267  || i_rBuildId.startsWith("OpenOffice.org 1"))
268  {
269  sBuildId = "645$8687";
270  }
271  else if (i_rBuildId.startsWith("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.indexOf("LibreOffice_project/") != -1)
280  {
281  OUStringBuffer sNumber;
282  auto const firstSlash = i_rBuildId.indexOf("/");
283  assert(firstSlash != -1);
284  for (sal_Int32 i = firstSlash + 1; i < i_rBuildId.getLength(); ++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.makeStringAndClear();
298  }
299  }
300 
301  if ( sBuildId.isEmpty() )
302  return;
303 
304  try
305  {
306  if( xImportInfo.is() )
307  {
308  const OUString aPropName("BuildId");
309  uno::Reference< beans::XPropertySetInfo > xSetInfo(
310  xImportInfo->getPropertySetInfo());
311  if( xSetInfo.is() && xSetInfo->hasPropertyByName( aPropName ) )
312  xImportInfo->setPropertyValue( aPropName, uno::makeAny( sBuildId ) );
313  }
314  }
315  catch(const uno::Exception&)
316  {
317  }
318 }
319 
320 /* 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:60
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:223
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:45
static void setBuildId(const OUString &rGenerator, const css::uno::Reference< css::beans::XPropertySet > &xImportInfo)
Definition: xmlmetai.cxx:232
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:1782
OUString GetAbsoluteReference(const OUString &rValue) const
Definition: xmlimp.cxx:1601
const OUString & GetXMLToken(enum XMLTokenEnum eToken)
return the OUString representation for eToken
Definition: xmltoken.cxx:3424
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:894
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:96
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:385
handles the top-level office:document-meta element of meta.xml documents
Definition: xmlmetai.hxx:35
virtual ~SvXMLMetaDocumentContext() override
Definition: xmlmetai.cxx:196