update dev300-m58
[ooovba.git] / configmgr / source / api2 / translatechanges.cxx
blobd78c89212b43cf5ed892f0e916a0e258e8f26a79
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: translatechanges.cxx,v $
10 * $Revision: 1.13 $
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_configmgr.hxx"
34 #include "translatechanges.hxx"
35 #include "noderef.hxx"
36 #include "nodechange.hxx"
37 #include "nodechangeinfo.hxx"
38 #include "apifactory.hxx"
40 namespace configmgr
42 // ---------------------------------------------------------------------------------------------------
43 namespace css = ::com::sun::star;
44 namespace uno = css::uno;
45 namespace lang = css::lang;
46 namespace util = css::util;
47 namespace beans = css::beans;
48 namespace container = css::container;
49 // ---------------------------------------------------------------------------------------------------
51 namespace configuration
53 class NodeChange;
54 class NodeChanges;
55 class NodeRef;
56 class NodeID;
58 // ---------------------------------------------------------------------------------------------------
60 namespace configapi
62 // ---------------------------------------------------------------------------------------------------
63 //interpreting NodeChanges
64 // resolve the relative path from a given base to the changed node
65 bool resolveChangeLocation(configuration::RelativePath& aPath, configuration::NodeChangeLocation const& aChange, rtl::Reference< configuration::Tree > const& aBaseTree, configuration::NodeRef const& aBaseNode)
67 OSL_ENSURE(aChange.isValidLocation(), "Trying to resolve against change location that wasn't set up properly");
69 namespace Path = configuration::Path;
71 rtl::Reference< configuration::Tree > aChangeBaseTree = aChange.getBaseTree();
73 configuration::AbsolutePath aOuterBasePath = aBaseTree->getAbsolutePath(aBaseNode);
74 configuration::AbsolutePath aChangeBasePath = aChangeBaseTree->getAbsolutePath(aChange.getBaseNode());
76 std::vector<configuration::Path::Component>::const_reverse_iterator aChangeIt = aChangeBasePath.begin(), aChangeEnd = aChangeBasePath.end();
77 std::vector<configuration::Path::Component>::const_reverse_iterator aOuterIt = aOuterBasePath.begin(), aOuterEnd = aOuterBasePath.end();
79 // First by resolve the base node pathes
80 while (aOuterIt != aOuterEnd && aChangeIt != aChangeEnd)
82 if ( ! Path::matches(*aOuterIt,*aChangeIt) ) return false; // mismatch
83 ++aOuterIt;
84 ++aChangeIt;
87 // Next consider the stored accessor
88 if (aChangeIt != aChangeEnd) // stepping outward - prepend
90 Path::Rep aRemaining(aChangeIt, aChangeEnd);
92 aPath = configuration::RelativePath(aRemaining).compose(aChange.getAccessor());
94 else if (aOuterIt == aOuterEnd) // exact match outside
96 aPath = aChange.getAccessor();
98 else //(aChangeIt == aChangeEnd) but outer left
100 configuration::RelativePath aAccessor = aChange.getAccessor();
101 aChangeIt = aAccessor.begin();
102 aChangeEnd = aAccessor.end();
104 // resolve the outer path against the change accessor
105 while (aOuterIt != aOuterEnd && aChangeIt != aChangeEnd)
107 if ( ! Path::matches(*aOuterIt,*aChangeIt) ) return false; // mismatch
108 ++aOuterIt;
109 ++aChangeIt;
112 if (aOuterIt == aOuterEnd)
114 Path::Rep aRemaining(aChangeIt, aChangeEnd);
116 aPath = configuration::RelativePath( aRemaining );
120 return (aOuterIt == aOuterEnd); // resolved completely and assigned ??
124 // ---------------------------------------------------------------------------------------------------
125 // change path and base settings to start from the given base
126 bool rebaseChange(configuration::NodeChangeLocation& aChange, rtl::Reference< configuration::Tree > const& _aBaseTreeRef)
128 return rebaseChange(aChange,_aBaseTreeRef,_aBaseTreeRef->getRootNode());
130 bool rebaseChange(configuration::NodeChangeLocation& aChange, rtl::Reference< configuration::Tree > const& _aBaseTreeRef, configuration::NodeRef const& aBaseNode)
132 OSL_ENSURE(aChange.isValidLocation(), "Trying to rebase change location that wasn't set up properly");
134 rtl::Reference< configuration::Tree > aBaseTree(_aBaseTreeRef);
136 configuration::RelativePath aNewPath;
137 if (resolveChangeLocation(aNewPath,aChange,aBaseTree,aBaseNode))
139 aChange.setBase( aBaseTree, aBaseNode);
140 aChange.setAccessor( aNewPath );
141 return true;
143 else
144 return false;
146 // ---------------------------------------------------------------------------------------------------
147 // resolve non-uno elements to Uno Objects
148 bool resolveUnoObjects(UnoChange& aUnoChange, configuration::NodeChangeData const& aChange,
149 Factory& rFactory)
151 if (aChange.isSetChange())
153 //Check we have ElementTree
154 if ((aChange.element.newValue == NULL) &&
155 (aChange.element.oldValue == NULL))
157 if( ( aChange.unoData.newValue.getValue()!=NULL) ||
158 ( aChange.unoData.newValue.getValue()!=NULL))
160 return true;
162 else return false;
165 //Check if complex or simple type
166 rtl::Reference< configuration::Tree > aTree = aChange.isRemoveSetChange()?
167 aChange.getOldElementTree():
168 aChange.getNewElementTree();
170 configuration::NodeRef aNodeRef = aTree->getRootNode();
172 if (configuration::isStructuralNode(aTree, aNodeRef))
174 uno::Reference<uno::XInterface> aNewUnoObject = rFactory.findUnoElement(aChange.getNewElementNodeID());
175 uno::Reference<uno::XInterface> aOldUnoObject = rFactory.findUnoElement(aChange.getOldElementNodeID());
177 bool bFound = aNewUnoObject.is() || aOldUnoObject.is();
178 aUnoChange.newValue <<= aNewUnoObject;
179 aUnoChange.oldValue <<= aOldUnoObject;
180 return bFound;
182 else
184 aUnoChange.newValue = configuration::getSimpleElementValue(aTree, aNodeRef);
186 if (aChange.isReplaceSetChange() )
188 rtl::Reference< configuration::Tree > aOldTree = aChange.getOldElementTree();
190 aNodeRef = aOldTree->getRootNode();
191 OSL_ENSURE(!configuration::isStructuralNode(aOldTree, aNodeRef), "resolveUnoObject types mismatch");
192 aUnoChange.oldValue = configuration::getSimpleElementValue(aOldTree, aNodeRef);
194 bool bFound = aUnoChange.newValue.hasValue() || aUnoChange.oldValue.hasValue();
195 return bFound;
198 else if (aChange.isValueChange())
200 aUnoChange.newValue = aChange.unoData.newValue;
201 aUnoChange.oldValue = aChange.unoData.oldValue;
202 return true;
204 else
206 return false;
209 // ---------------------------------------------------------------------------------------------------
210 // resolve non-uno elements to Uno Objects inplace
211 bool resolveToUno(configuration::NodeChangeData& aChange, Factory& rFactory)
213 struct UnoChange aUnoChange;
214 if (resolveUnoObjects(aUnoChange,aChange, rFactory))
216 aChange.unoData.newValue = aUnoChange.newValue;
217 aChange.unoData.oldValue = aUnoChange.oldValue;
218 return true;
220 else
221 return false;
223 // ---------------------------------------------------------------------------------------------------
225 /// fill a change info from a NodeChangeInfo
226 void fillChange(util::ElementChange& rChange, configuration::NodeChangeInformation const& aInfo, rtl::Reference< configuration::Tree > const& aBaseTree, Factory& rFactory)
228 fillChange(rChange,aInfo,aBaseTree,aBaseTree->getRootNode(),rFactory);
230 /// fill a change info from a NodeChangeInfo
231 void fillChange(util::ElementChange& rChange, configuration::NodeChangeInformation const& aInfo, rtl::Reference< configuration::Tree > const& aBaseTree, configuration::NodeRef const& aBaseNode, Factory& rFactory)
233 configuration::RelativePath aRelativePath;
234 if (!resolveChangeLocation(aRelativePath, aInfo.location, aBaseTree, aBaseNode))
235 OSL_ENSURE(false, "WARNING: Change is not part of the given Tree");
237 UnoChange aUnoChange;
239 if (!resolveUnoObjects(aUnoChange, aInfo.change, rFactory))
240 OSL_ENSURE(false, "WARNING: Cannot find out old/new UNO objects involved in change");
242 rChange.Accessor <<= aRelativePath.toString();
243 rChange.Element = aUnoChange.newValue;
244 rChange.ReplacedElement = aUnoChange.oldValue;
246 // ---------------------------------------------------------------------------------------------------
247 /// fill a change info from a NodeChangeInfo (base,path and uno objects are assumed to be resolved already)
248 void fillChangeFromResolved(util::ElementChange& rChange, configuration::NodeChangeInformation const& aInfo)
250 rChange.Accessor <<= aInfo.location.getAccessor().toString();
251 rChange.Element = aInfo.change.unoData.newValue;
252 rChange.ReplacedElement = aInfo.change.unoData.oldValue;
254 // ---------------------------------------------------------------------------------------------------
255 /// fill a event from a NodeChangeInfo (uno objects are assumed to be resolved already)
256 bool fillEventDataFromResolved(container::ContainerEvent& rEvent, configuration::NodeChangeInformation const& aInfo)
258 rEvent.Accessor <<= aInfo.location.getAccessor().getLocalName().getName();
259 rEvent.Element = aInfo.change.unoData.newValue;
260 rEvent.ReplacedElement = aInfo.change.unoData.oldValue;
262 return !aInfo.isEmptyChange();
264 // ---------------------------------------------------------------------------------------------------
265 /// fill a event from a NodeChangeInfo(uno objects are assumed to be resolved already)
266 bool fillEventDataFromResolved(beans::PropertyChangeEvent& rEvent, configuration::NodeChangeInformation const& aInfo, bool bMore)
268 if (!aInfo.isValueChange())
269 return false;
271 rEvent.PropertyName = aInfo.location.getAccessor().getLocalName().getName();
273 rEvent.NewValue = aInfo.change.unoData.newValue;
274 rEvent.OldValue = aInfo.change.unoData.oldValue;
276 rEvent.PropertyHandle = -1;
277 rEvent.Further = bMore;
279 return !aInfo.isEmptyChange();
281 // ---------------------------------------------------------------------------------------------------
282 // ---------------------------------------------------------------------------------------------------
284 // ---------------------------------------------------------------------------------------------------