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