LibreOffice Module xmlscript (master) 1
xml_element.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
21#include <o3tl/safeint.hxx>
22#include <osl/diagnose.h>
23#include <com/sun/star/xml/sax/XDocumentHandler.hpp>
24
25using namespace com::sun::star;
26using namespace com::sun::star::uno;
27
28namespace xmlscript
29{
30
31void XMLElement::addAttribute( OUString const & rAttrName, OUString const & rValue )
32{
33 _attrNames.push_back( rAttrName );
34 _attrValues.push_back( rValue );
35}
36
38{
39 _subElems.push_back( xElem );
40}
41
43{
44 return _subElems[ static_cast<size_t>(nIndex) ];
45}
46
48{
49 for (const Reference<XAttributeList> & _subElem : _subElems)
50 {
51 XMLElement * pElem = static_cast< XMLElement * >( _subElem.get() );
52 pElem->dump( xOut );
53 }
54}
55
57{
58 xOut->ignorableWhitespace( OUString() );
59 xOut->startElement( _name, static_cast< xml::sax::XAttributeList * >( this ) );
60 // write sub elements
61 dumpSubElements( xOut );
62 xOut->ignorableWhitespace( OUString() );
63 xOut->endElement( _name );
64}
65
66// XAttributeList
68{
69 return static_cast<sal_Int16>(_attrNames.size());
70}
71
72OUString XMLElement::getNameByIndex( sal_Int16 nPos )
73{
74 OSL_ASSERT( nPos >= 0 && o3tl::make_unsigned(nPos) < _attrNames.size() );
75 return _attrNames[ nPos ];
76}
77
78OUString XMLElement::getTypeByIndex( sal_Int16 nPos )
79{
80 OSL_ASSERT( nPos >= 0 && o3tl::make_unsigned(nPos) < _attrNames.size() );
81 // xxx todo
82 return OUString();
83}
84
85OUString XMLElement::getTypeByName( OUString const & /*rName*/ )
86{
87 // xxx todo
88 return OUString();
89}
90
91OUString XMLElement::getValueByIndex( sal_Int16 nPos )
92{
93 OSL_ASSERT( nPos >= 0 && o3tl::make_unsigned(nPos) < _attrNames.size() );
94 return _attrValues[ nPos ];
95}
96
97OUString XMLElement::getValueByName( OUString const & rName )
98{
99 for ( size_t nPos = 0; nPos < _attrNames.size(); ++nPos )
100 {
101 if (_attrNames[ nPos ] == rName)
102 {
103 return _attrValues[ nPos ];
104 }
105 }
106 return OUString();
107}
108
109}
110
111/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::xml::sax::XAttributeList > const & getSubElement(sal_Int32 nIndex)
Gets sub element of given index.
Definition: xml_element.cxx:42
void dumpSubElements(css::uno::Reference< css::xml::sax::XDocumentHandler > const &xOut)
Dumps out sub elements (and all further sub elements).
Definition: xml_element.cxx:47
void dump(css::uno::Reference< css::xml::sax::XDocumentHandler > const &xOut)
Dumps out element (and all sub elements).
Definition: xml_element.cxx:56
virtual OUString SAL_CALL getTypeByIndex(sal_Int16 nPos) override
Definition: xml_element.cxx:78
void addAttribute(OUString const &rAttrName, OUString const &rValue)
Adds an attribute to elements.
Definition: xml_element.cxx:31
::std::vector< OUString > _attrNames
Definition: xml_helper.hxx:95
void addSubElement(css::uno::Reference< css::xml::sax::XAttributeList > const &xElem)
Adds a sub element of element.
Definition: xml_element.cxx:37
virtual sal_Int16 SAL_CALL getLength() override
Definition: xml_element.cxx:67
virtual OUString SAL_CALL getNameByIndex(sal_Int16 nPos) override
Definition: xml_element.cxx:72
::std::vector< OUString > _attrValues
Definition: xml_helper.hxx:96
OUString const _name
Definition: xml_helper.hxx:94
virtual OUString SAL_CALL getValueByName(OUString const &rName) override
Definition: xml_element.cxx:97
virtual OUString SAL_CALL getTypeByName(OUString const &rName) override
Definition: xml_element.cxx:85
virtual OUString SAL_CALL getValueByIndex(sal_Int16 nPos) override
Definition: xml_element.cxx:91
::std::vector< css::uno::Reference< css::xml::sax::XAttributeList > > _subElems
Definition: xml_helper.hxx:93
sal_Int32 nIndex
sal_uInt16 nPos
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)