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 0 : 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 0 : mxDocBuilder(rDocBuilder)
72 : {
73 0 : }
74 :
75 0 : XMLDocumentBuilderContext::~XMLDocumentBuilderContext()
76 : {
77 0 : }
78 :
79 : SvXMLImportContext *
80 0 : XMLDocumentBuilderContext::CreateChildContext( sal_uInt16 nPrefix,
81 : const OUString& rLocalName,
82 : const uno::Reference< xml::sax::XAttributeList>& rAttrs)
83 : {
84 : return new XMLDocumentBuilderContext(
85 0 : GetImport(), nPrefix, rLocalName, rAttrs, mxDocBuilder);
86 : }
87 :
88 0 : void XMLDocumentBuilderContext::StartElement(
89 : const uno::Reference< xml::sax::XAttributeList >& xAttrList )
90 : {
91 0 : mxDocBuilder->startElement(
92 0 : GetImport().GetNamespaceMap().GetQNameByKey(GetPrefix(), GetLocalName()),
93 0 : xAttrList);
94 0 : }
95 :
96 0 : void XMLDocumentBuilderContext::Characters( const OUString& rChars )
97 : {
98 0 : mxDocBuilder->characters(rChars);
99 0 : }
100 :
101 0 : void XMLDocumentBuilderContext::EndElement()
102 : {
103 0 : mxDocBuilder->endElement(
104 0 : GetImport().GetNamespaceMap().GetQNameByKey(GetPrefix(), GetLocalName()));
105 0 : }
106 :
107 : static void
108 0 : lcl_initDocumentProperties(SvXMLImport & rImport,
109 : uno::Reference<xml::dom::XSAXDocumentBuilder2> const& xDocBuilder,
110 : uno::Reference<document::XDocumentProperties> const& xDocProps)
111 : {
112 0 : uno::Sequence< uno::Any > aSeq(1);
113 0 : aSeq[0] <<= xDocBuilder->getDocument();
114 : uno::Reference< lang::XInitialization > const xInit(xDocProps,
115 0 : uno::UNO_QUERY_THROW);
116 : try {
117 0 : xInit->initialize(aSeq);
118 0 : rImport.SetStatistics(xDocProps->getDocumentStatistics());
119 : // convert all URLs from relative to absolute
120 0 : xDocProps->setTemplateURL(rImport.GetAbsoluteReference(
121 0 : xDocProps->getTemplateURL()));
122 0 : xDocProps->setAutoloadURL(rImport.GetAbsoluteReference(
123 0 : xDocProps->getAutoloadURL()));
124 : SvXMLMetaDocumentContext::setBuildId(
125 0 : 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 0 : }
135 0 : }
136 :
137 : static void
138 0 : lcl_initGenerator(SvXMLImport & rImport,
139 : uno::Reference<xml::dom::XSAXDocumentBuilder2> const& xDocBuilder)
140 : {
141 0 : uno::Reference< xml::dom::XDocument > const xDoc(xDocBuilder->getDocument(),
142 0 : uno::UNO_SET_THROW);
143 : try {
144 : uno::Reference< xml::xpath::XXPathAPI > const xPath = xml::xpath::XPathAPI::create(
145 0 : rImport.GetComponentContext() );
146 0 : xPath->registerNS(GetXMLToken(XML_NP_OFFICE),GetXMLToken(XML_N_OFFICE));
147 0 : xPath->registerNS(GetXMLToken(XML_NP_META), GetXMLToken(XML_N_META));
148 :
149 0 : OUString const expr( "string(/office:document-meta/office:meta/meta:generator)");
150 : uno::Reference< xml::xpath::XXPathObject > const xObj(
151 0 : xPath->eval(xDoc.get(), expr), uno::UNO_SET_THROW);
152 0 : OUString const value(xObj->getString());
153 0 : 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 0 : }
162 0 : }
163 :
164 0 : 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 0 : 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 0 : }
176 :
177 0 : SvXMLMetaDocumentContext::~SvXMLMetaDocumentContext()
178 : {
179 0 : }
180 :
181 0 : SvXMLImportContext *SvXMLMetaDocumentContext::CreateChildContext(
182 : sal_uInt16 nPrefix, const OUString& rLocalName,
183 : const uno::Reference<xml::sax::XAttributeList>& rAttrs)
184 : {
185 0 : if ( (XML_NAMESPACE_OFFICE == nPrefix) &&
186 0 : IsXMLToken(rLocalName, XML_META) )
187 : {
188 : return new XMLDocumentBuilderContext(
189 0 : GetImport(), nPrefix, rLocalName, rAttrs, mxDocBuilder);
190 : }
191 : else
192 : {
193 0 : return new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
194 : }
195 : }
196 :
197 0 : void SvXMLMetaDocumentContext::StartElement(
198 : const uno::Reference< xml::sax::XAttributeList >& xAttrList )
199 : {
200 0 : mxDocBuilder->startDocument();
201 : // hardcode office:document-meta (necessary in case of flat file ODF)
202 0 : mxDocBuilder->startElement(
203 0 : GetImport().GetNamespaceMap().GetQNameByKey(GetPrefix(),
204 0 : GetXMLToken(XML_DOCUMENT_META)), xAttrList);
205 :
206 0 : }
207 :
208 0 : void SvXMLMetaDocumentContext::EndElement()
209 : {
210 : // hardcode office:document-meta (necessary in case of flat file ODF)
211 0 : mxDocBuilder->endElement(
212 0 : GetImport().GetNamespaceMap().GetQNameByKey(GetPrefix(),
213 0 : GetXMLToken(XML_DOCUMENT_META)));
214 0 : mxDocBuilder->endDocument();
215 0 : if (mxDocProps.is())
216 : {
217 0 : lcl_initDocumentProperties(GetImport(), mxDocBuilder, mxDocProps);
218 : }
219 : else
220 : {
221 0 : lcl_initGenerator(GetImport(), mxDocBuilder);
222 : }
223 0 : }
224 :
225 0 : void SvXMLMetaDocumentContext::setBuildId(OUString const& i_rBuildId, const uno::Reference<beans::XPropertySet>& xImportInfo )
226 : {
227 0 : OUString sBuildId;
228 : // skip to second product
229 0 : sal_Int32 nBegin = i_rBuildId.indexOf( ' ' );
230 0 : if ( nBegin != -1 )
231 : {
232 : // skip to build information
233 0 : nBegin = i_rBuildId.indexOf( '/', nBegin );
234 0 : if ( nBegin != -1 )
235 : {
236 0 : sal_Int32 nEnd = i_rBuildId.indexOf( 'm', nBegin );
237 0 : if ( nEnd != -1 )
238 : {
239 : OUStringBuffer sBuffer(
240 0 : i_rBuildId.copy( nBegin+1, nEnd-nBegin-1 ) );
241 : const OUString sBuildCompare(
242 0 : "$Build-" );
243 0 : nBegin = i_rBuildId.indexOf( sBuildCompare, nEnd );
244 0 : if ( nBegin != -1 )
245 : {
246 0 : sBuffer.append( '$' );
247 : sBuffer.append( i_rBuildId.copy(
248 0 : nBegin + sBuildCompare.getLength() ) );
249 0 : sBuildId = sBuffer.makeStringAndClear();
250 0 : }
251 : }
252 : }
253 : }
254 :
255 0 : if ( sBuildId.isEmpty() )
256 : {
257 0 : if ( i_rBuildId.startsWith("StarOffice 7")
258 0 : || i_rBuildId.startsWith("StarSuite 7")
259 0 : || i_rBuildId.startsWith("OpenOffice.org 1"))
260 : {
261 0 : sBuildId = "645$8687";
262 : }
263 0 : 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 0 : if (i_rBuildId.startsWith("LibreOffice/"))
270 : {
271 0 : OUStringBuffer sNumber;
272 0 : for (sal_Int32 i = sizeof("LibreOffice/") - 1;
273 0 : i < i_rBuildId.getLength(); ++i)
274 : {
275 0 : if (isdigit(i_rBuildId[i]))
276 : {
277 0 : sNumber.append(i_rBuildId[i]);
278 : }
279 0 : else if ('.' != i_rBuildId[i])
280 : {
281 0 : break;
282 : }
283 : }
284 0 : if (!sNumber.isEmpty())
285 : {
286 0 : sBuildId += (";" + sNumber.makeStringAndClear());
287 0 : }
288 : }
289 :
290 0 : if ( !sBuildId.isEmpty() ) try
291 : {
292 0 : if( xImportInfo.is() )
293 : {
294 0 : const OUString aPropName("BuildId");
295 : uno::Reference< beans::XPropertySetInfo > xSetInfo(
296 0 : xImportInfo->getPropertySetInfo());
297 0 : if( xSetInfo.is() && xSetInfo->hasPropertyByName( aPropName ) )
298 0 : xImportInfo->setPropertyValue( aPropName, uno::makeAny( sBuildId ) );
299 : }
300 : }
301 0 : catch(const uno::Exception&)
302 : {
303 0 : }
304 0 : }
305 :
306 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|