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>
29 #include <sax/saxdllapi.h>
31 #include <string_view>
34 namespace com::sun::star::xml::sax
{ class XFastTokenHandler
; }
35 namespace com::sun::star::xml
{ struct Attribute
; }
36 namespace com::sun::star::xml
{ struct FastAttribute
; }
38 namespace sax_fastparser
41 struct UnknownAttribute
43 OUString maNamespaceURL
;
47 UnknownAttribute( OUString sNamespaceURL
, OString aName
, OString value
);
48 UnknownAttribute( OString sName
, OString value
);
50 void FillAttribute( css::xml::Attribute
* pAttrib
) const;
53 /// A native C++ interface to tokenisation
54 class SAX_DLLPUBLIC FastTokenHandlerBase
:
55 public cppu::WeakImplHelper
< css::xml::sax::XFastTokenHandler
>
58 virtual ~FastTokenHandlerBase();
59 virtual sal_Int32
getTokenDirect( const char *pToken
, sal_Int32 nLength
) const = 0;
62 * Client method to attempt the use of this interface if possible.
63 * @xTokenHandler - the token lookup interface
64 * @str - string buffer to lookup
66 * @return Tokenized form of str
68 static sal_Int32
getTokenFromChars(
69 const FastTokenHandlerBase
*pTokenHandler
,
70 std::string_view str
);
74 class SAX_DLLPUBLIC FastAttributeList final
: public cppu::WeakImplHelper
< css::xml::sax::XFastAttributeList
, css::util::XCloneable
>
77 FastAttributeList( FastTokenHandlerBase
*pTokenHandler
);
78 FastAttributeList( const css::uno::Reference
< css::xml::sax::XFastAttributeList
> & xAttrList
);
79 virtual ~FastAttributeList() override
;
82 void reserve( sal_Int32 nNumTokens
)
84 maAttributeValues
.reserve(nNumTokens
+1);
85 maAttributeTokens
.reserve(nNumTokens
);
87 void add( const FastAttributeList
& );
88 void add( const css::uno::Reference
<css::xml::sax::XFastAttributeList
>& );
89 void add( sal_Int32 nToken
, std::string_view value
);
90 void add( sal_Int32 nToken
, std::u16string_view sValue
); // Converts to UTF-8
91 template <typename C
, typename T1
, typename T2
>
92 void add( sal_Int32 nToken
, rtl::StringConcat
<C
, T1
, T2
>&& value
) { add(nToken
, Concat2View(value
)); }
93 template <typename Val
, typename
... Rest
, std::enable_if_t
<(sizeof...(Rest
) > 0), int> = 0>
94 void add( sal_Int32 nToken
, Val
&& val
, Rest
&&... rest
)
96 add(nToken
, std::forward
<Val
>(val
));
97 add(std::forward
<Rest
>(rest
)...);
99 void addNS( sal_Int32 nNamespaceToken
, sal_Int32 nToken
, std::string_view sValue
);
100 void addNS( sal_Int32 nNamespaceToken
, sal_Int32 nToken
, std::u16string_view sValue
);
101 // note: rQName is *namespace-prefixed*
102 void addUnknown( const OUString
& rNamespaceURL
, const OString
& rQName
, const OString
& value
);
103 void addUnknown( const OString
& rName
, const OString
& value
);
104 const std::vector
< sal_Int32
>& getFastAttributeTokens() const { return maAttributeTokens
; }
105 const char* getFastAttributeValue(size_t nIndex
) const { return mpChunk
+ maAttributeValues
[nIndex
]; }
106 sal_Int32
AttributeValueLength(size_t i
) const { return maAttributeValues
[i
+ 1] - maAttributeValues
[i
] - 1; }
107 size_t size() const { return maAttributeValues
.size(); }
109 // performance sensitive shortcuts to avoid allocation ...
110 bool getAsInteger( sal_Int32 nToken
, sal_Int32
&rInt
) const;
111 bool getAsDouble( sal_Int32 nToken
, double &rDouble
) const;
112 bool getAsView( sal_Int32 nToken
, std::string_view
& rPos
) const;
113 sal_Int32
getAsIntegerByIndex( sal_Int32 nTokenIndex
) const
115 return o3tl::toInt32(getAsViewByIndex(nTokenIndex
));
117 std::string_view
getAsViewByIndex( sal_Int32 nTokenIndex
) const
119 return std::string_view(getFastAttributeValue(nTokenIndex
), AttributeValueLength(nTokenIndex
));
121 OUString
getValueByIndex( sal_Int32 nTokenIndex
) const
123 return OStringToOUString(getAsViewByIndex(nTokenIndex
), RTL_TEXTENCODING_UTF8
);
126 // XFastAttributeList
127 virtual sal_Bool SAL_CALL
hasAttribute( ::sal_Int32 Token
) override
;
128 virtual ::sal_Int32 SAL_CALL
getValueToken( ::sal_Int32 Token
) override
;
129 virtual ::sal_Int32 SAL_CALL
getOptionalValueToken( ::sal_Int32 Token
, ::sal_Int32 Default
) override
;
130 virtual OUString SAL_CALL
getValue( ::sal_Int32 Token
) override
;
131 virtual OUString SAL_CALL
getOptionalValue( ::sal_Int32 Token
) override
;
132 virtual css::uno::Sequence
< css::xml::Attribute
> SAL_CALL
getUnknownAttributes( ) override
;
133 virtual css::uno::Sequence
< css::xml::FastAttribute
> SAL_CALL
getFastAttributes() override
;
136 virtual ::css::uno::Reference
< ::css::util::XCloneable
> SAL_CALL
createClone() override
;
138 sal_Int32
getAttributeIndex( ::sal_Int32 Token
)
140 for (size_t i
=0; i
<maAttributeTokens
.size(); ++i
)
141 if (maAttributeTokens
[i
] == Token
)
146 /// Use for fast iteration and conversion of attributes
147 class FastAttributeIter
{
148 const FastAttributeList
&mrList
;
152 FastAttributeIter(const FastAttributeList
&rList
, size_t nIdx
)
153 : mrList(rList
), mnIdx(nIdx
)
157 FastAttributeIter
& operator++ ()
162 bool operator!=( const FastAttributeIter
& rhs
) const
164 return mnIdx
!= rhs
.mnIdx
;
167 const FastAttributeIter
& operator*() const
172 sal_Int32
getToken() const
174 assert(mnIdx
< mrList
.maAttributeTokens
.size());
175 return mrList
.maAttributeTokens
[mnIdx
];
179 assert(mnIdx
< mrList
.maAttributeTokens
.size());
180 return mrList
.AttributeValueLength(mnIdx
) < 1;
182 sal_Int32
toInt32() const
184 assert(mnIdx
< mrList
.maAttributeTokens
.size());
185 return mrList
.getAsIntegerByIndex(mnIdx
);
187 double toDouble() const
189 assert(mnIdx
< mrList
.maAttributeTokens
.size());
190 return o3tl::toDouble(mrList
.getAsViewByIndex(mnIdx
));
192 bool toBoolean() const
194 assert(mnIdx
< mrList
.maAttributeTokens
.size());
195 return rtl_str_toBoolean(mrList
.getFastAttributeValue(mnIdx
));
197 OUString
toString() const
199 assert(mnIdx
< mrList
.maAttributeTokens
.size());
200 return mrList
.getValueByIndex(mnIdx
);
202 const char* toCString() const
204 assert(mnIdx
< mrList
.maAttributeTokens
.size());
205 return mrList
.getFastAttributeValue(mnIdx
);
207 sal_Int32
getLength() const
209 assert(mnIdx
< mrList
.maAttributeTokens
.size());
210 return mrList
.AttributeValueLength(mnIdx
);
212 std::string_view
toView() const
214 assert(mnIdx
< mrList
.maAttributeTokens
.size());
215 return mrList
.getAsViewByIndex(mnIdx
);
217 bool isString(const char *str
) const
219 assert(mnIdx
< mrList
.maAttributeTokens
.size());
220 return !strcmp(str
, mrList
.getFastAttributeValue(mnIdx
));
223 FastAttributeIter
begin() const { return FastAttributeIter(*this, 0); }
224 FastAttributeIter
end() const { return FastAttributeIter(*this, maAttributeTokens
.size()); }
225 FastAttributeIter
find( sal_Int32 nToken
) const;
228 char *mpChunk
; ///< buffer to store all attribute values - null terminated strings
229 sal_Int32 mnChunkLength
; ///< size of allocated memory for mpChunk
230 // maAttributeValues stores pointers, relative to mpChunk, for each attribute value string
231 // length of the string is maAttributeValues[n+1] - maAttributeValues[n] - 1
232 // maAttributeValues[0] == 0
233 std::vector
< sal_Int32
> maAttributeValues
;
234 std::vector
< sal_Int32
> maAttributeTokens
;
235 std::vector
< UnknownAttribute
> maUnknownAttributes
;
236 FastTokenHandlerBase
* mpTokenHandler
;
239 inline FastAttributeList
& castToFastAttributeList(
240 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& xAttrList
)
242 assert( dynamic_cast <FastAttributeList
*> ( xAttrList
.get() ) != nullptr );
243 return *static_cast <FastAttributeList
*> ( xAttrList
.get() );
250 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */