xref: /aoo41x/main/sfx2/source/doc/Metadatable.cxx (revision d119d52d)
1*d119d52dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*d119d52dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*d119d52dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*d119d52dSAndrew Rist  * distributed with this work for additional information
6*d119d52dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*d119d52dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*d119d52dSAndrew Rist  * "License"); you may not use this file except in compliance
9*d119d52dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*d119d52dSAndrew Rist  *
11*d119d52dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*d119d52dSAndrew Rist  *
13*d119d52dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*d119d52dSAndrew Rist  * software distributed under the License is distributed on an
15*d119d52dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*d119d52dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*d119d52dSAndrew Rist  * specific language governing permissions and limitations
18*d119d52dSAndrew Rist  * under the License.
19*d119d52dSAndrew Rist  *
20*d119d52dSAndrew Rist  *************************************************************/
21*d119d52dSAndrew Rist 
22*d119d52dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "precompiled_sfx2.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <sfx2/Metadatable.hxx>
27cdf0e10cSrcweir #include <sfx2/XmlIdRegistry.hxx>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <vos/mutex.hxx>
30cdf0e10cSrcweir #include <vcl/svapp.hxx> // solarmutex
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <rtl/random.h>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <boost/bind.hpp>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include <memory>
37cdf0e10cSrcweir #include <hash_map>
38cdf0e10cSrcweir #include <list>
39cdf0e10cSrcweir #include <algorithm>
40cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
41cdf0e10cSrcweir #include <typeinfo>
42cdf0e10cSrcweir #endif
43cdf0e10cSrcweir 
44cdf0e10cSrcweir 
45cdf0e10cSrcweir /** XML ID handling.
46cdf0e10cSrcweir 
47cdf0e10cSrcweir     There is an abstract base class <type>XmlIdRegistry</type>, with
48cdf0e10cSrcweir     2 subclasses <type>XmlIdRegistryDocument</type> for "normal" documents,
49cdf0e10cSrcweir     and <type>XmlIdRegistryClipboard</type> for clipboard documents.
50cdf0e10cSrcweir     These classes are responsible for managing XML IDs for all elements
51cdf0e10cSrcweir     of the model. Only the implementation of the <type>Metadatable</type>
52cdf0e10cSrcweir     base class needs to know the registries, so they are not in the header.
53cdf0e10cSrcweir 
54cdf0e10cSrcweir     The handling of XML IDs differs between clipboard and non-clipboard
55cdf0e10cSrcweir     documents in several aspects. Most importantly, non-clipboard documents
56cdf0e10cSrcweir     can have several elements associated with one XML ID.
57cdf0e10cSrcweir     This is necessary because of the weird undo implementation:
58cdf0e10cSrcweir     deleting a text node moves the deleted node to the undo array, but
59cdf0e10cSrcweir     executing undo will then create a <em>copy</em> of that node in the
60cdf0e10cSrcweir     document array. These 2 nodes must have the same XML ID, because
61cdf0e10cSrcweir     we cannot know whether the user will do a redo next, or something else.
62cdf0e10cSrcweir 
63cdf0e10cSrcweir     Because we need to have a mechanism for several objects per XML ID anyway,
64cdf0e10cSrcweir     we use that also to enable some usability features:
65cdf0e10cSrcweir     The document registry has a list of Metadatables per XML ID.
66cdf0e10cSrcweir     This list is sorted by priority, i.e., the first element has highest
67cdf0e10cSrcweir     priority. When inserting copies, care must be taken that they are inserted
68cdf0e10cSrcweir     at the right position: either before or after the source.
69cdf0e10cSrcweir     This is done by <method>Metadatable::RegisterAsCopyOf</method>.
70cdf0e10cSrcweir     When a text node is split, then both resulting text nodes are inserted
71cdf0e10cSrcweir     into the list. If the user then deletes one text node, the other one
72cdf0e10cSrcweir     will have the XML ID.
73cdf0e10cSrcweir     Also, when a Metadatable is copied to the clipboard and then pasted,
74cdf0e10cSrcweir     the copy is inserted into the list. If the user then deletes the source,
75cdf0e10cSrcweir     the XML ID is not lost.
76cdf0e10cSrcweir     The goal is that it should be hard to lose an XML ID by accident, which
77cdf0e10cSrcweir     is especially important as long as we do not have an UI that displays them.
78cdf0e10cSrcweir 
79cdf0e10cSrcweir     There are two subclasses of <type>Metadatable</type>:
80cdf0e10cSrcweir     <ul><li><type>MetadatableClipboard</type>: for copies in the clipboard</li>
81cdf0e10cSrcweir         <li><type>MetadatableUndo</type>: for undo, because a Metadatable
82cdf0e10cSrcweir         may be destroyed on delete and a new one created on undo.</li></ul>
83cdf0e10cSrcweir     These serve only to track the position in an XML ID list in a document
84cdf0e10cSrcweir     registry, so that future actions can insert objects at the right position.
85cdf0e10cSrcweir     Unfortunately, inserting dummy objects seems to be necessary:
86cdf0e10cSrcweir     <ul><li>it is not sufficent to just remember the saved id, because then
87cdf0e10cSrcweir             the relative priorities might change when executing the undo</li>
88cdf0e10cSrcweir         <li>it is not sufficient to record the position as an integer, because
89cdf0e10cSrcweir             if we delete a text node and then undo, the node will be copied(!),
90cdf0e10cSrcweir             and we will have one more node in the list.<li>
91cdf0e10cSrcweir         <li>it is not sufficient to record the pointer of the previous/next
92cdf0e10cSrcweir             Metadatable, because if we delete a text node, undo, and then
93cdf0e10cSrcweir             do something to clear the redo array, the original text node is
94cdf0e10cSrcweir             destroyed, and is replaced by the copy created by undo</li></ul>
95cdf0e10cSrcweir 
96cdf0e10cSrcweir     If content from a non-clipboard document is copied into a clipboard
97cdf0e10cSrcweir     document, a dummy <type>MetadatableClipboard</type> is inserted into the
98cdf0e10cSrcweir     non-clipboard document registry in order to track the position of the
99cdf0e10cSrcweir     source element.  When the clipboard content is pasted back into the source
100cdf0e10cSrcweir     document, this dummy object is used to associate the pasted element with
101cdf0e10cSrcweir     that same XML ID.
102cdf0e10cSrcweir 
103cdf0e10cSrcweir     If a <type>Metadatable</type> is deleted or merged,
104cdf0e10cSrcweir     <method>Metadatable::CreateUndo</method> is called, and returns a
105cdf0e10cSrcweir     <type>MetadatableUndo<type> instance, which can be used to undo the action
106cdf0e10cSrcweir     by passing it to <method>Metadatable::RestoreMetadata</method>.
107cdf0e10cSrcweir 
108cdf0e10cSrcweir     @author mst
109cdf0e10cSrcweir  */
110cdf0e10cSrcweir 
111cdf0e10cSrcweir 
112cdf0e10cSrcweir using namespace ::com::sun::star;
113cdf0e10cSrcweir 
114cdf0e10cSrcweir using ::sfx2::isValidXmlId;
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 
117cdf0e10cSrcweir namespace sfx2 {
118cdf0e10cSrcweir 
119cdf0e10cSrcweir static const char s_content [] = "content.xml";
120cdf0e10cSrcweir static const char s_styles  [] = "styles.xml";
121cdf0e10cSrcweir static const char s_prefix  [] = "id";  // prefix for generated xml:id
122cdf0e10cSrcweir 
isContentFile(::rtl::OUString const & i_rPath)123cdf0e10cSrcweir static bool isContentFile(::rtl::OUString const & i_rPath)
124cdf0e10cSrcweir {
125cdf0e10cSrcweir     return i_rPath.equalsAscii(s_content);
126cdf0e10cSrcweir }
127cdf0e10cSrcweir 
isStylesFile(::rtl::OUString const & i_rPath)128cdf0e10cSrcweir static bool isStylesFile (::rtl::OUString const & i_rPath)
129cdf0e10cSrcweir {
130cdf0e10cSrcweir     return i_rPath.equalsAscii(s_styles);
131cdf0e10cSrcweir }
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 
134cdf0e10cSrcweir //=============================================================================
135cdf0e10cSrcweir // XML ID handling ---------------------------------------------------
136cdf0e10cSrcweir 
137cdf0e10cSrcweir /** handles registration of XMetadatable.
138cdf0e10cSrcweir 
139cdf0e10cSrcweir     This class is responsible for guaranteeing that XMetadatable objects
140cdf0e10cSrcweir     always have XML IDs that are unique within a stream.
141cdf0e10cSrcweir 
142cdf0e10cSrcweir     This is an abstract base class; see subclasses XmlIdRegistryDocument and
143cdf0e10cSrcweir     XmlIdRegistryClipboard.
144cdf0e10cSrcweir 
145cdf0e10cSrcweir     @see SwDoc::GetXmlIdRegistry
146cdf0e10cSrcweir     @see SwDocShell::GetXmlIdRegistry
147cdf0e10cSrcweir  */
148cdf0e10cSrcweir class XmlIdRegistry : public sfx2::IXmlIdRegistry
149cdf0e10cSrcweir {
150cdf0e10cSrcweir 
151cdf0e10cSrcweir public:
152cdf0e10cSrcweir     XmlIdRegistry();
153cdf0e10cSrcweir 
154cdf0e10cSrcweir     virtual ~XmlIdRegistry();
155cdf0e10cSrcweir 
156cdf0e10cSrcweir     /** get the ODF element with the given metadata reference. */
157cdf0e10cSrcweir     virtual ::com::sun::star::uno::Reference<
158cdf0e10cSrcweir             ::com::sun::star::rdf::XMetadatable > SAL_CALL
159cdf0e10cSrcweir         GetElementByMetadataReference(
160cdf0e10cSrcweir             const ::com::sun::star::beans::StringPair & i_rReference) const;
161cdf0e10cSrcweir 
162cdf0e10cSrcweir     /** register an ODF element at a newly generated, unique metadata reference.
163cdf0e10cSrcweir 
164cdf0e10cSrcweir         <p>
165cdf0e10cSrcweir         Find a fresh XML ID, and register it for the element.
166cdf0e10cSrcweir         The generated ID does not occur in any stream of the document.
167cdf0e10cSrcweir         </p>
168cdf0e10cSrcweir      */
169cdf0e10cSrcweir     virtual void RegisterMetadatableAndCreateID(Metadatable& i_xObject) = 0;
170cdf0e10cSrcweir 
171cdf0e10cSrcweir     /** try to register an ODF element at a given XML ID, or update its
172cdf0e10cSrcweir         registation to a different XML ID.
173cdf0e10cSrcweir 
174cdf0e10cSrcweir         <p>
175cdf0e10cSrcweir         If the given new metadata reference is not already occupied in the
176cdf0e10cSrcweir         document, unregister the element at its old metadata reference if
177cdf0e10cSrcweir         it has one, and register the new metadata reference for the element.
178cdf0e10cSrcweir         Note that this method only ensures that XML IDs are unique per stream,
179cdf0e10cSrcweir         so using the same XML ID in both content.xml and styles.xml is allowed.
180cdf0e10cSrcweir         </p>
181cdf0e10cSrcweir 
182cdf0e10cSrcweir         @returns
183cdf0e10cSrcweir             true iff the element has successfully been registered
184cdf0e10cSrcweir      */
185cdf0e10cSrcweir     virtual bool TryRegisterMetadatable(Metadatable& i_xObject,
186cdf0e10cSrcweir         ::rtl::OUString const& i_rStreamName, ::rtl::OUString const& i_rIdref)
187cdf0e10cSrcweir         = 0;
188cdf0e10cSrcweir 
189cdf0e10cSrcweir     /** unregister an ODF element.
190cdf0e10cSrcweir 
191cdf0e10cSrcweir         <p>
192cdf0e10cSrcweir         Unregister the element at its metadata reference.
193cdf0e10cSrcweir         Does not remove the metadata reference from the element.
194cdf0e10cSrcweir         </p>
195cdf0e10cSrcweir 
196cdf0e10cSrcweir         @see RemoveXmlIdForElement
197cdf0e10cSrcweir      */
198cdf0e10cSrcweir     virtual void UnregisterMetadatable(Metadatable const&) = 0;
199cdf0e10cSrcweir 
200cdf0e10cSrcweir     /** get the metadata reference for the given element. */
201cdf0e10cSrcweir     ::com::sun::star::beans::StringPair
202cdf0e10cSrcweir         GetXmlIdForElement(Metadatable const&) const;
203cdf0e10cSrcweir 
204cdf0e10cSrcweir     /** remove the metadata reference for the given element. */
205cdf0e10cSrcweir     virtual void RemoveXmlIdForElement(Metadatable const&) = 0;
206cdf0e10cSrcweir 
207cdf0e10cSrcweir protected:
208cdf0e10cSrcweir 
209cdf0e10cSrcweir     virtual bool LookupXmlId(const Metadatable& i_xObject,
210cdf0e10cSrcweir         ::rtl::OUString & o_rStream, ::rtl::OUString & o_rIdref) const = 0;
211cdf0e10cSrcweir 
212cdf0e10cSrcweir     virtual Metadatable* LookupElement(const ::rtl::OUString & i_rStreamName,
213cdf0e10cSrcweir         const ::rtl::OUString & i_rIdref) const = 0;
214cdf0e10cSrcweir };
215cdf0e10cSrcweir 
216cdf0e10cSrcweir // XmlIdRegistryDocument ---------------------------------------------
217cdf0e10cSrcweir 
218cdf0e10cSrcweir /** non-clipboard documents */
219cdf0e10cSrcweir class XmlIdRegistryDocument : public XmlIdRegistry
220cdf0e10cSrcweir {
221cdf0e10cSrcweir 
222cdf0e10cSrcweir public:
223cdf0e10cSrcweir     XmlIdRegistryDocument();
224cdf0e10cSrcweir 
225cdf0e10cSrcweir     virtual ~XmlIdRegistryDocument();
226cdf0e10cSrcweir 
227cdf0e10cSrcweir     virtual void RegisterMetadatableAndCreateID(Metadatable& i_xObject);
228cdf0e10cSrcweir 
229cdf0e10cSrcweir     virtual bool TryRegisterMetadatable(Metadatable& i_xObject,
230cdf0e10cSrcweir         ::rtl::OUString const& i_rStreamName, ::rtl::OUString const& i_rIdref);
231cdf0e10cSrcweir 
232cdf0e10cSrcweir     virtual void UnregisterMetadatable(Metadatable const&);
233cdf0e10cSrcweir 
234cdf0e10cSrcweir     virtual void RemoveXmlIdForElement(Metadatable const&);
235cdf0e10cSrcweir 
236cdf0e10cSrcweir     /** register i_rCopy as a copy of i_rSource,
237cdf0e10cSrcweir         with precedence iff i_bCopyPrecedesSource is true */
238cdf0e10cSrcweir     void RegisterCopy(Metadatable const& i_rSource, Metadatable & i_rCopy,
239cdf0e10cSrcweir         const bool i_bCopyPrecedesSource);
240cdf0e10cSrcweir 
241cdf0e10cSrcweir     /** create a Undo Metadatable for i_rObject. */
242cdf0e10cSrcweir     ::boost::shared_ptr<MetadatableUndo> CreateUndo(
243cdf0e10cSrcweir         Metadatable const& i_rObject);
244cdf0e10cSrcweir 
245cdf0e10cSrcweir     /** merge i_rMerged and i_rOther into i_rMerged. */
246cdf0e10cSrcweir     void JoinMetadatables(Metadatable & i_rMerged, Metadatable const& i_rOther);
247cdf0e10cSrcweir 
248cdf0e10cSrcweir     // unfortunately public, Metadatable::RegisterAsCopyOf needs this
249cdf0e10cSrcweir     virtual bool LookupXmlId(const Metadatable& i_xObject,
250cdf0e10cSrcweir         ::rtl::OUString & o_rStream, ::rtl::OUString & o_rIdref) const;
251cdf0e10cSrcweir 
252cdf0e10cSrcweir private:
253cdf0e10cSrcweir 
254cdf0e10cSrcweir     virtual Metadatable* LookupElement(const ::rtl::OUString & i_rStreamName,
255cdf0e10cSrcweir         const ::rtl::OUString & i_rIdref) const;
256cdf0e10cSrcweir 
257cdf0e10cSrcweir     struct XmlIdRegistry_Impl;
258cdf0e10cSrcweir     ::std::auto_ptr<XmlIdRegistry_Impl> m_pImpl;
259cdf0e10cSrcweir };
260cdf0e10cSrcweir 
261cdf0e10cSrcweir // MetadatableUndo ---------------------------------------------------
262cdf0e10cSrcweir 
263cdf0e10cSrcweir /** the horrible Undo Metadatable: is inserted into lists to track position */
264cdf0e10cSrcweir class MetadatableUndo : public Metadatable
265cdf0e10cSrcweir {
266cdf0e10cSrcweir     /// as determined by the stream of the source in original document
267cdf0e10cSrcweir     const bool m_isInContent;
268cdf0e10cSrcweir public:
MetadatableUndo(const bool i_isInContent)269cdf0e10cSrcweir     MetadatableUndo(const bool i_isInContent)
270cdf0e10cSrcweir         : m_isInContent(i_isInContent) { }
GetRegistry()271cdf0e10cSrcweir     virtual ::sfx2::XmlIdRegistry& GetRegistry()
272cdf0e10cSrcweir     {
273cdf0e10cSrcweir         // N.B. for Undo, m_pReg is initialized by registering this as copy in
274cdf0e10cSrcweir         // CreateUndo; it is never cleared
275cdf0e10cSrcweir         OSL_ENSURE(m_pReg, "no m_pReg in MetadatableUndo ?");
276cdf0e10cSrcweir         return *m_pReg;
277cdf0e10cSrcweir     }
IsInClipboard() const278cdf0e10cSrcweir     virtual bool IsInClipboard() const { return false; }
IsInUndo() const279cdf0e10cSrcweir     virtual bool IsInUndo() const { return true; }
IsInContent() const280cdf0e10cSrcweir     virtual bool IsInContent() const { return m_isInContent; }
281cdf0e10cSrcweir     virtual ::com::sun::star::uno::Reference<
MakeUnoObject()282cdf0e10cSrcweir         ::com::sun::star::rdf::XMetadatable > MakeUnoObject()
283cdf0e10cSrcweir     { OSL_ENSURE(false, "MetadatableUndo::MakeUnoObject"); throw; }
284cdf0e10cSrcweir };
285cdf0e10cSrcweir 
286cdf0e10cSrcweir // MetadatableClipboard ----------------------------------------------
287cdf0e10cSrcweir 
288cdf0e10cSrcweir /** the horrible Clipboard Metadatable: inserted into lists to track position */
289cdf0e10cSrcweir class MetadatableClipboard : public Metadatable
290cdf0e10cSrcweir {
291cdf0e10cSrcweir     /// as determined by the stream of the source in original document
292cdf0e10cSrcweir     const bool m_isInContent;
293cdf0e10cSrcweir public:
MetadatableClipboard(const bool i_isInContent)294cdf0e10cSrcweir     MetadatableClipboard(const bool i_isInContent)
295cdf0e10cSrcweir         : m_isInContent(i_isInContent) { }
GetRegistry()296cdf0e10cSrcweir     virtual ::sfx2::XmlIdRegistry& GetRegistry()
297cdf0e10cSrcweir     {
298cdf0e10cSrcweir     // N.B. for Clipboard, m_pReg is initialized by registering this as copy in
299cdf0e10cSrcweir     // RegisterAsCopyOf; it is only cleared by OriginNoLongerInBusinessAnymore
300cdf0e10cSrcweir         OSL_ENSURE(m_pReg, "no m_pReg in MetadatableClipboard ?");
301cdf0e10cSrcweir         return *m_pReg;
302cdf0e10cSrcweir     }
IsInClipboard() const303cdf0e10cSrcweir     virtual bool IsInClipboard() const { return true; }
IsInUndo() const304cdf0e10cSrcweir     virtual bool IsInUndo() const { return false; }
IsInContent() const305cdf0e10cSrcweir     virtual bool IsInContent() const { return m_isInContent; }
306cdf0e10cSrcweir     virtual ::com::sun::star::uno::Reference<
MakeUnoObject()307cdf0e10cSrcweir         ::com::sun::star::rdf::XMetadatable > MakeUnoObject()
308cdf0e10cSrcweir     { OSL_ENSURE(false, "MetadatableClipboard::MakeUnoObject"); throw; }
OriginNoLongerInBusinessAnymore()309cdf0e10cSrcweir     void OriginNoLongerInBusinessAnymore() { m_pReg = 0; }
310cdf0e10cSrcweir };
311cdf0e10cSrcweir 
312cdf0e10cSrcweir // XmlIdRegistryClipboard --------------------------------------------
313cdf0e10cSrcweir 
314cdf0e10cSrcweir class XmlIdRegistryClipboard : public XmlIdRegistry
315cdf0e10cSrcweir {
316cdf0e10cSrcweir 
317cdf0e10cSrcweir public:
318cdf0e10cSrcweir     XmlIdRegistryClipboard();
319cdf0e10cSrcweir     virtual ~XmlIdRegistryClipboard();
320cdf0e10cSrcweir 
321cdf0e10cSrcweir     virtual void RegisterMetadatableAndCreateID(Metadatable& i_xObject);
322cdf0e10cSrcweir 
323cdf0e10cSrcweir     virtual bool TryRegisterMetadatable(Metadatable& i_xObject,
324cdf0e10cSrcweir         ::rtl::OUString const& i_rStreamName, ::rtl::OUString const& i_rIdref);
325cdf0e10cSrcweir 
326cdf0e10cSrcweir     virtual void UnregisterMetadatable(Metadatable const&);
327cdf0e10cSrcweir 
328cdf0e10cSrcweir     virtual void RemoveXmlIdForElement(Metadatable const&);
329cdf0e10cSrcweir 
330cdf0e10cSrcweir     /** register i_rCopy as a copy of i_rSource */
331cdf0e10cSrcweir     MetadatableClipboard & RegisterCopyClipboard(Metadatable & i_rCopy,
332cdf0e10cSrcweir         beans::StringPair const & i_rReference,
333cdf0e10cSrcweir         const bool i_isLatent);
334cdf0e10cSrcweir 
335cdf0e10cSrcweir     /** get the Metadatable that links i_rObject to its origin registry */
336cdf0e10cSrcweir     MetadatableClipboard const* SourceLink(Metadatable const& i_rObject);
337cdf0e10cSrcweir 
338cdf0e10cSrcweir private:
339cdf0e10cSrcweir     virtual bool LookupXmlId(const Metadatable& i_xObject,
340cdf0e10cSrcweir         ::rtl::OUString & o_rStream, ::rtl::OUString & o_rIdref) const;
341cdf0e10cSrcweir 
342cdf0e10cSrcweir     virtual Metadatable* LookupElement(const ::rtl::OUString & i_rStreamName,
343cdf0e10cSrcweir         const ::rtl::OUString & i_rIdref) const;
344cdf0e10cSrcweir 
345cdf0e10cSrcweir     /** create a Clipboard Metadatable for i_rObject. */
346cdf0e10cSrcweir     ::boost::shared_ptr<MetadatableClipboard> CreateClipboard(
347cdf0e10cSrcweir         const bool i_isInContent);
348cdf0e10cSrcweir 
349cdf0e10cSrcweir     struct XmlIdRegistry_Impl;
350cdf0e10cSrcweir     ::std::auto_ptr<XmlIdRegistry_Impl> m_pImpl;
351cdf0e10cSrcweir };
352cdf0e10cSrcweir 
353cdf0e10cSrcweir 
354cdf0e10cSrcweir //=============================================================================
355cdf0e10cSrcweir // XmlIdRegistry
356cdf0e10cSrcweir 
createXmlIdRegistry(const bool i_DocIsClipboard)357cdf0e10cSrcweir ::sfx2::IXmlIdRegistry * createXmlIdRegistry(const bool i_DocIsClipboard)
358cdf0e10cSrcweir {
359cdf0e10cSrcweir     return i_DocIsClipboard
360cdf0e10cSrcweir         ? static_cast<XmlIdRegistry*>( new XmlIdRegistryClipboard )
361cdf0e10cSrcweir         : static_cast<XmlIdRegistry*>( new XmlIdRegistryDocument );
362cdf0e10cSrcweir }
363cdf0e10cSrcweir 
XmlIdRegistry()364cdf0e10cSrcweir XmlIdRegistry::XmlIdRegistry()
365cdf0e10cSrcweir {
366cdf0e10cSrcweir }
367cdf0e10cSrcweir 
~XmlIdRegistry()368cdf0e10cSrcweir XmlIdRegistry::~XmlIdRegistry()
369cdf0e10cSrcweir {
370cdf0e10cSrcweir }
371cdf0e10cSrcweir 
372cdf0e10cSrcweir ::com::sun::star::uno::Reference< ::com::sun::star::rdf::XMetadatable > SAL_CALL
GetElementByMetadataReference(const beans::StringPair & i_rReference) const373cdf0e10cSrcweir XmlIdRegistry::GetElementByMetadataReference(
374cdf0e10cSrcweir     const beans::StringPair & i_rReference) const
375cdf0e10cSrcweir {
376cdf0e10cSrcweir     Metadatable* pObject( LookupElement(i_rReference.First,
377cdf0e10cSrcweir         i_rReference.Second) );
378cdf0e10cSrcweir     return pObject ? pObject->MakeUnoObject() : 0;
379cdf0e10cSrcweir }
380cdf0e10cSrcweir 
381cdf0e10cSrcweir beans::StringPair
GetXmlIdForElement(const Metadatable & i_rObject) const382cdf0e10cSrcweir XmlIdRegistry::GetXmlIdForElement(const Metadatable& i_rObject) const
383cdf0e10cSrcweir {
384cdf0e10cSrcweir     ::rtl::OUString path;
385cdf0e10cSrcweir     ::rtl::OUString idref;
386cdf0e10cSrcweir     if (LookupXmlId(i_rObject, path, idref))
387cdf0e10cSrcweir     {
388cdf0e10cSrcweir         if (LookupElement(path, idref) == &i_rObject)
389cdf0e10cSrcweir         {
390cdf0e10cSrcweir             return beans::StringPair(path, idref);
391cdf0e10cSrcweir         }
392cdf0e10cSrcweir     }
393cdf0e10cSrcweir     return beans::StringPair();
394cdf0e10cSrcweir }
395cdf0e10cSrcweir 
396cdf0e10cSrcweir 
397cdf0e10cSrcweir /// generate unique xml:id
398cdf0e10cSrcweir template< typename T >
create_id(const::std::hash_map<::rtl::OUString,T,::rtl::OUStringHash> & i_rXmlIdMap)399cdf0e10cSrcweir /*static*/ ::rtl::OUString create_id(const
400cdf0e10cSrcweir     ::std::hash_map< ::rtl::OUString, T, ::rtl::OUStringHash > & i_rXmlIdMap)
401cdf0e10cSrcweir {
402cdf0e10cSrcweir     static rtlRandomPool s_Pool( rtl_random_createPool() );
403cdf0e10cSrcweir     const ::rtl::OUString prefix( ::rtl::OUString::createFromAscii(s_prefix) );
404cdf0e10cSrcweir     typename ::std::hash_map< ::rtl::OUString, T, ::rtl::OUStringHash >
405cdf0e10cSrcweir         ::const_iterator iter;
406cdf0e10cSrcweir     ::rtl::OUString id;
407cdf0e10cSrcweir     do
408cdf0e10cSrcweir     {
409cdf0e10cSrcweir         sal_Int32 n;
410cdf0e10cSrcweir         rtl_random_getBytes(s_Pool, & n, sizeof(n));
411cdf0e10cSrcweir         id = prefix + ::rtl::OUString::valueOf(static_cast<sal_Int32>(abs(n)));
412cdf0e10cSrcweir         iter = i_rXmlIdMap.find(id);
413cdf0e10cSrcweir     }
414cdf0e10cSrcweir     while (iter != i_rXmlIdMap.end());
415cdf0e10cSrcweir     return id;
416cdf0e10cSrcweir }
417cdf0e10cSrcweir 
418cdf0e10cSrcweir //=============================================================================
419cdf0e10cSrcweir // Document XML ID Registry (_Impl)
420cdf0e10cSrcweir 
421cdf0e10cSrcweir /// element list
422cdf0e10cSrcweir typedef ::std::list< Metadatable* > XmlIdList_t;
423cdf0e10cSrcweir 
424cdf0e10cSrcweir /// Idref -> (content.xml element list, styles.xml element list)
425cdf0e10cSrcweir typedef ::std::hash_map< ::rtl::OUString,
426cdf0e10cSrcweir     ::std::pair< XmlIdList_t, XmlIdList_t >, ::rtl::OUStringHash > XmlIdMap_t;
427cdf0e10cSrcweir 
428cdf0e10cSrcweir /// pointer hash template
429cdf0e10cSrcweir template<typename T> struct PtrHash
430cdf0e10cSrcweir {
operator ()sfx2::PtrHash431cdf0e10cSrcweir     size_t operator() (T const * i_pT) const
432cdf0e10cSrcweir     {
433cdf0e10cSrcweir         return reinterpret_cast<size_t>(i_pT);
434cdf0e10cSrcweir     }
435cdf0e10cSrcweir };
436cdf0e10cSrcweir 
437cdf0e10cSrcweir /// element -> (stream name, idref)
438cdf0e10cSrcweir typedef ::std::hash_map< const Metadatable*,
439cdf0e10cSrcweir     ::std::pair< ::rtl::OUString, ::rtl::OUString>, PtrHash<Metadatable> >
440cdf0e10cSrcweir     XmlIdReverseMap_t;
441cdf0e10cSrcweir 
442cdf0e10cSrcweir struct XmlIdRegistryDocument::XmlIdRegistry_Impl
443cdf0e10cSrcweir {
XmlIdRegistry_Implsfx2::XmlIdRegistryDocument::XmlIdRegistry_Impl444cdf0e10cSrcweir     XmlIdRegistry_Impl()
445cdf0e10cSrcweir         : m_XmlIdMap(), m_XmlIdReverseMap() { }
446cdf0e10cSrcweir 
447cdf0e10cSrcweir     bool TryInsertMetadatable(Metadatable& i_xObject,
448cdf0e10cSrcweir         const ::rtl::OUString & i_rStream, const ::rtl::OUString & i_rIdref);
449cdf0e10cSrcweir 
450cdf0e10cSrcweir     bool LookupXmlId(const Metadatable& i_xObject,
451cdf0e10cSrcweir         ::rtl::OUString & o_rStream, ::rtl::OUString & o_rIdref) const;
452cdf0e10cSrcweir 
453cdf0e10cSrcweir     Metadatable* LookupElement(const ::rtl::OUString & i_rStreamName,
454cdf0e10cSrcweir         const ::rtl::OUString & i_rIdref) const;
455cdf0e10cSrcweir 
456cdf0e10cSrcweir     const XmlIdList_t * LookupElementList(
457cdf0e10cSrcweir         const ::rtl::OUString & i_rStreamName,
458cdf0e10cSrcweir         const ::rtl::OUString & i_rIdref) const;
459cdf0e10cSrcweir 
LookupElementListsfx2::XmlIdRegistryDocument::XmlIdRegistry_Impl460cdf0e10cSrcweir           XmlIdList_t * LookupElementList(
461cdf0e10cSrcweir         const ::rtl::OUString & i_rStreamName,
462cdf0e10cSrcweir         const ::rtl::OUString & i_rIdref)
463cdf0e10cSrcweir     {
464cdf0e10cSrcweir         return const_cast<XmlIdList_t*>(
465cdf0e10cSrcweir             const_cast<const XmlIdRegistry_Impl*>(this)
466cdf0e10cSrcweir                 ->LookupElementList(i_rStreamName, i_rIdref));
467cdf0e10cSrcweir     }
468cdf0e10cSrcweir 
469cdf0e10cSrcweir     XmlIdMap_t m_XmlIdMap;
470cdf0e10cSrcweir     XmlIdReverseMap_t m_XmlIdReverseMap;
471cdf0e10cSrcweir };
472cdf0e10cSrcweir 
473cdf0e10cSrcweir // -------------------------------------------------------------------
474cdf0e10cSrcweir 
475cdf0e10cSrcweir static void
rmIter(XmlIdMap_t & i_rXmlIdMap,XmlIdMap_t::iterator const & i_rIter,::rtl::OUString const & i_rStream,Metadatable const & i_rObject)476cdf0e10cSrcweir rmIter(XmlIdMap_t & i_rXmlIdMap, XmlIdMap_t::iterator const& i_rIter,
477cdf0e10cSrcweir     ::rtl::OUString const & i_rStream, Metadatable const& i_rObject)
478cdf0e10cSrcweir {
479cdf0e10cSrcweir     if (i_rIter != i_rXmlIdMap.end())
480cdf0e10cSrcweir     {
481cdf0e10cSrcweir         XmlIdList_t & rList( isContentFile(i_rStream)
482cdf0e10cSrcweir             ? i_rIter->second.first : i_rIter->second.second );
483cdf0e10cSrcweir         rList.remove(&const_cast<Metadatable&>(i_rObject));
484cdf0e10cSrcweir         if (i_rIter->second.first.empty() && i_rIter->second.second.empty())
485cdf0e10cSrcweir         {
486cdf0e10cSrcweir             i_rXmlIdMap.erase(i_rIter);
487cdf0e10cSrcweir         }
488cdf0e10cSrcweir     }
489cdf0e10cSrcweir }
490cdf0e10cSrcweir 
491cdf0e10cSrcweir // -------------------------------------------------------------------
492cdf0e10cSrcweir 
493cdf0e10cSrcweir const XmlIdList_t *
LookupElementList(const::rtl::OUString & i_rStreamName,const::rtl::OUString & i_rIdref) const494cdf0e10cSrcweir XmlIdRegistryDocument::XmlIdRegistry_Impl::LookupElementList(
495cdf0e10cSrcweir     const ::rtl::OUString & i_rStreamName,
496cdf0e10cSrcweir     const ::rtl::OUString & i_rIdref) const
497cdf0e10cSrcweir {
498cdf0e10cSrcweir     const XmlIdMap_t::const_iterator iter( m_XmlIdMap.find(i_rIdref) );
499cdf0e10cSrcweir     if (iter != m_XmlIdMap.end())
500cdf0e10cSrcweir     {
501cdf0e10cSrcweir         OSL_ENSURE(!iter->second.first.empty() || !iter->second.second.empty(),
502cdf0e10cSrcweir             "null entry in m_XmlIdMap");
503cdf0e10cSrcweir         return (isContentFile(i_rStreamName))
504cdf0e10cSrcweir             ?  &iter->second.first
505cdf0e10cSrcweir             :  &iter->second.second;
506cdf0e10cSrcweir     }
507cdf0e10cSrcweir     else
508cdf0e10cSrcweir     {
509cdf0e10cSrcweir         return 0;
510cdf0e10cSrcweir     }
511cdf0e10cSrcweir }
512cdf0e10cSrcweir 
513cdf0e10cSrcweir Metadatable*
LookupElement(const::rtl::OUString & i_rStreamName,const::rtl::OUString & i_rIdref) const514cdf0e10cSrcweir XmlIdRegistryDocument::XmlIdRegistry_Impl::LookupElement(
515cdf0e10cSrcweir     const ::rtl::OUString & i_rStreamName,
516cdf0e10cSrcweir     const ::rtl::OUString & i_rIdref) const
517cdf0e10cSrcweir {
518cdf0e10cSrcweir     if (!isValidXmlId(i_rStreamName, i_rIdref))
519cdf0e10cSrcweir     {
520cdf0e10cSrcweir         throw lang::IllegalArgumentException(::rtl::OUString::createFromAscii(
521cdf0e10cSrcweir             "illegal XmlId"), 0, 0);
522cdf0e10cSrcweir     }
523cdf0e10cSrcweir 
524cdf0e10cSrcweir     const XmlIdList_t * pList( LookupElementList(i_rStreamName, i_rIdref) );
525cdf0e10cSrcweir     if (pList)
526cdf0e10cSrcweir     {
527cdf0e10cSrcweir         const XmlIdList_t::const_iterator iter(
528cdf0e10cSrcweir             ::std::find_if(pList->begin(), pList->end(),
529cdf0e10cSrcweir                 ::boost::bind(
530cdf0e10cSrcweir                     ::std::logical_not<bool>(),
531cdf0e10cSrcweir                         ::boost::bind(
532cdf0e10cSrcweir                             ::std::logical_or<bool>(),
533cdf0e10cSrcweir                                 ::boost::bind( &Metadatable::IsInUndo, _1 ),
534cdf0e10cSrcweir                                 ::boost::bind( &Metadatable::IsInClipboard, _1 )
535cdf0e10cSrcweir             ) ) ) );
536cdf0e10cSrcweir         if (iter != pList->end())
537cdf0e10cSrcweir         {
538cdf0e10cSrcweir             return *iter;
539cdf0e10cSrcweir         }
540cdf0e10cSrcweir     }
541cdf0e10cSrcweir     return 0;
542cdf0e10cSrcweir }
543cdf0e10cSrcweir 
544cdf0e10cSrcweir bool
LookupXmlId(const Metadatable & i_rObject,::rtl::OUString & o_rStream,::rtl::OUString & o_rIdref) const545cdf0e10cSrcweir XmlIdRegistryDocument::XmlIdRegistry_Impl::LookupXmlId(
546cdf0e10cSrcweir     const Metadatable& i_rObject,
547cdf0e10cSrcweir     ::rtl::OUString & o_rStream, ::rtl::OUString & o_rIdref) const
548cdf0e10cSrcweir {
549cdf0e10cSrcweir     const XmlIdReverseMap_t::const_iterator iter(
550cdf0e10cSrcweir         m_XmlIdReverseMap.find(&i_rObject) );
551cdf0e10cSrcweir     if (iter != m_XmlIdReverseMap.end())
552cdf0e10cSrcweir     {
553cdf0e10cSrcweir         OSL_ENSURE(!iter->second.first.equalsAscii(""),
554cdf0e10cSrcweir             "null stream in m_XmlIdReverseMap");
555cdf0e10cSrcweir         OSL_ENSURE(!iter->second.second.equalsAscii(""),
556cdf0e10cSrcweir             "null id in m_XmlIdReverseMap");
557cdf0e10cSrcweir         o_rStream = iter->second.first;
558cdf0e10cSrcweir         o_rIdref  = iter->second.second;
559cdf0e10cSrcweir         return true;
560cdf0e10cSrcweir     }
561cdf0e10cSrcweir     else
562cdf0e10cSrcweir     {
563cdf0e10cSrcweir         return false;
564cdf0e10cSrcweir     }
565cdf0e10cSrcweir }
566cdf0e10cSrcweir 
567cdf0e10cSrcweir bool
TryInsertMetadatable(Metadatable & i_rObject,const::rtl::OUString & i_rStreamName,const::rtl::OUString & i_rIdref)568cdf0e10cSrcweir XmlIdRegistryDocument::XmlIdRegistry_Impl::TryInsertMetadatable(
569cdf0e10cSrcweir     Metadatable & i_rObject,
570cdf0e10cSrcweir     const ::rtl::OUString & i_rStreamName, const ::rtl::OUString & i_rIdref)
571cdf0e10cSrcweir {
572cdf0e10cSrcweir     const bool bContent( isContentFile(i_rStreamName) );
573cdf0e10cSrcweir     OSL_ENSURE(isContentFile(i_rStreamName) || isStylesFile(i_rStreamName),
574cdf0e10cSrcweir         "invalid stream");
575cdf0e10cSrcweir 
576cdf0e10cSrcweir     XmlIdList_t * pList( LookupElementList(i_rStreamName, i_rIdref) );
577cdf0e10cSrcweir     if (pList)
578cdf0e10cSrcweir     {
579cdf0e10cSrcweir         if (pList->empty())
580cdf0e10cSrcweir         {
581cdf0e10cSrcweir             pList->push_back( &i_rObject );
582cdf0e10cSrcweir             return true;
583cdf0e10cSrcweir         }
584cdf0e10cSrcweir         else
585cdf0e10cSrcweir         {
586cdf0e10cSrcweir             // this is only called from TryRegister now, so check
587cdf0e10cSrcweir             // if all elements in the list are deleted (in undo) or
588cdf0e10cSrcweir             // placeholders, then "steal" the id from them
589cdf0e10cSrcweir             if ( pList->end() == ::std::find_if(pList->begin(), pList->end(),
590cdf0e10cSrcweir                 ::boost::bind(
591cdf0e10cSrcweir                     ::std::logical_not<bool>(),
592cdf0e10cSrcweir                         ::boost::bind(
593cdf0e10cSrcweir                             ::std::logical_or<bool>(),
594cdf0e10cSrcweir                                 ::boost::bind( &Metadatable::IsInUndo, _1 ),
595cdf0e10cSrcweir                                 ::boost::bind( &Metadatable::IsInClipboard, _1 )
596cdf0e10cSrcweir                 ) ) ) )
597cdf0e10cSrcweir             {
598cdf0e10cSrcweir // ???  this is not undoable
599cdf0e10cSrcweir //                pList->clear();
600cdf0e10cSrcweir //                pList->push_back( &i_rObject );
601cdf0e10cSrcweir                 pList->push_front( &i_rObject );
602cdf0e10cSrcweir                 return true;
603cdf0e10cSrcweir             }
604cdf0e10cSrcweir             else
605cdf0e10cSrcweir             {
606cdf0e10cSrcweir                 return false;
607cdf0e10cSrcweir             }
608cdf0e10cSrcweir         }
609cdf0e10cSrcweir     }
610cdf0e10cSrcweir     else
611cdf0e10cSrcweir     {
612cdf0e10cSrcweir         m_XmlIdMap.insert(::std::make_pair(i_rIdref, bContent
613cdf0e10cSrcweir             ? ::std::make_pair( XmlIdList_t( 1, &i_rObject ), XmlIdList_t() )
614cdf0e10cSrcweir             : ::std::make_pair( XmlIdList_t(), XmlIdList_t( 1, &i_rObject ) )));
615cdf0e10cSrcweir         return true;
616cdf0e10cSrcweir     }
617cdf0e10cSrcweir }
618cdf0e10cSrcweir 
619cdf0e10cSrcweir //=============================================================================
620cdf0e10cSrcweir // Document XML ID Registry
621cdf0e10cSrcweir 
622cdf0e10cSrcweir 
XmlIdRegistryDocument()623cdf0e10cSrcweir XmlIdRegistryDocument::XmlIdRegistryDocument()
624cdf0e10cSrcweir     :   m_pImpl( new XmlIdRegistry_Impl )
625cdf0e10cSrcweir {
626cdf0e10cSrcweir }
627cdf0e10cSrcweir 
628cdf0e10cSrcweir static void
removeLink(Metadatable * i_pObject)629cdf0e10cSrcweir removeLink(Metadatable* i_pObject)
630cdf0e10cSrcweir {
631cdf0e10cSrcweir     OSL_ENSURE(i_pObject, "null in list ???");
632cdf0e10cSrcweir     if (!i_pObject) return;
633cdf0e10cSrcweir     if (i_pObject->IsInClipboard())
634cdf0e10cSrcweir     {
635cdf0e10cSrcweir         MetadatableClipboard* pLink(
636cdf0e10cSrcweir             dynamic_cast<MetadatableClipboard*>( i_pObject ) );
637cdf0e10cSrcweir         OSL_ENSURE(pLink, "IsInClipboard, but no MetadatableClipboard ?");
638cdf0e10cSrcweir         if (pLink)
639cdf0e10cSrcweir         {
640cdf0e10cSrcweir             pLink->OriginNoLongerInBusinessAnymore();
641cdf0e10cSrcweir         }
642cdf0e10cSrcweir     }
643cdf0e10cSrcweir }
644cdf0e10cSrcweir 
~XmlIdRegistryDocument()645cdf0e10cSrcweir XmlIdRegistryDocument::~XmlIdRegistryDocument()
646cdf0e10cSrcweir {
647cdf0e10cSrcweir     // notify all list elements that are actually in the clipboard
648cdf0e10cSrcweir     for (XmlIdMap_t::iterator iter(m_pImpl->m_XmlIdMap.begin());
649cdf0e10cSrcweir         iter != m_pImpl->m_XmlIdMap.end(); ++iter)
650cdf0e10cSrcweir     {
651cdf0e10cSrcweir         ::std::for_each(iter->second.first.begin(), iter->second.first.end(),
652cdf0e10cSrcweir             removeLink);
653cdf0e10cSrcweir         ::std::for_each(iter->second.second.begin(), iter->second.second.end(),
654cdf0e10cSrcweir             removeLink);
655cdf0e10cSrcweir     }
656cdf0e10cSrcweir }
657cdf0e10cSrcweir 
658cdf0e10cSrcweir bool
LookupXmlId(const Metadatable & i_rObject,::rtl::OUString & o_rStream,::rtl::OUString & o_rIdref) const659cdf0e10cSrcweir XmlIdRegistryDocument::LookupXmlId(
660cdf0e10cSrcweir     const Metadatable& i_rObject,
661cdf0e10cSrcweir     ::rtl::OUString & o_rStream, ::rtl::OUString & o_rIdref) const
662cdf0e10cSrcweir {
663cdf0e10cSrcweir     return m_pImpl->LookupXmlId(i_rObject, o_rStream, o_rIdref);
664cdf0e10cSrcweir }
665cdf0e10cSrcweir 
666cdf0e10cSrcweir Metadatable*
LookupElement(const::rtl::OUString & i_rStreamName,const::rtl::OUString & i_rIdref) const667cdf0e10cSrcweir XmlIdRegistryDocument::LookupElement(
668cdf0e10cSrcweir     const ::rtl::OUString & i_rStreamName,
669cdf0e10cSrcweir     const ::rtl::OUString & i_rIdref) const
670cdf0e10cSrcweir {
671cdf0e10cSrcweir     return m_pImpl->LookupElement(i_rStreamName, i_rIdref);
672cdf0e10cSrcweir }
673cdf0e10cSrcweir 
674cdf0e10cSrcweir bool
TryRegisterMetadatable(Metadatable & i_rObject,::rtl::OUString const & i_rStreamName,::rtl::OUString const & i_rIdref)675cdf0e10cSrcweir XmlIdRegistryDocument::TryRegisterMetadatable(Metadatable & i_rObject,
676cdf0e10cSrcweir     ::rtl::OUString const& i_rStreamName, ::rtl::OUString const& i_rIdref)
677cdf0e10cSrcweir {
678cdf0e10cSrcweir     OSL_TRACE("TryRegisterMetadatable: %p (%s#%s)\n", &i_rObject,
679cdf0e10cSrcweir         ::rtl::OUStringToOString(i_rStreamName, RTL_TEXTENCODING_UTF8).getStr(),
680cdf0e10cSrcweir         ::rtl::OUStringToOString(i_rIdref, RTL_TEXTENCODING_UTF8).getStr());
681cdf0e10cSrcweir 
682cdf0e10cSrcweir     OSL_ENSURE(!dynamic_cast<MetadatableUndo*>(&i_rObject),
683cdf0e10cSrcweir         "TryRegisterMetadatable called for MetadatableUndo?");
684cdf0e10cSrcweir     OSL_ENSURE(!dynamic_cast<MetadatableClipboard*>(&i_rObject),
685cdf0e10cSrcweir         "TryRegisterMetadatable called for MetadatableClipboard?");
686cdf0e10cSrcweir 
687cdf0e10cSrcweir     if (!isValidXmlId(i_rStreamName, i_rIdref))
688cdf0e10cSrcweir     {
689cdf0e10cSrcweir         throw lang::IllegalArgumentException(::rtl::OUString::createFromAscii(
690cdf0e10cSrcweir             "illegal XmlId"), 0, 0);
691cdf0e10cSrcweir     }
692cdf0e10cSrcweir     if (i_rObject.IsInContent()
693cdf0e10cSrcweir         ?   !isContentFile(i_rStreamName)
694cdf0e10cSrcweir         :   !isStylesFile(i_rStreamName))
695cdf0e10cSrcweir     {
696cdf0e10cSrcweir         throw lang::IllegalArgumentException(::rtl::OUString::createFromAscii(
697cdf0e10cSrcweir             "illegal XmlId: wrong stream"), 0, 0);
698cdf0e10cSrcweir     }
699cdf0e10cSrcweir 
700cdf0e10cSrcweir     ::rtl::OUString old_path;
701cdf0e10cSrcweir     ::rtl::OUString old_idref;
702cdf0e10cSrcweir     m_pImpl->LookupXmlId(i_rObject, old_path, old_idref);
703cdf0e10cSrcweir     if (old_path  == i_rStreamName && old_idref == i_rIdref)
704cdf0e10cSrcweir     {
705cdf0e10cSrcweir         return (m_pImpl->LookupElement(old_path, old_idref) == &i_rObject);
706cdf0e10cSrcweir     }
707cdf0e10cSrcweir     XmlIdMap_t::iterator old_id( m_pImpl->m_XmlIdMap.end() );
708cdf0e10cSrcweir     if (!old_idref.equalsAscii(""))
709cdf0e10cSrcweir     {
710cdf0e10cSrcweir         old_id = m_pImpl->m_XmlIdMap.find(old_idref);
711cdf0e10cSrcweir         OSL_ENSURE(old_id != m_pImpl->m_XmlIdMap.end(), "old id not found");
712cdf0e10cSrcweir     }
713cdf0e10cSrcweir     if (m_pImpl->TryInsertMetadatable(i_rObject, i_rStreamName, i_rIdref))
714cdf0e10cSrcweir     {
715cdf0e10cSrcweir         rmIter(m_pImpl->m_XmlIdMap, old_id, old_path, i_rObject);
716cdf0e10cSrcweir         m_pImpl->m_XmlIdReverseMap[&i_rObject] =
717cdf0e10cSrcweir             ::std::make_pair(i_rStreamName, i_rIdref);
718cdf0e10cSrcweir         return true;
719cdf0e10cSrcweir     }
720cdf0e10cSrcweir     else
721cdf0e10cSrcweir     {
722cdf0e10cSrcweir         return false;
723cdf0e10cSrcweir     }
724cdf0e10cSrcweir }
725cdf0e10cSrcweir 
726cdf0e10cSrcweir void
RegisterMetadatableAndCreateID(Metadatable & i_rObject)727cdf0e10cSrcweir XmlIdRegistryDocument::RegisterMetadatableAndCreateID(Metadatable & i_rObject)
728cdf0e10cSrcweir {
729cdf0e10cSrcweir     OSL_TRACE("RegisterMetadatableAndCreateID: %p\n", &i_rObject);
730cdf0e10cSrcweir 
731cdf0e10cSrcweir     OSL_ENSURE(!dynamic_cast<MetadatableUndo*>(&i_rObject),
732cdf0e10cSrcweir         "RegisterMetadatableAndCreateID called for MetadatableUndo?");
733cdf0e10cSrcweir     OSL_ENSURE(!dynamic_cast<MetadatableClipboard*>(&i_rObject),
734cdf0e10cSrcweir         "RegisterMetadatableAndCreateID called for MetadatableClipboard?");
735cdf0e10cSrcweir 
736cdf0e10cSrcweir     const bool isInContent( i_rObject.IsInContent() );
737cdf0e10cSrcweir     const ::rtl::OUString stream( ::rtl::OUString::createFromAscii(
738cdf0e10cSrcweir         isInContent ? s_content : s_styles ) );
739cdf0e10cSrcweir     // check if we have a latent xmlid, and if yes, remove it
740cdf0e10cSrcweir     ::rtl::OUString old_path;
741cdf0e10cSrcweir     ::rtl::OUString old_idref;
742cdf0e10cSrcweir     m_pImpl->LookupXmlId(i_rObject, old_path, old_idref);
743cdf0e10cSrcweir 
744cdf0e10cSrcweir     XmlIdMap_t::iterator old_id( m_pImpl->m_XmlIdMap.end() );
745cdf0e10cSrcweir     if (!old_idref.equalsAscii(""))
746cdf0e10cSrcweir     {
747cdf0e10cSrcweir         old_id = m_pImpl->m_XmlIdMap.find(old_idref);
748cdf0e10cSrcweir         OSL_ENSURE(old_id != m_pImpl->m_XmlIdMap.end(), "old id not found");
749cdf0e10cSrcweir         if (m_pImpl->LookupElement(old_path, old_idref) == &i_rObject)
750cdf0e10cSrcweir         {
751cdf0e10cSrcweir             return;
752cdf0e10cSrcweir         }
753cdf0e10cSrcweir         else
754cdf0e10cSrcweir         {
755cdf0e10cSrcweir             // remove latent xmlid
756cdf0e10cSrcweir             rmIter(m_pImpl->m_XmlIdMap, old_id, old_path, i_rObject);
757cdf0e10cSrcweir         }
758cdf0e10cSrcweir     }
759cdf0e10cSrcweir 
760cdf0e10cSrcweir     // create id
761cdf0e10cSrcweir     const ::rtl::OUString id( create_id(m_pImpl->m_XmlIdMap) );
762cdf0e10cSrcweir     OSL_ENSURE(m_pImpl->m_XmlIdMap.find(id) == m_pImpl->m_XmlIdMap.end(),
763cdf0e10cSrcweir         "created id is in use");
764cdf0e10cSrcweir     m_pImpl->m_XmlIdMap.insert(::std::make_pair(id, isInContent
765cdf0e10cSrcweir         ? ::std::make_pair( XmlIdList_t( 1, &i_rObject ), XmlIdList_t() )
766cdf0e10cSrcweir         : ::std::make_pair( XmlIdList_t(), XmlIdList_t( 1, &i_rObject ) )));
767cdf0e10cSrcweir     m_pImpl->m_XmlIdReverseMap[&i_rObject] = ::std::make_pair(stream, id);
768cdf0e10cSrcweir }
769cdf0e10cSrcweir 
UnregisterMetadatable(const Metadatable & i_rObject)770cdf0e10cSrcweir void XmlIdRegistryDocument::UnregisterMetadatable(const Metadatable& i_rObject)
771cdf0e10cSrcweir {
772cdf0e10cSrcweir     OSL_TRACE("UnregisterMetadatable: %p\n", &i_rObject);
773cdf0e10cSrcweir 
774cdf0e10cSrcweir     ::rtl::OUString path;
775cdf0e10cSrcweir     ::rtl::OUString idref;
776cdf0e10cSrcweir     if (!m_pImpl->LookupXmlId(i_rObject, path, idref))
777cdf0e10cSrcweir     {
778cdf0e10cSrcweir         OSL_ENSURE(false, "unregister: no xml id?");
779cdf0e10cSrcweir         return;
780cdf0e10cSrcweir     }
781cdf0e10cSrcweir     const XmlIdMap_t::iterator iter( m_pImpl->m_XmlIdMap.find(idref) );
782cdf0e10cSrcweir     if (iter != m_pImpl->m_XmlIdMap.end())
783cdf0e10cSrcweir     {
784cdf0e10cSrcweir         rmIter(m_pImpl->m_XmlIdMap, iter, path, i_rObject);
785cdf0e10cSrcweir     }
786cdf0e10cSrcweir }
787cdf0e10cSrcweir 
RemoveXmlIdForElement(const Metadatable & i_rObject)788cdf0e10cSrcweir void XmlIdRegistryDocument::RemoveXmlIdForElement(const Metadatable& i_rObject)
789cdf0e10cSrcweir {
790cdf0e10cSrcweir     OSL_TRACE("RemoveXmlIdForElement: %p\n", &i_rObject);
791cdf0e10cSrcweir 
792cdf0e10cSrcweir     const XmlIdReverseMap_t::iterator iter(
793cdf0e10cSrcweir         m_pImpl->m_XmlIdReverseMap.find(&i_rObject) );
794cdf0e10cSrcweir     if (iter != m_pImpl->m_XmlIdReverseMap.end())
795cdf0e10cSrcweir     {
796cdf0e10cSrcweir         OSL_ENSURE(!iter->second.second.equalsAscii(""),
797cdf0e10cSrcweir             "null id in m_XmlIdReverseMap");
798cdf0e10cSrcweir         m_pImpl->m_XmlIdReverseMap.erase(iter);
799cdf0e10cSrcweir     }
800cdf0e10cSrcweir }
801cdf0e10cSrcweir 
802cdf0e10cSrcweir // -------------------------------------------------------------------
803cdf0e10cSrcweir 
RegisterCopy(Metadatable const & i_rSource,Metadatable & i_rCopy,const bool i_bCopyPrecedesSource)804cdf0e10cSrcweir void XmlIdRegistryDocument::RegisterCopy(Metadatable const& i_rSource,
805cdf0e10cSrcweir     Metadatable & i_rCopy, const bool i_bCopyPrecedesSource)
806cdf0e10cSrcweir {
807cdf0e10cSrcweir     OSL_TRACE("RegisterCopy: %p -> %p (%d)\n",
808cdf0e10cSrcweir         &i_rSource, &i_rCopy, i_bCopyPrecedesSource);
809cdf0e10cSrcweir 
810cdf0e10cSrcweir     // potential sources: clipboard, undo array, splitNode
811cdf0e10cSrcweir     // assumption: stream change can only happen via clipboard, and is handled
812cdf0e10cSrcweir     // by Metadatable::RegisterAsCopyOf
813cdf0e10cSrcweir     OSL_ENSURE(i_rSource.IsInUndo() || i_rCopy.IsInUndo() ||
814cdf0e10cSrcweir         (i_rSource.IsInContent() == i_rCopy.IsInContent()),
815cdf0e10cSrcweir         "RegisterCopy: not in same stream?");
816cdf0e10cSrcweir 
817cdf0e10cSrcweir     ::rtl::OUString path;
818cdf0e10cSrcweir     ::rtl::OUString idref;
819cdf0e10cSrcweir     if (!m_pImpl->LookupXmlId( i_rSource, path, idref ))
820cdf0e10cSrcweir     {
821cdf0e10cSrcweir         OSL_ENSURE(false, "no xml id?");
822cdf0e10cSrcweir         return;
823cdf0e10cSrcweir     }
824cdf0e10cSrcweir     XmlIdList_t * pList ( m_pImpl->LookupElementList(path, idref) );
825cdf0e10cSrcweir     OSL_ENSURE( ::std::find( pList->begin(), pList->end(), &i_rCopy )
826cdf0e10cSrcweir         == pList->end(), "copy already registered???");
827cdf0e10cSrcweir     XmlIdList_t::iterator srcpos(
828cdf0e10cSrcweir         ::std::find( pList->begin(), pList->end(), &i_rSource ) );
829cdf0e10cSrcweir     OSL_ENSURE(srcpos != pList->end(), "source not in list???");
830cdf0e10cSrcweir     if (srcpos == pList->end())
831cdf0e10cSrcweir     {
832cdf0e10cSrcweir         return;
833cdf0e10cSrcweir     }
834cdf0e10cSrcweir     if (i_bCopyPrecedesSource)
835cdf0e10cSrcweir     {
836cdf0e10cSrcweir         pList->insert( srcpos, &i_rCopy );
837cdf0e10cSrcweir     }
838cdf0e10cSrcweir     else
839cdf0e10cSrcweir     {
840cdf0e10cSrcweir         // for undo push_back does not work! must insert right after source
841cdf0e10cSrcweir         pList->insert( ++srcpos, &i_rCopy );
842cdf0e10cSrcweir     }
843cdf0e10cSrcweir     m_pImpl->m_XmlIdReverseMap.insert(::std::make_pair(&i_rCopy,
844cdf0e10cSrcweir         ::std::make_pair(path, idref)));
845cdf0e10cSrcweir }
846cdf0e10cSrcweir 
847cdf0e10cSrcweir ::boost::shared_ptr<MetadatableUndo>
CreateUndo(Metadatable const & i_rObject)848cdf0e10cSrcweir XmlIdRegistryDocument::CreateUndo(Metadatable const& i_rObject)
849cdf0e10cSrcweir {
850cdf0e10cSrcweir     OSL_TRACE("CreateUndo: %p\n", &i_rObject);
851cdf0e10cSrcweir 
852cdf0e10cSrcweir     return ::boost::shared_ptr<MetadatableUndo>(
853cdf0e10cSrcweir                 new MetadatableUndo(i_rObject.IsInContent()) );
854cdf0e10cSrcweir }
855cdf0e10cSrcweir 
856cdf0e10cSrcweir /*
857cdf0e10cSrcweir i_rMerged is both a source and the target node of the merge
858cdf0e10cSrcweir i_rOther is the other source, and will be deleted after the merge
859cdf0e10cSrcweir 
860cdf0e10cSrcweir dimensions: none|latent|actual empty|nonempty
861cdf0e10cSrcweir i_rMerged(1)    i_rOther(2)        result
862cdf0e10cSrcweir      *|empty         *|empty    => 1|2 (arbitrary)
863cdf0e10cSrcweir      *|empty         *|nonempty => 2
864cdf0e10cSrcweir      *|nonempty      *|empty    => 1
865cdf0e10cSrcweir   none|nonempty   none|nonempty => none
866cdf0e10cSrcweir   none|nonempty latent|nonempty => 2
867cdf0e10cSrcweir latent|nonempty   none|nonempty => 1
868cdf0e10cSrcweir latent|nonempty latent|nonempty => 1|2
869cdf0e10cSrcweir      *|nonempty actual|nonempty => 2
870cdf0e10cSrcweir actual|nonempty      *|nonempty => 1
871cdf0e10cSrcweir actual|nonempty actual|nonempty => 1|2
872cdf0e10cSrcweir */
873cdf0e10cSrcweir void
JoinMetadatables(Metadatable & i_rMerged,Metadatable const & i_rOther)874cdf0e10cSrcweir XmlIdRegistryDocument::JoinMetadatables(
875cdf0e10cSrcweir     Metadatable & i_rMerged, Metadatable const & i_rOther)
876cdf0e10cSrcweir {
877cdf0e10cSrcweir     OSL_TRACE("JoinMetadatables: %p <- %p\n", &i_rMerged, &i_rOther);
878cdf0e10cSrcweir 
879cdf0e10cSrcweir     bool mergedOwnsRef;
880cdf0e10cSrcweir     ::rtl::OUString path;
881cdf0e10cSrcweir     ::rtl::OUString idref;
882cdf0e10cSrcweir     if (m_pImpl->LookupXmlId(i_rMerged, path, idref))
883cdf0e10cSrcweir     {
884cdf0e10cSrcweir         mergedOwnsRef = (m_pImpl->LookupElement(path, idref) == &i_rMerged);
885cdf0e10cSrcweir     }
886cdf0e10cSrcweir     else
887cdf0e10cSrcweir     {
888cdf0e10cSrcweir         OSL_ENSURE(false, "JoinMetadatables: no xmlid?");
889cdf0e10cSrcweir         return;
890cdf0e10cSrcweir     }
891cdf0e10cSrcweir     if (!mergedOwnsRef)
892cdf0e10cSrcweir     {
893cdf0e10cSrcweir         i_rMerged.RemoveMetadataReference();
894cdf0e10cSrcweir         i_rMerged.RegisterAsCopyOf(i_rOther, true);
895cdf0e10cSrcweir         return;
896cdf0e10cSrcweir     }
897cdf0e10cSrcweir     // other cases: merged has actual ref and is nonempty,
898cdf0e10cSrcweir     // other has latent/actual ref and is nonempty: other loses => nothing to do
899cdf0e10cSrcweir }
900cdf0e10cSrcweir 
901cdf0e10cSrcweir 
902cdf0e10cSrcweir //=============================================================================
903cdf0e10cSrcweir // Clipboard XML ID Registry (_Impl)
904cdf0e10cSrcweir 
905cdf0e10cSrcweir struct RMapEntry
906cdf0e10cSrcweir {
RMapEntrysfx2::RMapEntry907cdf0e10cSrcweir     RMapEntry() : m_pLink() { }
RMapEntrysfx2::RMapEntry908cdf0e10cSrcweir     RMapEntry(::rtl::OUString const& i_rStream,
909cdf0e10cSrcweir             ::rtl::OUString const& i_rXmlId,
910cdf0e10cSrcweir             ::boost::shared_ptr<MetadatableClipboard> const& i_pLink
911cdf0e10cSrcweir                 = ::boost::shared_ptr<MetadatableClipboard>())
912cdf0e10cSrcweir         :   m_Stream(i_rStream), m_XmlId(i_rXmlId), m_pLink(i_pLink)
913cdf0e10cSrcweir         {}
914cdf0e10cSrcweir     ::rtl::OUString m_Stream;
915cdf0e10cSrcweir     ::rtl::OUString m_XmlId;
916cdf0e10cSrcweir     // this would have been an auto_ptr, if only that would have compiled...
917cdf0e10cSrcweir     ::boost::shared_ptr<MetadatableClipboard> m_pLink;
918cdf0e10cSrcweir };
919cdf0e10cSrcweir 
920cdf0e10cSrcweir /// element -> (stream name, idref, source)
921cdf0e10cSrcweir typedef ::std::hash_map< const Metadatable*,
922cdf0e10cSrcweir     struct RMapEntry,
923cdf0e10cSrcweir     PtrHash<Metadatable> >
924cdf0e10cSrcweir     ClipboardXmlIdReverseMap_t;
925cdf0e10cSrcweir 
926cdf0e10cSrcweir /// Idref -> (content.xml element, styles.xml element)
927cdf0e10cSrcweir typedef ::std::hash_map< ::rtl::OUString,
928cdf0e10cSrcweir     ::std::pair< Metadatable*, Metadatable* >, ::rtl::OUStringHash >
929cdf0e10cSrcweir     ClipboardXmlIdMap_t;
930cdf0e10cSrcweir 
931cdf0e10cSrcweir struct XmlIdRegistryClipboard::XmlIdRegistry_Impl
932cdf0e10cSrcweir {
XmlIdRegistry_Implsfx2::XmlIdRegistryClipboard::XmlIdRegistry_Impl933cdf0e10cSrcweir     XmlIdRegistry_Impl()
934cdf0e10cSrcweir         : m_XmlIdMap(), m_XmlIdReverseMap() { }
935cdf0e10cSrcweir 
936cdf0e10cSrcweir     bool TryInsertMetadatable(Metadatable& i_xObject,
937cdf0e10cSrcweir         const ::rtl::OUString & i_rStream, const ::rtl::OUString & i_rIdref);
938cdf0e10cSrcweir 
939cdf0e10cSrcweir     bool LookupXmlId(const Metadatable& i_xObject,
940cdf0e10cSrcweir         ::rtl::OUString & o_rStream, ::rtl::OUString & o_rIdref,
941cdf0e10cSrcweir         MetadatableClipboard const* &o_rpLink) const;
942cdf0e10cSrcweir 
943cdf0e10cSrcweir     Metadatable* LookupElement(const ::rtl::OUString & i_rStreamName,
944cdf0e10cSrcweir         const ::rtl::OUString & i_rIdref) const;
945cdf0e10cSrcweir 
946cdf0e10cSrcweir     Metadatable* const* LookupEntry(const ::rtl::OUString & i_rStreamName,
947cdf0e10cSrcweir         const ::rtl::OUString & i_rIdref) const;
948cdf0e10cSrcweir 
LookupEntrysfx2::XmlIdRegistryClipboard::XmlIdRegistry_Impl949cdf0e10cSrcweir     Metadatable*      * LookupEntry(const ::rtl::OUString & i_rStreamName,
950cdf0e10cSrcweir         const ::rtl::OUString & i_rIdref)
951cdf0e10cSrcweir     {
952cdf0e10cSrcweir         return const_cast<Metadatable**>(
953cdf0e10cSrcweir             const_cast<const XmlIdRegistry_Impl*>(this)
954cdf0e10cSrcweir                 ->LookupEntry(i_rStreamName, i_rIdref));
955cdf0e10cSrcweir     }
956cdf0e10cSrcweir 
957cdf0e10cSrcweir     ClipboardXmlIdMap_t m_XmlIdMap;
958cdf0e10cSrcweir     ClipboardXmlIdReverseMap_t m_XmlIdReverseMap;
959cdf0e10cSrcweir };
960cdf0e10cSrcweir 
961cdf0e10cSrcweir // -------------------------------------------------------------------
962cdf0e10cSrcweir 
963cdf0e10cSrcweir static void
rmIter(ClipboardXmlIdMap_t & i_rXmlIdMap,ClipboardXmlIdMap_t::iterator const & i_rIter,::rtl::OUString const & i_rStream,Metadatable const & i_rObject)964cdf0e10cSrcweir rmIter(ClipboardXmlIdMap_t & i_rXmlIdMap,
965cdf0e10cSrcweir     ClipboardXmlIdMap_t::iterator const& i_rIter,
966cdf0e10cSrcweir     ::rtl::OUString const & i_rStream, Metadatable const& i_rObject)
967cdf0e10cSrcweir {
968cdf0e10cSrcweir     if (i_rIter != i_rXmlIdMap.end())
969cdf0e10cSrcweir     {
970cdf0e10cSrcweir         Metadatable *& rMeta = isContentFile(i_rStream)
971cdf0e10cSrcweir             ? i_rIter->second.first : i_rIter->second.second;
972cdf0e10cSrcweir         if (rMeta == &i_rObject)
973cdf0e10cSrcweir         {
974cdf0e10cSrcweir             rMeta = 0;
975cdf0e10cSrcweir         }
976cdf0e10cSrcweir         if (!i_rIter->second.first && !i_rIter->second.second)
977cdf0e10cSrcweir         {
978cdf0e10cSrcweir             i_rXmlIdMap.erase(i_rIter);
979cdf0e10cSrcweir         }
980cdf0e10cSrcweir     }
981cdf0e10cSrcweir }
982cdf0e10cSrcweir 
983cdf0e10cSrcweir // -------------------------------------------------------------------
984cdf0e10cSrcweir 
985cdf0e10cSrcweir Metadatable* const*
LookupEntry(const::rtl::OUString & i_rStreamName,const::rtl::OUString & i_rIdref) const986cdf0e10cSrcweir XmlIdRegistryClipboard::XmlIdRegistry_Impl::LookupEntry(
987cdf0e10cSrcweir     const ::rtl::OUString & i_rStreamName,
988cdf0e10cSrcweir     const ::rtl::OUString & i_rIdref) const
989cdf0e10cSrcweir {
990cdf0e10cSrcweir     if (!isValidXmlId(i_rStreamName, i_rIdref))
991cdf0e10cSrcweir     {
992cdf0e10cSrcweir         throw lang::IllegalArgumentException(::rtl::OUString::createFromAscii(
993cdf0e10cSrcweir             "illegal XmlId"), 0, 0);
994cdf0e10cSrcweir     }
995cdf0e10cSrcweir 
996cdf0e10cSrcweir     const ClipboardXmlIdMap_t::const_iterator iter( m_XmlIdMap.find(i_rIdref) );
997cdf0e10cSrcweir     if (iter != m_XmlIdMap.end())
998cdf0e10cSrcweir     {
999cdf0e10cSrcweir         OSL_ENSURE(iter->second.first || iter->second.second,
1000cdf0e10cSrcweir             "null entry in m_XmlIdMap");
1001cdf0e10cSrcweir         return (isContentFile(i_rStreamName))
1002cdf0e10cSrcweir             ?  &iter->second.first
1003cdf0e10cSrcweir             :  &iter->second.second;
1004cdf0e10cSrcweir     }
1005cdf0e10cSrcweir     else
1006cdf0e10cSrcweir     {
1007cdf0e10cSrcweir         return 0;
1008cdf0e10cSrcweir     }
1009cdf0e10cSrcweir }
1010cdf0e10cSrcweir 
1011cdf0e10cSrcweir Metadatable*
LookupElement(const::rtl::OUString & i_rStreamName,const::rtl::OUString & i_rIdref) const1012cdf0e10cSrcweir XmlIdRegistryClipboard::XmlIdRegistry_Impl::LookupElement(
1013cdf0e10cSrcweir     const ::rtl::OUString & i_rStreamName,
1014cdf0e10cSrcweir     const ::rtl::OUString & i_rIdref) const
1015cdf0e10cSrcweir {
1016cdf0e10cSrcweir     Metadatable * const * ppEntry = LookupEntry(i_rStreamName, i_rIdref);
1017cdf0e10cSrcweir     return ppEntry ? *ppEntry : 0;
1018cdf0e10cSrcweir }
1019cdf0e10cSrcweir 
1020cdf0e10cSrcweir bool
LookupXmlId(const Metadatable & i_rObject,::rtl::OUString & o_rStream,::rtl::OUString & o_rIdref,MetadatableClipboard const * & o_rpLink) const1021cdf0e10cSrcweir XmlIdRegistryClipboard::XmlIdRegistry_Impl::LookupXmlId(
1022cdf0e10cSrcweir     const Metadatable& i_rObject,
1023cdf0e10cSrcweir     ::rtl::OUString & o_rStream, ::rtl::OUString & o_rIdref,
1024cdf0e10cSrcweir     MetadatableClipboard const* &o_rpLink) const
1025cdf0e10cSrcweir {
1026cdf0e10cSrcweir     const ClipboardXmlIdReverseMap_t::const_iterator iter(
1027cdf0e10cSrcweir         m_XmlIdReverseMap.find(&i_rObject) );
1028cdf0e10cSrcweir     if (iter != m_XmlIdReverseMap.end())
1029cdf0e10cSrcweir     {
1030cdf0e10cSrcweir         OSL_ENSURE(!iter->second.m_Stream.equalsAscii(""),
1031cdf0e10cSrcweir             "null stream in m_XmlIdReverseMap");
1032cdf0e10cSrcweir         OSL_ENSURE(!iter->second.m_XmlId.equalsAscii(""),
1033cdf0e10cSrcweir             "null id in m_XmlIdReverseMap");
1034cdf0e10cSrcweir         o_rStream = iter->second.m_Stream;
1035cdf0e10cSrcweir         o_rIdref  = iter->second.m_XmlId;
1036cdf0e10cSrcweir         o_rpLink  = iter->second.m_pLink.get();
1037cdf0e10cSrcweir         return true;
1038cdf0e10cSrcweir     }
1039cdf0e10cSrcweir     else
1040cdf0e10cSrcweir     {
1041cdf0e10cSrcweir         return false;
1042cdf0e10cSrcweir     }
1043cdf0e10cSrcweir }
1044cdf0e10cSrcweir 
1045cdf0e10cSrcweir bool
TryInsertMetadatable(Metadatable & i_rObject,const::rtl::OUString & i_rStreamName,const::rtl::OUString & i_rIdref)1046cdf0e10cSrcweir XmlIdRegistryClipboard::XmlIdRegistry_Impl::TryInsertMetadatable(
1047cdf0e10cSrcweir     Metadatable & i_rObject,
1048cdf0e10cSrcweir     const ::rtl::OUString & i_rStreamName, const ::rtl::OUString & i_rIdref)
1049cdf0e10cSrcweir {
1050cdf0e10cSrcweir     bool bContent( isContentFile(i_rStreamName) );
1051cdf0e10cSrcweir     OSL_ENSURE(isContentFile(i_rStreamName) || isStylesFile(i_rStreamName),
1052cdf0e10cSrcweir         "invalid stream");
1053cdf0e10cSrcweir 
1054cdf0e10cSrcweir     //wntmsci12 won't parse this:
1055cdf0e10cSrcweir //    Metadatable ** ppEntry( LookupEntry(i_rStreamName, i_rIdref) );
1056cdf0e10cSrcweir     Metadatable ** ppEntry = LookupEntry(i_rStreamName, i_rIdref);
1057cdf0e10cSrcweir     if (ppEntry)
1058cdf0e10cSrcweir     {
1059cdf0e10cSrcweir         if (*ppEntry)
1060cdf0e10cSrcweir         {
1061cdf0e10cSrcweir             return false;
1062cdf0e10cSrcweir         }
1063cdf0e10cSrcweir         else
1064cdf0e10cSrcweir         {
1065cdf0e10cSrcweir             *ppEntry = &i_rObject;
1066cdf0e10cSrcweir             return true;
1067cdf0e10cSrcweir         }
1068cdf0e10cSrcweir     }
1069cdf0e10cSrcweir     else
1070cdf0e10cSrcweir     {
1071cdf0e10cSrcweir         m_XmlIdMap.insert(::std::make_pair(i_rIdref, bContent
1072cdf0e10cSrcweir             ? ::std::make_pair( &i_rObject, static_cast<Metadatable*>(0) )
1073cdf0e10cSrcweir             : ::std::make_pair( static_cast<Metadatable*>(0), &i_rObject )));
1074cdf0e10cSrcweir         return true;
1075cdf0e10cSrcweir     }
1076cdf0e10cSrcweir }
1077cdf0e10cSrcweir 
1078cdf0e10cSrcweir //=============================================================================
1079cdf0e10cSrcweir // Clipboard XML ID Registry
1080cdf0e10cSrcweir 
1081cdf0e10cSrcweir 
XmlIdRegistryClipboard()1082cdf0e10cSrcweir XmlIdRegistryClipboard::XmlIdRegistryClipboard()
1083cdf0e10cSrcweir     :   m_pImpl( new XmlIdRegistry_Impl )
1084cdf0e10cSrcweir {
1085cdf0e10cSrcweir }
1086cdf0e10cSrcweir 
~XmlIdRegistryClipboard()1087cdf0e10cSrcweir XmlIdRegistryClipboard::~XmlIdRegistryClipboard()
1088cdf0e10cSrcweir {
1089cdf0e10cSrcweir }
1090cdf0e10cSrcweir 
1091cdf0e10cSrcweir bool
LookupXmlId(const Metadatable & i_rObject,::rtl::OUString & o_rStream,::rtl::OUString & o_rIdref) const1092cdf0e10cSrcweir XmlIdRegistryClipboard::LookupXmlId(
1093cdf0e10cSrcweir     const Metadatable& i_rObject,
1094cdf0e10cSrcweir     ::rtl::OUString & o_rStream, ::rtl::OUString & o_rIdref) const
1095cdf0e10cSrcweir {
1096cdf0e10cSrcweir     const MetadatableClipboard * pLink;
1097cdf0e10cSrcweir     return m_pImpl->LookupXmlId(i_rObject, o_rStream, o_rIdref, pLink);
1098cdf0e10cSrcweir }
1099cdf0e10cSrcweir 
1100cdf0e10cSrcweir Metadatable*
LookupElement(const::rtl::OUString & i_rStreamName,const::rtl::OUString & i_rIdref) const1101cdf0e10cSrcweir XmlIdRegistryClipboard::LookupElement(
1102cdf0e10cSrcweir     const ::rtl::OUString & i_rStreamName,
1103cdf0e10cSrcweir     const ::rtl::OUString & i_rIdref) const
1104cdf0e10cSrcweir {
1105cdf0e10cSrcweir     return m_pImpl->LookupElement(i_rStreamName, i_rIdref);
1106cdf0e10cSrcweir }
1107cdf0e10cSrcweir 
1108cdf0e10cSrcweir bool
TryRegisterMetadatable(Metadatable & i_rObject,::rtl::OUString const & i_rStreamName,::rtl::OUString const & i_rIdref)1109cdf0e10cSrcweir XmlIdRegistryClipboard::TryRegisterMetadatable(Metadatable & i_rObject,
1110cdf0e10cSrcweir     ::rtl::OUString const& i_rStreamName, ::rtl::OUString const& i_rIdref)
1111cdf0e10cSrcweir {
1112cdf0e10cSrcweir     OSL_TRACE("TryRegisterMetadatable: %p (%s#%s)\n", &i_rObject,
1113cdf0e10cSrcweir         ::rtl::OUStringToOString(i_rStreamName, RTL_TEXTENCODING_UTF8).getStr(),
1114cdf0e10cSrcweir         ::rtl::OUStringToOString(i_rIdref, RTL_TEXTENCODING_UTF8).getStr());
1115cdf0e10cSrcweir 
1116cdf0e10cSrcweir     OSL_ENSURE(!dynamic_cast<MetadatableUndo*>(&i_rObject),
1117cdf0e10cSrcweir         "TryRegisterMetadatable called for MetadatableUndo?");
1118cdf0e10cSrcweir     OSL_ENSURE(!dynamic_cast<MetadatableClipboard*>(&i_rObject),
1119cdf0e10cSrcweir         "TryRegisterMetadatable called for MetadatableClipboard?");
1120cdf0e10cSrcweir 
1121cdf0e10cSrcweir     if (!isValidXmlId(i_rStreamName, i_rIdref))
1122cdf0e10cSrcweir     {
1123cdf0e10cSrcweir         throw lang::IllegalArgumentException(::rtl::OUString::createFromAscii(
1124cdf0e10cSrcweir             "illegal XmlId"), 0, 0);
1125cdf0e10cSrcweir     }
1126cdf0e10cSrcweir     if (i_rObject.IsInContent()
1127cdf0e10cSrcweir         ?   !isContentFile(i_rStreamName)
1128cdf0e10cSrcweir         :   !isStylesFile(i_rStreamName))
1129cdf0e10cSrcweir     {
1130cdf0e10cSrcweir         throw lang::IllegalArgumentException(::rtl::OUString::createFromAscii(
1131cdf0e10cSrcweir             "illegal XmlId: wrong stream"), 0, 0);
1132cdf0e10cSrcweir     }
1133cdf0e10cSrcweir 
1134cdf0e10cSrcweir     ::rtl::OUString old_path;
1135cdf0e10cSrcweir     ::rtl::OUString old_idref;
1136cdf0e10cSrcweir     const MetadatableClipboard * pLink;
1137cdf0e10cSrcweir     m_pImpl->LookupXmlId(i_rObject, old_path, old_idref, pLink);
1138cdf0e10cSrcweir     if (old_path  == i_rStreamName && old_idref == i_rIdref)
1139cdf0e10cSrcweir     {
1140cdf0e10cSrcweir         return (m_pImpl->LookupElement(old_path, old_idref) == &i_rObject);
1141cdf0e10cSrcweir     }
1142cdf0e10cSrcweir     ClipboardXmlIdMap_t::iterator old_id( m_pImpl->m_XmlIdMap.end() );
1143cdf0e10cSrcweir     if (!old_idref.equalsAscii(""))
1144cdf0e10cSrcweir     {
1145cdf0e10cSrcweir         old_id = m_pImpl->m_XmlIdMap.find(old_idref);
1146cdf0e10cSrcweir         OSL_ENSURE(old_id != m_pImpl->m_XmlIdMap.end(), "old id not found");
1147cdf0e10cSrcweir     }
1148cdf0e10cSrcweir     if (m_pImpl->TryInsertMetadatable(i_rObject, i_rStreamName, i_rIdref))
1149cdf0e10cSrcweir     {
1150cdf0e10cSrcweir         rmIter(m_pImpl->m_XmlIdMap, old_id, old_path, i_rObject);
1151cdf0e10cSrcweir         m_pImpl->m_XmlIdReverseMap[&i_rObject] =
1152cdf0e10cSrcweir             RMapEntry(i_rStreamName, i_rIdref);
1153cdf0e10cSrcweir         return true;
1154cdf0e10cSrcweir     }
1155cdf0e10cSrcweir     else
1156cdf0e10cSrcweir     {
1157cdf0e10cSrcweir         return false;
1158cdf0e10cSrcweir     }
1159cdf0e10cSrcweir }
1160cdf0e10cSrcweir 
1161cdf0e10cSrcweir void
RegisterMetadatableAndCreateID(Metadatable & i_rObject)1162cdf0e10cSrcweir XmlIdRegistryClipboard::RegisterMetadatableAndCreateID(Metadatable & i_rObject)
1163cdf0e10cSrcweir {
1164cdf0e10cSrcweir     OSL_TRACE("RegisterMetadatableAndCreateID: %p\n", &i_rObject);
1165cdf0e10cSrcweir 
1166cdf0e10cSrcweir     OSL_ENSURE(!dynamic_cast<MetadatableUndo*>(&i_rObject),
1167cdf0e10cSrcweir         "RegisterMetadatableAndCreateID called for MetadatableUndo?");
1168cdf0e10cSrcweir     OSL_ENSURE(!dynamic_cast<MetadatableClipboard*>(&i_rObject),
1169cdf0e10cSrcweir         "RegisterMetadatableAndCreateID called for MetadatableClipboard?");
1170cdf0e10cSrcweir 
1171cdf0e10cSrcweir     bool isInContent( i_rObject.IsInContent() );
1172cdf0e10cSrcweir     ::rtl::OUString stream( ::rtl::OUString::createFromAscii(
1173cdf0e10cSrcweir         isInContent ? s_content : s_styles ) );
1174cdf0e10cSrcweir 
1175cdf0e10cSrcweir     ::rtl::OUString old_path;
1176cdf0e10cSrcweir     ::rtl::OUString old_idref;
1177cdf0e10cSrcweir     LookupXmlId(i_rObject, old_path, old_idref);
1178cdf0e10cSrcweir     if (!old_idref.equalsAscii("") &&
1179cdf0e10cSrcweir         (m_pImpl->LookupElement(old_path, old_idref) == &i_rObject))
1180cdf0e10cSrcweir     {
1181cdf0e10cSrcweir         return;
1182cdf0e10cSrcweir     }
1183cdf0e10cSrcweir 
1184cdf0e10cSrcweir     // create id
1185cdf0e10cSrcweir     const ::rtl::OUString id( create_id(m_pImpl->m_XmlIdMap) );
1186cdf0e10cSrcweir     OSL_ENSURE(m_pImpl->m_XmlIdMap.find(id) == m_pImpl->m_XmlIdMap.end(),
1187cdf0e10cSrcweir         "created id is in use");
1188cdf0e10cSrcweir     m_pImpl->m_XmlIdMap.insert(::std::make_pair(id, isInContent
1189cdf0e10cSrcweir         ? ::std::make_pair( &i_rObject, static_cast<Metadatable*>(0) )
1190cdf0e10cSrcweir         : ::std::make_pair( static_cast<Metadatable*>(0), &i_rObject )));
1191cdf0e10cSrcweir     // N.B.: if i_rObject had a latent XmlId, then we implicitly delete the
1192cdf0e10cSrcweir     // MetadatableClipboard and thus the latent XmlId here
1193cdf0e10cSrcweir     m_pImpl->m_XmlIdReverseMap[&i_rObject] = RMapEntry(stream, id);
1194cdf0e10cSrcweir }
1195cdf0e10cSrcweir 
UnregisterMetadatable(const Metadatable & i_rObject)1196cdf0e10cSrcweir void XmlIdRegistryClipboard::UnregisterMetadatable(const Metadatable& i_rObject)
1197cdf0e10cSrcweir {
1198cdf0e10cSrcweir     OSL_TRACE("UnregisterMetadatable: %p\n", &i_rObject);
1199cdf0e10cSrcweir 
1200cdf0e10cSrcweir     ::rtl::OUString path;
1201cdf0e10cSrcweir     ::rtl::OUString idref;
1202cdf0e10cSrcweir     const MetadatableClipboard * pLink;
1203cdf0e10cSrcweir     if (!m_pImpl->LookupXmlId(i_rObject, path, idref, pLink))
1204cdf0e10cSrcweir     {
1205cdf0e10cSrcweir         OSL_ENSURE(false, "unregister: no xml id?");
1206cdf0e10cSrcweir         return;
1207cdf0e10cSrcweir     }
1208cdf0e10cSrcweir     const ClipboardXmlIdMap_t::iterator iter( m_pImpl->m_XmlIdMap.find(idref) );
1209cdf0e10cSrcweir     if (iter != m_pImpl->m_XmlIdMap.end())
1210cdf0e10cSrcweir     {
1211cdf0e10cSrcweir         rmIter(m_pImpl->m_XmlIdMap, iter, path, i_rObject);
1212cdf0e10cSrcweir     }
1213cdf0e10cSrcweir }
1214cdf0e10cSrcweir 
1215cdf0e10cSrcweir 
RemoveXmlIdForElement(const Metadatable & i_rObject)1216cdf0e10cSrcweir void XmlIdRegistryClipboard::RemoveXmlIdForElement(const Metadatable& i_rObject)
1217cdf0e10cSrcweir {
1218cdf0e10cSrcweir     OSL_TRACE("RemoveXmlIdForElement: %p\n", &i_rObject);
1219cdf0e10cSrcweir 
1220cdf0e10cSrcweir     ClipboardXmlIdReverseMap_t::iterator iter(
1221cdf0e10cSrcweir         m_pImpl->m_XmlIdReverseMap.find(&i_rObject) );
1222cdf0e10cSrcweir     if (iter != m_pImpl->m_XmlIdReverseMap.end())
1223cdf0e10cSrcweir     {
1224cdf0e10cSrcweir         OSL_ENSURE(!iter->second.m_XmlId.equalsAscii(""),
1225cdf0e10cSrcweir             "null id in m_XmlIdReverseMap");
1226cdf0e10cSrcweir         m_pImpl->m_XmlIdReverseMap.erase(iter);
1227cdf0e10cSrcweir     }
1228cdf0e10cSrcweir }
1229cdf0e10cSrcweir 
1230cdf0e10cSrcweir // -------------------------------------------------------------------
1231cdf0e10cSrcweir 
1232cdf0e10cSrcweir ::boost::shared_ptr<MetadatableClipboard>
CreateClipboard(const bool i_isInContent)1233cdf0e10cSrcweir XmlIdRegistryClipboard::CreateClipboard(const bool i_isInContent)
1234cdf0e10cSrcweir {
1235cdf0e10cSrcweir     OSL_TRACE("CreateClipboard: \n");
1236cdf0e10cSrcweir 
1237cdf0e10cSrcweir     return ::boost::shared_ptr<MetadatableClipboard>(
1238cdf0e10cSrcweir         new MetadatableClipboard(i_isInContent) );
1239cdf0e10cSrcweir }
1240cdf0e10cSrcweir 
1241cdf0e10cSrcweir MetadatableClipboard &
RegisterCopyClipboard(Metadatable & i_rCopy,beans::StringPair const & i_rReference,const bool i_isLatent)1242cdf0e10cSrcweir XmlIdRegistryClipboard::RegisterCopyClipboard(Metadatable & i_rCopy,
1243cdf0e10cSrcweir     beans::StringPair const & i_rReference,
1244cdf0e10cSrcweir     const bool i_isLatent)
1245cdf0e10cSrcweir {
1246cdf0e10cSrcweir     OSL_TRACE("RegisterCopyClipboard: %p -> "/*"%p"*/"(%s#%s) (%d)\n",
1247cdf0e10cSrcweir         /*&i_rSource,*/ &i_rCopy,
1248cdf0e10cSrcweir         ::rtl::OUStringToOString(i_rReference.First,
1249cdf0e10cSrcweir             RTL_TEXTENCODING_UTF8).getStr(),
1250cdf0e10cSrcweir         ::rtl::OUStringToOString(i_rReference.Second,
1251cdf0e10cSrcweir             RTL_TEXTENCODING_UTF8).getStr(),
1252cdf0e10cSrcweir         i_isLatent);
1253cdf0e10cSrcweir 
1254cdf0e10cSrcweir     // N.B.: when copying to the clipboard, the selection is always inserted
1255cdf0e10cSrcweir     //       into the body, even if the source is a header/footer!
1256cdf0e10cSrcweir     //       so we do not check whether the stream is right in this function
1257cdf0e10cSrcweir 
1258cdf0e10cSrcweir     if (!isValidXmlId(i_rReference.First, i_rReference.Second))
1259cdf0e10cSrcweir     {
1260cdf0e10cSrcweir         throw lang::IllegalArgumentException(::rtl::OUString::createFromAscii(
1261cdf0e10cSrcweir             "illegal XmlId"), 0, 0);
1262cdf0e10cSrcweir     }
1263cdf0e10cSrcweir 
1264cdf0e10cSrcweir     if (!i_isLatent)
1265cdf0e10cSrcweir     {
1266cdf0e10cSrcweir         // this should succeed assuming clipboard has a single source document
1267cdf0e10cSrcweir         const bool success( m_pImpl->TryInsertMetadatable(i_rCopy,
1268cdf0e10cSrcweir                 i_rReference.First, i_rReference.Second) );
1269cdf0e10cSrcweir         OSL_ENSURE(success, "RegisterCopyClipboard: TryInsert failed?");
1270cdf0e10cSrcweir         (void) success;
1271cdf0e10cSrcweir     }
1272cdf0e10cSrcweir     const ::boost::shared_ptr<MetadatableClipboard> pLink(
1273cdf0e10cSrcweir         CreateClipboard( isContentFile(i_rReference.First)) );
1274cdf0e10cSrcweir     m_pImpl->m_XmlIdReverseMap.insert(::std::make_pair(&i_rCopy,
1275cdf0e10cSrcweir         RMapEntry(i_rReference.First, i_rReference.Second, pLink)));
1276cdf0e10cSrcweir     return *pLink.get();
1277cdf0e10cSrcweir }
1278cdf0e10cSrcweir 
1279cdf0e10cSrcweir MetadatableClipboard const*
SourceLink(Metadatable const & i_rObject)1280cdf0e10cSrcweir XmlIdRegistryClipboard::SourceLink(Metadatable const& i_rObject)
1281cdf0e10cSrcweir {
1282cdf0e10cSrcweir     ::rtl::OUString path;
1283cdf0e10cSrcweir     ::rtl::OUString idref;
1284cdf0e10cSrcweir     const MetadatableClipboard * pLink( 0 );
1285cdf0e10cSrcweir     m_pImpl->LookupXmlId(i_rObject, path, idref, pLink);
1286cdf0e10cSrcweir     return pLink;
1287cdf0e10cSrcweir }
1288cdf0e10cSrcweir 
1289cdf0e10cSrcweir 
1290cdf0e10cSrcweir //=============================================================================
1291cdf0e10cSrcweir // Metadatable mixin
1292cdf0e10cSrcweir 
1293cdf0e10cSrcweir 
~Metadatable()1294cdf0e10cSrcweir Metadatable::~Metadatable()
1295cdf0e10cSrcweir {
1296cdf0e10cSrcweir     RemoveMetadataReference();
1297cdf0e10cSrcweir }
1298cdf0e10cSrcweir 
RemoveMetadataReference()1299cdf0e10cSrcweir void Metadatable::RemoveMetadataReference()
1300cdf0e10cSrcweir {
1301cdf0e10cSrcweir     try
1302cdf0e10cSrcweir     {
1303cdf0e10cSrcweir         if (m_pReg)
1304cdf0e10cSrcweir         {
1305cdf0e10cSrcweir             m_pReg->UnregisterMetadatable( *this );
1306cdf0e10cSrcweir             m_pReg->RemoveXmlIdForElement( *this );
1307cdf0e10cSrcweir             m_pReg = 0;
1308cdf0e10cSrcweir         }
1309cdf0e10cSrcweir     }
1310cdf0e10cSrcweir     catch (uno::Exception &)
1311cdf0e10cSrcweir     {
1312cdf0e10cSrcweir         OSL_ENSURE(false, "Metadatable::RemoveMetadataReference: exception");
1313cdf0e10cSrcweir     }
1314cdf0e10cSrcweir }
1315cdf0e10cSrcweir 
1316cdf0e10cSrcweir // ::com::sun::star::rdf::XMetadatable:
1317cdf0e10cSrcweir beans::StringPair
GetMetadataReference() const1318cdf0e10cSrcweir Metadatable::GetMetadataReference() const
1319cdf0e10cSrcweir {
1320cdf0e10cSrcweir     if (m_pReg)
1321cdf0e10cSrcweir     {
1322cdf0e10cSrcweir         return m_pReg->GetXmlIdForElement(*this);
1323cdf0e10cSrcweir     }
1324cdf0e10cSrcweir     return beans::StringPair();
1325cdf0e10cSrcweir }
1326cdf0e10cSrcweir 
1327cdf0e10cSrcweir void
SetMetadataReference(const::com::sun::star::beans::StringPair & i_rReference)1328cdf0e10cSrcweir Metadatable::SetMetadataReference(
1329cdf0e10cSrcweir     const ::com::sun::star::beans::StringPair & i_rReference)
1330cdf0e10cSrcweir {
1331cdf0e10cSrcweir     if (i_rReference.Second.equalsAscii(""))
1332cdf0e10cSrcweir     {
1333cdf0e10cSrcweir         RemoveMetadataReference();
1334cdf0e10cSrcweir     }
1335cdf0e10cSrcweir     else
1336cdf0e10cSrcweir     {
1337cdf0e10cSrcweir         ::rtl::OUString streamName( i_rReference.First );
1338cdf0e10cSrcweir         if (streamName.equalsAscii(""))
1339cdf0e10cSrcweir         {
1340cdf0e10cSrcweir             // handle empty stream name as auto-detect.
1341cdf0e10cSrcweir             // necessary for importing flat file format.
1342cdf0e10cSrcweir             streamName = ::rtl::OUString::createFromAscii(
1343cdf0e10cSrcweir                             IsInContent() ? s_content : s_styles );
1344cdf0e10cSrcweir         }
1345cdf0e10cSrcweir         XmlIdRegistry & rReg( dynamic_cast<XmlIdRegistry&>( GetRegistry() ) );
1346cdf0e10cSrcweir         if (rReg.TryRegisterMetadatable(*this, streamName, i_rReference.Second))
1347cdf0e10cSrcweir         {
1348cdf0e10cSrcweir             m_pReg = &rReg;
1349cdf0e10cSrcweir         }
1350cdf0e10cSrcweir         else
1351cdf0e10cSrcweir         {
1352cdf0e10cSrcweir             throw lang::IllegalArgumentException(
1353cdf0e10cSrcweir                 ::rtl::OUString::createFromAscii("Metadatable::"
1354cdf0e10cSrcweir                     "SetMetadataReference: argument is invalid"), /*this*/0, 0);
1355cdf0e10cSrcweir         }
1356cdf0e10cSrcweir     }
1357cdf0e10cSrcweir }
1358cdf0e10cSrcweir 
EnsureMetadataReference()1359cdf0e10cSrcweir void Metadatable::EnsureMetadataReference()
1360cdf0e10cSrcweir {
1361cdf0e10cSrcweir     XmlIdRegistry& rReg(
1362cdf0e10cSrcweir         m_pReg ? *m_pReg : dynamic_cast<XmlIdRegistry&>( GetRegistry() ) );
1363cdf0e10cSrcweir     rReg.RegisterMetadatableAndCreateID( *this );
1364cdf0e10cSrcweir     m_pReg = &rReg;
1365cdf0e10cSrcweir }
1366cdf0e10cSrcweir 
GetRegistryConst(Metadatable const & i_rObject)1367cdf0e10cSrcweir const ::sfx2::IXmlIdRegistry& GetRegistryConst(Metadatable const& i_rObject)
1368cdf0e10cSrcweir {
1369cdf0e10cSrcweir     return const_cast< Metadatable& >( i_rObject ).GetRegistry();
1370cdf0e10cSrcweir }
1371cdf0e10cSrcweir 
1372cdf0e10cSrcweir void
RegisterAsCopyOf(Metadatable const & i_rSource,const bool i_bCopyPrecedesSource)1373cdf0e10cSrcweir Metadatable::RegisterAsCopyOf(Metadatable const & i_rSource,
1374cdf0e10cSrcweir     const bool i_bCopyPrecedesSource)
1375cdf0e10cSrcweir {
1376cdf0e10cSrcweir     OSL_ENSURE(typeid(*this) == typeid(i_rSource)
1377cdf0e10cSrcweir         || typeid(i_rSource) == typeid(MetadatableUndo)
1378cdf0e10cSrcweir         || typeid(*this)     == typeid(MetadatableUndo)
1379cdf0e10cSrcweir         || typeid(i_rSource) == typeid(MetadatableClipboard)
1380cdf0e10cSrcweir         || typeid(*this)     == typeid(MetadatableClipboard),
1381cdf0e10cSrcweir         "RegisterAsCopyOf element with different class?");
1382cdf0e10cSrcweir     OSL_ENSURE(!this->m_pReg, "RegisterAsCopyOf called on element with XmlId?");
1383cdf0e10cSrcweir 
1384cdf0e10cSrcweir     if (this->m_pReg)
1385cdf0e10cSrcweir     {
1386cdf0e10cSrcweir         RemoveMetadataReference();
1387cdf0e10cSrcweir     }
1388cdf0e10cSrcweir 
1389cdf0e10cSrcweir     try
1390cdf0e10cSrcweir     {
1391cdf0e10cSrcweir         if (i_rSource.m_pReg)
1392cdf0e10cSrcweir         {
1393cdf0e10cSrcweir             XmlIdRegistry & rReg(
1394cdf0e10cSrcweir                 dynamic_cast<XmlIdRegistry&>( GetRegistry() ) );
1395cdf0e10cSrcweir             if (i_rSource.m_pReg == &rReg)
1396cdf0e10cSrcweir             {
1397cdf0e10cSrcweir                 OSL_ENSURE(!IsInClipboard(),
1398cdf0e10cSrcweir                     "RegisterAsCopy: both in clipboard?");
1399cdf0e10cSrcweir                 if (!IsInClipboard())
1400cdf0e10cSrcweir                 {
1401cdf0e10cSrcweir                     XmlIdRegistryDocument & rRegDoc(
1402cdf0e10cSrcweir                         dynamic_cast<XmlIdRegistryDocument&>( rReg ) );
1403cdf0e10cSrcweir                     rRegDoc.RegisterCopy(i_rSource, *this,
1404cdf0e10cSrcweir                         i_bCopyPrecedesSource);
1405cdf0e10cSrcweir                     this->m_pReg = &rRegDoc;
1406cdf0e10cSrcweir                 }
1407cdf0e10cSrcweir                 return;
1408cdf0e10cSrcweir             }
1409cdf0e10cSrcweir             // source is in different document
1410cdf0e10cSrcweir             XmlIdRegistryDocument  * pRegDoc(
1411cdf0e10cSrcweir                 dynamic_cast<XmlIdRegistryDocument *>(&rReg) );
1412cdf0e10cSrcweir             XmlIdRegistryClipboard * pRegClp(
1413cdf0e10cSrcweir                 dynamic_cast<XmlIdRegistryClipboard*>(&rReg) );
1414cdf0e10cSrcweir 
1415cdf0e10cSrcweir             if (pRegClp)
1416cdf0e10cSrcweir             {
1417cdf0e10cSrcweir                 beans::StringPair SourceRef(
1418cdf0e10cSrcweir                     i_rSource.m_pReg->GetXmlIdForElement(i_rSource) );
1419cdf0e10cSrcweir                 bool isLatent( SourceRef.Second.equalsAscii("") );
1420cdf0e10cSrcweir                 XmlIdRegistryDocument * pSourceRegDoc(
1421cdf0e10cSrcweir                     dynamic_cast<XmlIdRegistryDocument*>(i_rSource.m_pReg) );
1422cdf0e10cSrcweir                 OSL_ENSURE(pSourceRegDoc, "RegisterAsCopyOf: 2 clipboards?");
1423cdf0e10cSrcweir                 if (!pSourceRegDoc) return;
1424cdf0e10cSrcweir                 // this is a copy _to_ the clipboard
1425cdf0e10cSrcweir                 if (isLatent)
1426cdf0e10cSrcweir                 {
1427cdf0e10cSrcweir                     pSourceRegDoc->LookupXmlId(i_rSource,
1428cdf0e10cSrcweir                         SourceRef.First, SourceRef.Second);
1429cdf0e10cSrcweir                 }
1430cdf0e10cSrcweir                 Metadatable & rLink(
1431cdf0e10cSrcweir                     pRegClp->RegisterCopyClipboard(*this, SourceRef, isLatent));
1432cdf0e10cSrcweir                 this->m_pReg = pRegClp;
1433cdf0e10cSrcweir                 // register as copy in the non-clipboard registry
1434cdf0e10cSrcweir                 pSourceRegDoc->RegisterCopy(i_rSource, rLink,
1435cdf0e10cSrcweir                     false); // i_bCopyPrecedesSource);
1436cdf0e10cSrcweir                 rLink.m_pReg = pSourceRegDoc;
1437cdf0e10cSrcweir             }
1438cdf0e10cSrcweir             else if (pRegDoc)
1439cdf0e10cSrcweir             {
1440cdf0e10cSrcweir                 XmlIdRegistryClipboard * pSourceRegClp(
1441cdf0e10cSrcweir                     dynamic_cast<XmlIdRegistryClipboard*>(i_rSource.m_pReg) );
1442cdf0e10cSrcweir                 OSL_ENSURE(pSourceRegClp,
1443cdf0e10cSrcweir                     "RegisterAsCopyOf: 2 non-clipboards?");
1444cdf0e10cSrcweir                 if (!pSourceRegClp) return;
1445cdf0e10cSrcweir                 const MetadatableClipboard * pLink(
1446cdf0e10cSrcweir                     pSourceRegClp->SourceLink(i_rSource) );
1447cdf0e10cSrcweir                 // may happen if src got its id via UNO call
1448cdf0e10cSrcweir                 if (!pLink) return;
1449cdf0e10cSrcweir                 // only register copy if clipboard content is from this SwDoc!
1450cdf0e10cSrcweir                 if (pLink && (&GetRegistryConst(*pLink) == pRegDoc))
1451cdf0e10cSrcweir                 {
1452cdf0e10cSrcweir                     // this is a copy _from_ the clipboard; check if the
1453cdf0e10cSrcweir                     // element is still in the same stream
1454cdf0e10cSrcweir                     // N.B.: we check the stream of pLink, not of i_rSource!
1455cdf0e10cSrcweir                     bool srcInContent( pLink->IsInContent() );
1456cdf0e10cSrcweir                     bool tgtInContent( this->IsInContent() );
1457cdf0e10cSrcweir                     if (srcInContent == tgtInContent)
1458cdf0e10cSrcweir                     {
1459cdf0e10cSrcweir                         pRegDoc->RegisterCopy(*pLink, *this,
1460cdf0e10cSrcweir                             true); // i_bCopyPrecedesSource);
1461cdf0e10cSrcweir                         this->m_pReg = pRegDoc;
1462cdf0e10cSrcweir                     }
1463cdf0e10cSrcweir                     // otherwise: stream change! do not register!
1464cdf0e10cSrcweir                 }
1465cdf0e10cSrcweir             }
1466cdf0e10cSrcweir             else
1467cdf0e10cSrcweir             {
1468cdf0e10cSrcweir                 OSL_ENSURE(false, "neither RegDoc nor RegClp cannot happen");
1469cdf0e10cSrcweir             }
1470cdf0e10cSrcweir #if 0
1471cdf0e10cSrcweir                 {
1472cdf0e10cSrcweir                     //FIXME: do we need this at all???
1473cdf0e10cSrcweir                     XmlIdRegistryDocument & rRegDoc(
1474cdf0e10cSrcweir                         dynamic_cast<XmlIdRegistryDocument&>( rReg ) );
1475cdf0e10cSrcweir                     {
1476cdf0e10cSrcweir                         if (rRegDoc.TryRegisterMetadatable(*this, SourceRef))
1477cdf0e10cSrcweir                         {
1478cdf0e10cSrcweir                             this->m_pReg = &rRegDoc;
1479cdf0e10cSrcweir                         }
1480cdf0e10cSrcweir                     }
1481cdf0e10cSrcweir                 }
1482cdf0e10cSrcweir #endif
1483cdf0e10cSrcweir         }
1484cdf0e10cSrcweir     }
1485cdf0e10cSrcweir     catch (uno::Exception &)
1486cdf0e10cSrcweir     {
1487cdf0e10cSrcweir         OSL_ENSURE(false, "Metadatable::RegisterAsCopyOf: exception");
1488cdf0e10cSrcweir     }
1489cdf0e10cSrcweir }
1490cdf0e10cSrcweir 
CreateUndo() const1491cdf0e10cSrcweir ::boost::shared_ptr<MetadatableUndo> Metadatable::CreateUndo() const
1492cdf0e10cSrcweir {
1493cdf0e10cSrcweir     OSL_ENSURE(!IsInUndo(), "CreateUndo called for object in undo?");
1494cdf0e10cSrcweir     OSL_ENSURE(!IsInClipboard(), "CreateUndo called for object in clipboard?");
1495cdf0e10cSrcweir     try
1496cdf0e10cSrcweir     {
1497cdf0e10cSrcweir         if (!IsInClipboard() && !IsInUndo() && m_pReg)
1498cdf0e10cSrcweir         {
1499cdf0e10cSrcweir             XmlIdRegistryDocument * pRegDoc(
1500cdf0e10cSrcweir                 dynamic_cast<XmlIdRegistryDocument*>( m_pReg ) );
1501cdf0e10cSrcweir             ::boost::shared_ptr<MetadatableUndo> pUndo(
1502cdf0e10cSrcweir                 pRegDoc->CreateUndo(*this) );
1503cdf0e10cSrcweir             pRegDoc->RegisterCopy(*this, *pUndo, false);
1504cdf0e10cSrcweir             pUndo->m_pReg = pRegDoc;
1505cdf0e10cSrcweir             return pUndo;
1506cdf0e10cSrcweir         }
1507cdf0e10cSrcweir     }
1508cdf0e10cSrcweir     catch (uno::Exception &)
1509cdf0e10cSrcweir     {
1510cdf0e10cSrcweir         OSL_ENSURE(false, "Metadatable::CreateUndo: exception");
1511cdf0e10cSrcweir     }
1512cdf0e10cSrcweir     return ::boost::shared_ptr<MetadatableUndo>();
1513cdf0e10cSrcweir }
1514cdf0e10cSrcweir 
CreateUndoForDelete()1515cdf0e10cSrcweir ::boost::shared_ptr<MetadatableUndo> Metadatable::CreateUndoForDelete()
1516cdf0e10cSrcweir {
1517cdf0e10cSrcweir     ::boost::shared_ptr<MetadatableUndo> const pUndo( CreateUndo() );
1518cdf0e10cSrcweir     RemoveMetadataReference();
1519cdf0e10cSrcweir     return pUndo;
1520cdf0e10cSrcweir }
1521cdf0e10cSrcweir 
RestoreMetadata(::boost::shared_ptr<MetadatableUndo> const & i_pUndo)1522cdf0e10cSrcweir void Metadatable::RestoreMetadata(
1523cdf0e10cSrcweir     ::boost::shared_ptr<MetadatableUndo> const& i_pUndo)
1524cdf0e10cSrcweir {
1525cdf0e10cSrcweir     OSL_ENSURE(!IsInUndo(), "RestoreMetadata called for object in undo?");
1526cdf0e10cSrcweir     OSL_ENSURE(!IsInClipboard(),
1527cdf0e10cSrcweir         "RestoreMetadata called for object in clipboard?");
1528cdf0e10cSrcweir     if (IsInClipboard() || IsInUndo()) return;
1529cdf0e10cSrcweir     RemoveMetadataReference();
1530cdf0e10cSrcweir     if (i_pUndo)
1531cdf0e10cSrcweir     {
1532cdf0e10cSrcweir         this->RegisterAsCopyOf(*i_pUndo, true);
1533cdf0e10cSrcweir     }
1534cdf0e10cSrcweir }
1535cdf0e10cSrcweir 
1536cdf0e10cSrcweir void
JoinMetadatable(Metadatable const & i_rOther,const bool i_isMergedEmpty,const bool i_isOtherEmpty)1537cdf0e10cSrcweir Metadatable::JoinMetadatable(Metadatable const & i_rOther,
1538cdf0e10cSrcweir     const bool i_isMergedEmpty, const bool i_isOtherEmpty)
1539cdf0e10cSrcweir {
1540cdf0e10cSrcweir     OSL_ENSURE(!IsInUndo(), "JoinMetadatables called for object in undo?");
1541cdf0e10cSrcweir     OSL_ENSURE(!IsInClipboard(),
1542cdf0e10cSrcweir         "JoinMetadatables called for object in clipboard?");
1543cdf0e10cSrcweir     if (IsInClipboard() || IsInUndo()) return;
1544cdf0e10cSrcweir 
1545cdf0e10cSrcweir     if (i_isOtherEmpty && !i_isMergedEmpty)
1546cdf0e10cSrcweir     {
1547cdf0e10cSrcweir         // other is empty, thus loses => nothing to do
1548cdf0e10cSrcweir         return;
1549cdf0e10cSrcweir     }
1550cdf0e10cSrcweir     if (i_isMergedEmpty && !i_isOtherEmpty)
1551cdf0e10cSrcweir     {
1552cdf0e10cSrcweir         this->RemoveMetadataReference();
1553cdf0e10cSrcweir         this->RegisterAsCopyOf(i_rOther, true);
1554cdf0e10cSrcweir         return;
1555cdf0e10cSrcweir     }
1556cdf0e10cSrcweir 
1557cdf0e10cSrcweir     if (!i_rOther.m_pReg)
1558cdf0e10cSrcweir     {
1559cdf0e10cSrcweir         // other doesn't have xmlid, thus loses => nothing to do
1560cdf0e10cSrcweir         return;
1561cdf0e10cSrcweir     }
1562cdf0e10cSrcweir     if (!m_pReg)
1563cdf0e10cSrcweir     {
1564cdf0e10cSrcweir         this->RegisterAsCopyOf(i_rOther, true);
1565cdf0e10cSrcweir         // assumption: i_rOther will be deleted, so don't unregister it here
1566cdf0e10cSrcweir         return;
1567cdf0e10cSrcweir     }
1568cdf0e10cSrcweir     try
1569cdf0e10cSrcweir     {
1570cdf0e10cSrcweir         XmlIdRegistryDocument * pRegDoc(
1571cdf0e10cSrcweir             dynamic_cast<XmlIdRegistryDocument*>( m_pReg ) );
1572cdf0e10cSrcweir         OSL_ENSURE(pRegDoc, "JoinMetadatable: no pRegDoc?");
1573cdf0e10cSrcweir         if (pRegDoc)
1574cdf0e10cSrcweir         {
1575cdf0e10cSrcweir             pRegDoc->JoinMetadatables(*this, i_rOther);
1576cdf0e10cSrcweir         }
1577cdf0e10cSrcweir     }
1578cdf0e10cSrcweir     catch (uno::Exception &)
1579cdf0e10cSrcweir     {
1580cdf0e10cSrcweir         OSL_ENSURE(false, "Metadatable::JoinMetadatable: exception");
1581cdf0e10cSrcweir     }
1582cdf0e10cSrcweir }
1583cdf0e10cSrcweir 
1584cdf0e10cSrcweir 
1585cdf0e10cSrcweir //=============================================================================
1586cdf0e10cSrcweir // XMetadatable mixin
1587cdf0e10cSrcweir 
1588cdf0e10cSrcweir // ::com::sun::star::rdf::XNode:
getStringValue()1589cdf0e10cSrcweir ::rtl::OUString SAL_CALL MetadatableMixin::getStringValue()
1590cdf0e10cSrcweir     throw (::com::sun::star::uno::RuntimeException)
1591cdf0e10cSrcweir {
1592cdf0e10cSrcweir     return getNamespace() + getLocalName();
1593cdf0e10cSrcweir }
1594cdf0e10cSrcweir 
1595cdf0e10cSrcweir // ::com::sun::star::rdf::XURI:
getLocalName()1596cdf0e10cSrcweir ::rtl::OUString SAL_CALL MetadatableMixin::getLocalName()
1597cdf0e10cSrcweir     throw (::com::sun::star::uno::RuntimeException)
1598cdf0e10cSrcweir {
1599cdf0e10cSrcweir     ::vos::OGuard aGuard( Application::GetSolarMutex() );
1600cdf0e10cSrcweir     beans::StringPair mdref( getMetadataReference() );
1601cdf0e10cSrcweir     if (!mdref.Second.getLength())
1602cdf0e10cSrcweir     {
1603cdf0e10cSrcweir         ensureMetadataReference(); // N.B.: side effect!
1604cdf0e10cSrcweir         mdref = getMetadataReference();
1605cdf0e10cSrcweir     }
1606cdf0e10cSrcweir     ::rtl::OUStringBuffer buf;
1607cdf0e10cSrcweir     buf.append(mdref.First);
1608cdf0e10cSrcweir     buf.append(static_cast<sal_Unicode>('#'));
1609cdf0e10cSrcweir     buf.append(mdref.Second);
1610cdf0e10cSrcweir     return buf.makeStringAndClear();
1611cdf0e10cSrcweir }
1612cdf0e10cSrcweir 
getNamespace()1613cdf0e10cSrcweir ::rtl::OUString SAL_CALL MetadatableMixin::getNamespace()
1614cdf0e10cSrcweir     throw (::com::sun::star::uno::RuntimeException)
1615cdf0e10cSrcweir {
1616cdf0e10cSrcweir     ::vos::OGuard aGuard( Application::GetSolarMutex() );
1617cdf0e10cSrcweir     const uno::Reference< frame::XModel > xModel( GetModel() );
1618cdf0e10cSrcweir     const uno::Reference< rdf::XURI > xDMA( xModel, uno::UNO_QUERY_THROW );
1619cdf0e10cSrcweir     return xDMA->getStringValue();
1620cdf0e10cSrcweir }
1621cdf0e10cSrcweir 
1622cdf0e10cSrcweir // ::com::sun::star::rdf::XMetadatable:
1623cdf0e10cSrcweir beans::StringPair SAL_CALL
getMetadataReference()1624cdf0e10cSrcweir MetadatableMixin::getMetadataReference()
1625cdf0e10cSrcweir throw (uno::RuntimeException)
1626cdf0e10cSrcweir {
1627cdf0e10cSrcweir     ::vos::OGuard aGuard( Application::GetSolarMutex() );
1628cdf0e10cSrcweir 
1629cdf0e10cSrcweir     Metadatable *const pObject( GetCoreObject() );
1630cdf0e10cSrcweir     if (!pObject)
1631cdf0e10cSrcweir     {
1632cdf0e10cSrcweir         throw uno::RuntimeException(
1633cdf0e10cSrcweir             ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
1634cdf0e10cSrcweir                 "MetadatableMixin: cannot get core object; not inserted?")),
1635cdf0e10cSrcweir             *this);
1636cdf0e10cSrcweir     }
1637cdf0e10cSrcweir     return pObject->GetMetadataReference();
1638cdf0e10cSrcweir }
1639cdf0e10cSrcweir 
1640cdf0e10cSrcweir void SAL_CALL
setMetadataReference(const beans::StringPair & i_rReference)1641cdf0e10cSrcweir MetadatableMixin::setMetadataReference(
1642cdf0e10cSrcweir     const beans::StringPair & i_rReference)
1643cdf0e10cSrcweir throw (uno::RuntimeException, lang::IllegalArgumentException)
1644cdf0e10cSrcweir {
1645cdf0e10cSrcweir     ::vos::OGuard aGuard( Application::GetSolarMutex() );
1646cdf0e10cSrcweir 
1647cdf0e10cSrcweir     Metadatable *const pObject( GetCoreObject() );
1648cdf0e10cSrcweir     if (!pObject)
1649cdf0e10cSrcweir     {
1650cdf0e10cSrcweir         throw uno::RuntimeException(
1651cdf0e10cSrcweir             ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
1652cdf0e10cSrcweir                 "MetadatableMixin: cannot get core object; not inserted?")),
1653cdf0e10cSrcweir             *this);
1654cdf0e10cSrcweir     }
1655cdf0e10cSrcweir     return pObject->SetMetadataReference(i_rReference);
1656cdf0e10cSrcweir }
1657cdf0e10cSrcweir 
ensureMetadataReference()1658cdf0e10cSrcweir void SAL_CALL MetadatableMixin::ensureMetadataReference()
1659cdf0e10cSrcweir throw (uno::RuntimeException)
1660cdf0e10cSrcweir {
1661cdf0e10cSrcweir     ::vos::OGuard aGuard( Application::GetSolarMutex() );
1662cdf0e10cSrcweir 
1663cdf0e10cSrcweir     Metadatable *const pObject( GetCoreObject() );
1664cdf0e10cSrcweir     if (!pObject)
1665cdf0e10cSrcweir     {
1666cdf0e10cSrcweir         throw uno::RuntimeException(
1667cdf0e10cSrcweir             ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
1668cdf0e10cSrcweir                 "MetadatableMixin: cannot get core object; not inserted?")),
1669cdf0e10cSrcweir             *this);
1670cdf0e10cSrcweir     }
1671cdf0e10cSrcweir     return pObject->EnsureMetadataReference();
1672cdf0e10cSrcweir }
1673cdf0e10cSrcweir 
1674cdf0e10cSrcweir } // namespace sfx2
1675cdf0e10cSrcweir 
1676cdf0e10cSrcweir 
1677cdf0e10cSrcweir //=============================================================================
1678cdf0e10cSrcweir 
1679cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
1680cdf0e10cSrcweir 
1681cdf0e10cSrcweir #include <stdio.h>
1682cdf0e10cSrcweir 
1683cdf0e10cSrcweir static void dump(sfx2::XmlIdList_t * pList)
1684cdf0e10cSrcweir #ifdef GCC
1685cdf0e10cSrcweir __attribute__ ((unused))
1686cdf0e10cSrcweir #endif
1687cdf0e10cSrcweir ;
dump(sfx2::XmlIdList_t * pList)1688cdf0e10cSrcweir static void dump(sfx2::XmlIdList_t * pList)
1689cdf0e10cSrcweir {
1690cdf0e10cSrcweir     fprintf(stderr, "\nXmlIdList(%p):  ", pList);
1691cdf0e10cSrcweir     for (sfx2::XmlIdList_t::iterator i = pList->begin(); i != pList->end(); ++i)
1692cdf0e10cSrcweir     {
1693cdf0e10cSrcweir         fprintf(stderr, "%p  ", *i);
1694cdf0e10cSrcweir     }
1695cdf0e10cSrcweir     fprintf(stderr, "\n");
1696cdf0e10cSrcweir }
1697cdf0e10cSrcweir 
1698cdf0e10cSrcweir #endif
1699cdf0e10cSrcweir 
1700