Line data Source code
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 :
20 : #include <algorithm>
21 :
22 : #include <sax/fastattribs.hxx>
23 :
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
28 : {
29 :
30 : // wasteage to keep MSVC happy vs. an in-line {}
31 34660 : FastTokenHandlerBase::~FastTokenHandlerBase()
32 : {
33 34660 : }
34 :
35 18713 : UnknownAttribute::UnknownAttribute( const OUString& rNamespaceURL, const OString& rName, const OString& value )
36 18713 : : maNamespaceURL( rNamespaceURL ), maName( rName ), maValue( value )
37 : {
38 18713 : }
39 :
40 1845 : UnknownAttribute::UnknownAttribute( const OString& rName, const OString& value )
41 1845 : : maName( rName ), maValue( value )
42 : {
43 1845 : }
44 :
45 4 : void UnknownAttribute::FillAttribute( Attribute* pAttrib ) const
46 : {
47 4 : if( pAttrib )
48 : {
49 4 : pAttrib->Name = OStringToOUString( maName, RTL_TEXTENCODING_UTF8 );
50 4 : pAttrib->NamespaceURL = maNamespaceURL;
51 4 : pAttrib->Value = OStringToOUString( maValue, RTL_TEXTENCODING_UTF8 );
52 : }
53 4 : }
54 :
55 703143 : FastAttributeList::FastAttributeList( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastTokenHandler >& xTokenHandler,
56 : sax_fastparser::FastTokenHandlerBase *pTokenHandler)
57 : : mxTokenHandler( xTokenHandler ),
58 703143 : mpTokenHandler( pTokenHandler )
59 : {
60 : // random initial size of buffer to store attribute values
61 703143 : mnChunkLength = 58;
62 703143 : mpChunk = static_cast<sal_Char *>(malloc( mnChunkLength ));
63 703143 : maAttributeValues.push_back( 0 );
64 703143 : }
65 :
66 2109092 : FastAttributeList::~FastAttributeList()
67 : {
68 703031 : free( mpChunk );
69 1406061 : }
70 :
71 2190001 : void FastAttributeList::clear()
72 : {
73 2190001 : maAttributeTokens.clear();
74 2190001 : maAttributeValues.resize(1);
75 : assert(maAttributeValues[0] == 0);
76 2190001 : if (!maUnknownAttributes.empty())
77 10186 : maUnknownAttributes.clear();
78 2190001 : }
79 :
80 4076395 : void FastAttributeList::add( sal_Int32 nToken, const sal_Char* pValue, size_t nValueLength )
81 : {
82 4076395 : maAttributeTokens.push_back( nToken );
83 4076396 : sal_Int32 nWritePosition = maAttributeValues.back();
84 4076395 : maAttributeValues.push_back( maAttributeValues.back() + nValueLength + 1 );
85 4076396 : if (maAttributeValues.back() > mnChunkLength)
86 : {
87 52188 : mnChunkLength = maAttributeValues.back();
88 52188 : mpChunk = static_cast<sal_Char *>(realloc( mpChunk, mnChunkLength ));
89 : }
90 4076396 : strncpy(mpChunk + nWritePosition, pValue, nValueLength);
91 4076396 : mpChunk[nWritePosition + nValueLength] = '\0';
92 4076396 : }
93 :
94 147334 : void FastAttributeList::add( sal_Int32 nToken, const sal_Char* pValue )
95 : {
96 147334 : add( nToken, pValue, strlen( pValue ));
97 147334 : }
98 :
99 681902 : void FastAttributeList::add( sal_Int32 nToken, const OString& rValue )
100 : {
101 681902 : add( nToken, rValue.getStr(), rValue.getLength() );
102 681902 : }
103 :
104 376 : void FastAttributeList::addNS( sal_Int32 nNamespaceToken, sal_Int32 nToken, const OString& rValue )
105 : {
106 376 : sal_Int32 nCombinedToken = (nNamespaceToken << 16) | nToken;
107 376 : add( nCombinedToken, rValue );
108 376 : }
109 :
110 18713 : void FastAttributeList::addUnknown( const OUString& rNamespaceURL, const OString& rName, const OString& value )
111 : {
112 18713 : maUnknownAttributes.push_back( UnknownAttribute( rNamespaceURL, rName, value ) );
113 18713 : }
114 :
115 1845 : void FastAttributeList::addUnknown( const OString& rName, const OString& value )
116 : {
117 1845 : maUnknownAttributes.push_back( UnknownAttribute( rName, value ) );
118 1845 : }
119 :
120 : // XFastAttributeList
121 5369561 : sal_Bool FastAttributeList::hasAttribute( ::sal_Int32 Token ) throw (RuntimeException, std::exception)
122 : {
123 13291447 : for (size_t i = 0; i < maAttributeTokens.size(); ++i)
124 10787420 : if (maAttributeTokens[i] == Token)
125 2865534 : return sal_True;
126 :
127 2504027 : return sal_False;
128 : }
129 :
130 0 : sal_Int32 FastAttributeList::getValueToken( ::sal_Int32 Token ) throw (SAXException, RuntimeException, std::exception)
131 : {
132 0 : for (size_t i = 0; i < maAttributeTokens.size(); ++i)
133 0 : if (maAttributeTokens[i] == Token)
134 : return FastTokenHandlerBase::getTokenFromChars(
135 : mxTokenHandler, mpTokenHandler,
136 : getFastAttributeValue(i),
137 0 : AttributeValueLength( i ) );
138 :
139 0 : throw SAXException();
140 : }
141 :
142 492347 : sal_Int32 FastAttributeList::getOptionalValueToken( ::sal_Int32 Token, ::sal_Int32 Default ) throw (RuntimeException, std::exception)
143 : {
144 984439 : for (size_t i = 0; i < maAttributeTokens.size(); ++i)
145 599774 : if (maAttributeTokens[i] == Token)
146 : return FastTokenHandlerBase::getTokenFromChars(
147 : mxTokenHandler, mpTokenHandler,
148 : getFastAttributeValue(i),
149 107681 : AttributeValueLength( i ) );
150 :
151 384663 : return Default;
152 : }
153 :
154 : // performance sensitive shortcuts to avoid allocation ...
155 499007 : bool FastAttributeList::getAsInteger( sal_Int32 nToken, sal_Int32 &rInt)
156 : {
157 499007 : rInt = 0;
158 921324 : for (size_t i = 0; i < maAttributeTokens.size(); ++i)
159 845180 : if (maAttributeTokens[i] == nToken)
160 : {
161 422864 : rInt = rtl_str_toInt32( getFastAttributeValue(i), 10 );
162 422864 : return true;
163 : }
164 76144 : return false;
165 : }
166 :
167 20749 : bool FastAttributeList::getAsDouble( sal_Int32 nToken, double &rDouble)
168 : {
169 20749 : rDouble = 0.0;
170 31210 : for (size_t i = 0; i < maAttributeTokens.size(); ++i)
171 29416 : if (maAttributeTokens[i] == nToken)
172 : {
173 18955 : rDouble = rtl_str_toDouble( getFastAttributeValue(i) );
174 18955 : return true;
175 : }
176 1794 : return false;
177 : }
178 :
179 590397 : bool FastAttributeList::getAsChar( sal_Int32 nToken, const char*& rPos ) const
180 : {
181 1306986 : for (size_t i = 0, n = maAttributeTokens.size(); i < n; ++i)
182 : {
183 1256210 : if (maAttributeTokens[i] != nToken)
184 716589 : continue;
185 :
186 539625 : sal_Int32 nOffset = maAttributeValues[i];
187 539625 : rPos = mpChunk + nOffset;
188 539625 : return true;
189 : }
190 :
191 50773 : return false;
192 : }
193 :
194 1908096 : OUString FastAttributeList::getValue( ::sal_Int32 Token ) throw (SAXException, RuntimeException, std::exception)
195 : {
196 3591217 : for (size_t i = 0; i < maAttributeTokens.size(); ++i)
197 3591206 : if (maAttributeTokens[i] == Token)
198 3816170 : return OUString( getFastAttributeValue(i), AttributeValueLength(i), RTL_TEXTENCODING_UTF8 );
199 :
200 11 : throw SAXException();
201 : }
202 :
203 367181 : OUString FastAttributeList::getOptionalValue( ::sal_Int32 Token ) throw (RuntimeException, std::exception)
204 : {
205 651391 : for (size_t i = 0; i < maAttributeTokens.size(); ++i)
206 605231 : if (maAttributeTokens[i] == Token)
207 321021 : return OUString( getFastAttributeValue(i), AttributeValueLength(i), RTL_TEXTENCODING_UTF8 );
208 :
209 46161 : return OUString();
210 : }
211 2 : Sequence< Attribute > FastAttributeList::getUnknownAttributes( ) throw (RuntimeException, std::exception)
212 : {
213 2 : Sequence< Attribute > aSeq( maUnknownAttributes.size() );
214 2 : Attribute* pAttr = aSeq.getArray();
215 6 : for( UnknownAttributeList::iterator attrIter = maUnknownAttributes.begin(); attrIter != maUnknownAttributes.end(); ++attrIter )
216 4 : (*attrIter).FillAttribute( pAttr++ );
217 2 : return aSeq;
218 : }
219 5382 : Sequence< FastAttribute > FastAttributeList::getFastAttributes( ) throw (RuntimeException, std::exception)
220 : {
221 5382 : Sequence< FastAttribute > aSeq( maAttributeTokens.size() );
222 5382 : FastAttribute* pAttr = aSeq.getArray();
223 8098 : for (size_t i = 0; i < maAttributeTokens.size(); ++i)
224 : {
225 2716 : pAttr->Token = maAttributeTokens[i];
226 2716 : pAttr->Value = OUString( getFastAttributeValue(i), AttributeValueLength(i), RTL_TEXTENCODING_UTF8 );
227 2716 : pAttr++;
228 : }
229 5382 : return aSeq;
230 : }
231 :
232 5795553 : 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 */ )
236 : {
237 : sal_Int32 nRet;
238 :
239 5795553 : if( !nLen )
240 0 : nLen = strlen( pToken );
241 :
242 5795553 : if( pTokenHandler )
243 5737347 : nRet = pTokenHandler->getTokenDirect( pToken, (sal_Int32) nLen );
244 : else
245 : {
246 : // heap allocate, copy & then free
247 58206 : Sequence< sal_Int8 > aSeq( reinterpret_cast<sal_Int8 const *>(pToken), nLen );
248 58206 : nRet = xTokenHandler->getTokenFromUTF8( aSeq );
249 : }
250 :
251 5795541 : return nRet;
252 : }
253 :
254 : }
255 :
256 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|