Line data Source code
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 <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
21 : #include <com/sun/star/xml/dom/SAXDocumentBuilder.hpp>
22 : #include <com/sun/star/xml/dom/XSAXDocumentBuilder2.hpp>
23 : #include <com/sun/star/xml/xpath/XPathAPI.hpp>
24 : #include <com/sun/star/beans/XPropertySet.hpp>
25 : #include <com/sun/star/beans/XPropertySetInfo.hpp>
26 : #include <comphelper/processfactory.hxx>
27 : #include <xmloff/xmlmetai.hxx>
28 : #include <xmloff/xmlimp.hxx>
29 : #include <xmloff/nmspmap.hxx>
30 : #include <xmloff/xmltoken.hxx>
31 : #include <xmloff/xmlnmspe.hxx>
32 :
33 : using namespace com::sun::star;
34 : using namespace ::xmloff::token;
35 :
36 : /// builds a DOM tree from SAX events, by forwarding to SAXDocumentBuilder
37 : class XMLDocumentBuilderContext : public SvXMLImportContext
38 : {
39 : private:
40 : ::com::sun::star::uno::Reference<
41 : ::com::sun::star::xml::dom::XSAXDocumentBuilder2> mxDocBuilder;
42 :
43 : public:
44 : XMLDocumentBuilderContext(SvXMLImport& rImport, sal_uInt16 nPrfx,
45 : const OUString& rLName,
46 : const ::com::sun::star::uno::Reference<
47 : ::com::sun::star::xml::sax::XAttributeList>& xAttrList,
48 : const ::com::sun::star::uno::Reference<
49 : ::com::sun::star::xml::dom::XSAXDocumentBuilder2>& rDocBuilder);
50 :
51 : virtual ~XMLDocumentBuilderContext();
52 :
53 : virtual SvXMLImportContext *CreateChildContext( sal_uInt16 nPrefix,
54 : const OUString& rLocalName,
55 : const ::com::sun::star::uno::Reference<
56 : ::com::sun::star::xml::sax::XAttributeList>& xAttrList ) SAL_OVERRIDE;
57 :
58 : virtual void StartElement( const ::com::sun::star::uno::Reference<
59 : ::com::sun::star::xml::sax::XAttributeList >& xAttrList ) SAL_OVERRIDE;
60 :
61 : virtual void Characters( const OUString& rChars ) SAL_OVERRIDE;
62 :
63 : virtual void EndElement() SAL_OVERRIDE;
64 : };
65 :
66 4683 : XMLDocumentBuilderContext::XMLDocumentBuilderContext(SvXMLImport& rImport,
67 : sal_uInt16 nPrfx, const OUString& rLName,
68 : const uno::Reference<xml::sax::XAttributeList>&,
69 : const uno::Reference<xml::dom::XSAXDocumentBuilder2>& rDocBuilder) :
70 : SvXMLImportContext( rImport, nPrfx, rLName ),
71 4683 : mxDocBuilder(rDocBuilder)
72 : {
73 4683 : }
74 :
75 9366 : XMLDocumentBuilderContext::~XMLDocumentBuilderContext()
76 : {
77 9366 : }
78 :
79 : SvXMLImportContext *
80 4003 : XMLDocumentBuilderContext::CreateChildContext( sal_uInt16 nPrefix,
81 : const OUString& rLocalName,
82 : const uno::Reference< xml::sax::XAttributeList>& rAttrs)
83 : {
84 : return new XMLDocumentBuilderContext(
85 4003 : GetImport(), nPrefix, rLocalName, rAttrs, mxDocBuilder);
86 : }
87 :
88 4683 : void XMLDocumentBuilderContext::StartElement(
89 : const uno::Reference< xml::sax::XAttributeList >& xAttrList )
90 : {
91 4683 : mxDocBuilder->startElement(
92 9366 : GetImport().GetNamespaceMap().GetQNameByKey(GetPrefix(), GetLocalName()),
93 14049 : xAttrList);
94 4683 : }
95 :
96 3473 : void XMLDocumentBuilderContext::Characters( const OUString& rChars )
97 : {
98 3473 : mxDocBuilder->characters(rChars);
99 3473 : }
100 :
101 4683 : void XMLDocumentBuilderContext::EndElement()
102 : {
103 4683 : mxDocBuilder->endElement(
104 4683 : GetImport().GetNamespaceMap().GetQNameByKey(GetPrefix(), GetLocalName()));
105 4683 : }
106 :
107 : static void
108 641 : lcl_initDocumentProperties(SvXMLImport & rImport,
109 : uno::Reference<xml::dom::XSAXDocumentBuilder2> const& xDocBuilder,
110 : uno::Reference<document::XDocumentProperties> const& xDocProps)
111 : {
112 641 : uno::Sequence< uno::Any > aSeq(1);
113 641 : aSeq[0] <<= xDocBuilder->getDocument();
114 : uno::Reference< lang::XInitialization > const xInit(xDocProps,
115 1282 : uno::UNO_QUERY_THROW);
116 : try {
117 641 : xInit->initialize(aSeq);
118 641 : rImport.SetStatistics(xDocProps->getDocumentStatistics());
119 : // convert all URLs from relative to absolute
120 641 : xDocProps->setTemplateURL(rImport.GetAbsoluteReference(
121 641 : xDocProps->getTemplateURL()));
122 641 : xDocProps->setAutoloadURL(rImport.GetAbsoluteReference(
123 641 : xDocProps->getAutoloadURL()));
124 : SvXMLMetaDocumentContext::setBuildId(
125 641 : xDocProps->getGenerator(), rImport.getImportInfo());
126 0 : } catch (const uno::RuntimeException&) {
127 0 : throw;
128 0 : } catch (const uno::Exception& e) {
129 : throw lang::WrappedTargetRuntimeException(
130 : OUString(
131 : "SvXMLMetaDocumentContext::initDocumentProperties: "
132 : "properties init exception"),
133 0 : rImport, makeAny(e));
134 641 : }
135 641 : }
136 :
137 : static void
138 57 : lcl_initGenerator(SvXMLImport & rImport,
139 : uno::Reference<xml::dom::XSAXDocumentBuilder2> const& xDocBuilder)
140 : {
141 114 : uno::Reference< xml::dom::XDocument > const xDoc(xDocBuilder->getDocument(),
142 114 : uno::UNO_SET_THROW);
143 : try {
144 : uno::Reference< xml::xpath::XXPathAPI > const xPath = xml::xpath::XPathAPI::create(
145 57 : rImport.GetComponentContext() );
146 57 : xPath->registerNS(GetXMLToken(XML_NP_OFFICE),GetXMLToken(XML_N_OFFICE));
147 57 : xPath->registerNS(GetXMLToken(XML_NP_META), GetXMLToken(XML_N_META));
148 :
149 114 : OUString const expr( "string(/office:document-meta/office:meta/meta:generator)");
150 : uno::Reference< xml::xpath::XXPathObject > const xObj(
151 114 : xPath->eval(xDoc.get(), expr), uno::UNO_SET_THROW);
152 114 : OUString const value(xObj->getString());
153 114 : SvXMLMetaDocumentContext::setBuildId(value, rImport.getImportInfo());
154 0 : } catch (const uno::RuntimeException&) {
155 0 : throw;
156 0 : } catch (const uno::Exception& e) {
157 : throw lang::WrappedTargetRuntimeException(
158 : OUString(
159 : "SvXMLMetaDocumentContext::initGenerator: exception"),
160 0 : rImport, makeAny(e));
161 57 : }
162 57 : }
163 :
164 698 : SvXMLMetaDocumentContext::SvXMLMetaDocumentContext(SvXMLImport& rImport,
165 : sal_uInt16 nPrfx, const OUString& rLName,
166 : const uno::Reference<document::XDocumentProperties>& xDocProps) :
167 : SvXMLImportContext( rImport, nPrfx, rLName ),
168 : mxDocProps(xDocProps),
169 : mxDocBuilder(
170 : xml::dom::SAXDocumentBuilder::create(
171 698 : comphelper::getProcessComponentContext()))
172 : {
173 : // #i103539#: must always read meta.xml for generator, xDocProps unwanted then
174 : // OSL_ENSURE(xDocProps.is(), "SvXMLMetaDocumentContext: no document props");
175 698 : }
176 :
177 1810 : SvXMLMetaDocumentContext::~SvXMLMetaDocumentContext()
178 : {
179 1810 : }
180 :
181 680 : SvXMLImportContext *SvXMLMetaDocumentContext::CreateChildContext(
182 : sal_uInt16 nPrefix, const OUString& rLocalName,
183 : const uno::Reference<xml::sax::XAttributeList>& rAttrs)
184 : {
185 1360 : if ( (XML_NAMESPACE_OFFICE == nPrefix) &&
186 680 : IsXMLToken(rLocalName, XML_META) )
187 : {
188 : return new XMLDocumentBuilderContext(
189 680 : GetImport(), nPrefix, rLocalName, rAttrs, mxDocBuilder);
190 : }
191 : else
192 : {
193 0 : return new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
194 : }
195 : }
196 :
197 698 : void SvXMLMetaDocumentContext::StartElement(
198 : const uno::Reference< xml::sax::XAttributeList >& xAttrList )
199 : {
200 698 : mxDocBuilder->startDocument();
201 : // hardcode office:document-meta (necessary in case of flat file ODF)
202 698 : mxDocBuilder->startElement(
203 1396 : GetImport().GetNamespaceMap().GetQNameByKey(GetPrefix(),
204 2094 : GetXMLToken(XML_DOCUMENT_META)), xAttrList);
205 :
206 698 : }
207 :
208 698 : void SvXMLMetaDocumentContext::EndElement()
209 : {
210 : // hardcode office:document-meta (necessary in case of flat file ODF)
211 698 : mxDocBuilder->endElement(
212 1396 : GetImport().GetNamespaceMap().GetQNameByKey(GetPrefix(),
213 2094 : GetXMLToken(XML_DOCUMENT_META)));
214 698 : mxDocBuilder->endDocument();
215 698 : if (mxDocProps.is())
216 : {
217 641 : lcl_initDocumentProperties(GetImport(), mxDocBuilder, mxDocProps);
218 : }
219 : else
220 : {
221 57 : lcl_initGenerator(GetImport(), mxDocBuilder);
222 : }
223 698 : }
224 :
225 780 : void SvXMLMetaDocumentContext::setBuildId(OUString const& i_rBuildId, const uno::Reference<beans::XPropertySet>& xImportInfo )
226 : {
227 780 : OUString sBuildId;
228 : // skip to second product
229 780 : sal_Int32 nBegin = i_rBuildId.indexOf( ' ' );
230 780 : if ( nBegin != -1 )
231 : {
232 : // skip to build information
233 640 : nBegin = i_rBuildId.indexOf( '/', nBegin );
234 640 : if ( nBegin != -1 )
235 : {
236 630 : sal_Int32 nEnd = i_rBuildId.indexOf( 'm', nBegin );
237 630 : if ( nEnd != -1 )
238 : {
239 : OUStringBuffer sBuffer(
240 97 : i_rBuildId.copy( nBegin+1, nEnd-nBegin-1 ) );
241 : const OUString sBuildCompare(
242 194 : "$Build-" );
243 97 : nBegin = i_rBuildId.indexOf( sBuildCompare, nEnd );
244 97 : if ( nBegin != -1 )
245 : {
246 97 : sBuffer.append( '$' );
247 : sBuffer.append( i_rBuildId.copy(
248 97 : nBegin + sBuildCompare.getLength() ) );
249 97 : sBuildId = sBuffer.makeStringAndClear();
250 97 : }
251 : }
252 : }
253 : }
254 :
255 780 : if ( sBuildId.isEmpty() )
256 : {
257 1366 : if ( i_rBuildId.startsWith("StarOffice 7")
258 681 : || i_rBuildId.startsWith("StarSuite 7")
259 1364 : || i_rBuildId.startsWith("OpenOffice.org 1"))
260 : {
261 2 : sBuildId = "645$8687";
262 : }
263 681 : else if (i_rBuildId.startsWith("NeoOffice/2"))
264 : {
265 0 : sBuildId = "680$9134"; // fake NeoOffice as OpenOffice.org 2.2 release
266 : }
267 : }
268 :
269 1560 : OUString rest;
270 2081 : if (i_rBuildId.startsWith("LibreOffice/", &rest) ||
271 957 : i_rBuildId.startsWith("LibreOfficeDev/", &rest) ||
272 177 : i_rBuildId.startsWith("LOdev/", &rest))
273 : {
274 603 : OUStringBuffer sNumber;
275 4984 : for (sal_Int32 i = 0; i < rest.getLength(); ++i)
276 : {
277 4984 : if (isdigit(rest[i]))
278 : {
279 2286 : sNumber.append(rest[i]);
280 : }
281 2698 : else if ('.' != rest[i])
282 : {
283 603 : break;
284 : }
285 : }
286 603 : if (!sNumber.isEmpty())
287 : {
288 603 : sBuildId += (";" + sNumber.makeStringAndClear());
289 603 : }
290 : }
291 :
292 780 : if ( !sBuildId.isEmpty() ) try
293 : {
294 631 : if( xImportInfo.is() )
295 : {
296 630 : const OUString aPropName("BuildId");
297 : uno::Reference< beans::XPropertySetInfo > xSetInfo(
298 1260 : xImportInfo->getPropertySetInfo());
299 630 : if( xSetInfo.is() && xSetInfo->hasPropertyByName( aPropName ) )
300 1255 : xImportInfo->setPropertyValue( aPropName, uno::makeAny( sBuildId ) );
301 : }
302 : }
303 0 : catch(const uno::Exception&)
304 : {
305 780 : }
306 780 : }
307 :
308 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|