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>
34#include <rtl/character.hxx>
35#include <rtl/ustrbuf.hxx>
51 css::uno::Reference< css::xml::dom::XSAXDocumentBuilder2> mxDocBuilder;
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,
60 virtual void SAL_CALL
characters(
const OUString& aChars )
override;
63 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
override;
65 virtual void SAL_CALL
endFastElement( sal_Int32 nElement )
override;
68 const css::uno::Reference< css::xml::sax::XFastAttributeList >& Attribs )
override;
70 virtual void SAL_CALL
endUnknownElement(
const OUString& Namespace,
const OUString& Name )
override;
73 sal_Int32 nElement,
const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
override;
79XMLDocumentBuilderContext::XMLDocumentBuilderContext(SvXMLImport& rImport,
80 sal_Int32 ,
const uno::Reference<xml::sax::XFastAttributeList>&,
81 uno::Reference<xml::dom::XSAXDocumentBuilder2> xDocBuilder,
84 , mxDocBuilder(
std::move(xDocBuilder))
85 , m_pTopLevel(pTopLevel)
89void SAL_CALL XMLDocumentBuilderContext::startFastElement( sal_Int32 nElement,
90 const uno::Reference< xml::sax::XFastAttributeList >& xAttribs )
95void SAL_CALL XMLDocumentBuilderContext::endFastElement( sal_Int32 nElement )
97 mxDocBuilder->endFastElement(nElement);
103 m_pTopLevel->FinishMetaElement();
107void SAL_CALL XMLDocumentBuilderContext::startUnknownElement(
const OUString& rNamespace,
108 const OUString& rName,
const uno::Reference< xml::sax::XFastAttributeList >& xAttrList )
110 mxDocBuilder->startUnknownElement(rNamespace, rName, xAttrList);
113void SAL_CALL XMLDocumentBuilderContext::endUnknownElement(
const OUString& rNamespace,
const OUString& rName )
115 mxDocBuilder->endUnknownElement(rNamespace, rName);
118void SAL_CALL XMLDocumentBuilderContext::characters(
const OUString& rChars )
120 mxDocBuilder->characters(rChars);
123uno::Reference< xml::sax::XFastContextHandler > SAL_CALL XMLDocumentBuilderContext::createFastChildContext(
124 sal_Int32 nElement,
const uno::Reference< xml::sax::XFastAttributeList >& xAttrList )
126 return new XMLDocumentBuilderContext(GetImport(), nElement, xAttrList, mxDocBuilder,
nullptr);
131 uno::Reference<xml::dom::XSAXDocumentBuilder2>
const& xDocBuilder,
132 uno::Reference<document::XDocumentProperties>
const& xDocProps)
134 uno::Reference< lang::XInitialization >
const xInit(xDocProps,
135 uno::UNO_QUERY_THROW);
137 xInit->initialize({
uno::Any(xDocBuilder->getDocument()) });
138 rImport.SetStatistics(xDocProps->getDocumentStatistics());
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&) {
148 }
catch (
const uno::Exception&) {
150 throw lang::WrappedTargetRuntimeException(
151 "SvXMLMetaDocumentContext::initDocumentProperties: "
152 "properties init exception",
159 uno::Reference<xml::dom::XSAXDocumentBuilder2>
const& xDocBuilder)
161 uno::Reference< xml::dom::XDocument >
const xDoc(xDocBuilder->getDocument(),
164 uno::Reference< xml::xpath::XXPathAPI >
const xPath = xml::xpath::XPathAPI::create(
165 rImport.GetComponentContext() );
169 uno::Reference< xml::xpath::XXPathObject >
const xObj(
170 xPath->eval(xDoc,
"string(/office:document-meta/office:meta/meta:generator)"),
172 OUString
const value(xObj->getString());
174 }
catch (
const uno::RuntimeException&) {
176 }
catch (
const uno::Exception&) {
178 throw lang::WrappedTargetRuntimeException(
179 "SvXMLMetaDocumentContext::initGenerator: exception",
185 uno::Reference<document::XDocumentProperties> xDocProps) :
187 mxDocProps(
std::move(xDocProps)),
189 xml::dom::SAXDocumentBuilder::
create(
201 const uno::Reference< xml::sax::XFastAttributeList >& xAttrList )
224 sal_Int32 nElement,
const uno::Reference< xml::sax::XFastAttributeList >& xAttrList )
227 return new XMLDocumentBuilderContext(
236 size_t nBegin = i_rBuildId.find(
' ' );
237 if ( nBegin != std::u16string_view::npos )
240 nBegin = i_rBuildId.find(
'/', nBegin );
241 if ( nBegin != std::u16string_view::npos )
243 size_t nEnd = i_rBuildId.find(
'm', nBegin );
244 if ( nEnd != std::u16string_view::npos )
246 OUStringBuffer sBuffer(
247 i_rBuildId.substr( nBegin+1, nEnd-nBegin-1 ) );
248 static constexpr OUStringLiteral sBuildCompare(
250 nBegin = i_rBuildId.find( sBuildCompare, nEnd );
251 if ( nBegin != std::u16string_view::npos )
253 sBuffer.append(
'$' );
254 sBuffer.append( i_rBuildId.substr(nBegin + sBuildCompare.getLength()) );
255 sBuildId = sBuffer.makeStringAndClear();
261 if ( sBuildId.isEmpty() )
269 sBuildId =
"645$8687";
273 sBuildId =
"680$9134";
279 if (i_rBuildId.find(
u"LibreOffice_project/") != std::u16string_view::npos)
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)
286 if (rtl::isAsciiDigit(i_rBuildId[
i]) ||
'.' == i_rBuildId[
i])
288 sNumber.append(i_rBuildId[
i]);
295 if (!sNumber.isEmpty())
297 sBuildId +=
";" + sNumber;
301 if ( sBuildId.isEmpty() )
306 if( xImportInfo.is() )
308 static constexpr OUStringLiteral
aPropName(
u"BuildId");
309 uno::Reference< beans::XPropertySetInfo > xSetInfo(
310 xImportInfo->getPropertySetInfo());
311 if( xSetInfo.is() && xSetInfo->hasPropertyByName(
aPropName ) )
315 catch(
const uno::Exception&)
This class deliberately does not support XWeak, to improve performance when loading large documents.
virtual void SAL_CALL startFastElement(sal_Int32 Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > &Attribs) override
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 ...
SvXMLImport & GetImport()
virtual css::uno::Reference< XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 Element, const css::uno::Reference< css::xml::sax::XFastAttributeList > &Attribs) override
virtual void SAL_CALL endUnknownElement(const OUString &Namespace, const OUString &Name) override
virtual void SAL_CALL startUnknownElement(const OUString &Namespace, const OUString &Name, const css::uno::Reference< css::xml::sax::XFastAttributeList > &Attribs) override
virtual void SAL_CALL characters(const OUString &aChars) override
This method is called for all characters that are contained in the current element.
handles the top-level office:document-meta element of meta.xml documents
css::uno::Reference< css::document::XDocumentProperties > mxDocProps
static void setBuildId(std::u16string_view rGenerator, const css::uno::Reference< css::beans::XPropertySet > &xImportInfo)
css::uno::Reference< css::xml::dom::XSAXDocumentBuilder2 > mxDocBuilder
SvXMLMetaDocumentContext(SvXMLImport &rImport, css::uno::Reference< css::document::XDocumentProperties > xDocProps)
virtual ~SvXMLMetaDocumentContext() override
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
virtual void SAL_CALL startFastElement(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
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)
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
#define XML_ELEMENT(prefix, name)