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