update dev300-m58
[ooovba.git] / configmgr / source / xml / elementformatter.cxx
blob3b0a48e9f0c35b61317950d78a2debbe54de12e7
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: elementformatter.cxx,v $
10 * $Revision: 1.17 $
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 "elementformatter.hxx"
35 #include "xmlstrings.hxx"
36 #include "typeconverter.hxx"
38 #include <comphelper/attributelist.hxx>
40 #include <rtl/ustrbuf.hxx>
42 #include <com/sun/star/configuration/backend/SchemaAttribute.hpp>
43 #include <com/sun/star/configuration/backend/NodeAttribute.hpp>
45 // -----------------------------------------------------------------------------
47 namespace configmgr
49 // -----------------------------------------------------------------------------
50 namespace xml
52 // -----------------------------------------------------------------------------
53 namespace uno = ::com::sun::star::uno;
54 namespace sax = ::com::sun::star::xml::sax;
55 // -----------------------------------------------------------------------------
57 ElementFormatter::ElementFormatter()
58 : m_aElementType(ElementType::unknown)
59 , m_xAttributes()
62 // -----------------------------------------------------------------------------
64 ElementFormatter::~ElementFormatter()
67 // -----------------------------------------------------------------------------
69 void ElementFormatter::reset()
71 m_aElementType = ElementType::unknown;
72 m_xAttributes.clear();
74 // -----------------------------------------------------------------------------
76 void ElementFormatter::addAttribute(rtl::OUString const & _anAttributeName, rtl::OUString const & _aValue)
78 OSL_PRECOND(m_xAttributes.is(),"Trying to add an attribute to a non-existing list");
80 m_xAttributes->AddAttribute(_anAttributeName,
81 XML_ATTRTYPE_CDATA,
82 _aValue);
84 // -----------------------------------------------------------------------------
86 void ElementFormatter::addAttribute(rtl::OUString const & _anAttributeName, bool _bValue)
88 OSL_PRECOND(m_xAttributes.is(),"Trying to add an attribute to a non-existing list");
90 m_xAttributes->AddAttribute(_anAttributeName,
91 XML_ATTRTYPE_CDATA,
92 _bValue ? ATTR_VALUE_TRUE : ATTR_VALUE_FALSE);
94 // -----------------------------------------------------------------------------
96 void ElementFormatter::addNamespaces()
98 static rtl::OUString const sNamespaceDecl( RTL_CONSTASCII_USTRINGPARAM("xmlns:") );
100 addAttribute( sNamespaceDecl.concat(NS_PREFIX_OOR), static_cast<rtl::OUString const &>(NS_URI_OOR));
101 addAttribute( sNamespaceDecl.concat(NS_PREFIX_XS ), static_cast<rtl::OUString const &>(NS_URI_XS ));
103 // -----------------------------------------------------------------------------
105 void ElementFormatter::prepareElement(ElementInfo const& _aInfo)
107 if (!m_xAttributes.is())
109 m_xAttributes.set( new ::comphelper::AttributeList() );
110 addNamespaces();
112 else
113 m_xAttributes->Clear();
115 m_aElementType = _aInfo.type;
117 addName(_aInfo.name);
118 addNodeFlags(_aInfo.flags);
119 addOperation(_aInfo.op);
121 // -----------------------------------------------------------------------------
123 void ElementFormatter::prepareSimpleElement(ElementType::Enum _eType)
125 if (!m_xAttributes.is())
127 m_xAttributes.set( new ::comphelper::AttributeList() );
128 addNamespaces();
130 else
131 m_xAttributes->Clear();
133 m_aElementType = _eType;
135 // -----------------------------------------------------------------------------
137 void ElementFormatter::addName(rtl::OUString const & _aName)
139 if (_aName.getLength())
141 switch( m_aElementType )
143 case ElementType::schema:
144 case ElementType::layer:
146 sal_Int32 nIndex = _aName.lastIndexOf('.');
148 rtl::OUString aNodeName = _aName.copy(nIndex + 1);
149 addAttribute(ATTR_NAME, aNodeName);
151 OSL_ENSURE(nIndex > 0,"Found component root element without a package part in its name");
152 if (nIndex > 0)
154 rtl::OUString aPackage = _aName.copy(0, nIndex);
155 addAttribute(ATTR_PACKAGE, aPackage);
158 break;
160 default:
161 addAttribute(ATTR_NAME, _aName);
162 break;
166 // -----------------------------------------------------------------------------
168 inline
169 void ElementFormatter::maybeAddFlag(sal_Int16 _eFlags, sal_Int16 _eSelect, rtl::OUString const & _anAttributeName, bool _bValue)
171 if (_eFlags & _eSelect) addAttribute(_anAttributeName,_bValue);
173 // -----------------------------------------------------------------------------
175 void ElementFormatter::addNodeFlags(sal_Int16 _eFlags)
177 maybeAddFlag(_eFlags,com::sun::star::configuration::backend::SchemaAttribute::REQUIRED, ATTR_FLAG_NULLABLE, false);
178 maybeAddFlag(_eFlags,com::sun::star::configuration::backend::SchemaAttribute::LOCALIZED, ATTR_FLAG_LOCALIZED);
179 maybeAddFlag(_eFlags,com::sun::star::configuration::backend::SchemaAttribute::EXTENSIBLE, ATTR_FLAG_EXTENSIBLE);
181 maybeAddFlag(_eFlags,com::sun::star::configuration::backend::NodeAttribute::FINALIZED, ATTR_FLAG_FINALIZED);
182 maybeAddFlag(_eFlags,com::sun::star::configuration::backend::NodeAttribute::MANDATORY, ATTR_FLAG_MANDATORY);
183 maybeAddFlag(_eFlags,com::sun::star::configuration::backend::NodeAttribute::READONLY, ATTR_FLAG_READONLY);
185 // -----------------------------------------------------------------------------
187 void ElementFormatter::addOperation(Operation::Enum _eOp)
189 switch (_eOp)
191 case Operation::none: break;
192 case Operation::modify: break ; //addAttribute(ATTR_OPERATION, static_cast<rtl::OUString const &>(OPERATION_MODIFY)); break;
193 case Operation::clear: OSL_ENSURE(false,"'clear' operation is not yet supported"); break ;
194 //addAttribute(ATTR_OPERATION, static_cast<rtl::OUString const &>(OPERATION_CLEAR)); break;
195 case Operation::replace: addAttribute(ATTR_OPERATION, static_cast<rtl::OUString const &>(OPERATION_REPLACE)); break;
196 case Operation::fuse: addAttribute(ATTR_OPERATION, static_cast<rtl::OUString const &>(OPERATION_FUSE)); break;
197 case Operation::remove: addAttribute(ATTR_OPERATION, static_cast<rtl::OUString const &>(OPERATION_REMOVE)); break;
199 case Operation::unknown:
200 OSL_ENSURE(false, "ElementFormatter: Trying to add attribute for 'unknown' operation");
201 break;
202 default:
203 OSL_ENSURE(false, "ElementFormatter: Trying to add attribute for invalid operation");
204 break;
207 // -----------------------------------------------------------------------------
209 void ElementFormatter::addInstanceType(rtl::OUString const & /*_aElementType*/, rtl::OUString const & /*_aElementTypeModule*/)
212 // -----------------------------------------------------------------------------
214 static ::rtl::OUString toXmlTypeName(const uno::TypeClass& _rTypeClass)
216 ::rtl::OUString aRet;
217 switch(_rTypeClass)
219 case uno::TypeClass_BOOLEAN: aRet = VALUETYPE_BOOLEAN; break;
220 case uno::TypeClass_SHORT: aRet = VALUETYPE_SHORT; break;
221 case uno::TypeClass_LONG: aRet = VALUETYPE_INT; break;
222 case uno::TypeClass_HYPER: aRet = VALUETYPE_LONG; break;
223 case uno::TypeClass_DOUBLE: aRet = VALUETYPE_DOUBLE; break;
224 case uno::TypeClass_STRING: aRet = VALUETYPE_STRING; break;
225 case uno::TypeClass_SEQUENCE: aRet = VALUETYPE_BINARY; break;
226 case uno::TypeClass_ANY: aRet = VALUETYPE_ANY; break;
227 default:
228 OSL_ENSURE(false,"Cannot get type name: unknown typeclass");
229 break;
231 return aRet;
233 // -----------------------------------------------------------------------------
235 void ElementFormatter::addPropertyValueType(uno::Type const& _aType)
237 if (_aType == uno::Type()) return;
239 bool bList = false;
240 uno::Type aSimpleType = getBasicType(_aType, bList);
241 uno::TypeClass aSimpleTypeClass = aSimpleType.getTypeClass();
242 rtl::OUString aSimpleTypeName = toXmlTypeName(aSimpleTypeClass);
244 rtl::OUString sNsPrefix = (bList || aSimpleTypeClass == uno::TypeClass_ANY) ?
245 rtl::OUString( NS_PREFIX_OOR ) : rtl::OUString( NS_PREFIX_XS );
247 rtl::OUStringBuffer aTypeNameBuf(sNsPrefix);
249 if (sNsPrefix.getLength())
250 aTypeNameBuf. append(k_NS_SEPARATOR);
252 aTypeNameBuf. append(aSimpleTypeName);
254 if (bList)
255 aTypeNameBuf. append(VALUETYPE_LIST_SUFFIX);
257 addAttribute( ATTR_VALUETYPE, aTypeNameBuf.makeStringAndClear());
259 // -----------------------------------------------------------------------------
261 void ElementFormatter::addLanguage(rtl::OUString const & _sLanguage)
263 OSL_ENSURE(_sLanguage.getLength(), "ElementFormatter: Trying to add empty language attribute");
264 addAttribute(EXT_ATTR_LANGUAGE, _sLanguage);
266 // -----------------------------------------------------------------------------
268 void ElementFormatter::addIsNull(bool _bIsNull)
270 addAttribute( EXT_ATTR_NULL, _bIsNull);
272 // -----------------------------------------------------------------------------
274 void ElementFormatter::addSeparator(rtl::OUString const& _sSeparator)
276 addAttribute( ATTR_VALUESEPARATOR, _sSeparator);
278 // -----------------------------------------------------------------------------
280 rtl::OUString ElementFormatter::getElementTag() const
282 switch (m_aElementType)
284 case ElementType::schema: return rtl::OUString( TAG_SCHEMA );
285 case ElementType::layer: return rtl::OUString( TAG_LAYER );
287 case ElementType::component: return rtl::OUString( TAG_COMPONENT );
288 case ElementType::templates: return rtl::OUString( TAG_TEMPLATES );
290 case ElementType::property: return rtl::OUString( TAG_PROP );
291 case ElementType::node: return rtl::OUString( TAG_NODE );
292 case ElementType::group: return rtl::OUString( TAG_GROUP );
293 case ElementType::set: return rtl::OUString( TAG_SET );
295 case ElementType::import: return rtl::OUString( TAG_IMPORT );
296 case ElementType::instance: return rtl::OUString( TAG_INSTANCE );
297 case ElementType::item_type: return rtl::OUString( TAG_ITEMTYPE );
298 case ElementType::value: return rtl::OUString( TAG_VALUE );
299 case ElementType::uses: return rtl::OUString( TAG_USES );
301 case ElementType::unknown:
302 OSL_ENSURE(false, "ElementFormatter: Trying to get Tag for 'unknown' element type");
303 break;
304 case ElementType::other:
305 OSL_ENSURE(false, "ElementFormatter: Trying to get Tag for 'other' element type");
306 break;
307 default:
308 OSL_ENSURE(false, "ElementFormatter: Trying to get Tag for invalid element type");
309 break;
311 return rtl::OUString();
313 // -----------------------------------------------------------------------------
315 uno::Reference< sax::XAttributeList > ElementFormatter::getElementAttributes() const
317 return m_xAttributes.get();
319 // -----------------------------------------------------------------------------
321 // -----------------------------------------------------------------------------
322 } // namespace
323 } // namespace