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 <tools/debug.hxx> 32*cdf0e10cSrcweir #include <tools/inetdef.hxx> 33*cdf0e10cSrcweir #include <i18npool/mslangid.hxx> 34*cdf0e10cSrcweir #include <tools/urlobj.hxx> 35*cdf0e10cSrcweir #include <tools/time.hxx> 36*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx> 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir #include <xmloff/xmlmetae.hxx> 39*cdf0e10cSrcweir #include <xmloff/xmlexp.hxx> 40*cdf0e10cSrcweir #include <xmloff/xmluconv.hxx> 41*cdf0e10cSrcweir #include <xmloff/nmspmap.hxx> 42*cdf0e10cSrcweir #include "xmloff/xmlnmspe.hxx" 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertyAccess.hpp> 45*cdf0e10cSrcweir #include <com/sun/star/beans/StringPair.hpp> 46*cdf0e10cSrcweir #include <com/sun/star/xml/dom/XDocument.hpp> 47*cdf0e10cSrcweir #include <com/sun/star/xml/sax/XSAXSerializable.hpp> 48*cdf0e10cSrcweir 49*cdf0e10cSrcweir #include <comphelper/sequenceasvector.hxx> 50*cdf0e10cSrcweir #include <unotools/docinfohelper.hxx> 51*cdf0e10cSrcweir 52*cdf0e10cSrcweir #include <string.h> 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir 55*cdf0e10cSrcweir using namespace com::sun::star; 56*cdf0e10cSrcweir using namespace ::xmloff::token; 57*cdf0e10cSrcweir 58*cdf0e10cSrcweir 59*cdf0e10cSrcweir //------------------------------------------------------------------------- 60*cdf0e10cSrcweir 61*cdf0e10cSrcweir void lcl_AddTwoDigits( rtl::OUStringBuffer& rStr, sal_Int32 nVal ) 62*cdf0e10cSrcweir { 63*cdf0e10cSrcweir if ( nVal < 10 ) 64*cdf0e10cSrcweir rStr.append( sal_Unicode('0') ); 65*cdf0e10cSrcweir rStr.append( nVal ); 66*cdf0e10cSrcweir } 67*cdf0e10cSrcweir 68*cdf0e10cSrcweir rtl::OUString 69*cdf0e10cSrcweir SvXMLMetaExport::GetISODateTimeString( const util::DateTime& rDateTime ) 70*cdf0e10cSrcweir { 71*cdf0e10cSrcweir // return ISO date string "YYYY-MM-DDThh:mm:ss" 72*cdf0e10cSrcweir 73*cdf0e10cSrcweir rtl::OUStringBuffer sTmp; 74*cdf0e10cSrcweir sTmp.append( (sal_Int32) rDateTime.Year ); 75*cdf0e10cSrcweir sTmp.append( sal_Unicode('-') ); 76*cdf0e10cSrcweir lcl_AddTwoDigits( sTmp, rDateTime.Month ); 77*cdf0e10cSrcweir sTmp.append( sal_Unicode('-') ); 78*cdf0e10cSrcweir lcl_AddTwoDigits( sTmp, rDateTime.Day ); 79*cdf0e10cSrcweir sTmp.append( sal_Unicode('T') ); 80*cdf0e10cSrcweir lcl_AddTwoDigits( sTmp, rDateTime.Hours ); 81*cdf0e10cSrcweir sTmp.append( sal_Unicode(':') ); 82*cdf0e10cSrcweir lcl_AddTwoDigits( sTmp, rDateTime.Minutes ); 83*cdf0e10cSrcweir sTmp.append( sal_Unicode(':') ); 84*cdf0e10cSrcweir lcl_AddTwoDigits( sTmp, rDateTime.Seconds ); 85*cdf0e10cSrcweir 86*cdf0e10cSrcweir return sTmp.makeStringAndClear(); 87*cdf0e10cSrcweir } 88*cdf0e10cSrcweir 89*cdf0e10cSrcweir //------------------------------------------------------------------------- 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir void SvXMLMetaExport::SimpleStringElement( const rtl::OUString& rText, 92*cdf0e10cSrcweir sal_uInt16 nNamespace, enum XMLTokenEnum eElementName ) 93*cdf0e10cSrcweir { 94*cdf0e10cSrcweir if ( rText.getLength() ) { 95*cdf0e10cSrcweir SvXMLElementExport aElem( mrExport, nNamespace, eElementName, 96*cdf0e10cSrcweir sal_True, sal_False ); 97*cdf0e10cSrcweir mrExport.Characters( rText ); 98*cdf0e10cSrcweir } 99*cdf0e10cSrcweir } 100*cdf0e10cSrcweir 101*cdf0e10cSrcweir void SvXMLMetaExport::SimpleDateTimeElement( const util::DateTime & rDate, 102*cdf0e10cSrcweir sal_uInt16 nNamespace, enum XMLTokenEnum eElementName ) 103*cdf0e10cSrcweir { 104*cdf0e10cSrcweir if (rDate.Month != 0) { // invalid dates are 0-0-0 105*cdf0e10cSrcweir rtl::OUString sValue = GetISODateTimeString( rDate ); 106*cdf0e10cSrcweir if ( sValue.getLength() ) { 107*cdf0e10cSrcweir SvXMLElementExport aElem( mrExport, nNamespace, eElementName, 108*cdf0e10cSrcweir sal_True, sal_False ); 109*cdf0e10cSrcweir mrExport.Characters( sValue ); 110*cdf0e10cSrcweir } 111*cdf0e10cSrcweir } 112*cdf0e10cSrcweir } 113*cdf0e10cSrcweir 114*cdf0e10cSrcweir void SvXMLMetaExport::_MExport() 115*cdf0e10cSrcweir { 116*cdf0e10cSrcweir // generator 117*cdf0e10cSrcweir { 118*cdf0e10cSrcweir SvXMLElementExport aElem( mrExport, XML_NAMESPACE_META, XML_GENERATOR, 119*cdf0e10cSrcweir sal_True, sal_True ); 120*cdf0e10cSrcweir mrExport.Characters( ::utl::DocInfoHelper::GetGeneratorString() ); 121*cdf0e10cSrcweir } 122*cdf0e10cSrcweir 123*cdf0e10cSrcweir // document title 124*cdf0e10cSrcweir SimpleStringElement ( mxDocProps->getTitle(), 125*cdf0e10cSrcweir XML_NAMESPACE_DC, XML_TITLE ); 126*cdf0e10cSrcweir 127*cdf0e10cSrcweir // description 128*cdf0e10cSrcweir SimpleStringElement ( mxDocProps->getDescription(), 129*cdf0e10cSrcweir XML_NAMESPACE_DC, XML_DESCRIPTION ); 130*cdf0e10cSrcweir 131*cdf0e10cSrcweir // subject 132*cdf0e10cSrcweir SimpleStringElement ( mxDocProps->getSubject(), 133*cdf0e10cSrcweir XML_NAMESPACE_DC, XML_SUBJECT ); 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir // created... 136*cdf0e10cSrcweir SimpleStringElement ( mxDocProps->getAuthor(), 137*cdf0e10cSrcweir XML_NAMESPACE_META, XML_INITIAL_CREATOR ); 138*cdf0e10cSrcweir SimpleDateTimeElement( mxDocProps->getCreationDate(), 139*cdf0e10cSrcweir XML_NAMESPACE_META, XML_CREATION_DATE ); 140*cdf0e10cSrcweir 141*cdf0e10cSrcweir // modified... 142*cdf0e10cSrcweir SimpleStringElement ( mxDocProps->getModifiedBy(), 143*cdf0e10cSrcweir XML_NAMESPACE_DC, XML_CREATOR ); 144*cdf0e10cSrcweir SimpleDateTimeElement( mxDocProps->getModificationDate(), 145*cdf0e10cSrcweir XML_NAMESPACE_DC, XML_DATE ); 146*cdf0e10cSrcweir 147*cdf0e10cSrcweir // printed... 148*cdf0e10cSrcweir SimpleStringElement ( mxDocProps->getPrintedBy(), 149*cdf0e10cSrcweir XML_NAMESPACE_META, XML_PRINTED_BY ); 150*cdf0e10cSrcweir SimpleDateTimeElement( mxDocProps->getPrintDate(), 151*cdf0e10cSrcweir XML_NAMESPACE_META, XML_PRINT_DATE ); 152*cdf0e10cSrcweir 153*cdf0e10cSrcweir // keywords 154*cdf0e10cSrcweir const uno::Sequence< ::rtl::OUString > keywords = mxDocProps->getKeywords(); 155*cdf0e10cSrcweir for (sal_Int32 i = 0; i < keywords.getLength(); ++i) { 156*cdf0e10cSrcweir SvXMLElementExport aKwElem( mrExport, XML_NAMESPACE_META, XML_KEYWORD, 157*cdf0e10cSrcweir sal_True, sal_False ); 158*cdf0e10cSrcweir mrExport.Characters( keywords[i] ); 159*cdf0e10cSrcweir } 160*cdf0e10cSrcweir 161*cdf0e10cSrcweir // document language 162*cdf0e10cSrcweir { 163*cdf0e10cSrcweir const lang::Locale aLocale = mxDocProps->getLanguage(); 164*cdf0e10cSrcweir ::rtl::OUString sValue = aLocale.Language; 165*cdf0e10cSrcweir if (sValue.getLength()) { 166*cdf0e10cSrcweir if ( aLocale.Country.getLength() ) 167*cdf0e10cSrcweir { 168*cdf0e10cSrcweir sValue += rtl::OUString::valueOf((sal_Unicode)'-'); 169*cdf0e10cSrcweir sValue += aLocale.Country; 170*cdf0e10cSrcweir } 171*cdf0e10cSrcweir SvXMLElementExport aElem( mrExport, XML_NAMESPACE_DC, XML_LANGUAGE, 172*cdf0e10cSrcweir sal_True, sal_False ); 173*cdf0e10cSrcweir mrExport.Characters( sValue ); 174*cdf0e10cSrcweir } 175*cdf0e10cSrcweir } 176*cdf0e10cSrcweir 177*cdf0e10cSrcweir // editing cycles 178*cdf0e10cSrcweir { 179*cdf0e10cSrcweir SvXMLElementExport aElem( mrExport, 180*cdf0e10cSrcweir XML_NAMESPACE_META, XML_EDITING_CYCLES, 181*cdf0e10cSrcweir sal_True, sal_False ); 182*cdf0e10cSrcweir mrExport.Characters( ::rtl::OUString::valueOf( 183*cdf0e10cSrcweir static_cast<sal_Int32>(mxDocProps->getEditingCycles()) ) ); 184*cdf0e10cSrcweir } 185*cdf0e10cSrcweir 186*cdf0e10cSrcweir // editing duration 187*cdf0e10cSrcweir // property is a int32 (seconds) 188*cdf0e10cSrcweir { 189*cdf0e10cSrcweir sal_Int32 secs = mxDocProps->getEditingDuration(); 190*cdf0e10cSrcweir SvXMLElementExport aElem( mrExport, 191*cdf0e10cSrcweir XML_NAMESPACE_META, XML_EDITING_DURATION, 192*cdf0e10cSrcweir sal_True, sal_False ); 193*cdf0e10cSrcweir mrExport.Characters( SvXMLUnitConverter::convertTimeDuration( 194*cdf0e10cSrcweir Time(secs/3600, (secs%3600)/60, secs%60)) ); 195*cdf0e10cSrcweir } 196*cdf0e10cSrcweir 197*cdf0e10cSrcweir // default target 198*cdf0e10cSrcweir const ::rtl::OUString sDefTarget = mxDocProps->getDefaultTarget(); 199*cdf0e10cSrcweir if ( sDefTarget.getLength() ) 200*cdf0e10cSrcweir { 201*cdf0e10cSrcweir mrExport.AddAttribute( XML_NAMESPACE_OFFICE, XML_TARGET_FRAME_NAME, 202*cdf0e10cSrcweir sDefTarget ); 203*cdf0e10cSrcweir 204*cdf0e10cSrcweir //! define strings for xlink:show values 205*cdf0e10cSrcweir const XMLTokenEnum eShow = 206*cdf0e10cSrcweir sDefTarget.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("_blank")) 207*cdf0e10cSrcweir ? XML_NEW : XML_REPLACE; 208*cdf0e10cSrcweir mrExport.AddAttribute( XML_NAMESPACE_XLINK, XML_SHOW, eShow ); 209*cdf0e10cSrcweir 210*cdf0e10cSrcweir SvXMLElementExport aElem( mrExport, 211*cdf0e10cSrcweir XML_NAMESPACE_META,XML_HYPERLINK_BEHAVIOUR, 212*cdf0e10cSrcweir sal_True, sal_False ); 213*cdf0e10cSrcweir } 214*cdf0e10cSrcweir 215*cdf0e10cSrcweir // auto-reload 216*cdf0e10cSrcweir const ::rtl::OUString sReloadURL = mxDocProps->getAutoloadURL(); 217*cdf0e10cSrcweir const sal_Int32 sReloadDelay = mxDocProps->getAutoloadSecs(); 218*cdf0e10cSrcweir if (sReloadDelay != 0 || sReloadURL.getLength() != 0) 219*cdf0e10cSrcweir { 220*cdf0e10cSrcweir mrExport.AddAttribute( XML_NAMESPACE_XLINK, XML_HREF, 221*cdf0e10cSrcweir mrExport.GetRelativeReference( sReloadURL ) ); 222*cdf0e10cSrcweir 223*cdf0e10cSrcweir mrExport.AddAttribute( XML_NAMESPACE_META, XML_DELAY, 224*cdf0e10cSrcweir SvXMLUnitConverter::convertTimeDuration( 225*cdf0e10cSrcweir Time(sReloadDelay/3600, (sReloadDelay%3600)/60, 226*cdf0e10cSrcweir sReloadDelay%60 )) ); 227*cdf0e10cSrcweir 228*cdf0e10cSrcweir SvXMLElementExport aElem( mrExport, XML_NAMESPACE_META, XML_AUTO_RELOAD, 229*cdf0e10cSrcweir sal_True, sal_False ); 230*cdf0e10cSrcweir } 231*cdf0e10cSrcweir 232*cdf0e10cSrcweir // template 233*cdf0e10cSrcweir const rtl::OUString sTplPath = mxDocProps->getTemplateURL(); 234*cdf0e10cSrcweir if ( sTplPath.getLength() ) 235*cdf0e10cSrcweir { 236*cdf0e10cSrcweir mrExport.AddAttribute( XML_NAMESPACE_XLINK, XML_TYPE, XML_SIMPLE ); 237*cdf0e10cSrcweir mrExport.AddAttribute( XML_NAMESPACE_XLINK, XML_ACTUATE, XML_ONREQUEST ); 238*cdf0e10cSrcweir 239*cdf0e10cSrcweir // template URL 240*cdf0e10cSrcweir mrExport.AddAttribute( XML_NAMESPACE_XLINK, XML_HREF, 241*cdf0e10cSrcweir mrExport.GetRelativeReference(sTplPath) ); 242*cdf0e10cSrcweir 243*cdf0e10cSrcweir // template name 244*cdf0e10cSrcweir mrExport.AddAttribute( XML_NAMESPACE_XLINK, XML_TITLE, 245*cdf0e10cSrcweir mxDocProps->getTemplateName() ); 246*cdf0e10cSrcweir 247*cdf0e10cSrcweir // template date 248*cdf0e10cSrcweir mrExport.AddAttribute( XML_NAMESPACE_META, XML_DATE, 249*cdf0e10cSrcweir GetISODateTimeString( mxDocProps->getTemplateDate() ) ); 250*cdf0e10cSrcweir 251*cdf0e10cSrcweir SvXMLElementExport aElem( mrExport, XML_NAMESPACE_META, XML_TEMPLATE, 252*cdf0e10cSrcweir sal_True, sal_False ); 253*cdf0e10cSrcweir } 254*cdf0e10cSrcweir 255*cdf0e10cSrcweir // user defined fields 256*cdf0e10cSrcweir uno::Reference< beans::XPropertyAccess > xUserDefined( 257*cdf0e10cSrcweir mxDocProps->getUserDefinedProperties(), uno::UNO_QUERY_THROW); 258*cdf0e10cSrcweir const uno::Sequence< beans::PropertyValue > props = 259*cdf0e10cSrcweir xUserDefined->getPropertyValues(); 260*cdf0e10cSrcweir for (sal_Int32 i = 0; i < props.getLength(); ++i) { 261*cdf0e10cSrcweir ::rtl::OUStringBuffer sValueBuffer; 262*cdf0e10cSrcweir ::rtl::OUStringBuffer sType; 263*cdf0e10cSrcweir if (!SvXMLUnitConverter::convertAny( 264*cdf0e10cSrcweir sValueBuffer, sType, props[i].Value)) { 265*cdf0e10cSrcweir continue; 266*cdf0e10cSrcweir } 267*cdf0e10cSrcweir mrExport.AddAttribute( XML_NAMESPACE_META, XML_NAME, props[i].Name ); 268*cdf0e10cSrcweir mrExport.AddAttribute( XML_NAMESPACE_META, XML_VALUE_TYPE, 269*cdf0e10cSrcweir sType.makeStringAndClear() ); 270*cdf0e10cSrcweir SvXMLElementExport aElem( mrExport, XML_NAMESPACE_META, 271*cdf0e10cSrcweir XML_USER_DEFINED, sal_True, sal_False ); 272*cdf0e10cSrcweir mrExport.Characters( sValueBuffer.makeStringAndClear() ); 273*cdf0e10cSrcweir } 274*cdf0e10cSrcweir 275*cdf0e10cSrcweir const uno::Sequence< beans::NamedValue > aDocStatistic = 276*cdf0e10cSrcweir mxDocProps->getDocumentStatistics(); 277*cdf0e10cSrcweir // write document statistic if there is any provided 278*cdf0e10cSrcweir if ( aDocStatistic.getLength() ) 279*cdf0e10cSrcweir { 280*cdf0e10cSrcweir for ( sal_Int32 nInd = 0; nInd < aDocStatistic.getLength(); nInd++ ) 281*cdf0e10cSrcweir { 282*cdf0e10cSrcweir sal_Int32 nValue = 0; 283*cdf0e10cSrcweir if ( aDocStatistic[nInd].Value >>= nValue ) 284*cdf0e10cSrcweir { 285*cdf0e10cSrcweir ::rtl::OUString aValue = rtl::OUString::valueOf( nValue ); 286*cdf0e10cSrcweir if ( aDocStatistic[nInd].Name.equals( ::rtl::OUString( 287*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM( "TableCount" ) ) ) ) 288*cdf0e10cSrcweir mrExport.AddAttribute( 289*cdf0e10cSrcweir XML_NAMESPACE_META, XML_TABLE_COUNT, aValue ); 290*cdf0e10cSrcweir else if ( aDocStatistic[nInd].Name.equals( ::rtl::OUString( 291*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM( "ObjectCount" ) ) ) ) 292*cdf0e10cSrcweir mrExport.AddAttribute( 293*cdf0e10cSrcweir XML_NAMESPACE_META, XML_OBJECT_COUNT, aValue ); 294*cdf0e10cSrcweir else if ( aDocStatistic[nInd].Name.equals( ::rtl::OUString( 295*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM( "ImageCount" ) ) ) ) 296*cdf0e10cSrcweir mrExport.AddAttribute( 297*cdf0e10cSrcweir XML_NAMESPACE_META, XML_IMAGE_COUNT, aValue ); 298*cdf0e10cSrcweir else if ( aDocStatistic[nInd].Name.equals( ::rtl::OUString( 299*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM( "PageCount" ) ) ) ) 300*cdf0e10cSrcweir mrExport.AddAttribute( 301*cdf0e10cSrcweir XML_NAMESPACE_META, XML_PAGE_COUNT, aValue ); 302*cdf0e10cSrcweir else if ( aDocStatistic[nInd].Name.equals( ::rtl::OUString( 303*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM( "ParagraphCount" ) ) ) ) 304*cdf0e10cSrcweir mrExport.AddAttribute( 305*cdf0e10cSrcweir XML_NAMESPACE_META, XML_PARAGRAPH_COUNT, aValue ); 306*cdf0e10cSrcweir else if ( aDocStatistic[nInd].Name.equals( ::rtl::OUString( 307*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM( "WordCount" ) ) ) ) 308*cdf0e10cSrcweir mrExport.AddAttribute( 309*cdf0e10cSrcweir XML_NAMESPACE_META, XML_WORD_COUNT, aValue ); 310*cdf0e10cSrcweir else if ( aDocStatistic[nInd].Name.equals( ::rtl::OUString( 311*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM( "CharacterCount" ) ) ) ) 312*cdf0e10cSrcweir mrExport.AddAttribute( 313*cdf0e10cSrcweir XML_NAMESPACE_META, XML_CHARACTER_COUNT, aValue ); 314*cdf0e10cSrcweir else if ( aDocStatistic[nInd].Name.equals( ::rtl::OUString( 315*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM( "CellCount" ) ) ) ) 316*cdf0e10cSrcweir mrExport.AddAttribute( 317*cdf0e10cSrcweir XML_NAMESPACE_META, XML_CELL_COUNT, aValue ); 318*cdf0e10cSrcweir else 319*cdf0e10cSrcweir { 320*cdf0e10cSrcweir DBG_ASSERT( sal_False, "Unknown statistic value!\n" ); 321*cdf0e10cSrcweir } 322*cdf0e10cSrcweir } 323*cdf0e10cSrcweir } 324*cdf0e10cSrcweir SvXMLElementExport aElem( mrExport, 325*cdf0e10cSrcweir XML_NAMESPACE_META, XML_DOCUMENT_STATISTIC, sal_True, sal_True ); 326*cdf0e10cSrcweir } 327*cdf0e10cSrcweir } 328*cdf0e10cSrcweir 329*cdf0e10cSrcweir //------------------------------------------------------------------------- 330*cdf0e10cSrcweir 331*cdf0e10cSrcweir static const char *s_xmlns = "xmlns"; 332*cdf0e10cSrcweir static const char *s_xmlns2 = "xmlns:"; 333*cdf0e10cSrcweir static const char *s_meta = "meta:"; 334*cdf0e10cSrcweir static const char *s_href = "xlink:href"; 335*cdf0e10cSrcweir 336*cdf0e10cSrcweir SvXMLMetaExport::SvXMLMetaExport( 337*cdf0e10cSrcweir SvXMLExport& i_rExp, 338*cdf0e10cSrcweir const uno::Reference<document::XDocumentProperties>& i_rDocProps ) : 339*cdf0e10cSrcweir mrExport( i_rExp ), 340*cdf0e10cSrcweir mxDocProps( i_rDocProps ), 341*cdf0e10cSrcweir m_level( 0 ), 342*cdf0e10cSrcweir m_preservedNSs() 343*cdf0e10cSrcweir { 344*cdf0e10cSrcweir DBG_ASSERT( mxDocProps.is(), "no document properties" ); 345*cdf0e10cSrcweir } 346*cdf0e10cSrcweir 347*cdf0e10cSrcweir SvXMLMetaExport::~SvXMLMetaExport() 348*cdf0e10cSrcweir { 349*cdf0e10cSrcweir } 350*cdf0e10cSrcweir 351*cdf0e10cSrcweir void SvXMLMetaExport::Export() 352*cdf0e10cSrcweir { 353*cdf0e10cSrcweir // exportDom(xDOM, mrExport); // this would not work (root node, namespaces) 354*cdf0e10cSrcweir uno::Reference< xml::sax::XSAXSerializable> xSAXable(mxDocProps, 355*cdf0e10cSrcweir uno::UNO_QUERY); 356*cdf0e10cSrcweir if (xSAXable.is()) { 357*cdf0e10cSrcweir ::comphelper::SequenceAsVector< beans::StringPair > namespaces; 358*cdf0e10cSrcweir const SvXMLNamespaceMap & rNsMap(mrExport.GetNamespaceMap()); 359*cdf0e10cSrcweir for (sal_uInt16 key = rNsMap.GetFirstKey(); 360*cdf0e10cSrcweir key != USHRT_MAX; key = rNsMap.GetNextKey(key)) { 361*cdf0e10cSrcweir beans::StringPair ns; 362*cdf0e10cSrcweir const ::rtl::OUString attrname = rNsMap.GetAttrNameByKey(key); 363*cdf0e10cSrcweir if (attrname.matchAsciiL(s_xmlns2, strlen(s_xmlns2))) { 364*cdf0e10cSrcweir ns.First = attrname.copy(strlen(s_xmlns2)); 365*cdf0e10cSrcweir } else if (attrname.equalsAsciiL(s_xmlns, strlen(s_xmlns))) { 366*cdf0e10cSrcweir // default initialized empty string 367*cdf0e10cSrcweir } else { 368*cdf0e10cSrcweir DBG_ERROR("namespace attribute not starting with xmlns unexpected"); 369*cdf0e10cSrcweir } 370*cdf0e10cSrcweir ns.Second = rNsMap.GetNameByKey(key); 371*cdf0e10cSrcweir namespaces.push_back(ns); 372*cdf0e10cSrcweir } 373*cdf0e10cSrcweir xSAXable->serialize(this, namespaces.getAsConstList()); 374*cdf0e10cSrcweir } else { 375*cdf0e10cSrcweir // office:meta 376*cdf0e10cSrcweir SvXMLElementExport aElem( mrExport, XML_NAMESPACE_OFFICE, XML_META, 377*cdf0e10cSrcweir sal_True, sal_True ); 378*cdf0e10cSrcweir // fall back to using public interface of XDocumentProperties 379*cdf0e10cSrcweir _MExport(); 380*cdf0e10cSrcweir } 381*cdf0e10cSrcweir } 382*cdf0e10cSrcweir 383*cdf0e10cSrcweir // ::com::sun::star::xml::sax::XDocumentHandler: 384*cdf0e10cSrcweir void SAL_CALL 385*cdf0e10cSrcweir SvXMLMetaExport::startDocument() 386*cdf0e10cSrcweir throw (uno::RuntimeException, xml::sax::SAXException) 387*cdf0e10cSrcweir { 388*cdf0e10cSrcweir // ignore: has already been done by SvXMLExport::exportDoc 389*cdf0e10cSrcweir DBG_ASSERT( m_level == 0, "SvXMLMetaExport: level error" ); 390*cdf0e10cSrcweir } 391*cdf0e10cSrcweir 392*cdf0e10cSrcweir void SAL_CALL 393*cdf0e10cSrcweir SvXMLMetaExport::endDocument() 394*cdf0e10cSrcweir throw (uno::RuntimeException, xml::sax::SAXException) 395*cdf0e10cSrcweir { 396*cdf0e10cSrcweir // ignore: will be done by SvXMLExport::exportDoc 397*cdf0e10cSrcweir DBG_ASSERT( m_level == 0, "SvXMLMetaExport: level error" ); 398*cdf0e10cSrcweir } 399*cdf0e10cSrcweir 400*cdf0e10cSrcweir // unfortunately, this method contains far too much ugly namespace mangling. 401*cdf0e10cSrcweir void SAL_CALL 402*cdf0e10cSrcweir SvXMLMetaExport::startElement(const ::rtl::OUString & i_rName, 403*cdf0e10cSrcweir const uno::Reference< xml::sax::XAttributeList > & i_xAttribs) 404*cdf0e10cSrcweir throw (uno::RuntimeException, xml::sax::SAXException) 405*cdf0e10cSrcweir { 406*cdf0e10cSrcweir 407*cdf0e10cSrcweir if (m_level == 0) { 408*cdf0e10cSrcweir // namepace decls: default ones have been written at the root element 409*cdf0e10cSrcweir // non-default ones must be preserved here 410*cdf0e10cSrcweir const sal_Int16 nCount = i_xAttribs->getLength(); 411*cdf0e10cSrcweir for (sal_Int16 i = 0; i < nCount; ++i) { 412*cdf0e10cSrcweir const ::rtl::OUString name(i_xAttribs->getNameByIndex(i)); 413*cdf0e10cSrcweir if (name.matchAsciiL(s_xmlns, strlen(s_xmlns))) { 414*cdf0e10cSrcweir bool found(false); 415*cdf0e10cSrcweir const SvXMLNamespaceMap & rNsMap(mrExport.GetNamespaceMap()); 416*cdf0e10cSrcweir for (sal_uInt16 key = rNsMap.GetFirstKey(); 417*cdf0e10cSrcweir key != USHRT_MAX; key = rNsMap.GetNextKey(key)) { 418*cdf0e10cSrcweir if (name.equals(rNsMap.GetAttrNameByKey(key))) { 419*cdf0e10cSrcweir found = true; 420*cdf0e10cSrcweir break; 421*cdf0e10cSrcweir } 422*cdf0e10cSrcweir } 423*cdf0e10cSrcweir if (!found) { 424*cdf0e10cSrcweir m_preservedNSs.push_back(beans::StringPair(name, 425*cdf0e10cSrcweir i_xAttribs->getValueByIndex(i))); 426*cdf0e10cSrcweir } 427*cdf0e10cSrcweir } 428*cdf0e10cSrcweir } 429*cdf0e10cSrcweir // ignore the root: it has been written already 430*cdf0e10cSrcweir ++m_level; 431*cdf0e10cSrcweir return; 432*cdf0e10cSrcweir } 433*cdf0e10cSrcweir 434*cdf0e10cSrcweir if (m_level == 1) { 435*cdf0e10cSrcweir // attach preserved namespace decls from root node here 436*cdf0e10cSrcweir for (std::vector<beans::StringPair>::const_iterator iter = 437*cdf0e10cSrcweir m_preservedNSs.begin(); iter != m_preservedNSs.end(); ++iter) { 438*cdf0e10cSrcweir const ::rtl::OUString ns(iter->First); 439*cdf0e10cSrcweir bool found(false); 440*cdf0e10cSrcweir // but only if it is not already there 441*cdf0e10cSrcweir const sal_Int16 nCount = i_xAttribs->getLength(); 442*cdf0e10cSrcweir for (sal_Int16 i = 0; i < nCount; ++i) { 443*cdf0e10cSrcweir const ::rtl::OUString name(i_xAttribs->getNameByIndex(i)); 444*cdf0e10cSrcweir if (ns.equals(name)) { 445*cdf0e10cSrcweir found = true; 446*cdf0e10cSrcweir break; 447*cdf0e10cSrcweir } 448*cdf0e10cSrcweir } 449*cdf0e10cSrcweir if (!found) { 450*cdf0e10cSrcweir mrExport.AddAttribute(ns, iter->Second); 451*cdf0e10cSrcweir } 452*cdf0e10cSrcweir } 453*cdf0e10cSrcweir } 454*cdf0e10cSrcweir 455*cdf0e10cSrcweir // attach the attributes 456*cdf0e10cSrcweir if (i_rName.matchAsciiL(s_meta, strlen(s_meta))) { 457*cdf0e10cSrcweir // special handling for all elements that may have 458*cdf0e10cSrcweir // xlink:href attributes; these must be made relative 459*cdf0e10cSrcweir const sal_Int16 nLength = i_xAttribs->getLength(); 460*cdf0e10cSrcweir for (sal_Int16 i = 0; i < nLength; ++i) { 461*cdf0e10cSrcweir const ::rtl::OUString name (i_xAttribs->getNameByIndex (i)); 462*cdf0e10cSrcweir ::rtl::OUString value(i_xAttribs->getValueByIndex(i)); 463*cdf0e10cSrcweir if (name.matchAsciiL(s_href, strlen(s_href))) { 464*cdf0e10cSrcweir value = mrExport.GetRelativeReference(value); 465*cdf0e10cSrcweir } 466*cdf0e10cSrcweir mrExport.AddAttribute(name, value); 467*cdf0e10cSrcweir } 468*cdf0e10cSrcweir } else { 469*cdf0e10cSrcweir const sal_Int16 nLength = i_xAttribs->getLength(); 470*cdf0e10cSrcweir for (sal_Int16 i = 0; i < nLength; ++i) { 471*cdf0e10cSrcweir const ::rtl::OUString name (i_xAttribs->getNameByIndex(i)); 472*cdf0e10cSrcweir const ::rtl::OUString value (i_xAttribs->getValueByIndex(i)); 473*cdf0e10cSrcweir mrExport.AddAttribute(name, value); 474*cdf0e10cSrcweir } 475*cdf0e10cSrcweir } 476*cdf0e10cSrcweir 477*cdf0e10cSrcweir // finally, start the element 478*cdf0e10cSrcweir // #i107240# no whitespace here, because the DOM may already contain 479*cdf0e10cSrcweir // whitespace, which is not cleared when loading and thus accumulates. 480*cdf0e10cSrcweir mrExport.StartElement(i_rName, (m_level > 1) ? sal_False : sal_True); 481*cdf0e10cSrcweir ++m_level; 482*cdf0e10cSrcweir } 483*cdf0e10cSrcweir 484*cdf0e10cSrcweir void SAL_CALL 485*cdf0e10cSrcweir SvXMLMetaExport::endElement(const ::rtl::OUString & i_rName) 486*cdf0e10cSrcweir throw (uno::RuntimeException, xml::sax::SAXException) 487*cdf0e10cSrcweir { 488*cdf0e10cSrcweir --m_level; 489*cdf0e10cSrcweir if (m_level == 0) { 490*cdf0e10cSrcweir // ignore the root; see startElement 491*cdf0e10cSrcweir return; 492*cdf0e10cSrcweir } 493*cdf0e10cSrcweir DBG_ASSERT( m_level >= 0, "SvXMLMetaExport: level error" ); 494*cdf0e10cSrcweir mrExport.EndElement(i_rName, sal_False); 495*cdf0e10cSrcweir } 496*cdf0e10cSrcweir 497*cdf0e10cSrcweir void SAL_CALL 498*cdf0e10cSrcweir SvXMLMetaExport::characters(const ::rtl::OUString & i_rChars) 499*cdf0e10cSrcweir throw (uno::RuntimeException, xml::sax::SAXException) 500*cdf0e10cSrcweir { 501*cdf0e10cSrcweir mrExport.Characters(i_rChars); 502*cdf0e10cSrcweir } 503*cdf0e10cSrcweir 504*cdf0e10cSrcweir void SAL_CALL 505*cdf0e10cSrcweir SvXMLMetaExport::ignorableWhitespace(const ::rtl::OUString & /*i_rWhitespaces*/) 506*cdf0e10cSrcweir throw (uno::RuntimeException, xml::sax::SAXException) 507*cdf0e10cSrcweir { 508*cdf0e10cSrcweir mrExport.IgnorableWhitespace(/*i_rWhitespaces*/); 509*cdf0e10cSrcweir } 510*cdf0e10cSrcweir 511*cdf0e10cSrcweir void SAL_CALL 512*cdf0e10cSrcweir SvXMLMetaExport::processingInstruction(const ::rtl::OUString & i_rTarget, 513*cdf0e10cSrcweir const ::rtl::OUString & i_rData) 514*cdf0e10cSrcweir throw (uno::RuntimeException, xml::sax::SAXException) 515*cdf0e10cSrcweir { 516*cdf0e10cSrcweir // ignore; the exporter cannot handle these 517*cdf0e10cSrcweir (void) i_rTarget; 518*cdf0e10cSrcweir (void) i_rData; 519*cdf0e10cSrcweir } 520*cdf0e10cSrcweir 521*cdf0e10cSrcweir void SAL_CALL 522*cdf0e10cSrcweir SvXMLMetaExport::setDocumentLocator(const uno::Reference<xml::sax::XLocator>&) 523*cdf0e10cSrcweir throw (uno::RuntimeException, xml::sax::SAXException) 524*cdf0e10cSrcweir { 525*cdf0e10cSrcweir // nothing to do here, move along... 526*cdf0e10cSrcweir } 527*cdf0e10cSrcweir 528*cdf0e10cSrcweir 529