LibreOffice Module sax (master) 1
fastattribs.hxx
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
20#ifndef INCLUDED_SAX_FASTATTRIBS_HXX
21#define INCLUDED_SAX_FASTATTRIBS_HXX
22
23#include <com/sun/star/xml/sax/XFastAttributeList.hpp>
24#include <com/sun/star/xml/sax/XFastTokenHandler.hpp>
25#include <com/sun/star/util/XCloneable.hpp>
26
28#include <o3tl/string_view.hxx>
29#include <rtl/math.h>
30#include <sax/saxdllapi.h>
31
32#include <string_view>
33#include <vector>
34
35namespace com::sun::star::xml::sax { class XFastTokenHandler; }
36namespace com::sun::star::xml { struct Attribute; }
37namespace com::sun::star::xml { struct FastAttribute; }
38
39namespace sax_fastparser
40{
41
43{
45 OString maName;
46 OString maValue;
47
48 UnknownAttribute( OUString sNamespaceURL, OString aName, OString value );
49 UnknownAttribute( OString sName, OString value );
50
51 void FillAttribute( css::xml::Attribute* pAttrib ) const;
52};
53
56 public cppu::WeakImplHelper< css::xml::sax::XFastTokenHandler >
57{
58 public:
59 virtual ~FastTokenHandlerBase();
60 virtual sal_Int32 getTokenDirect( const char *pToken, sal_Int32 nLength ) const = 0;
61
69 static sal_Int32 getTokenFromChars(
70 const FastTokenHandlerBase *pTokenHandler,
71 std::string_view str );
72};
73
74
75class SAX_DLLPUBLIC FastAttributeList final : public cppu::WeakImplHelper< css::xml::sax::XFastAttributeList, css::util::XCloneable >
76{
77public:
79 FastAttributeList( const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList );
80 virtual ~FastAttributeList() override;
81
82 void clear();
83 void reserve( sal_Int32 nNumTokens )
84 {
85 maAttributeValues.reserve(nNumTokens+1);
86 maAttributeTokens.reserve(nNumTokens);
87 }
88 void add( const FastAttributeList& );
89 void add( const css::uno::Reference<css::xml::sax::XFastAttributeList>& );
90 void add( sal_Int32 nToken, std::string_view value );
91 void add( sal_Int32 nToken, std::u16string_view sValue ); // Converts to UTF-8
92 template <typename C, typename T1, typename T2>
93 void add( sal_Int32 nToken, rtl::StringConcat<C, T1, T2>&& value) { add(nToken, Concat2View(value)); }
94 template <typename Val, typename... Rest, std::enable_if_t<(sizeof...(Rest) > 0), int> = 0>
95 void add( sal_Int32 nToken, Val&& val, Rest&&... rest )
96 {
97 add(nToken, std::forward<Val>(val));
98 add(std::forward<Rest>(rest)...);
99 }
100 void addNS( sal_Int32 nNamespaceToken, sal_Int32 nToken, std::string_view sValue );
101 void addNS( sal_Int32 nNamespaceToken, sal_Int32 nToken, std::u16string_view sValue );
102 // note: rQName is *namespace-prefixed*
103 void addUnknown( const OUString& rNamespaceURL, const OString& rQName, const OString& value );
104 void addUnknown( const OString& rName, const OString& value );
105 const std::vector< sal_Int32 >& getFastAttributeTokens() const { return maAttributeTokens; }
106 const char* getFastAttributeValue(size_t nIndex) const { return mpChunk + maAttributeValues[nIndex]; }
107 sal_Int32 AttributeValueLength(size_t i) const { return maAttributeValues[i + 1] - maAttributeValues[i] - 1; }
108 size_t size() const { return maAttributeValues.size(); }
109
110 // performance sensitive shortcuts to avoid allocation ...
111 bool getAsInteger( sal_Int32 nToken, sal_Int32 &rInt) const;
112 bool getAsDouble( sal_Int32 nToken, double &rDouble) const;
113 bool getAsView( sal_Int32 nToken, std::string_view& rPos ) const;
114 sal_Int32 getAsIntegerByIndex( sal_Int32 nTokenIndex ) const
115 {
116 return o3tl::toInt32(getAsViewByIndex(nTokenIndex));
117 }
118 std::string_view getAsViewByIndex( sal_Int32 nTokenIndex ) const
119 {
120 return std::string_view(getFastAttributeValue(nTokenIndex), AttributeValueLength(nTokenIndex));
121 }
122 OUString getValueByIndex( sal_Int32 nTokenIndex ) const
123 {
124 return OStringToOUString(getAsViewByIndex(nTokenIndex), RTL_TEXTENCODING_UTF8);
125 }
126
127 // XFastAttributeList
128 virtual sal_Bool SAL_CALL hasAttribute( ::sal_Int32 Token ) override;
129 virtual ::sal_Int32 SAL_CALL getValueToken( ::sal_Int32 Token ) override;
130 virtual ::sal_Int32 SAL_CALL getOptionalValueToken( ::sal_Int32 Token, ::sal_Int32 Default ) override;
131 virtual OUString SAL_CALL getValue( ::sal_Int32 Token ) override;
132 virtual OUString SAL_CALL getOptionalValue( ::sal_Int32 Token ) override;
133 virtual css::uno::Sequence< css::xml::Attribute > SAL_CALL getUnknownAttributes( ) override;
134 virtual css::uno::Sequence< css::xml::FastAttribute > SAL_CALL getFastAttributes() override;
135
136 // XCloneable
137 virtual ::css::uno::Reference< ::css::util::XCloneable > SAL_CALL createClone() override;
138
139 sal_Int32 getAttributeIndex( ::sal_Int32 Token )
140 {
141 for (size_t i=0; i<maAttributeTokens.size(); ++i)
142 if (maAttributeTokens[i] == Token)
143 return i;
144 return -1;
145 }
146
150 size_t mnIdx;
151
152 public:
153 FastAttributeIter(const FastAttributeList &rList, size_t nIdx)
154 : mrList(rList), mnIdx(nIdx)
155 {
156 }
157
158 FastAttributeIter& operator++ ()
159 {
160 mnIdx++;
161 return *this;
162 }
163 bool operator!=( const FastAttributeIter& rhs ) const
164 {
165 return mnIdx != rhs.mnIdx;
166 }
167
169 {
170 return *this;
171 }
172
173 sal_Int32 getToken() const
174 {
175 assert(mnIdx < mrList.maAttributeTokens.size());
176 return mrList.maAttributeTokens[mnIdx];
177 }
178 bool isEmpty() const
179 {
180 assert(mnIdx < mrList.maAttributeTokens.size());
181 return mrList.AttributeValueLength(mnIdx) < 1;
182 }
183 sal_Int32 toInt32() const
184 {
185 assert(mnIdx < mrList.maAttributeTokens.size());
186 return mrList.getAsIntegerByIndex(mnIdx);
187 }
188 double toDouble() const
189 {
190 assert(mnIdx < mrList.maAttributeTokens.size());
191 return o3tl::toDouble(mrList.getAsViewByIndex(mnIdx));
192 }
193 bool toBoolean() const
194 {
195 assert(mnIdx < mrList.maAttributeTokens.size());
196 return rtl_str_toBoolean(mrList.getFastAttributeValue(mnIdx));
197 }
198 OUString toString() const
199 {
200 assert(mnIdx < mrList.maAttributeTokens.size());
201 return mrList.getValueByIndex(mnIdx);
202 }
203 const char* toCString() const
204 {
205 assert(mnIdx < mrList.maAttributeTokens.size());
206 return mrList.getFastAttributeValue(mnIdx);
207 }
208 sal_Int32 getLength() const
209 {
210 assert(mnIdx < mrList.maAttributeTokens.size());
211 return mrList.AttributeValueLength(mnIdx);
212 }
213 std::string_view toView() const
214 {
215 assert(mnIdx < mrList.maAttributeTokens.size());
216 return mrList.getAsViewByIndex(mnIdx);
217 }
218 bool isString(const char *str) const
219 {
220 assert(mnIdx < mrList.maAttributeTokens.size());
221 return !strcmp(str, mrList.getFastAttributeValue(mnIdx));
222 }
223 };
224 FastAttributeIter begin() const { return FastAttributeIter(*this, 0); }
225 FastAttributeIter end() const { return FastAttributeIter(*this, maAttributeTokens.size()); }
226 FastAttributeIter find( sal_Int32 nToken ) const;
227
228private:
229 char *mpChunk;
230 sal_Int32 mnChunkLength;
231 // maAttributeValues stores pointers, relative to mpChunk, for each attribute value string
232 // length of the string is maAttributeValues[n+1] - maAttributeValues[n] - 1
233 // maAttributeValues[0] == 0
234 std::vector< sal_Int32 > maAttributeValues;
235 std::vector< sal_Int32 > maAttributeTokens;
236 std::vector< UnknownAttribute > maUnknownAttributes;
238};
239
241 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
242{
243 assert( dynamic_cast <FastAttributeList *> ( xAttrList.get() ) != nullptr );
244 return *static_cast <FastAttributeList *> ( xAttrList.get() );
245}
246
247}
248
249#endif
250
251/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::ptrdiff_t mnIdx
Use for fast iteration and conversion of attributes.
FastAttributeIter(const FastAttributeList &rList, size_t nIdx)
const FastAttributeIter & operator*() const
bool operator!=(const FastAttributeIter &rhs) const
sal_Int32 AttributeValueLength(size_t i) const
std::vector< sal_Int32 > maAttributeValues
std::vector< UnknownAttribute > maUnknownAttributes
FastAttributeIter begin() const
OUString getValueByIndex(sal_Int32 nTokenIndex) const
std::string_view getAsViewByIndex(sal_Int32 nTokenIndex) const
char * mpChunk
buffer to store all attribute values - null terminated strings
void reserve(sal_Int32 nNumTokens)
Definition: fastattribs.hxx:83
sal_Int32 getAttributeIndex(::sal_Int32 Token)
const std::vector< sal_Int32 > & getFastAttributeTokens() const
std::vector< sal_Int32 > maAttributeTokens
FastAttributeIter end() const
sal_Int32 getAsIntegerByIndex(sal_Int32 nTokenIndex) const
sal_Int32 mnChunkLength
size of allocated memory for mpChunk
void add(sal_Int32 nToken, rtl::StringConcat< C, T1, T2 > &&value)
Definition: fastattribs.hxx:93
FastTokenHandlerBase * mpTokenHandler
void add(sal_Int32 nToken, Val &&val, Rest &&... rest)
Definition: fastattribs.hxx:95
const char * getFastAttributeValue(size_t nIndex) const
A native C++ interface to tokenisation.
Definition: fastattribs.hxx:57
virtual sal_Int32 getTokenDirect(const char *pToken, sal_Int32 nLength) const =0
Any value
sal_Int32 nIndex
OSQLColumns::const_iterator find(const OSQLColumns::const_iterator &first, const OSQLColumns::const_iterator &last, std::u16string_view _rVal, const ::comphelper::UStringMixEqual &_rCase)
int i
css::beans::Optional< css::uno::Any > getValue(std::u16string_view id)
sal_Int32 toInt32(std::u16string_view str, sal_Int16 radix=10)
double toDouble(std::u16string_view str)
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
DefTokenId nToken
#define SAX_DLLPUBLIC
Definition: saxdllapi.h:28
void FillAttribute(css::xml::Attribute *pAttrib) const
Definition: fastattribs.cxx:48
UnknownAttribute(OUString sNamespaceURL, OString aName, OString value)
Definition: fastattribs.cxx:38
unsigned char sal_Bool