1*63bba73cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*63bba73cSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*63bba73cSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*63bba73cSAndrew Rist * distributed with this work for additional information
6*63bba73cSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*63bba73cSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*63bba73cSAndrew Rist * "License"); you may not use this file except in compliance
9*63bba73cSAndrew Rist * with the License. You may obtain a copy of the License at
10*63bba73cSAndrew Rist *
11*63bba73cSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*63bba73cSAndrew Rist *
13*63bba73cSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*63bba73cSAndrew Rist * software distributed under the License is distributed on an
15*63bba73cSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*63bba73cSAndrew Rist * KIND, either express or implied. See the License for the
17*63bba73cSAndrew Rist * specific language governing permissions and limitations
18*63bba73cSAndrew Rist * under the License.
19*63bba73cSAndrew Rist *
20*63bba73cSAndrew Rist *************************************************************/
21*63bba73cSAndrew Rist
22*63bba73cSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_xmloff.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
28cdf0e10cSrcweir #include <com/sun/star/xml/dom/XSAXDocumentBuilder.hpp>
29cdf0e10cSrcweir #include <com/sun/star/xml/xpath/XXPathAPI.hpp>
30cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
31cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySetInfo.hpp>
32cdf0e10cSrcweir
33cdf0e10cSrcweir #include <tools/debug.hxx>
34cdf0e10cSrcweir
35cdf0e10cSrcweir #include <xmloff/xmlmetai.hxx>
36cdf0e10cSrcweir #include <xmloff/xmlimp.hxx>
37cdf0e10cSrcweir #include <xmloff/nmspmap.hxx>
38cdf0e10cSrcweir #include <xmloff/xmltoken.hxx>
39cdf0e10cSrcweir #include "xmloff/xmlnmspe.hxx"
40cdf0e10cSrcweir
41cdf0e10cSrcweir
42cdf0e10cSrcweir using ::rtl::OUString;
43cdf0e10cSrcweir using ::rtl::OUStringBuffer;
44cdf0e10cSrcweir using namespace com::sun::star;
45cdf0e10cSrcweir using namespace ::xmloff::token;
46cdf0e10cSrcweir
47cdf0e10cSrcweir
48cdf0e10cSrcweir //===========================================================================
49cdf0e10cSrcweir
50cdf0e10cSrcweir /// builds a DOM tree from SAX events, by forwarding to SAXDocumentBuilder
51cdf0e10cSrcweir class XMLDocumentBuilderContext : public SvXMLImportContext
52cdf0e10cSrcweir {
53cdf0e10cSrcweir private:
54cdf0e10cSrcweir ::com::sun::star::uno::Reference<
55cdf0e10cSrcweir ::com::sun::star::xml::sax::XDocumentHandler> mxDocBuilder;
56cdf0e10cSrcweir
57cdf0e10cSrcweir public:
58cdf0e10cSrcweir XMLDocumentBuilderContext(SvXMLImport& rImport, sal_uInt16 nPrfx,
59cdf0e10cSrcweir const ::rtl::OUString& rLName,
60cdf0e10cSrcweir const ::com::sun::star::uno::Reference<
61cdf0e10cSrcweir ::com::sun::star::xml::sax::XAttributeList>& xAttrList,
62cdf0e10cSrcweir const ::com::sun::star::uno::Reference<
63cdf0e10cSrcweir ::com::sun::star::xml::sax::XDocumentHandler>& rDocBuilder);
64cdf0e10cSrcweir
65cdf0e10cSrcweir virtual ~XMLDocumentBuilderContext();
66cdf0e10cSrcweir
67cdf0e10cSrcweir virtual SvXMLImportContext *CreateChildContext( sal_uInt16 nPrefix,
68cdf0e10cSrcweir const rtl::OUString& rLocalName,
69cdf0e10cSrcweir const ::com::sun::star::uno::Reference<
70cdf0e10cSrcweir ::com::sun::star::xml::sax::XAttributeList>& xAttrList );
71cdf0e10cSrcweir
72cdf0e10cSrcweir virtual void StartElement( const ::com::sun::star::uno::Reference<
73cdf0e10cSrcweir ::com::sun::star::xml::sax::XAttributeList >& xAttrList );
74cdf0e10cSrcweir
75cdf0e10cSrcweir virtual void Characters( const ::rtl::OUString& rChars );
76cdf0e10cSrcweir
77cdf0e10cSrcweir virtual void EndElement();
78cdf0e10cSrcweir };
79cdf0e10cSrcweir
XMLDocumentBuilderContext(SvXMLImport & rImport,sal_uInt16 nPrfx,const::rtl::OUString & rLName,const uno::Reference<xml::sax::XAttributeList> &,const uno::Reference<xml::sax::XDocumentHandler> & rDocBuilder)80cdf0e10cSrcweir XMLDocumentBuilderContext::XMLDocumentBuilderContext(SvXMLImport& rImport,
81cdf0e10cSrcweir sal_uInt16 nPrfx, const ::rtl::OUString& rLName,
82cdf0e10cSrcweir const uno::Reference<xml::sax::XAttributeList>&,
83cdf0e10cSrcweir const uno::Reference<xml::sax::XDocumentHandler>& rDocBuilder) :
84cdf0e10cSrcweir SvXMLImportContext( rImport, nPrfx, rLName ),
85cdf0e10cSrcweir mxDocBuilder(rDocBuilder)
86cdf0e10cSrcweir {
87cdf0e10cSrcweir }
88cdf0e10cSrcweir
~XMLDocumentBuilderContext()89cdf0e10cSrcweir XMLDocumentBuilderContext::~XMLDocumentBuilderContext()
90cdf0e10cSrcweir {
91cdf0e10cSrcweir }
92cdf0e10cSrcweir
93cdf0e10cSrcweir SvXMLImportContext *
CreateChildContext(sal_uInt16 nPrefix,const rtl::OUString & rLocalName,const uno::Reference<xml::sax::XAttributeList> & rAttrs)94cdf0e10cSrcweir XMLDocumentBuilderContext::CreateChildContext( sal_uInt16 nPrefix,
95cdf0e10cSrcweir const rtl::OUString& rLocalName,
96cdf0e10cSrcweir const uno::Reference< xml::sax::XAttributeList>& rAttrs)
97cdf0e10cSrcweir {
98cdf0e10cSrcweir return new XMLDocumentBuilderContext(
99cdf0e10cSrcweir GetImport(), nPrefix, rLocalName, rAttrs, mxDocBuilder);
100cdf0e10cSrcweir }
101cdf0e10cSrcweir
StartElement(const uno::Reference<xml::sax::XAttributeList> & xAttrList)102cdf0e10cSrcweir void XMLDocumentBuilderContext::StartElement(
103cdf0e10cSrcweir const uno::Reference< xml::sax::XAttributeList >& xAttrList )
104cdf0e10cSrcweir {
105cdf0e10cSrcweir mxDocBuilder->startElement(
106cdf0e10cSrcweir GetImport().GetNamespaceMap().GetQNameByKey(GetPrefix(), GetLocalName()),
107cdf0e10cSrcweir xAttrList);
108cdf0e10cSrcweir }
109cdf0e10cSrcweir
Characters(const::rtl::OUString & rChars)110cdf0e10cSrcweir void XMLDocumentBuilderContext::Characters( const ::rtl::OUString& rChars )
111cdf0e10cSrcweir {
112cdf0e10cSrcweir mxDocBuilder->characters(rChars);
113cdf0e10cSrcweir }
114cdf0e10cSrcweir
EndElement()115cdf0e10cSrcweir void XMLDocumentBuilderContext::EndElement()
116cdf0e10cSrcweir {
117cdf0e10cSrcweir mxDocBuilder->endElement(
118cdf0e10cSrcweir GetImport().GetNamespaceMap().GetQNameByKey(GetPrefix(), GetLocalName()));
119cdf0e10cSrcweir }
120cdf0e10cSrcweir
121cdf0e10cSrcweir
122cdf0e10cSrcweir //===========================================================================
123cdf0e10cSrcweir
124cdf0e10cSrcweir static void
lcl_initDocumentProperties(SvXMLImport & rImport,uno::Reference<xml::sax::XDocumentHandler> const & xDocBuilder,uno::Reference<document::XDocumentProperties> const & xDocProps)125cdf0e10cSrcweir lcl_initDocumentProperties(SvXMLImport & rImport,
126cdf0e10cSrcweir uno::Reference<xml::sax::XDocumentHandler> const& xDocBuilder,
127cdf0e10cSrcweir uno::Reference<document::XDocumentProperties> const& xDocProps)
128cdf0e10cSrcweir {
129cdf0e10cSrcweir uno::Sequence< uno::Any > aSeq(1);
130cdf0e10cSrcweir uno::Reference< xml::dom::XSAXDocumentBuilder > const xDB(xDocBuilder,
131cdf0e10cSrcweir uno::UNO_QUERY_THROW);
132cdf0e10cSrcweir aSeq[0] <<= xDB->getDocument();
133cdf0e10cSrcweir uno::Reference< lang::XInitialization > const xInit(xDocProps,
134cdf0e10cSrcweir uno::UNO_QUERY_THROW);
135cdf0e10cSrcweir try {
136cdf0e10cSrcweir xInit->initialize(aSeq);
137cdf0e10cSrcweir rImport.SetStatistics(xDocProps->getDocumentStatistics());
138cdf0e10cSrcweir // convert all URLs from relative to absolute
139cdf0e10cSrcweir xDocProps->setTemplateURL(rImport.GetAbsoluteReference(
140cdf0e10cSrcweir xDocProps->getTemplateURL()));
141cdf0e10cSrcweir xDocProps->setAutoloadURL(rImport.GetAbsoluteReference(
142cdf0e10cSrcweir xDocProps->getAutoloadURL()));
143cdf0e10cSrcweir SvXMLMetaDocumentContext::setBuildId(
144cdf0e10cSrcweir xDocProps->getGenerator(), rImport.getImportInfo());
145cdf0e10cSrcweir } catch (uno::RuntimeException) {
146cdf0e10cSrcweir throw;
147cdf0e10cSrcweir } catch (uno::Exception & e) {
148cdf0e10cSrcweir throw lang::WrappedTargetRuntimeException(
149cdf0e10cSrcweir ::rtl::OUString::createFromAscii(
150cdf0e10cSrcweir "SvXMLMetaDocumentContext::initDocumentProperties: "
151cdf0e10cSrcweir "properties init exception"),
152cdf0e10cSrcweir rImport, makeAny(e));
153cdf0e10cSrcweir }
154cdf0e10cSrcweir }
155cdf0e10cSrcweir
156cdf0e10cSrcweir static void
lcl_initGenerator(SvXMLImport & rImport,uno::Reference<xml::sax::XDocumentHandler> const & xDocBuilder)157cdf0e10cSrcweir lcl_initGenerator(SvXMLImport & rImport,
158cdf0e10cSrcweir uno::Reference<xml::sax::XDocumentHandler> const& xDocBuilder)
159cdf0e10cSrcweir {
160cdf0e10cSrcweir uno::Reference< xml::dom::XSAXDocumentBuilder > const xDB(xDocBuilder,
161cdf0e10cSrcweir uno::UNO_QUERY_THROW);
162cdf0e10cSrcweir uno::Reference< xml::dom::XDocument > const xDoc(xDB->getDocument(),
163cdf0e10cSrcweir uno::UNO_SET_THROW);
164cdf0e10cSrcweir try {
165cdf0e10cSrcweir uno::Reference< xml::xpath::XXPathAPI > const xPath(
166cdf0e10cSrcweir rImport.getServiceFactory()->createInstance(
167cdf0e10cSrcweir ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
168cdf0e10cSrcweir "com.sun.star.xml.xpath.XPathAPI"))),
169cdf0e10cSrcweir uno::UNO_QUERY_THROW );
170cdf0e10cSrcweir xPath->registerNS(GetXMLToken(XML_NP_OFFICE),GetXMLToken(XML_N_OFFICE));
171cdf0e10cSrcweir xPath->registerNS(GetXMLToken(XML_NP_META), GetXMLToken(XML_N_META));
172cdf0e10cSrcweir
173cdf0e10cSrcweir ::rtl::OUString const expr(RTL_CONSTASCII_USTRINGPARAM(
174cdf0e10cSrcweir "string(/office:document-meta/office:meta/meta:generator)"));
175cdf0e10cSrcweir uno::Reference< xml::xpath::XXPathObject > const xObj(
176cdf0e10cSrcweir xPath->eval(xDoc.get(), expr), uno::UNO_SET_THROW);
177cdf0e10cSrcweir OUString const value(xObj->getString());
178cdf0e10cSrcweir SvXMLMetaDocumentContext::setBuildId(value, rImport.getImportInfo());
179cdf0e10cSrcweir } catch (uno::RuntimeException) {
180cdf0e10cSrcweir throw;
181cdf0e10cSrcweir } catch (uno::Exception & e) {
182cdf0e10cSrcweir throw lang::WrappedTargetRuntimeException(
183cdf0e10cSrcweir ::rtl::OUString::createFromAscii(
184cdf0e10cSrcweir "SvXMLMetaDocumentContext::initGenerator: exception"),
185cdf0e10cSrcweir rImport, makeAny(e));
186cdf0e10cSrcweir }
187cdf0e10cSrcweir }
188cdf0e10cSrcweir
SvXMLMetaDocumentContext(SvXMLImport & rImport,sal_uInt16 nPrfx,const rtl::OUString & rLName,const uno::Reference<document::XDocumentProperties> & xDocProps,const uno::Reference<xml::sax::XDocumentHandler> & xDocBuilder)189cdf0e10cSrcweir SvXMLMetaDocumentContext::SvXMLMetaDocumentContext(SvXMLImport& rImport,
190cdf0e10cSrcweir sal_uInt16 nPrfx, const rtl::OUString& rLName,
191cdf0e10cSrcweir const uno::Reference<document::XDocumentProperties>& xDocProps,
192cdf0e10cSrcweir const uno::Reference<xml::sax::XDocumentHandler>& xDocBuilder) :
193cdf0e10cSrcweir SvXMLImportContext( rImport, nPrfx, rLName ),
194cdf0e10cSrcweir mxDocProps(xDocProps),
195cdf0e10cSrcweir mxDocBuilder(xDocBuilder)
196cdf0e10cSrcweir {
197cdf0e10cSrcweir // #i103539#: must always read meta.xml for generator, xDocProps unwanted then
198cdf0e10cSrcweir // OSL_ENSURE(xDocProps.is(), "SvXMLMetaDocumentContext: no document props");
199cdf0e10cSrcweir OSL_ENSURE(xDocBuilder.is(), "SvXMLMetaDocumentContext: no document hdlr");
200cdf0e10cSrcweir // here are no attributes
201cdf0e10cSrcweir }
202cdf0e10cSrcweir
~SvXMLMetaDocumentContext()203cdf0e10cSrcweir SvXMLMetaDocumentContext::~SvXMLMetaDocumentContext()
204cdf0e10cSrcweir {
205cdf0e10cSrcweir }
206cdf0e10cSrcweir
CreateChildContext(sal_uInt16 nPrefix,const rtl::OUString & rLocalName,const uno::Reference<xml::sax::XAttributeList> & rAttrs)207cdf0e10cSrcweir SvXMLImportContext *SvXMLMetaDocumentContext::CreateChildContext(
208cdf0e10cSrcweir sal_uInt16 nPrefix, const rtl::OUString& rLocalName,
209cdf0e10cSrcweir const uno::Reference<xml::sax::XAttributeList>& rAttrs)
210cdf0e10cSrcweir {
211cdf0e10cSrcweir if ( (XML_NAMESPACE_OFFICE == nPrefix) &&
212cdf0e10cSrcweir IsXMLToken(rLocalName, XML_META) )
213cdf0e10cSrcweir {
214cdf0e10cSrcweir return new XMLDocumentBuilderContext(
215cdf0e10cSrcweir GetImport(), nPrefix, rLocalName, rAttrs, mxDocBuilder);
216cdf0e10cSrcweir }
217cdf0e10cSrcweir else
218cdf0e10cSrcweir {
219cdf0e10cSrcweir return new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
220cdf0e10cSrcweir }
221cdf0e10cSrcweir }
222cdf0e10cSrcweir
223cdf0e10cSrcweir
StartElement(const uno::Reference<xml::sax::XAttributeList> & xAttrList)224cdf0e10cSrcweir void SvXMLMetaDocumentContext::StartElement(
225cdf0e10cSrcweir const uno::Reference< xml::sax::XAttributeList >& xAttrList )
226cdf0e10cSrcweir {
227cdf0e10cSrcweir mxDocBuilder->startDocument();
228cdf0e10cSrcweir // hardcode office:document-meta (necessary in case of flat file ODF)
229cdf0e10cSrcweir mxDocBuilder->startElement(
230cdf0e10cSrcweir GetImport().GetNamespaceMap().GetQNameByKey(GetPrefix(),
231cdf0e10cSrcweir GetXMLToken(XML_DOCUMENT_META)), xAttrList);
232cdf0e10cSrcweir
233cdf0e10cSrcweir }
234cdf0e10cSrcweir
EndElement()235cdf0e10cSrcweir void SvXMLMetaDocumentContext::EndElement()
236cdf0e10cSrcweir {
237cdf0e10cSrcweir // hardcode office:document-meta (necessary in case of flat file ODF)
238cdf0e10cSrcweir mxDocBuilder->endElement(
239cdf0e10cSrcweir GetImport().GetNamespaceMap().GetQNameByKey(GetPrefix(),
240cdf0e10cSrcweir GetXMLToken(XML_DOCUMENT_META)));
241cdf0e10cSrcweir mxDocBuilder->endDocument();
242cdf0e10cSrcweir if (mxDocProps.is())
243cdf0e10cSrcweir {
244cdf0e10cSrcweir lcl_initDocumentProperties(GetImport(), mxDocBuilder, mxDocProps);
245cdf0e10cSrcweir }
246cdf0e10cSrcweir else
247cdf0e10cSrcweir {
248cdf0e10cSrcweir lcl_initGenerator(GetImport(), mxDocBuilder);
249cdf0e10cSrcweir }
250cdf0e10cSrcweir }
251cdf0e10cSrcweir
setBuildId(::rtl::OUString const & i_rBuildId,const uno::Reference<beans::XPropertySet> & xImportInfo)252cdf0e10cSrcweir void SvXMLMetaDocumentContext::setBuildId(::rtl::OUString const& i_rBuildId, const uno::Reference<beans::XPropertySet>& xImportInfo )
253cdf0e10cSrcweir {
254cdf0e10cSrcweir OUString sBuildId;
255cdf0e10cSrcweir // skip to second product
256cdf0e10cSrcweir sal_Int32 nBegin = i_rBuildId.indexOf( ' ' );
257cdf0e10cSrcweir if ( nBegin != -1 )
258cdf0e10cSrcweir {
259cdf0e10cSrcweir // skip to build information
260cdf0e10cSrcweir nBegin = i_rBuildId.indexOf( '/', nBegin );
261cdf0e10cSrcweir if ( nBegin != -1 )
262cdf0e10cSrcweir {
263cdf0e10cSrcweir sal_Int32 nEnd = i_rBuildId.indexOf( 'm', nBegin );
264cdf0e10cSrcweir if ( nEnd != -1 )
265cdf0e10cSrcweir {
266cdf0e10cSrcweir OUStringBuffer sBuffer(
267cdf0e10cSrcweir i_rBuildId.copy( nBegin+1, nEnd-nBegin-1 ) );
268cdf0e10cSrcweir const OUString sBuildCompare(
269cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM( "$Build-" ) );
270cdf0e10cSrcweir nBegin = i_rBuildId.indexOf( sBuildCompare, nEnd );
271cdf0e10cSrcweir if ( nBegin != -1 )
272cdf0e10cSrcweir {
273cdf0e10cSrcweir sBuffer.append( (sal_Unicode)'$' );
274cdf0e10cSrcweir sBuffer.append( i_rBuildId.copy(
275cdf0e10cSrcweir nBegin + sBuildCompare.getLength() ) );
276cdf0e10cSrcweir sBuildId = sBuffer.makeStringAndClear();
277cdf0e10cSrcweir }
278cdf0e10cSrcweir }
279cdf0e10cSrcweir }
280cdf0e10cSrcweir }
281cdf0e10cSrcweir
282cdf0e10cSrcweir if ( sBuildId.getLength() == 0 )
283cdf0e10cSrcweir {
284cdf0e10cSrcweir if ((i_rBuildId.compareToAscii(
285cdf0e10cSrcweir RTL_CONSTASCII_STRINGPARAM("StarOffice 7") ) == 0) ||
286cdf0e10cSrcweir (i_rBuildId.compareToAscii(
287cdf0e10cSrcweir RTL_CONSTASCII_STRINGPARAM("StarSuite 7") ) == 0) ||
288cdf0e10cSrcweir (i_rBuildId.compareToAscii(
289cdf0e10cSrcweir RTL_CONSTASCII_STRINGPARAM("OpenOffice.org 1") ) == 0))
290cdf0e10cSrcweir {
291cdf0e10cSrcweir sBuildId = OUString::createFromAscii( "645$8687" );
292cdf0e10cSrcweir }
293cdf0e10cSrcweir if ((i_rBuildId.compareToAscii( RTL_CONSTASCII_STRINGPARAM("NeoOffice/2") ) == 0) )
294cdf0e10cSrcweir {
295cdf0e10cSrcweir sBuildId = OUString::createFromAscii( "680$9134" ); // fake NeoOffice as OpenOffice.org 2.2 release
296cdf0e10cSrcweir }
297cdf0e10cSrcweir }
298f6348da9SArmin Le Grand else
299f6348da9SArmin Le Grand {
300f6348da9SArmin Le Grand if ((i_rBuildId.compareToAscii( RTL_CONSTASCII_STRINGPARAM("LibreOffice/3") ) == 0) )
301f6348da9SArmin Le Grand {
302f6348da9SArmin Le Grand // #118558# fake LibreOffice3 as OpenOffice.org 3.3 release
303f6348da9SArmin Le Grand sBuildId = OUString::createFromAscii( "330$9567" );
304f6348da9SArmin Le Grand }
305f6348da9SArmin Le Grand }
306cdf0e10cSrcweir
307cdf0e10cSrcweir if ( sBuildId.getLength() ) try
308cdf0e10cSrcweir {
309cdf0e10cSrcweir if( xImportInfo.is() )
310cdf0e10cSrcweir {
311cdf0e10cSrcweir const OUString aPropName(RTL_CONSTASCII_USTRINGPARAM("BuildId"));
312cdf0e10cSrcweir uno::Reference< beans::XPropertySetInfo > xSetInfo(
313cdf0e10cSrcweir xImportInfo->getPropertySetInfo());
314cdf0e10cSrcweir if( xSetInfo.is() && xSetInfo->hasPropertyByName( aPropName ) )
315cdf0e10cSrcweir xImportInfo->setPropertyValue( aPropName, uno::makeAny( sBuildId ) );
316cdf0e10cSrcweir }
317cdf0e10cSrcweir }
318cdf0e10cSrcweir catch( uno::Exception& )
319cdf0e10cSrcweir {
320cdf0e10cSrcweir }
321cdf0e10cSrcweir }
322cdf0e10cSrcweir
323