update dev300-m58
[ooovba.git] / stoc / source / implementationregistration / mergekeys.cxx
blobfb5c952c3da515aa2b2bb25334594b71c9298725
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: mergekeys.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_stoc.hxx"
34 #include <vector>
36 #include <com/sun/star/registry/XRegistryKey.hpp>
37 #include <com/sun/star/registry/MergeConflictException.hpp>
39 #include "mergekeys.hxx"
41 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
43 using namespace ::rtl;
44 using namespace ::osl;
45 using namespace ::com::sun::star::uno;
46 using namespace ::com::sun::star;
48 namespace stoc_impreg
51 struct Link
53 OUString m_name;
54 OUString m_target;
56 inline Link( OUString const & name, OUString const & target )
57 : m_name( name )
58 , m_target( target )
61 typedef ::std::vector< Link > t_links;
63 //==================================================================================================
64 static void mergeKeys(
65 Reference< registry::XRegistryKey > const & xDest,
66 Reference< registry::XRegistryKey > const & xSource,
67 t_links & links )
68 // throw( registry::InvalidRegistryException, registry::MergeConflictException, RuntimeException )
70 if (!xSource.is() || !xSource->isValid()) {
71 throw registry::InvalidRegistryException(
72 OUSTR("source key is null or invalid!"),
73 Reference<XInterface>() );
75 if (!xDest.is() || !xDest->isValid()) {
76 throw registry::InvalidRegistryException(
77 OUSTR("destination key is null or invalid!"),
78 Reference<XInterface>() );
81 // write value
82 switch (xSource->getValueType())
84 case registry::RegistryValueType_NOT_DEFINED:
85 break;
86 case registry::RegistryValueType_LONG:
87 xDest->setLongValue( xSource->getLongValue() );
88 break;
89 case registry::RegistryValueType_ASCII:
90 xDest->setAsciiValue( xSource->getAsciiValue() );
91 break;
92 case registry::RegistryValueType_STRING:
93 xDest->setStringValue( xSource->getStringValue() );
94 break;
95 case registry::RegistryValueType_BINARY:
96 xDest->setBinaryValue( xSource->getBinaryValue() );
97 break;
98 case registry::RegistryValueType_LONGLIST:
99 xDest->setLongListValue( xSource->getLongListValue() );
100 break;
101 case registry::RegistryValueType_ASCIILIST:
102 xDest->setAsciiListValue( xSource->getAsciiListValue() );
103 break;
104 case registry::RegistryValueType_STRINGLIST:
105 xDest->setStringListValue( xSource->getStringListValue() );
106 break;
107 default:
108 OSL_ASSERT(false);
109 break;
112 // sub keys
113 Sequence< OUString > sourceKeys( xSource->getKeyNames() );
114 OUString const * pSourceKeys = sourceKeys.getConstArray();
115 for ( sal_Int32 nPos = sourceKeys.getLength(); nPos--; )
117 // key name
118 OUString name( pSourceKeys[ nPos ] );
119 sal_Int32 nSlash = name.lastIndexOf( '/' );
120 if (nSlash >= 0)
122 name = name.copy( nSlash +1 );
125 if (xSource->getKeyType( name ) == registry::RegistryKeyType_KEY)
127 // try to open exisiting dest key or create new one
128 Reference< registry::XRegistryKey > xDestKey( xDest->createKey( name ) );
129 Reference< registry::XRegistryKey > xSourceKey( xSource->openKey( name ) );
130 mergeKeys( xDestKey, xSourceKey, links );
131 xSourceKey->closeKey();
132 xDestKey->closeKey();
134 else // link
136 // remove existing key
137 Reference< registry::XRegistryKey > xDestKey( xDest->openKey( name ) );
138 if (xDestKey.is() && xDestKey->isValid()) // something to remove
140 xDestKey->closeKey();
141 if (xDest->getKeyType( name ) == registry::RegistryKeyType_LINK)
143 xDest->deleteLink( name );
145 else
147 xDest->deleteKey( name );
151 links.push_back( Link(
152 pSourceKeys[ nPos ], // abs path
153 xSource->getResolvedName( name ) // abs resolved name
154 ) );
159 //==================================================================================================
160 void mergeKeys(
161 Reference< registry::XRegistryKey > const & xDest,
162 Reference< registry::XRegistryKey > const & xSource )
163 // throw( registry::InvalidRegistryException, registry::MergeConflictException, RuntimeException )
165 if (!xDest.is() || !xDest->isValid()) {
166 throw registry::InvalidRegistryException(
167 OUSTR("destination key is null or invalid!"),
168 Reference<XInterface>() );
170 if (xDest->isReadOnly())
172 throw registry::InvalidRegistryException(
173 OUString( RTL_CONSTASCII_USTRINGPARAM(
174 "destination registry is read-only! cannot merge!") ),
175 Reference< XInterface >() );
178 t_links links;
179 links.reserve( 16 );
180 mergeKeys( xDest, xSource, links );
182 for ( size_t nPos = links.size(); nPos--; )
184 Link const & r = links[ nPos ];
185 OSL_VERIFY( xDest->createLink( r.m_name, r.m_target ) );