1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: configset.cxx,v $
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"
33 #include <stdio.h> // needed for Solaris 8
34 #include "configset.hxx"
35 #include "nodechange.hxx"
36 #include "nodechangeimpl.hxx"
38 #include "treefragment.hxx"
39 #include "template.hxx"
40 #include "templateimpl.hxx"
41 #include "configgroup.hxx"
42 #include "valuenode.hxx"
43 #include "valuenodeimpl.hxx"
44 #include "setnodeimpl.hxx"
45 #include <vos/refernce.hxx>
49 namespace configuration
51 //-----------------------------------------------------------------------------
52 // class SetElementFactory
53 //-----------------------------------------------------------------------------
55 SetElementFactory::SetElementFactory(TemplateProvider
const& aProvider
)
56 : m_aProvider(aProvider
)
58 OSL_ENSURE(aProvider
.m_aImpl
.is(), "WARNING: Template Instance Factory created without template provider - cannot instantiate elements");
60 //-----------------------------------------------------------------------------
62 SetElementFactory::SetElementFactory(SetElementFactory
const& aOther
)
63 : m_aProvider(aOther
.m_aProvider
)
66 //-----------------------------------------------------------------------------
68 SetElementFactory
& SetElementFactory::operator=(SetElementFactory
const& aOther
)
70 m_aProvider
= aOther
.m_aProvider
;
73 //-----------------------------------------------------------------------------
75 SetElementFactory::~SetElementFactory()
79 //-----------------------------------------------------------------------------
80 rtl::Reference
< ElementTree
> SetElementFactory::instantiateTemplate(rtl::Reference
<Template
> const& aTemplate
)
82 OSL_ENSURE(m_aProvider
.m_aImpl
.is(), "ERROR: Template Instance Factory has no template provider - cannot instantiate element");
83 OSL_ENSURE(aTemplate
.is(), "ERROR: Template is NULL - cannot instantiate element");
85 rtl::Reference
< data::TreeSegment
> aInstanceTree( m_aProvider
.m_aImpl
->instantiate(aTemplate
) );
86 OSL_ENSURE(aInstanceTree
.is(), "ERROR: Cannot create Element Instance: Provider could not instantiate template");
89 aInstanceTree
->fragment
->header
.state
|= data::State::flag_removable
;
91 return new ElementTree( aInstanceTree
, aTemplate
, m_aProvider
);
93 //-----------------------------------------------------------------------------
94 rtl::Reference
< ElementTree
> SetElementFactory::instantiateOnDefault(rtl::Reference
< data::TreeSegment
> const& _aElementData
, rtl::Reference
<Template
> const& aDummyTemplate
)
96 // OSL_ENSURE(m_aProvider.m_aImpl(), "ERROR: Template Instance Factory has no template provider - cannot instantiate element");
97 OSL_ENSURE(_aElementData
.is(), "ERROR: Tree is NULL - cannot instantiate element");
98 OSL_ENSURE(aDummyTemplate
.is(), "ERROR: Template is NULL - cannot instantiate element");
100 return new ElementTree( _aElementData
, aDummyTemplate
, m_aProvider
);
102 //-----------------------------------------------------------------------------
104 TemplateProvider
SetElementFactory::findTemplateProvider(rtl::Reference
< Tree
> const& aTree
, NodeRef
const& aNode
)
106 OSL_ENSURE(!isEmpty(aTree
.get()), "ERROR: Getting Element Factory requires a valid tree");
107 OSL_ENSURE(aNode
.isValid(), "ERROR: Getting Element Factory requires a valid node");
108 OSL_ENSURE(aTree
->isValidNode(aNode
.getOffset()), "ERROR: Tree/Node mismatch");
109 if (aNode
.isValid() )
111 view::ViewTreeAccess
aView(aTree
.get());
113 OSL_ENSURE (aView
.isSetNode(aNode
), "WARNING: Getting Element Factory requires a SET node");
114 if (aView
.isSetNode(aNode
))
115 return aView
.getTemplateProvider(aView
.toSetNode(aNode
));
117 return TemplateProvider();
120 //-----------------------------------------------------------------------------
122 //-----------------------------------------------------------------------------
123 // class TreeSetUpdater and ValueSetUpdater
124 //-----------------------------------------------------------------------------
126 static node::Attributes
getNewElementAttributes(bool bInserting
)
128 node::Attributes aResult
;
129 aResult
.setState( node::isReplaced
);
130 //Check if you are inserting new dynamic property
133 aResult
.markRemovable();
138 // Value Element Factory methods
139 //-----------------------------------------------------------------------------
141 rtl::Reference
<ElementTree
> ValueSetUpdater::makeValueElement(rtl::OUString
const& aName
, com::sun::star::uno::Any
const& aValue
, bool bInserting
)
144 const node::Attributes aNewValueAttributes
= getNewElementAttributes(bInserting
); // TODO: get real value
146 com::sun::star::uno::Type aType
= m_aTemplate
->getInstanceType();
148 rtl::OUString aTypeName
= m_aTemplate
->getName();
150 std::auto_ptr
<INode
> pNode
;
151 if (aValue
.hasValue())
152 pNode
.reset( new ValueNode(aTypeName
, aValue
, aNewValueAttributes
) );
154 pNode
.reset( new ValueNode(aTypeName
, aType
, aNewValueAttributes
) );
156 rtl::Reference
< data::TreeSegment
> aValueTree
= data::TreeSegment::create(aName
, pNode
);
158 return new ElementTree(aValueTree
, m_aTemplate
, TemplateProvider() );
160 //-----------------------------------------------------------------------------
162 rtl::Reference
<ElementTree
> ValueSetUpdater::makeValueElement(rtl::OUString
const& aName
, rtl::Reference
< Tree
> const& , com::sun::star::uno::Any
const& aValue
, bool bInserting
)
164 // for now ignoring the node.
165 // TODO: merge attributes etc. from that node's value
166 return makeValueElement(aName
, aValue
,bInserting
);
168 //-----------------------------------------------------------------------------
171 TreeSetUpdater::TreeSetUpdater(rtl::Reference
< Tree
> const& aParentTree
, NodeRef
const& aSetNode
, rtl::Reference
< Template
> const& aTemplate
)
172 : m_aParentTree(aParentTree
)
173 , m_aSetNode(aSetNode
)
174 , m_aTemplate(aTemplate
)
178 //-----------------------------------------------------------------------------
180 ValueSetUpdater::ValueSetUpdater(rtl::Reference
< Tree
> const& aParentTree
, NodeRef
const& aSetNode
,
181 rtl::Reference
< Template
> const& aTemplate
, com::sun::star::uno::Reference
<com::sun::star::script::XTypeConverter
> const& xConverter
)
182 : m_aParentTree(aParentTree
)
183 , m_aSetNode(aSetNode
)
184 , m_aTemplate(aTemplate
)
185 , m_xTypeConverter(xConverter
)
189 //-----------------------------------------------------------------------------
191 SetDefaulter::SetDefaulter(rtl::Reference
< Tree
> const& aParentTree
, NodeRef
const& aSetNode
,
192 DefaultProvider
const& aDefaultProvider
)
193 : m_aParentTree(aParentTree
)
194 , m_aSetNode(aSetNode
)
195 , m_aDefaultProvider(aDefaultProvider
)
199 //-----------------------------------------------------------------------------
201 /// validates that a actual set and an updater's construction parameters match
202 static void doValidateSet(rtl::Reference
< Tree
> const& aParentTree
, NodeRef
const& aSetNode
)
204 if (isEmpty(aParentTree
.get()))
205 throw Exception("INTERNAL ERROR: Set Update: Unexpected NULL tree");
207 if (!aSetNode
.isValid())
208 throw Exception("INTERNAL ERROR: Set Update: Unexpected NULL node");
210 if (!aParentTree
->isValidNode(aSetNode
.getOffset()))
211 throw Exception("INTERNAL ERROR: Set Update: node does not match tree");
213 if (! view::ViewTreeAccess(aParentTree
.get()).isSetNode(aSetNode
))
214 throw Exception("INTERNAL ERROR: Set Update: node is not a set");
216 if (aParentTree
->getAttributes(aSetNode
).isReadonly())
217 throw ConstraintViolation( "Set Update: Set is read-only !" );
219 //-----------------------------------------------------------------------------
221 /// validates that the actual set and the construction parameters match
222 void TreeSetUpdater::implValidateSet()
224 doValidateSet(m_aParentTree
,m_aSetNode
);
226 if (!m_aTemplate
.is())
227 throw Exception("INTERNAL ERROR: No template available for tree set update");
229 if (m_aTemplate
->isInstanceValue())
230 throw Exception("INTERNAL ERROR: Tree set update invoked on a value-set");
232 view::ViewTreeAccess
aParentView(m_aParentTree
.get());
234 if ( aParentView
.getElementTemplate(aParentView
.toSetNode(m_aSetNode
)) != m_aTemplate
)
235 throw Exception("INTERNAL ERROR: Set Update: template mismatch");
237 //-----------------------------------------------------------------------------
239 /// validates that the actual set and the construction parameters match
240 void ValueSetUpdater::implValidateSet()
242 doValidateSet(m_aParentTree
,m_aSetNode
);
244 com::sun::star::uno::Type aThisType
= m_aTemplate
->getInstanceType();
246 switch ( aThisType
.getTypeClass())
248 case uno::TypeClass_VOID
: throw Exception("INTERNAL ERROR: Value set element type is void");
249 case uno::TypeClass_INTERFACE
: throw Exception("INTERNAL ERROR: Value update invoked on a complex set");
251 case uno::TypeClass_STRUCT
:
252 case uno::TypeClass_EXCEPTION
: throw Exception("INTERNAL ERROR: Unexpected/Invalid type for set elements");
257 view::ViewTreeAccess
aParentView(m_aParentTree
.get());
259 if ( aParentView
.getElementTemplate(aParentView
.toSetNode(m_aSetNode
))->getInstanceType() != aThisType
)
260 throw Exception("INTERNAL ERROR: Set Update: element type mismatch");
262 //-----------------------------------------------------------------------------
264 /// validates that the actual set and the construction parameters match
265 void SetDefaulter::implValidateSet()
267 doValidateSet(m_aParentTree
,m_aSetNode
);
269 if (!m_aDefaultProvider
.isValid())
270 throw Exception("INTERNAL ERROR: No default provider available for restoring set default state");
272 //-----------------------------------------------------------------------------
274 static void doValidateElement(rtl::Reference
< ElementTree
> const& aElement
, bool bReqRemovable
)
277 throw Exception("INTERNAL ERROR: Set Update: Unexpected NULL element");
281 rtl::Reference
< Tree
> aElementTree
= aElement
.get();
283 if(!aElementTree
->getAttributes(aElementTree
->getRootNode()).isRemovable())
284 throw ConstraintViolation( "New Set Update: Existing element cannot be removed (or replaced) !" );
287 //-----------------------------------------------------------------------------
289 /// validates that the given element is valid in this context and returns its name
290 Path::Component
TreeSetUpdater::implValidateElement(rtl::Reference
< ElementTree
> const& aElement
, bool bReqRemovable
)
292 doValidateElement(aElement
,bReqRemovable
);
293 return aElement
->getExtendedRootName();
295 //-----------------------------------------------------------------------------
297 /// validates that the given element is valid and can be replaced in this context and returns its name
298 Path::Component
ValueSetUpdater::implValidateElement(rtl::Reference
< ElementTree
> const& aElement
, bool mReqRemovable
)
300 doValidateElement(aElement
,mReqRemovable
);
302 #if OSL_DEBUG_LEVEL > 0
303 com::sun::star::uno::Type aNodeType
= ElementHelper::getUnoType(aElement
);
305 OSL_ENSURE(aNodeType
.getTypeClass() != uno::TypeClass_VOID
, "INTERNAL ERROR: Set Element without associated type found");
306 OSL_ENSURE(aNodeType
.getTypeClass() != uno::TypeClass_INTERFACE
,"INTERNAL ERROR: Set Element with complex type found");
308 OSL_ENSURE(aNodeType
== m_aTemplate
->getInstanceType() ||
309 uno::TypeClass_ANY
== m_aTemplate
->getInstanceType().getTypeClass(),
310 "INTERNAL ERROR: Set Update: existing element does not match template type");
313 return aElement
->getExtendedRootName();
315 //-----------------------------------------------------------------------------
316 static void checkEligibleChild(rtl::Reference
< ElementTree
> const& aElementTree
, rtl::Reference
< Tree
> const& aParentTree
)
318 ElementTree
const * const pElement
= aElementTree
.get(); OSL_ASSERT(pElement
);
320 if (pElement
->getContextTree() != NULL
)
321 throw ConstraintViolation( "Set Update: cannot insert an element that already has a parent." );
323 Tree
const* pAncestor
= aParentTree
.get();
324 while (pAncestor
!= NULL
)
326 if (pElement
== pAncestor
)
327 throw ConstraintViolation( "Set Update: Circular insertion - trying to insert an element into self or descendant" );
329 pAncestor
= pAncestor
->getContextTree();
330 OSL_ENSURE(pAncestor
!= aParentTree
.get(), "ERROR: Circular tree found");
334 //-----------------------------------------------------------------------------
336 void TreeSetUpdater::implValidateTree(rtl::Reference
< ElementTree
> const& aElementTree
)
338 if (!aElementTree
.is())
339 throw ConstraintViolation( "Set Update: cannot replace element of complex set with NULL node. Remove the element instead !" );
341 checkEligibleChild(aElementTree
,m_aParentTree
);
343 if (! aElementTree
->isTemplateInstance())
345 throw TypeMismatch(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("<Unnamed> (Template missing)")),
346 m_aTemplate
->getName(), " - new element without template in Set Update");
349 if (!aElementTree
->isInstanceOf(m_aTemplate
))
351 throw TypeMismatch( aElementTree
->getTemplate()->getPathString(),
352 m_aTemplate
->getPathString(), " - new element without template in Set Update");
355 //-----------------------------------------------------------------------------
357 com::sun::star::uno::Any
ValueSetUpdater::implValidateValue(com::sun::star::uno::Any
const& aValue
)
359 com::sun::star::uno::Type
const aThisType
= m_aTemplate
->getInstanceType();
361 OSL_ENSURE( aThisType
.getTypeClass() == uno::TypeClass_ANY
|| isPossibleValueType(aThisType
),
362 "Invalid element type for value set" );
364 com::sun::star::uno::Any aRet
;
365 if (aValue
.hasValue())
367 com::sun::star::uno::Type
const aValType
= aValue
.getValueType();
369 if (aValType
.getTypeClass() == uno::TypeClass_INTERFACE
)
370 throw TypeMismatch(aValType
.getTypeName(), aThisType
.getTypeName(), " - cannot replace value by complex tree in Set update");
372 if (aValType
== aThisType
)
377 else if ( uno::TypeClass_ANY
== aThisType
.getTypeClass() )
379 if ( ! isPossibleValueType(aValType
) )
380 throw TypeMismatch(aValType
.getTypeName(), aThisType
.getTypeName(), " - new element has no legal configuration data type");
387 if (!convertCompatibleValue(m_xTypeConverter
, aRet
, aValue
, aThisType
))
388 throw TypeMismatch(aValType
.getTypeName(), aThisType
.getTypeName(), " - new element does not match template type in SetUpdate");
391 OSL_ASSERT( isPossibleValueType(aRet
.getValueType()) );
395 // cannot do anything about null values here
396 OSL_ASSERT(aValue
.getValueTypeClass() == uno::TypeClass_VOID
);
401 //-----------------------------------------------------------------------------
403 com::sun::star::uno::Any
ValueSetUpdater::implValidateValue(rtl::Reference
< Tree
> const& aElementTree
, com::sun::star::uno::Any
const& aValue
)
405 node::Attributes aAttributes
= aElementTree
->getAttributes(aElementTree
->getRootNode());
406 // Here we assume writable == removable/replaceable
407 if (aAttributes
.isReadonly())
408 throw ConstraintViolation( "Set Update: Existing element is read-only !" );
410 // Here we assume nullable != removable
411 if (!aValue
.hasValue())
413 if (!aAttributes
.isNullable())
414 throw ConstraintViolation( "Set Update: Value is not nullable !" );
416 return implValidateValue( aValue
);
418 //-----------------------------------------------------------------------------
420 NodeChange
TreeSetUpdater::validateInsertElement (rtl::OUString
const& aName
, rtl::Reference
< ElementTree
> const& aNewElement
)
422 view::ViewTreeAccess
aParentView(m_aParentTree
.get());
424 SetEntry anEntry
= aParentView
.findElement(aParentView
.toSetNode(m_aSetNode
),aName
);
425 if (anEntry
.isValid())
426 throw Exception("INTERNAL ERROR: Set Update: Element to be inserted already exists");
428 implValidateTree(aNewElement
);
430 std::auto_ptr
<SetElementChangeImpl
> pChange( new SetInsertImpl(aNewElement
->makeExtendedName(aName
), aNewElement
) );
432 pChange
->setTarget(aParentView
.makeNode(m_aSetNode
));
434 return NodeChange(pChange
.release());
436 //-----------------------------------------------------------------------------
438 NodeChange
ValueSetUpdater::validateInsertElement (rtl::OUString
const& aName
, com::sun::star::uno::Any
const& aNewValue
)
440 view::ViewTreeAccess
aParentView(m_aParentTree
.get());
442 SetEntry anEntry
= aParentView
.findElement(aParentView
.toSetNode(m_aSetNode
),aName
);
443 if (anEntry
.isValid())
444 throw Exception("INTERNAL ERROR: Set Update: Element to be inserted already exists");
446 com::sun::star::uno::Any aValidValue
= implValidateValue(aNewValue
);
448 rtl::Reference
<ElementTree
> aNewElement
= makeValueElement(aName
, aValidValue
,true);
450 std::auto_ptr
<SetElementChangeImpl
> pChange( new SetInsertImpl(aNewElement
->makeExtendedName(aName
), aNewElement
) );
452 pChange
->setTarget(aParentView
.makeNode(m_aSetNode
));
454 return NodeChange(pChange
.release());
456 //-----------------------------------------------------------------------------
458 NodeChange
TreeSetUpdater::validateReplaceElement(rtl::Reference
< ElementTree
> const& aElement
, rtl::Reference
< ElementTree
> const& aNewElement
)
460 Path::Component aName
= implValidateElement(aElement
,true);
462 implValidateTree(aNewElement
);
464 std::auto_ptr
<SetElementChangeImpl
> pChange( new SetReplaceImpl(aName
, aNewElement
) );
466 pChange
->setTarget(view::ViewTreeAccess(m_aParentTree
.get()).makeNode(m_aSetNode
));
468 return NodeChange(pChange
.release());
470 //-----------------------------------------------------------------------------
472 NodeChange
ValueSetUpdater::validateReplaceElement(rtl::Reference
< ElementTree
> const& aElement
, com::sun::star::uno::Any
const& aNewValue
)
474 Path::Component aName
= implValidateElement(aElement
,false);
476 rtl::Reference
< Tree
> aElementNode
= extractElementNode(aElement
);
478 com::sun::star::uno::Any aValidValue
= implValidateValue(aElementNode
, aNewValue
);
480 rtl::Reference
< Tree
> aElementTree
= aElement
.get();
482 rtl::Reference
<ElementTree
> aNewElement
;
483 if(aElementTree
->getAttributes(aElementTree
->getRootNode()).isRemovable())
485 aNewElement
= makeValueElement(aName
.getName(), aElementNode
, aValidValue
,true);
489 aNewElement
= makeValueElement(aName
.getName(), aElementNode
, aValidValue
,false);
492 std::auto_ptr
<SetElementChangeImpl
> pChange( new SetReplaceImpl(aName
, aNewElement
) );
494 pChange
->setTarget(view::ViewTreeAccess(m_aParentTree
.get()).makeNode(m_aSetNode
));
496 return NodeChange(pChange
.release());
498 //-----------------------------------------------------------------------------
500 NodeChange
TreeSetUpdater::validateRemoveElement (rtl::Reference
< ElementTree
> const& aElement
)
502 Path::Component aName
= implValidateElement(aElement
,true);
504 std::auto_ptr
<SetElementChangeImpl
> pChange( new SetRemoveImpl(aName
) );
506 pChange
->setTarget(view::ViewTreeAccess(m_aParentTree
.get()).makeNode(m_aSetNode
));
508 return NodeChange(pChange
.release());
511 //-----------------------------------------------------------------------------
513 NodeChange
ValueSetUpdater::validateRemoveElement (rtl::Reference
< ElementTree
> const& aElement
)
515 Path::Component aName
= implValidateElement(aElement
,true);
517 std::auto_ptr
<SetElementChangeImpl
> pChange( new SetRemoveImpl(aName
) );
519 pChange
->setTarget(view::ViewTreeAccess(m_aParentTree
.get()).makeNode(m_aSetNode
));
521 return NodeChange(pChange
.release());
524 //-----------------------------------------------------------------------------
526 NodeChange
SetDefaulter::validateSetToDefaultState()
528 std::auto_ptr
< ISubtree
> aDefault
= m_aDefaultProvider
.getDefaultTree(m_aParentTree
,m_aSetNode
);
530 // now build the specific change
531 std::auto_ptr
<SetChangeImpl
> pChange
;
535 TemplateProvider aProvider
= SetElementFactory::findTemplateProvider(m_aParentTree
,m_aSetNode
);
537 configmgr::configuration::SetElementFactory
aTmp(aProvider
);
538 pChange
.reset( new SetResetImpl(aTmp
, aDefault
) );
539 pChange
->setTarget(view::ViewTreeAccess(m_aParentTree
.get()).makeNode(m_aSetNode
));
541 return NodeChange(pChange
.release());
543 //-----------------------------------------------------------------------------
545 rtl::Reference
< Tree
> ValueSetUpdater::extractElementNode (rtl::Reference
< ElementTree
> const& aElement
)
547 return aElement
.get();
549 //-----------------------------------------------------------------------------
551 #if OSL_DEBUG_LEVEL > 0
552 com::sun::star::uno::Type
ElementHelper::getUnoType(rtl::Reference
< ElementTree
> const& aElement
)
554 OSL_PRECOND( aElement
.is(), "ERROR: Configuration: ElementTree operation requires valid node" );
556 rtl::Reference
< Tree
> aElementTree(aElement
.get());
558 NodeRef aNode
= aElementTree
->getRootNode();
559 OSL_ASSERT( aNode
.isValid() );
561 view::ViewTreeAccess
aElementView(aElementTree
.get());
563 if ( aElementView
.isValueNode(aNode
) )
565 return aElementView
.getValueType(aElementView
.toValueNode(aNode
));
569 uno::Reference
< uno::XInterface
> const * const selectInterface
=0;
570 return ::getCppuType(selectInterface
);
574 //-----------------------------------------------------------------------------