1*ecfe53c5SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*ecfe53c5SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*ecfe53c5SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*ecfe53c5SAndrew Rist * distributed with this work for additional information 6*ecfe53c5SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*ecfe53c5SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*ecfe53c5SAndrew Rist * "License"); you may not use this file except in compliance 9*ecfe53c5SAndrew Rist * with the License. You may obtain a copy of the License at 10*ecfe53c5SAndrew Rist * 11*ecfe53c5SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*ecfe53c5SAndrew Rist * 13*ecfe53c5SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*ecfe53c5SAndrew Rist * software distributed under the License is distributed on an 15*ecfe53c5SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*ecfe53c5SAndrew Rist * KIND, either express or implied. See the License for the 17*ecfe53c5SAndrew Rist * specific language governing permissions and limitations 18*ecfe53c5SAndrew Rist * under the License. 19*ecfe53c5SAndrew Rist * 20*ecfe53c5SAndrew Rist *************************************************************/ 21*ecfe53c5SAndrew Rist 22*ecfe53c5SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir 25cdf0e10cSrcweir #ifndef _XMLOFF_XMLPROPERTYBACKPATCHER_HXX 26cdf0e10cSrcweir #define _XMLOFF_XMLPROPERTYBACKPATCHER_HXX 27cdf0e10cSrcweir 28cdf0e10cSrcweir #ifndef __SGI_STL_MAP 29cdf0e10cSrcweir #include <map> 30cdf0e10cSrcweir #endif 31cdf0e10cSrcweir 32cdf0e10cSrcweir #ifndef __SGI_STL_VECTOR 33cdf0e10cSrcweir #include <vector> 34cdf0e10cSrcweir #endif 35cdf0e10cSrcweir #include <comphelper/stl_types.hxx> 36cdf0e10cSrcweir 37cdf0e10cSrcweir namespace rtl { class OUString; } 38cdf0e10cSrcweir namespace com { namespace sun { namespace star { 39cdf0e10cSrcweir namespace beans { class XPropertySet; } 40cdf0e10cSrcweir namespace uno { template<class A> class Reference; } 41cdf0e10cSrcweir } } } 42cdf0e10cSrcweir 43cdf0e10cSrcweir 44cdf0e10cSrcweir /** This class maintains an OUString->sal_Int16 mapping for cases in 45cdf0e10cSrcweir * which an XPropertySet needs to be filled with values that are not 46cdf0e10cSrcweir * yet known. 47cdf0e10cSrcweir * 48cdf0e10cSrcweir * A good example for appropriate use are footnotes and references to 49cdf0e10cSrcweir * footnoes. Internally, the StarOffice API numbers footnotes, and 50cdf0e10cSrcweir * references to footnotes refer to that internal numbering. In the 51cdf0e10cSrcweir * XML file format, these numbers are replaced with name strings. Now 52cdf0e10cSrcweir * if during import of a document a reference to a footnote is 53cdf0e10cSrcweir * encountered, two things can happen: 1) The footnote already 54cdf0e10cSrcweir * appeared in the document. In this case the name is already known 55cdf0e10cSrcweir * and the proper ID can be requested from the footnote. 2) The 56cdf0e10cSrcweir * footnote will appear later in the document. In this case the ID is 57cdf0e10cSrcweir * not yet known, and the reference-ID property of the reference 58cdf0e10cSrcweir * cannot be determined. Hence, the reference has to be stored and the 59cdf0e10cSrcweir * ID needs to bet set later, when the footnote is eventually found in 60cdf0e10cSrcweir * the document. 61cdf0e10cSrcweir * 62cdf0e10cSrcweir * This class simplifies this process: If the footnote is found, 63cdf0e10cSrcweir * ResolveId with the XML name and the ID is called. When a reference 64cdf0e10cSrcweir * is encountered, SetProperty gets called with the reference's 65cdf0e10cSrcweir * XPropertySet and the XML name. All remaining tasks are handled by 66cdf0e10cSrcweir * the class. 67cdf0e10cSrcweir */ 68cdf0e10cSrcweir template <class A> 69cdf0e10cSrcweir class XMLPropertyBackpatcher 70cdf0e10cSrcweir { 71cdf0e10cSrcweir 72cdf0e10cSrcweir /// name of property that gets set or backpatched 73cdf0e10cSrcweir ::rtl::OUString sPropertyName; 74cdf0e10cSrcweir 75cdf0e10cSrcweir /// should a default value be set for unresolved properties 76cdf0e10cSrcweir sal_Bool bDefaultHandling; 77cdf0e10cSrcweir 78cdf0e10cSrcweir /// should the sPreservePropertyName be preserved 79cdf0e10cSrcweir sal_Bool bPreserveProperty; 80cdf0e10cSrcweir 81cdf0e10cSrcweir /// name of the property to preserve 82cdf0e10cSrcweir ::rtl::OUString sPreservePropertyName; 83cdf0e10cSrcweir 84cdf0e10cSrcweir /// default value for unresolved properties (if bDefaultHandling) 85cdf0e10cSrcweir A aDefault; 86cdf0e10cSrcweir 87cdf0e10cSrcweir /// backpatch list type 88cdf0e10cSrcweir typedef ::std::vector< 89cdf0e10cSrcweir ::com::sun::star::uno::Reference< 90cdf0e10cSrcweir ::com::sun::star::beans::XPropertySet> > BackpatchListType; 91cdf0e10cSrcweir 92cdf0e10cSrcweir /* use void* instead of BackpatchListType to avoid linker problems 93cdf0e10cSrcweir with long typenames. The real typename (commented out) contains 94cdf0e10cSrcweir >1200 chars. */ 95cdf0e10cSrcweir 96cdf0e10cSrcweir /// backpatch list for unresolved IDs 97cdf0e10cSrcweir //::std::map<const ::rtl::OUString, BackpatchListType*> aBackpatchListMap; 98cdf0e10cSrcweir ::std::map<const ::rtl::OUString, void*, ::comphelper::UStringLess> aBackpatchListMap; 99cdf0e10cSrcweir 100cdf0e10cSrcweir /// mapping of names -> IDs 101cdf0e10cSrcweir ::std::map<const ::rtl::OUString, A, ::comphelper::UStringLess> aIDMap; 102cdf0e10cSrcweir 103cdf0e10cSrcweir public: 104cdf0e10cSrcweir 105cdf0e10cSrcweir XMLPropertyBackpatcher( 106cdf0e10cSrcweir const ::rtl::OUString& sPropertyName); 107cdf0e10cSrcweir 108cdf0e10cSrcweir XMLPropertyBackpatcher( 109cdf0e10cSrcweir const ::rtl::OUString& sPropertyName, 110cdf0e10cSrcweir const ::rtl::OUString& sPreservePropertyName, 111cdf0e10cSrcweir sal_Bool bDefault, 112cdf0e10cSrcweir A aDef); 113cdf0e10cSrcweir 114cdf0e10cSrcweir XMLPropertyBackpatcher( 115cdf0e10cSrcweir const sal_Char* pPropertyName); 116cdf0e10cSrcweir 117cdf0e10cSrcweir XMLPropertyBackpatcher( 118cdf0e10cSrcweir const sal_Char* pPropertyName, 119cdf0e10cSrcweir const sal_Char* pPreservePropertyName, 120cdf0e10cSrcweir sal_Bool bDefault, 121cdf0e10cSrcweir A aDef); 122cdf0e10cSrcweir 123cdf0e10cSrcweir ~XMLPropertyBackpatcher(); 124cdf0e10cSrcweir 125cdf0e10cSrcweir /// resolve a known ID. 126cdf0e10cSrcweir /// Call this as soon as the value for a particular name is known. 127cdf0e10cSrcweir void ResolveId( 128cdf0e10cSrcweir const ::rtl::OUString& sName, 129cdf0e10cSrcweir A aValue); 130cdf0e10cSrcweir 131cdf0e10cSrcweir /// Set property with the proper value for this name. If the value 132cdf0e10cSrcweir /// is not yet known, store the XPropertySet in the backpatch list. 133cdf0e10cSrcweir /// Use this whenever the value should be set, even if it is not yet known. 134cdf0e10cSrcweir /// const version 135cdf0e10cSrcweir void SetProperty( 136cdf0e10cSrcweir const ::com::sun::star::uno::Reference< 137cdf0e10cSrcweir ::com::sun::star::beans::XPropertySet> & xPropSet, 138cdf0e10cSrcweir const ::rtl::OUString& sName); 139cdf0e10cSrcweir 140cdf0e10cSrcweir /// non-const version of SetProperty 141cdf0e10cSrcweir void SetProperty( 142cdf0e10cSrcweir ::com::sun::star::uno::Reference< 143cdf0e10cSrcweir ::com::sun::star::beans::XPropertySet> & xPropSet, 144cdf0e10cSrcweir const ::rtl::OUString& sName); 145cdf0e10cSrcweir 146cdf0e10cSrcweir /// set default (if bDefaultHandling) for unresolved names 147cdf0e10cSrcweir /// called by destructor 148cdf0e10cSrcweir void SetDefault(); 149cdf0e10cSrcweir 150cdf0e10cSrcweir }; 151cdf0e10cSrcweir 152cdf0e10cSrcweir #endif 153