Bump version to 5.0-14
[LibreOffice.git] / sax / source / tools / fastattribs.cxx
blobcb97acccf21c778bb44a4da8534d31754703a6b2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <algorithm>
22 #include <sax/fastattribs.hxx>
24 using namespace ::com::sun::star::uno;
25 using namespace ::com::sun::star::xml;
26 using namespace ::com::sun::star::xml::sax;
27 namespace sax_fastparser
30 // wasteage to keep MSVC happy vs. an in-line {}
31 FastTokenHandlerBase::~FastTokenHandlerBase()
35 UnknownAttribute::UnknownAttribute( const OUString& rNamespaceURL, const OString& rName, const OString& value )
36 : maNamespaceURL( rNamespaceURL ), maName( rName ), maValue( value )
40 UnknownAttribute::UnknownAttribute( const OString& rName, const OString& value )
41 : maName( rName ), maValue( value )
45 void UnknownAttribute::FillAttribute( Attribute* pAttrib ) const
47 if( pAttrib )
49 pAttrib->Name = OStringToOUString( maName, RTL_TEXTENCODING_UTF8 );
50 pAttrib->NamespaceURL = maNamespaceURL;
51 pAttrib->Value = OStringToOUString( maValue, RTL_TEXTENCODING_UTF8 );
55 FastAttributeList::FastAttributeList( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastTokenHandler >& xTokenHandler,
56 sax_fastparser::FastTokenHandlerBase *pTokenHandler)
57 : mxTokenHandler( xTokenHandler ),
58 mpTokenHandler( pTokenHandler )
60 // random initial size of buffer to store attribute values
61 mnChunkLength = 58;
62 mpChunk = static_cast<sal_Char *>(malloc( mnChunkLength ));
63 maAttributeValues.push_back( 0 );
66 FastAttributeList::~FastAttributeList()
68 free( mpChunk );
71 void FastAttributeList::clear()
73 maAttributeTokens.clear();
74 maAttributeValues.resize(1);
75 assert(maAttributeValues[0] == 0);
76 if (!maUnknownAttributes.empty())
77 maUnknownAttributes.clear();
80 void FastAttributeList::add( sal_Int32 nToken, const sal_Char* pValue, size_t nValueLength )
82 maAttributeTokens.push_back( nToken );
83 sal_Int32 nWritePosition = maAttributeValues.back();
84 maAttributeValues.push_back( maAttributeValues.back() + nValueLength + 1 );
85 if (maAttributeValues.back() > mnChunkLength)
87 mnChunkLength = maAttributeValues.back();
88 mpChunk = static_cast<sal_Char *>(realloc( mpChunk, mnChunkLength ));
90 strncpy(mpChunk + nWritePosition, pValue, nValueLength);
91 mpChunk[nWritePosition + nValueLength] = '\0';
94 void FastAttributeList::add( sal_Int32 nToken, const sal_Char* pValue )
96 add( nToken, pValue, strlen( pValue ));
99 void FastAttributeList::add( sal_Int32 nToken, const OString& rValue )
101 add( nToken, rValue.getStr(), rValue.getLength() );
104 void FastAttributeList::addNS( sal_Int32 nNamespaceToken, sal_Int32 nToken, const OString& rValue )
106 sal_Int32 nCombinedToken = (nNamespaceToken << 16) | nToken;
107 add( nCombinedToken, rValue );
110 void FastAttributeList::addUnknown( const OUString& rNamespaceURL, const OString& rName, const OString& value )
112 maUnknownAttributes.push_back( UnknownAttribute( rNamespaceURL, rName, value ) );
115 void FastAttributeList::addUnknown( const OString& rName, const OString& value )
117 maUnknownAttributes.push_back( UnknownAttribute( rName, value ) );
120 // XFastAttributeList
121 sal_Bool FastAttributeList::hasAttribute( ::sal_Int32 Token ) throw (RuntimeException, std::exception)
123 for (size_t i = 0; i < maAttributeTokens.size(); ++i)
124 if (maAttributeTokens[i] == Token)
125 return sal_True;
127 return sal_False;
130 sal_Int32 FastAttributeList::getValueToken( ::sal_Int32 Token ) throw (SAXException, RuntimeException, std::exception)
132 for (size_t i = 0; i < maAttributeTokens.size(); ++i)
133 if (maAttributeTokens[i] == Token)
134 return FastTokenHandlerBase::getTokenFromChars(
135 mxTokenHandler, mpTokenHandler,
136 getFastAttributeValue(i),
137 AttributeValueLength( i ) );
139 throw SAXException();
142 sal_Int32 FastAttributeList::getOptionalValueToken( ::sal_Int32 Token, ::sal_Int32 Default ) throw (RuntimeException, std::exception)
144 for (size_t i = 0; i < maAttributeTokens.size(); ++i)
145 if (maAttributeTokens[i] == Token)
146 return FastTokenHandlerBase::getTokenFromChars(
147 mxTokenHandler, mpTokenHandler,
148 getFastAttributeValue(i),
149 AttributeValueLength( i ) );
151 return Default;
154 // performance sensitive shortcuts to avoid allocation ...
155 bool FastAttributeList::getAsInteger( sal_Int32 nToken, sal_Int32 &rInt)
157 rInt = 0;
158 for (size_t i = 0; i < maAttributeTokens.size(); ++i)
159 if (maAttributeTokens[i] == nToken)
161 rInt = rtl_str_toInt32( getFastAttributeValue(i), 10 );
162 return true;
164 return false;
167 bool FastAttributeList::getAsDouble( sal_Int32 nToken, double &rDouble)
169 rDouble = 0.0;
170 for (size_t i = 0; i < maAttributeTokens.size(); ++i)
171 if (maAttributeTokens[i] == nToken)
173 rDouble = rtl_str_toDouble( getFastAttributeValue(i) );
174 return true;
176 return false;
179 bool FastAttributeList::getAsChar( sal_Int32 nToken, const char*& rPos ) const
181 for (size_t i = 0, n = maAttributeTokens.size(); i < n; ++i)
183 if (maAttributeTokens[i] != nToken)
184 continue;
186 sal_Int32 nOffset = maAttributeValues[i];
187 rPos = mpChunk + nOffset;
188 return true;
191 return false;
194 OUString FastAttributeList::getValue( ::sal_Int32 Token ) throw (SAXException, RuntimeException, std::exception)
196 for (size_t i = 0; i < maAttributeTokens.size(); ++i)
197 if (maAttributeTokens[i] == Token)
198 return OUString( getFastAttributeValue(i), AttributeValueLength(i), RTL_TEXTENCODING_UTF8 );
200 throw SAXException();
203 OUString FastAttributeList::getOptionalValue( ::sal_Int32 Token ) throw (RuntimeException, std::exception)
205 for (size_t i = 0; i < maAttributeTokens.size(); ++i)
206 if (maAttributeTokens[i] == Token)
207 return OUString( getFastAttributeValue(i), AttributeValueLength(i), RTL_TEXTENCODING_UTF8 );
209 return OUString();
211 Sequence< Attribute > FastAttributeList::getUnknownAttributes( ) throw (RuntimeException, std::exception)
213 Sequence< Attribute > aSeq( maUnknownAttributes.size() );
214 Attribute* pAttr = aSeq.getArray();
215 for( UnknownAttributeList::iterator attrIter = maUnknownAttributes.begin(); attrIter != maUnknownAttributes.end(); ++attrIter )
216 (*attrIter).FillAttribute( pAttr++ );
217 return aSeq;
219 Sequence< FastAttribute > FastAttributeList::getFastAttributes( ) throw (RuntimeException, std::exception)
221 Sequence< FastAttribute > aSeq( maAttributeTokens.size() );
222 FastAttribute* pAttr = aSeq.getArray();
223 for (size_t i = 0; i < maAttributeTokens.size(); ++i)
225 pAttr->Token = maAttributeTokens[i];
226 pAttr->Value = OUString( getFastAttributeValue(i), AttributeValueLength(i), RTL_TEXTENCODING_UTF8 );
227 pAttr++;
229 return aSeq;
232 sal_Int32 FastTokenHandlerBase::getTokenFromChars(
233 const css::uno::Reference< css::xml::sax::XFastTokenHandler > &xTokenHandler,
234 FastTokenHandlerBase *pTokenHandler,
235 const char *pToken, size_t nLen /* = 0 */ )
237 sal_Int32 nRet;
239 if( !nLen )
240 nLen = strlen( pToken );
242 if( pTokenHandler )
243 nRet = pTokenHandler->getTokenDirect( pToken, (sal_Int32) nLen );
244 else
246 // heap allocate, copy & then free
247 Sequence< sal_Int8 > aSeq( reinterpret_cast<sal_Int8 const *>(pToken), nLen );
248 nRet = xTokenHandler->getTokenFromUTF8( aSeq );
251 return nRet;
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */