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 60416 : FastTokenHandlerBase::~FastTokenHandlerBase()
32 : {
33 60416 : }
34 :
35 35490 : UnknownAttribute::UnknownAttribute( const OUString& rNamespaceURL, const OString& rName, const sal_Char* pValue )
36 35490 : : maNamespaceURL( rNamespaceURL ), maName( rName ), maValue( pValue )
37 : {
38 35490 : }
39 :
40 3420 : UnknownAttribute::UnknownAttribute( const OString& rName, const sal_Char* pValue )
41 3420 : : maName( rName ), maValue( pValue )
42 : {
43 3420 : }
44 :
45 8 : void UnknownAttribute::FillAttribute( Attribute* pAttrib ) const
46 : {
47 8 : if( pAttrib )
48 : {
49 8 : pAttrib->Name = OStringToOUString( maName, RTL_TEXTENCODING_UTF8 );
50 8 : pAttrib->NamespaceURL = maNamespaceURL;
51 8 : pAttrib->Value = OStringToOUString( maValue, RTL_TEXTENCODING_UTF8 );
52 : }
53 8 : }
54 :
55 1239817 : FastAttributeList::FastAttributeList( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastTokenHandler >& xTokenHandler,
56 : sax_fastparser::FastTokenHandlerBase *pTokenHandler)
57 : : mxTokenHandler( xTokenHandler ),
58 1239817 : mpTokenHandler( pTokenHandler )
59 : {
60 : // random initial size of buffer to store attribute values
61 1239817 : mnChunkLength = 58;
62 1239817 : mpChunk = (sal_Char *) malloc( mnChunkLength );
63 1239817 : maAttributeValues.push_back( 0 );
64 1239817 : }
65 :
66 3718735 : FastAttributeList::~FastAttributeList()
67 : {
68 1239579 : free( mpChunk );
69 2479156 : }
70 :
71 4072541 : void FastAttributeList::clear()
72 : {
73 4072541 : maAttributeTokens.clear();
74 4072541 : maAttributeValues.clear();
75 4072541 : maAttributeValues.push_back( 0 );
76 4072541 : maUnknownAttributes.clear();
77 4072541 : }
78 :
79 7533777 : void FastAttributeList::add( sal_Int32 nToken, const sal_Char* pValue, size_t nValueLength )
80 : {
81 7533777 : maAttributeTokens.push_back( nToken );
82 7533775 : if (nValueLength == 0)
83 6289476 : nValueLength = strlen(pValue);
84 7533775 : sal_Int32 nWritePosition = maAttributeValues.back();
85 7533775 : maAttributeValues.push_back( maAttributeValues.back() + nValueLength + 1 );
86 7533777 : if (maAttributeValues.back() > mnChunkLength)
87 : {
88 88094 : mnChunkLength = maAttributeValues.back();
89 88094 : mpChunk = (sal_Char *) realloc( mpChunk, mnChunkLength );
90 : }
91 7533777 : strncpy(mpChunk + nWritePosition, pValue, nValueLength);
92 7533777 : mpChunk[nWritePosition + nValueLength] = '\0';
93 7533777 : }
94 :
95 1254596 : void FastAttributeList::add( sal_Int32 nToken, const OString& rValue )
96 : {
97 1254596 : add( nToken, rValue.getStr(), rValue.getLength() );
98 1254596 : }
99 :
100 738 : void FastAttributeList::addNS( sal_Int32 nNamespaceToken, sal_Int32 nToken, const OString& rValue )
101 : {
102 738 : sal_Int32 nCombinedToken = (nNamespaceToken << 16) | nToken;
103 738 : add( nCombinedToken, rValue );
104 738 : }
105 :
106 35490 : void FastAttributeList::addUnknown( const OUString& rNamespaceURL, const OString& rName, const sal_Char* pValue )
107 : {
108 35490 : maUnknownAttributes.push_back( UnknownAttribute( rNamespaceURL, rName, pValue ) );
109 35490 : }
110 :
111 3420 : void FastAttributeList::addUnknown( const OString& rName, const sal_Char* pValue )
112 : {
113 3420 : maUnknownAttributes.push_back( UnknownAttribute( rName, pValue ) );
114 3420 : }
115 :
116 : // XFastAttributeList
117 10053466 : sal_Bool FastAttributeList::hasAttribute( ::sal_Int32 Token ) throw (RuntimeException, std::exception)
118 : {
119 24969630 : for (size_t i = 0; i < maAttributeTokens.size(); ++i)
120 20254594 : if (maAttributeTokens[i] == Token)
121 5338430 : return sal_True;
122 :
123 4715036 : return sal_False;
124 : }
125 :
126 0 : sal_Int32 FastAttributeList::getValueToken( ::sal_Int32 Token ) throw (SAXException, RuntimeException, std::exception)
127 : {
128 0 : for (size_t i = 0; i < maAttributeTokens.size(); ++i)
129 0 : if (maAttributeTokens[i] == Token)
130 : return FastTokenHandlerBase::getTokenFromChars(
131 : mxTokenHandler, mpTokenHandler,
132 : getFastAttributeValue(i),
133 0 : AttributeValueLength( i ) );
134 :
135 0 : throw SAXException();
136 : }
137 :
138 840522 : sal_Int32 FastAttributeList::getOptionalValueToken( ::sal_Int32 Token, ::sal_Int32 Default ) throw (RuntimeException, std::exception)
139 : {
140 1664036 : for (size_t i = 0; i < maAttributeTokens.size(); ++i)
141 1007422 : if (maAttributeTokens[i] == Token)
142 : return FastTokenHandlerBase::getTokenFromChars(
143 : mxTokenHandler, mpTokenHandler,
144 : getFastAttributeValue(i),
145 183908 : AttributeValueLength( i ) );
146 :
147 656614 : return Default;
148 : }
149 :
150 : // performance sensitive shortcuts to avoid allocation ...
151 889180 : bool FastAttributeList::getAsInteger( sal_Int32 nToken, sal_Int32 &rInt)
152 : {
153 889180 : rInt = 0;
154 1610671 : for (size_t i = 0; i < maAttributeTokens.size(); ++i)
155 1481586 : if (maAttributeTokens[i] == nToken)
156 : {
157 760095 : rInt = rtl_str_toInt32( getFastAttributeValue(i), 10 );
158 760095 : return true;
159 : }
160 129084 : return false;
161 : }
162 :
163 34880 : bool FastAttributeList::getAsDouble( sal_Int32 nToken, double &rDouble)
164 : {
165 34880 : rDouble = 0.0;
166 49114 : for (size_t i = 0; i < maAttributeTokens.size(); ++i)
167 46754 : if (maAttributeTokens[i] == nToken)
168 : {
169 32520 : rDouble = rtl_str_toDouble( getFastAttributeValue(i) );
170 32520 : return true;
171 : }
172 2360 : return false;
173 : }
174 :
175 1091740 : bool FastAttributeList::getAsChar( sal_Int32 nToken, const char*& rPos ) const
176 : {
177 2365946 : for (size_t i = 0, n = maAttributeTokens.size(); i < n; ++i)
178 : {
179 2285532 : if (maAttributeTokens[i] != nToken)
180 1274206 : continue;
181 :
182 1011326 : sal_Int32 nOffset = maAttributeValues[i];
183 1011326 : rPos = mpChunk + nOffset;
184 1011326 : return true;
185 : }
186 :
187 80414 : return false;
188 : }
189 :
190 3578914 : OUString FastAttributeList::getValue( ::sal_Int32 Token ) throw (SAXException, RuntimeException, std::exception)
191 : {
192 6735172 : for (size_t i = 0; i < maAttributeTokens.size(); ++i)
193 6735154 : if (maAttributeTokens[i] == Token)
194 7157792 : return OUString( getFastAttributeValue(i), AttributeValueLength(i), RTL_TEXTENCODING_UTF8 );
195 :
196 18 : throw SAXException();
197 : }
198 :
199 642144 : OUString FastAttributeList::getOptionalValue( ::sal_Int32 Token ) throw (RuntimeException, std::exception)
200 : {
201 1158036 : for (size_t i = 0; i < maAttributeTokens.size(); ++i)
202 1073738 : if (maAttributeTokens[i] == Token)
203 557846 : return OUString( getFastAttributeValue(i), AttributeValueLength(i), RTL_TEXTENCODING_UTF8 );
204 :
205 84298 : return OUString();
206 : }
207 4 : Sequence< Attribute > FastAttributeList::getUnknownAttributes( ) throw (RuntimeException, std::exception)
208 : {
209 4 : Sequence< Attribute > aSeq( maUnknownAttributes.size() );
210 4 : Attribute* pAttr = aSeq.getArray();
211 12 : for( UnknownAttributeList::iterator attrIter = maUnknownAttributes.begin(); attrIter != maUnknownAttributes.end(); ++attrIter )
212 8 : (*attrIter).FillAttribute( pAttr++ );
213 4 : return aSeq;
214 : }
215 10774 : Sequence< FastAttribute > FastAttributeList::getFastAttributes( ) throw (RuntimeException, std::exception)
216 : {
217 10774 : Sequence< FastAttribute > aSeq( maAttributeTokens.size() );
218 10774 : FastAttribute* pAttr = aSeq.getArray();
219 16202 : for (size_t i = 0; i < maAttributeTokens.size(); ++i)
220 : {
221 5428 : pAttr->Token = maAttributeTokens[i];
222 5428 : pAttr->Value = OUString( getFastAttributeValue(i), AttributeValueLength(i), RTL_TEXTENCODING_UTF8 );
223 5428 : pAttr++;
224 : }
225 10774 : return aSeq;
226 : }
227 :
228 10633726 : sal_Int32 FastTokenHandlerBase::getTokenFromChars(
229 : const css::uno::Reference< css::xml::sax::XFastTokenHandler > &xTokenHandler,
230 : FastTokenHandlerBase *pTokenHandler,
231 : const char *pToken, size_t nLen /* = 0 */ )
232 : {
233 : sal_Int32 nRet;
234 :
235 10633726 : if( !nLen )
236 18 : nLen = strlen( pToken );
237 :
238 10633726 : if( pTokenHandler )
239 10524036 : nRet = pTokenHandler->getTokenDirect( pToken, (sal_Int32) nLen );
240 : else
241 : {
242 : // heap allocate, copy & then free
243 109690 : Sequence< sal_Int8 > aSeq( (sal_Int8*)pToken, nLen );
244 109690 : nRet = xTokenHandler->getTokenFromUTF8( aSeq );
245 : }
246 :
247 10633679 : return nRet;
248 : }
249 :
250 : }
251 :
252 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|