Bump version to 6.4-15
[LibreOffice.git] / include / sax / fastattribs.hxx
blob7484a249172518058bbf1d011a5b1229a61722d0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
25 #include <cppuhelper/implbase.hxx>
26 #include <sax/saxdllapi.h>
28 #include <vector>
30 namespace com { namespace sun { namespace star { namespace xml { namespace sax { class XFastTokenHandler; } } } } }
31 namespace com { namespace sun { namespace star { namespace xml { struct Attribute; } } } }
32 namespace com { namespace sun { namespace star { namespace xml { struct FastAttribute; } } } }
34 namespace sax_fastparser
37 struct UnknownAttribute
39 OUString const maNamespaceURL;
40 OString const maName;
41 OString const maValue;
43 UnknownAttribute( const OUString& rNamespaceURL, const OString& rName, const OString& value );
44 UnknownAttribute( const OString& rName, const OString& value );
46 void FillAttribute( css::xml::Attribute* pAttrib ) const;
49 typedef std::vector< UnknownAttribute > UnknownAttributeList;
51 /// A native C++ interface to tokenisation
52 class SAX_DLLPUBLIC FastTokenHandlerBase
54 public:
55 virtual ~FastTokenHandlerBase();
56 virtual sal_Int32 getTokenDirect( const char *pToken, sal_Int32 nLength ) const = 0;
58 /**
59 * Client method to attempt the use of this interface if possible.
60 * @xTokenHandler - the UNO handle for the token lookup interface
61 * @pTokenHandler - a dynamic_cast version of @xTokenHandler to this interface
62 * @pStr - string buffer to lookup
63 * @nLength - optional length of chars in that buffer
65 * @return Tokenized form of pStr
67 static sal_Int32 getTokenFromChars(
68 const css::uno::Reference<css::xml::sax::XFastTokenHandler > &xTokenHandler,
69 const FastTokenHandlerBase *pTokenHandler /* can be NULL */,
70 const char *pStr, size_t nLength );
73 class SAX_DLLPUBLIC FastAttributeList final : public cppu::WeakImplHelper< css::xml::sax::XFastAttributeList >
75 public:
76 FastAttributeList( const css::uno::Reference< css::xml::sax::XFastTokenHandler >& xTokenHandler,
77 FastTokenHandlerBase *pOptHandlerBase = nullptr );
78 virtual ~FastAttributeList() override;
80 void clear();
81 void reserve( sal_Int32 nNumTokens )
83 maAttributeValues.reserve(nNumTokens+1);
84 maAttributeTokens.reserve(nNumTokens);
86 void add( sal_Int32 nToken, const sal_Char* pValue );
87 void add( sal_Int32 nToken, const sal_Char* pValue, size_t nValueLength );
88 void add( sal_Int32 nToken, const OString& rValue );
89 void addNS( sal_Int32 nNamespaceToken, sal_Int32 nToken, const OString& rValue );
90 // note: rQName is *namespace-prefixed*
91 void addUnknown( const OUString& rNamespaceURL, const OString& rQName, const OString& value );
92 void addUnknown( const OString& rName, const OString& value );
93 const std::vector< sal_Int32 >& getFastAttributeTokens() const { return maAttributeTokens; }
94 const char* getFastAttributeValue(size_t nIndex) const { return mpChunk + maAttributeValues[nIndex]; }
95 sal_Int32 AttributeValueLength(size_t i) const { return maAttributeValues[i + 1] - maAttributeValues[i] - 1; }
97 // performance sensitive shortcuts to avoid allocation ...
98 bool getAsInteger( sal_Int32 nToken, sal_Int32 &rInt) const;
99 bool getAsDouble( sal_Int32 nToken, double &rDouble) const;
100 bool getAsChar( sal_Int32 nToken, const char*& rPos ) const;
101 sal_Int32 getAsIntegerByIndex( sal_Int32 nTokenIndex ) const;
102 const char* getAsCharByIndex( sal_Int32 nTokenIndex ) const;
103 OUString getValueByIndex( sal_Int32 nTokenIndex ) const;
105 // XFastAttributeList
106 virtual sal_Bool SAL_CALL hasAttribute( ::sal_Int32 Token ) override;
107 virtual ::sal_Int32 SAL_CALL getValueToken( ::sal_Int32 Token ) override;
108 virtual ::sal_Int32 SAL_CALL getOptionalValueToken( ::sal_Int32 Token, ::sal_Int32 Default ) override;
109 virtual OUString SAL_CALL getValue( ::sal_Int32 Token ) override;
110 virtual OUString SAL_CALL getOptionalValue( ::sal_Int32 Token ) override;
111 virtual css::uno::Sequence< css::xml::Attribute > SAL_CALL getUnknownAttributes( ) override;
112 virtual css::uno::Sequence< css::xml::FastAttribute > SAL_CALL getFastAttributes() override;
114 sal_Int32 getAttributeIndex( ::sal_Int32 Token )
116 for (size_t i=0; i<maAttributeTokens.size(); ++i)
117 if (maAttributeTokens[i] == Token)
118 return i;
119 return -1;
122 static FastAttributeList* castToFastAttributeList(
123 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
125 assert( dynamic_cast <FastAttributeList *> ( xAttrList.get() ) != nullptr );
126 return static_cast <FastAttributeList *> ( xAttrList.get() );
129 /// Use for fast iteration and conversion of attributes
130 class FastAttributeIter {
131 const FastAttributeList &mrList;
132 size_t mnIdx;
134 public:
135 FastAttributeIter(const FastAttributeList &rList, size_t nIdx)
136 : mrList(rList), mnIdx(nIdx)
140 FastAttributeIter& operator++ ()
142 mnIdx++;
143 return *this;
145 bool operator!=( const FastAttributeIter& rhs ) const
147 return mnIdx != rhs.mnIdx;
150 const FastAttributeIter& operator*() const
152 return *this;
155 sal_Int32 getToken() const
157 assert(mnIdx < mrList.maAttributeTokens.size());
158 return mrList.maAttributeTokens[mnIdx];
160 bool isEmpty() const
162 assert(mnIdx < mrList.maAttributeTokens.size());
163 return mrList.AttributeValueLength(mnIdx) < 1;
165 sal_Int32 toInt32() const
167 assert(mnIdx < mrList.maAttributeTokens.size());
168 return rtl_str_toInt32(mrList.getFastAttributeValue(mnIdx), 10);
170 double toDouble() const
172 assert(mnIdx < mrList.maAttributeTokens.size());
173 return rtl_str_toDouble(mrList.getFastAttributeValue(mnIdx));
175 OUString toString() const
177 assert(mnIdx < mrList.maAttributeTokens.size());
178 return OUString(mrList.getFastAttributeValue(mnIdx),
179 mrList.AttributeValueLength(mnIdx),
180 RTL_TEXTENCODING_UTF8);
182 const char* toCString() const
184 assert(mnIdx < mrList.maAttributeTokens.size());
185 return mrList.getFastAttributeValue(mnIdx);
187 sal_Int32 getLength() const
189 assert(mnIdx < mrList.maAttributeTokens.size());
190 return mrList.AttributeValueLength(mnIdx);
192 bool isString(const char *str) const
194 assert(mnIdx < mrList.maAttributeTokens.size());
195 return !strcmp(str, mrList.getFastAttributeValue(mnIdx));
198 FastAttributeIter begin() const { return FastAttributeIter(*this, 0); }
199 FastAttributeIter end() const { return FastAttributeIter(*this, maAttributeTokens.size()); }
200 FastAttributeIter find( sal_Int32 nToken ) const;
202 private:
203 sal_Char *mpChunk; ///< buffer to store all attribute values - null terminated strings
204 sal_Int32 mnChunkLength; ///< size of allocated memory for mpChunk
205 // maAttributeValues stores pointers, relative to mpChunk, for each attribute value string
206 // length of the string is maAttributeValues[n+1] - maAttributeValues[n] - 1
207 // maAttributeValues[0] == 0
208 std::vector< sal_Int32 > maAttributeValues;
209 std::vector< sal_Int32 > maAttributeTokens;
210 UnknownAttributeList maUnknownAttributes;
211 css::uno::Reference< css::xml::sax::XFastTokenHandler > mxTokenHandler;
212 FastTokenHandlerBase * const mpTokenHandler;
217 #endif
219 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */