xref: /AOO41X/main/comphelper/source/xml/attributelist.cxx (revision dde7d3faf6dcd9cbeb7b48ba6d0cea5ffcc883d0)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_comphelper.hxx"
26 #include <comphelper/attributelist.hxx>
27 #include <vos/diagnose.hxx>
28 
29 #include <vector>
30 
31 using namespace rtl;
32 using namespace osl;
33 using namespace com::sun::star;
34 
35 namespace comphelper {
36 
37 struct TagAttribute_Impl
38 {
TagAttribute_Implcomphelper::TagAttribute_Impl39     TagAttribute_Impl(){}
TagAttribute_Implcomphelper::TagAttribute_Impl40     TagAttribute_Impl( const OUString &aName, const OUString &aType,
41                          const OUString &aValue )
42     {
43         this->sName     = aName;
44         this->sType     = aType;
45         this->sValue    = aValue;
46     }
47 
48     OUString sName;
49     OUString sType;
50     OUString sValue;
51 };
52 
53 struct AttributeList_Impl
54 {
AttributeList_Implcomphelper::AttributeList_Impl55     AttributeList_Impl()
56     {
57         // performance improvement during adding
58         vecAttribute.reserve(20);
59     }
60     ::std::vector<struct TagAttribute_Impl> vecAttribute;
61 };
62 
getLength(void)63 sal_Int16 SAL_CALL AttributeList::getLength(void) throw( ::com::sun::star::uno::RuntimeException )
64 {
65     return (sal_Int16)(m_pImpl->vecAttribute.size());
66 }
67 
getNameByIndex(sal_Int16 i)68 OUString SAL_CALL AttributeList::getNameByIndex(sal_Int16 i) throw( ::com::sun::star::uno::RuntimeException )
69 {
70     return ( i < static_cast < sal_Int16 > (m_pImpl->vecAttribute.size()) ) ? m_pImpl->vecAttribute[i].sName : OUString();
71 }
72 
getTypeByIndex(sal_Int16 i)73 OUString SAL_CALL AttributeList::getTypeByIndex(sal_Int16 i) throw( ::com::sun::star::uno::RuntimeException )
74 {
75     if( i < static_cast < sal_Int16 > (m_pImpl->vecAttribute.size() ) ) {
76         return m_pImpl->vecAttribute[i].sType;
77     }
78     return OUString();
79 }
80 
getValueByIndex(sal_Int16 i)81 OUString SAL_CALL  AttributeList::getValueByIndex(sal_Int16 i) throw( ::com::sun::star::uno::RuntimeException )
82 {
83     return ( i < static_cast < sal_Int16 > (m_pImpl->vecAttribute.size() ) ) ? m_pImpl->vecAttribute[i].sValue : OUString();
84 }
85 
getTypeByName(const OUString & sName)86 OUString SAL_CALL AttributeList::getTypeByName( const OUString& sName ) throw( ::com::sun::star::uno::RuntimeException )
87 {
88     ::std::vector<struct TagAttribute_Impl>::iterator ii = m_pImpl->vecAttribute.begin();
89 
90     for( ; ii != m_pImpl->vecAttribute.end() ; ii ++ ) {
91         if( (*ii).sName == sName ) {
92             return (*ii).sType;
93         }
94     }
95     return OUString();
96 }
97 
getValueByName(const OUString & sName)98 OUString SAL_CALL AttributeList::getValueByName(const OUString& sName) throw( ::com::sun::star::uno::RuntimeException )
99 {
100     ::std::vector<struct TagAttribute_Impl>::iterator ii = m_pImpl->vecAttribute.begin();
101 
102     for( ; ii != m_pImpl->vecAttribute.end() ; ii ++ ) {
103         if( (*ii).sName == sName ) {
104             return (*ii).sValue;
105         }
106     }
107     return OUString();
108 }
109 
110 
AttributeList()111 AttributeList::AttributeList()
112 {
113     m_pImpl = new AttributeList_Impl;
114 }
115 
116 
117 
~AttributeList()118 AttributeList::~AttributeList()
119 {
120     delete m_pImpl;
121 }
122 
AddAttribute(const OUString & sName,const OUString & sType,const OUString & sValue)123 void AttributeList::AddAttribute(   const OUString &sName ,
124                                         const OUString &sType ,
125                                         const OUString &sValue )
126 {
127     m_pImpl->vecAttribute.push_back( TagAttribute_Impl( sName , sType , sValue ) );
128 }
129 
Clear()130 void AttributeList::Clear()
131 {
132     m_pImpl->vecAttribute.clear();
133 
134     VOS_ENSURE( ! getLength(), "Length > 0 after AttributeList::Clear!");
135 }
136 
RemoveAttribute(const OUString sName)137 void AttributeList::RemoveAttribute( const OUString sName )
138 {
139     ::std::vector<struct TagAttribute_Impl>::iterator ii = m_pImpl->vecAttribute.begin();
140 
141     for( ; ii != m_pImpl->vecAttribute.end() ; ii ++ ) {
142         if( (*ii).sName == sName ) {
143             m_pImpl->vecAttribute.erase( ii );
144             break;
145         }
146     }
147 }
148 
149 
SetAttributeList(const uno::Reference<::com::sun::star::xml::sax::XAttributeList> & r)150 void AttributeList::SetAttributeList( const uno::Reference< ::com::sun::star::xml::sax::XAttributeList >  &r )
151 {
152     Clear();
153     AppendAttributeList( r );
154 }
155 
AppendAttributeList(const uno::Reference<::com::sun::star::xml::sax::XAttributeList> & r)156 void AttributeList::AppendAttributeList( const uno::Reference< ::com::sun::star::xml::sax::XAttributeList >  &r )
157 {
158     VOS_ENSURE( r.is(), "r isn't!" );
159 
160     sal_Int32 nMax = r->getLength();
161     sal_Int32 nTotalSize = m_pImpl->vecAttribute.size() + nMax;
162     m_pImpl->vecAttribute.reserve( nTotalSize );
163 
164     for( sal_Int16 i = 0 ; i < nMax ; i ++ ) {
165         m_pImpl->vecAttribute.push_back( TagAttribute_Impl(
166             r->getNameByIndex( i ) ,
167             r->getTypeByIndex( i ) ,
168             r->getValueByIndex( i )));
169     }
170 
171     VOS_ENSURE( nTotalSize == getLength(), "nTotalSize != getLength()");
172 }
173 
174 } // namespace comphelper
175 
176