Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <xmloff/unointerfacetouniqueidentifiermapper.hxx>
21 :
22 : using namespace ::com::sun::star;
23 : using ::com::sun::star::uno::Reference;
24 : using ::com::sun::star::uno::XInterface;
25 :
26 : namespace comphelper
27 : {
28 :
29 8562 : UnoInterfaceToUniqueIdentifierMapper::UnoInterfaceToUniqueIdentifierMapper()
30 8562 : : mnNextId( 1 )
31 : {
32 8562 : }
33 :
34 0 : const OUString& UnoInterfaceToUniqueIdentifierMapper::registerReference( const Reference< XInterface >& rInterface )
35 : {
36 : // Be certain that the references we store in our table are to the
37 : // leading / primary XInterface - cf. findReference
38 0 : uno::Reference< uno::XInterface > xRef( rInterface, uno::UNO_QUERY );
39 :
40 0 : IdMap_t::const_iterator aIter;
41 0 : if( findReference( xRef, aIter ) )
42 : {
43 0 : return (*aIter).first;
44 : }
45 : else
46 : {
47 0 : OUString aId( "id" );
48 0 : aId += OUString::number( mnNextId++ );
49 0 : return (*maEntries.insert( IdMap_t::value_type( aId, xRef ) ).first).first;
50 0 : }
51 : }
52 :
53 18 : bool UnoInterfaceToUniqueIdentifierMapper::registerReference( const OUString& rIdentifier, const Reference< XInterface >& rInterface )
54 : {
55 18 : IdMap_t::const_iterator aIter;
56 :
57 : // Be certain that the references we store in our table are to the
58 : // leading / primary XInterface - cf. findReference
59 18 : uno::Reference< uno::XInterface > xRef( rInterface, uno::UNO_QUERY );
60 :
61 18 : if( findReference( xRef, aIter ) )
62 : {
63 0 : return rIdentifier != (*aIter).first;
64 : }
65 18 : else if( findIdentifier( rIdentifier, aIter ) )
66 : {
67 1 : return false;
68 : }
69 : else
70 : {
71 17 : insertReference( rIdentifier, xRef );
72 : }
73 :
74 17 : return true;
75 : }
76 :
77 1 : void UnoInterfaceToUniqueIdentifierMapper::registerReferenceAlways( const OUString& rIdentifier, const Reference< XInterface >& rInterface )
78 : {
79 : // Be certain that the references we store in our table are to the
80 : // leading / primary XInterface - cf. findReference
81 1 : uno::Reference< uno::XInterface > xRef( rInterface, uno::UNO_QUERY );
82 :
83 1 : insertReference( rIdentifier, xRef );
84 1 : }
85 :
86 1429 : const OUString& UnoInterfaceToUniqueIdentifierMapper::getIdentifier( const Reference< XInterface >& rInterface ) const
87 : {
88 1429 : IdMap_t::const_iterator aIter;
89 1429 : if( findReference( rInterface, aIter ) )
90 : {
91 0 : return (*aIter).first;
92 : }
93 : else
94 : {
95 1429 : static const OUString aEmpty;
96 1429 : return aEmpty;
97 : }
98 : }
99 :
100 5 : const Reference< XInterface >& UnoInterfaceToUniqueIdentifierMapper::getReference( const OUString& rIdentifier ) const
101 : {
102 5 : IdMap_t::const_iterator aIter;
103 5 : if( findIdentifier( rIdentifier, aIter ) )
104 : {
105 5 : return (*aIter).second;
106 : }
107 : else
108 : {
109 0 : static const Reference< XInterface > aEmpty;
110 0 : return aEmpty;
111 : }
112 : }
113 :
114 1447 : bool UnoInterfaceToUniqueIdentifierMapper::findReference( const Reference< XInterface >& rInterface, IdMap_t::const_iterator& rIter ) const
115 : {
116 1447 : uno::Reference< uno::XInterface > xRef( rInterface, uno::UNO_QUERY );
117 :
118 1447 : rIter = maEntries.begin();
119 :
120 1447 : const IdMap_t::const_iterator aEnd( maEntries.end() );
121 2987 : while( rIter != aEnd )
122 : {
123 : // The Reference == operator, does a repeated queryInterface on
124 : // this to ensure we got the right XInterface base-class. However,
125 : // we can be sure that this has been done already by the time we
126 : // get to here.
127 93 : if( (*rIter).second.get() == xRef.get() )
128 0 : return true;
129 :
130 93 : ++rIter;
131 : }
132 :
133 1447 : return false;
134 : }
135 :
136 23 : bool UnoInterfaceToUniqueIdentifierMapper::findIdentifier( const OUString& rIdentifier, IdMap_t::const_iterator& rIter ) const
137 : {
138 23 : rIter = maEntries.find( rIdentifier );
139 23 : return rIter != maEntries.end();
140 : }
141 :
142 18 : void UnoInterfaceToUniqueIdentifierMapper::insertReference( const OUString& rIdentifier, const Reference< XInterface >& rInterface )
143 : {
144 18 : maEntries[rIdentifier] = rInterface;
145 :
146 : // see if this is a reference like something we would generate in the future
147 18 : const sal_Unicode *p = rIdentifier.getStr();
148 18 : sal_Int32 nLength = rIdentifier.getLength();
149 :
150 : // see if the identifier is 'id' followed by a pure integer value
151 18 : if( nLength < 2 || p[0] != 'i' || p[1] != 'd' )
152 0 : return;
153 :
154 18 : nLength -= 2;
155 18 : p += 2;
156 :
157 181 : while(nLength--)
158 : {
159 145 : if( (*p < '0') || (*p > '9') )
160 0 : return; // a custom id, that will never conflict with genereated id's
161 :
162 145 : p++;
163 : }
164 :
165 : // the identifier is a pure integer value
166 : // so we make sure we will never generate
167 : // an integer value like this one
168 18 : sal_Int32 nId = rIdentifier.copy(2).toInt32();
169 18 : if( mnNextId <= nId )
170 4 : mnNextId = nId + 1;
171 : }
172 :
173 : }
174 :
175 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|