1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 
29 #ifndef _XMLOFF_XMLPROPERTYBACKPATCHER_HXX
30 #define _XMLOFF_XMLPROPERTYBACKPATCHER_HXX
31 
32 #ifndef __SGI_STL_MAP
33 #include <map>
34 #endif
35 
36 #ifndef __SGI_STL_VECTOR
37 #include <vector>
38 #endif
39 #include <comphelper/stl_types.hxx>
40 
41 namespace rtl { class OUString; }
42 namespace com { namespace sun { namespace star {
43 	namespace beans { class XPropertySet; }
44 	namespace uno { template<class A> class Reference; }
45 } } }
46 
47 
48 /** This class maintains an OUString->sal_Int16 mapping for cases in
49  * which an XPropertySet needs to be filled with values that are not
50  * yet known.
51  *
52  * A good example for appropriate use are footnotes and references to
53  * footnoes. Internally, the StarOffice API numbers footnotes, and
54  * references to footnotes refer to that internal numbering. In the
55  * XML file format, these numbers are replaced with name strings. Now
56  * if during import of a document a reference to a footnote is
57  * encountered, two things can happen: 1) The footnote already
58  * appeared in the document. In this case the name is already known
59  * and the proper ID can be requested from the footnote. 2) The
60  * footnote will appear later in the document. In this case the ID is
61  * not yet known, and the reference-ID property of the reference
62  * cannot be determined. Hence, the reference has to be stored and the
63  * ID needs to bet set later, when the footnote is eventually found in
64  * the document.
65  *
66  * This class simplifies this process: If the footnote is found,
67  * ResolveId with the XML name and the ID is called. When a reference
68  * is encountered, SetProperty gets called with the reference's
69  * XPropertySet and the XML name. All remaining tasks are handled by
70  * the class.
71  */
72 template <class A>
73 class XMLPropertyBackpatcher
74 {
75 
76 	/// name of property that gets set or backpatched
77  	::rtl::OUString sPropertyName;
78 
79 	/// should a default value be set for unresolved properties
80 	sal_Bool bDefaultHandling;
81 
82 	/// should the sPreservePropertyName be preserved
83 	sal_Bool bPreserveProperty;
84 
85 	/// name of the property to preserve
86 	::rtl::OUString sPreservePropertyName;
87 
88 	/// default value for unresolved properties (if bDefaultHandling)
89 	A aDefault;
90 
91 	/// backpatch list type
92 	typedef ::std::vector<
93 				::com::sun::star::uno::Reference<
94 					::com::sun::star::beans::XPropertySet> > BackpatchListType;
95 
96 	/* use void* instead of BackpatchListType to avoid linker problems
97        with long typenames. The real typename (commented out) contains
98        >1200 chars. */
99 
100 	/// backpatch list for unresolved IDs
101 	//::std::map<const ::rtl::OUString, BackpatchListType*> aBackpatchListMap;
102 	::std::map<const ::rtl::OUString, void*, ::comphelper::UStringLess> aBackpatchListMap;
103 
104 	/// mapping of names -> IDs
105 	::std::map<const ::rtl::OUString, A, ::comphelper::UStringLess> aIDMap;
106 
107 public:
108 
109 	XMLPropertyBackpatcher(
110 		const ::rtl::OUString& sPropertyName);
111 
112 	XMLPropertyBackpatcher(
113 		const ::rtl::OUString& sPropertyName,
114 		const ::rtl::OUString& sPreservePropertyName,
115 		sal_Bool bDefault,
116 		A aDef);
117 
118 	XMLPropertyBackpatcher(
119 		const sal_Char* pPropertyName);
120 
121 	XMLPropertyBackpatcher(
122 		const sal_Char* pPropertyName,
123 		const sal_Char* pPreservePropertyName,
124 		sal_Bool bDefault,
125 		A aDef);
126 
127 	~XMLPropertyBackpatcher();
128 
129 	/// resolve a known ID.
130 	/// Call this as soon as the value for a particular name is known.
131 	void ResolveId(
132 		const ::rtl::OUString& sName,
133 		A aValue);
134 
135 	/// Set property with the proper value for this name. If the value
136 	/// is not yet known, store the XPropertySet in the backpatch list.
137 	/// Use this whenever the value should be set, even if it is not yet known.
138 	/// const version
139 	void SetProperty(
140 		const ::com::sun::star::uno::Reference<
141 				::com::sun::star::beans::XPropertySet> & xPropSet,
142 		const ::rtl::OUString& sName);
143 
144 	/// non-const version of SetProperty
145 	void SetProperty(
146 		::com::sun::star::uno::Reference<
147 				::com::sun::star::beans::XPropertySet> & xPropSet,
148 		const ::rtl::OUString& sName);
149 
150 	/// set default (if bDefaultHandling) for unresolved names
151 	/// called by destructor
152 	void SetDefault();
153 
154 };
155 
156 #endif
157