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