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