1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 #ifndef INCLUDED_SAX_FASTATTRIBS_HXX
21 #define INCLUDED_SAX_FASTATTRIBS_HXX
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>
27 #include <cppuhelper/implbase.hxx>
28 #include <o3tl/string_view.hxx>
30 #include <sax/saxdllapi.h>
32 #include <string_view>
35 namespace com::sun::star::xml::sax
{ class XFastTokenHandler
; }
36 namespace com::sun::star::xml
{ struct Attribute
; }
37 namespace com::sun::star::xml
{ struct FastAttribute
; }
39 namespace sax_fastparser
42 struct UnknownAttribute
44 OUString maNamespaceURL
;
48 UnknownAttribute( OUString sNamespaceURL
, OString aName
, OString value
);
49 UnknownAttribute( OString sName
, OString value
);
51 void FillAttribute( css::xml::Attribute
* pAttrib
) const;
54 /// A native C++ interface to tokenisation
55 class SAX_DLLPUBLIC FastTokenHandlerBase
:
56 public cppu::WeakImplHelper
< css::xml::sax::XFastTokenHandler
>
59 virtual ~FastTokenHandlerBase();
60 virtual sal_Int32
getTokenDirect( const char *pToken
, sal_Int32 nLength
) const = 0;
63 * Client method to attempt the use of this interface if possible.
64 * @xTokenHandler - the token lookup interface
65 * @str - string buffer to lookup
67 * @return Tokenized form of str
69 static sal_Int32
getTokenFromChars(
70 const FastTokenHandlerBase
*pTokenHandler
,
71 std::string_view str
);
75 class SAX_DLLPUBLIC FastAttributeList final
: public cppu::WeakImplHelper
< css::xml::sax::XFastAttributeList
, css::util::XCloneable
>
78 FastAttributeList( FastTokenHandlerBase
*pTokenHandler
);
79 FastAttributeList( const css::uno::Reference
< css::xml::sax::XFastAttributeList
> & xAttrList
);
80 virtual ~FastAttributeList() override
;
83 void reserve( sal_Int32 nNumTokens
)
85 maAttributeValues
.reserve(nNumTokens
+1);
86 maAttributeTokens
.reserve(nNumTokens
);
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
)
97 add(nToken
, std::forward
<Val
>(val
));
98 add(std::forward
<Rest
>(rest
)...);
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(); }
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
116 return o3tl::toInt32(getAsViewByIndex(nTokenIndex
));
118 std::string_view
getAsViewByIndex( sal_Int32 nTokenIndex
) const
120 return std::string_view(getFastAttributeValue(nTokenIndex
), AttributeValueLength(nTokenIndex
));
122 OUString
getValueByIndex( sal_Int32 nTokenIndex
) const
124 return OStringToOUString(getAsViewByIndex(nTokenIndex
), RTL_TEXTENCODING_UTF8
);
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
;
137 virtual ::css::uno::Reference
< ::css::util::XCloneable
> SAL_CALL
createClone() override
;
139 sal_Int32
getAttributeIndex( ::sal_Int32 Token
)
141 for (size_t i
=0; i
<maAttributeTokens
.size(); ++i
)
142 if (maAttributeTokens
[i
] == Token
)
147 /// Use for fast iteration and conversion of attributes
148 class FastAttributeIter
{
149 const FastAttributeList
&mrList
;
153 FastAttributeIter(const FastAttributeList
&rList
, size_t nIdx
)
154 : mrList(rList
), mnIdx(nIdx
)
158 FastAttributeIter
& operator++ ()
163 bool operator!=( const FastAttributeIter
& rhs
) const
165 return mnIdx
!= rhs
.mnIdx
;
168 const FastAttributeIter
& operator*() const
173 sal_Int32
getToken() const
175 assert(mnIdx
< mrList
.maAttributeTokens
.size());
176 return mrList
.maAttributeTokens
[mnIdx
];
180 assert(mnIdx
< mrList
.maAttributeTokens
.size());
181 return mrList
.AttributeValueLength(mnIdx
) < 1;
183 sal_Int32
toInt32() const
185 assert(mnIdx
< mrList
.maAttributeTokens
.size());
186 return mrList
.getAsIntegerByIndex(mnIdx
);
188 double toDouble() const
190 assert(mnIdx
< mrList
.maAttributeTokens
.size());
191 return o3tl::toDouble(mrList
.getAsViewByIndex(mnIdx
));
193 bool toBoolean() const
195 assert(mnIdx
< mrList
.maAttributeTokens
.size());
196 return rtl_str_toBoolean(mrList
.getFastAttributeValue(mnIdx
));
198 OUString
toString() const
200 assert(mnIdx
< mrList
.maAttributeTokens
.size());
201 return mrList
.getValueByIndex(mnIdx
);
203 const char* toCString() const
205 assert(mnIdx
< mrList
.maAttributeTokens
.size());
206 return mrList
.getFastAttributeValue(mnIdx
);
208 sal_Int32
getLength() const
210 assert(mnIdx
< mrList
.maAttributeTokens
.size());
211 return mrList
.AttributeValueLength(mnIdx
);
213 std::string_view
toView() const
215 assert(mnIdx
< mrList
.maAttributeTokens
.size());
216 return mrList
.getAsViewByIndex(mnIdx
);
218 bool isString(const char *str
) const
220 assert(mnIdx
< mrList
.maAttributeTokens
.size());
221 return !strcmp(str
, mrList
.getFastAttributeValue(mnIdx
));
224 FastAttributeIter
begin() const { return FastAttributeIter(*this, 0); }
225 FastAttributeIter
end() const { return FastAttributeIter(*this, maAttributeTokens
.size()); }
226 FastAttributeIter
find( sal_Int32 nToken
) const;
229 char *mpChunk
; ///< buffer to store all attribute values - null terminated strings
230 sal_Int32 mnChunkLength
; ///< size of allocated memory for mpChunk
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
;
237 FastTokenHandlerBase
* mpTokenHandler
;
240 inline FastAttributeList
& castToFastAttributeList(
241 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& xAttrList
)
243 assert( dynamic_cast <FastAttributeList
*> ( xAttrList
.get() ) != nullptr );
244 return *static_cast <FastAttributeList
*> ( xAttrList
.get() );
251 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */