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 29*cdf0e10cSrcweir #ifndef _XMLOFF_XMLPROPERTYBACKPATCHER_HXX 30*cdf0e10cSrcweir #define _XMLOFF_XMLPROPERTYBACKPATCHER_HXX 31*cdf0e10cSrcweir 32*cdf0e10cSrcweir #ifndef __SGI_STL_MAP 33*cdf0e10cSrcweir #include <map> 34*cdf0e10cSrcweir #endif 35*cdf0e10cSrcweir 36*cdf0e10cSrcweir #ifndef __SGI_STL_VECTOR 37*cdf0e10cSrcweir #include <vector> 38*cdf0e10cSrcweir #endif 39*cdf0e10cSrcweir #include <comphelper/stl_types.hxx> 40*cdf0e10cSrcweir 41*cdf0e10cSrcweir namespace rtl { class OUString; } 42*cdf0e10cSrcweir namespace com { namespace sun { namespace star { 43*cdf0e10cSrcweir namespace beans { class XPropertySet; } 44*cdf0e10cSrcweir namespace uno { template<class A> class Reference; } 45*cdf0e10cSrcweir } } } 46*cdf0e10cSrcweir 47*cdf0e10cSrcweir 48*cdf0e10cSrcweir /** This class maintains an OUString->sal_Int16 mapping for cases in 49*cdf0e10cSrcweir * which an XPropertySet needs to be filled with values that are not 50*cdf0e10cSrcweir * yet known. 51*cdf0e10cSrcweir * 52*cdf0e10cSrcweir * A good example for appropriate use are footnotes and references to 53*cdf0e10cSrcweir * footnoes. Internally, the StarOffice API numbers footnotes, and 54*cdf0e10cSrcweir * references to footnotes refer to that internal numbering. In the 55*cdf0e10cSrcweir * XML file format, these numbers are replaced with name strings. Now 56*cdf0e10cSrcweir * if during import of a document a reference to a footnote is 57*cdf0e10cSrcweir * encountered, two things can happen: 1) The footnote already 58*cdf0e10cSrcweir * appeared in the document. In this case the name is already known 59*cdf0e10cSrcweir * and the proper ID can be requested from the footnote. 2) The 60*cdf0e10cSrcweir * footnote will appear later in the document. In this case the ID is 61*cdf0e10cSrcweir * not yet known, and the reference-ID property of the reference 62*cdf0e10cSrcweir * cannot be determined. Hence, the reference has to be stored and the 63*cdf0e10cSrcweir * ID needs to bet set later, when the footnote is eventually found in 64*cdf0e10cSrcweir * the document. 65*cdf0e10cSrcweir * 66*cdf0e10cSrcweir * This class simplifies this process: If the footnote is found, 67*cdf0e10cSrcweir * ResolveId with the XML name and the ID is called. When a reference 68*cdf0e10cSrcweir * is encountered, SetProperty gets called with the reference's 69*cdf0e10cSrcweir * XPropertySet and the XML name. All remaining tasks are handled by 70*cdf0e10cSrcweir * the class. 71*cdf0e10cSrcweir */ 72*cdf0e10cSrcweir template <class A> 73*cdf0e10cSrcweir class XMLPropertyBackpatcher 74*cdf0e10cSrcweir { 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir /// name of property that gets set or backpatched 77*cdf0e10cSrcweir ::rtl::OUString sPropertyName; 78*cdf0e10cSrcweir 79*cdf0e10cSrcweir /// should a default value be set for unresolved properties 80*cdf0e10cSrcweir sal_Bool bDefaultHandling; 81*cdf0e10cSrcweir 82*cdf0e10cSrcweir /// should the sPreservePropertyName be preserved 83*cdf0e10cSrcweir sal_Bool bPreserveProperty; 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir /// name of the property to preserve 86*cdf0e10cSrcweir ::rtl::OUString sPreservePropertyName; 87*cdf0e10cSrcweir 88*cdf0e10cSrcweir /// default value for unresolved properties (if bDefaultHandling) 89*cdf0e10cSrcweir A aDefault; 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir /// backpatch list type 92*cdf0e10cSrcweir typedef ::std::vector< 93*cdf0e10cSrcweir ::com::sun::star::uno::Reference< 94*cdf0e10cSrcweir ::com::sun::star::beans::XPropertySet> > BackpatchListType; 95*cdf0e10cSrcweir 96*cdf0e10cSrcweir /* use void* instead of BackpatchListType to avoid linker problems 97*cdf0e10cSrcweir with long typenames. The real typename (commented out) contains 98*cdf0e10cSrcweir >1200 chars. */ 99*cdf0e10cSrcweir 100*cdf0e10cSrcweir /// backpatch list for unresolved IDs 101*cdf0e10cSrcweir //::std::map<const ::rtl::OUString, BackpatchListType*> aBackpatchListMap; 102*cdf0e10cSrcweir ::std::map<const ::rtl::OUString, void*, ::comphelper::UStringLess> aBackpatchListMap; 103*cdf0e10cSrcweir 104*cdf0e10cSrcweir /// mapping of names -> IDs 105*cdf0e10cSrcweir ::std::map<const ::rtl::OUString, A, ::comphelper::UStringLess> aIDMap; 106*cdf0e10cSrcweir 107*cdf0e10cSrcweir public: 108*cdf0e10cSrcweir 109*cdf0e10cSrcweir XMLPropertyBackpatcher( 110*cdf0e10cSrcweir const ::rtl::OUString& sPropertyName); 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir XMLPropertyBackpatcher( 113*cdf0e10cSrcweir const ::rtl::OUString& sPropertyName, 114*cdf0e10cSrcweir const ::rtl::OUString& sPreservePropertyName, 115*cdf0e10cSrcweir sal_Bool bDefault, 116*cdf0e10cSrcweir A aDef); 117*cdf0e10cSrcweir 118*cdf0e10cSrcweir XMLPropertyBackpatcher( 119*cdf0e10cSrcweir const sal_Char* pPropertyName); 120*cdf0e10cSrcweir 121*cdf0e10cSrcweir XMLPropertyBackpatcher( 122*cdf0e10cSrcweir const sal_Char* pPropertyName, 123*cdf0e10cSrcweir const sal_Char* pPreservePropertyName, 124*cdf0e10cSrcweir sal_Bool bDefault, 125*cdf0e10cSrcweir A aDef); 126*cdf0e10cSrcweir 127*cdf0e10cSrcweir ~XMLPropertyBackpatcher(); 128*cdf0e10cSrcweir 129*cdf0e10cSrcweir /// resolve a known ID. 130*cdf0e10cSrcweir /// Call this as soon as the value for a particular name is known. 131*cdf0e10cSrcweir void ResolveId( 132*cdf0e10cSrcweir const ::rtl::OUString& sName, 133*cdf0e10cSrcweir A aValue); 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir /// Set property with the proper value for this name. If the value 136*cdf0e10cSrcweir /// is not yet known, store the XPropertySet in the backpatch list. 137*cdf0e10cSrcweir /// Use this whenever the value should be set, even if it is not yet known. 138*cdf0e10cSrcweir /// const version 139*cdf0e10cSrcweir void SetProperty( 140*cdf0e10cSrcweir const ::com::sun::star::uno::Reference< 141*cdf0e10cSrcweir ::com::sun::star::beans::XPropertySet> & xPropSet, 142*cdf0e10cSrcweir const ::rtl::OUString& sName); 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir /// non-const version of SetProperty 145*cdf0e10cSrcweir void SetProperty( 146*cdf0e10cSrcweir ::com::sun::star::uno::Reference< 147*cdf0e10cSrcweir ::com::sun::star::beans::XPropertySet> & xPropSet, 148*cdf0e10cSrcweir const ::rtl::OUString& sName); 149*cdf0e10cSrcweir 150*cdf0e10cSrcweir /// set default (if bDefaultHandling) for unresolved names 151*cdf0e10cSrcweir /// called by destructor 152*cdf0e10cSrcweir void SetDefault(); 153*cdf0e10cSrcweir 154*cdf0e10cSrcweir }; 155*cdf0e10cSrcweir 156*cdf0e10cSrcweir #endif 157