update dev300-m58
[ooovba.git] / configmgr / source / treemgr / collectchanges.cxx
blobbe8d1d5396e55c4160abe786dc314f9f7af17636
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: collectchanges.cxx,v $
10 * $Revision: 1.11 $
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 <string.h>
35 #include "collectchanges.hxx"
37 #include "nodechangeinfo.hxx"
39 #include <osl/diagnose.h>
41 namespace configmgr
43 namespace configuration
46 //-----------------------------------------------------------------------------
47 // conversion helper function
48 //-----------------------------------------------------------------------------
49 bool convertNodeChange(NodeChangeData& aData_, ValueChange const& aChange_)
51 switch(aChange_.getMode())
53 case ValueChange::wasDefault:
54 case ValueChange::changeValue:
55 aData_.type = NodeChangeData::eSetValue;
56 break;
58 case ValueChange::setToDefault:
59 aData_.type = NodeChangeData::eSetDefault;
60 break;
62 case ValueChange::changeDefault:
63 aData_.type = NodeChangeData::eNoChange; // ??
64 break;
66 default:
67 OSL_ENSURE(false,"Unknown change type found");
68 return false;
71 aData_.unoData.newValue = aChange_.getNewValue();
72 aData_.unoData.oldValue = aChange_.getOldValue();
73 return true;
75 //-----------------------------------------------------------------------------
77 bool convertNodeChange(NodeChangeData& aData_, AddNode const& aChange_)
79 aData_.type = aChange_.isReplacing()
80 ? NodeChangeData::eReplaceElement
81 : NodeChangeData::eInsertElement;
83 return true;
85 //-----------------------------------------------------------------------------
87 bool convertNodeChange(NodeChangeData& aData_, RemoveNode const& /*aChange_*/)
89 aData_.type = NodeChangeData::eRemoveElement;
91 return true;
93 //-----------------------------------------------------------------------------
95 //-----------------------------------------------------------------------------
96 // CollectChanges visitor class
97 //-----------------------------------------------------------------------------
99 CollectChanges::CollectChanges( NodeChangesInformation& rTargetList_,
100 Tree& rStartTree_, unsigned int nStartNode_,
101 rtl::Reference<Template> aElementTemplate_,
102 unsigned int nMaxDepth)
103 : m_rTargetList(rTargetList_)
104 , m_aAccessor()
105 , m_aContextTypeName()
106 , m_pBaseTree(&rStartTree_)
107 , m_nBaseNode(nStartNode_)
108 , m_nDepthLeft( nMaxDepth )
110 if (aElementTemplate_.is())
111 m_aContextTypeName = aElementTemplate_->getName();
114 //-----------------------------------------------------------------------------
115 CollectChanges::CollectChanges( CollectChanges const& rBase, Path::Component const& rChildName, rtl::OUString const& aSubTypeName_)
116 : m_rTargetList(rBase.m_rTargetList)
117 , m_aAccessor(rBase.m_aAccessor.compose(rChildName))
118 , m_aContextTypeName(aSubTypeName_)
119 , m_pBaseTree(rBase.m_pBaseTree)
120 , m_nBaseNode(rBase.m_nBaseNode)
121 , m_nDepthLeft(childDepth(rBase.m_nDepthLeft))
123 OSL_ASSERT(rBase.m_nDepthLeft > 0);
126 //-----------------------------------------------------------------------------
127 inline
128 Path::Component CollectChanges::implGetNodeName(Change const& aChange_) const
130 rtl::OUString aSimpleNodeName( aChange_.getNodeName() );
132 if (m_aContextTypeName.getLength() == 0)
134 OSL_ENSURE(isSimpleName(aSimpleNodeName),"Unexpected: Found non-simple name without a type");
135 return Path::wrapSafeName(aSimpleNodeName);
137 else
138 return Path::makeCompositeName(aSimpleNodeName, m_aContextTypeName);
141 //-----------------------------------------------------------------------------
142 void CollectChanges::collectFrom(ValueChange const& aChange_)
144 NodeChangeInformation aInfo;
146 if ( convertNodeChange( aInfo.change, aChange_ ) &&
147 implSetLocation( aInfo.location, aChange_, false ) )
149 implAdd( aInfo );
153 //-----------------------------------------------------------------------------
154 void CollectChanges::collectFrom(AddNode const& aChange_)
156 NodeChangeInformation aInfo;
158 if ( convertNodeChange( aInfo.change, aChange_ ) &&
159 implSetLocation( aInfo.location, aChange_, true ) )
161 implAdd( aInfo );
165 //-----------------------------------------------------------------------------
166 void CollectChanges::collectFrom(RemoveNode const& aChange_)
168 NodeChangeInformation aInfo;
170 if ( convertNodeChange( aInfo.change, aChange_ ) &&
171 implSetLocation( aInfo.location, aChange_, true ) )
173 implAdd( aInfo );
177 //-----------------------------------------------------------------------------
178 void CollectChanges::collectFrom(SubtreeChange const& aChanges_)
180 if (m_nDepthLeft > 0)
182 rtl::OUString aSubTypeName( aChanges_.getElementTemplateName() );
184 CollectChanges aSubcollector( *this, implGetNodeName(aChanges_), aSubTypeName );
186 aSubcollector.applyToChildren(aChanges_);
190 //-----------------------------------------------------------------------------
191 void CollectChanges::implAdd(NodeChangeInformation const& aChangeInfo_)
193 m_rTargetList.push_back(aChangeInfo_);
196 //-----------------------------------------------------------------------------
197 bool CollectChanges::implSetLocation(NodeChangeLocation& rLocation_, Change const& aOriginal_, bool bSet_) const
199 NodeID aBaseID(m_pBaseTree,m_nBaseNode);
200 if (aBaseID.isEmpty())
201 return false;
203 rLocation_.setBase( aBaseID );
205 if (bSet_ && m_aAccessor.isEmpty()) // It is a set change affecting the base ...
206 rLocation_.setAffected( aBaseID );
208 Path::Component aChangeName = implGetNodeName( aOriginal_ );
209 rLocation_.setAccessor( m_aAccessor.compose( aChangeName ) );
211 return true;
214 // ChangeTreeAction implementations
215 //-----------------------------------------------------------------------------
216 void CollectChanges::handle(ValueChange const& aValueNode_)
218 collectFrom(aValueNode_);
221 //-----------------------------------------------------------------------------
222 void CollectChanges::handle(AddNode const& aAddNode_)
224 collectFrom(aAddNode_);
227 //-----------------------------------------------------------------------------
228 void CollectChanges::handle(RemoveNode const& aRemoveNode_)
230 collectFrom(aRemoveNode_);
233 //-----------------------------------------------------------------------------
234 void CollectChanges::handle(SubtreeChange const& aSubtree_)
236 collectFrom( aSubtree_ );
239 //-----------------------------------------------------------------------------
240 //-----------------------------------------------------------------------------