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 : #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/xml/Attribute.hpp>
26 : #include <com/sun/star/xml/FastAttribute.hpp>
27 :
28 : #include <cppuhelper/implbase1.hxx>
29 : #include <sax/saxdllapi.h>
30 :
31 : #include <map>
32 : #include <vector>
33 :
34 : namespace sax_fastparser
35 : {
36 :
37 0 : struct UnknownAttribute
38 : {
39 : OUString maNamespaceURL;
40 : OString maName;
41 : OString maValue;
42 :
43 : UnknownAttribute( const OUString& rNamespaceURL, const OString& rName, const sal_Char* pValue );
44 :
45 : UnknownAttribute( const OString& rName, const sal_Char* pValue );
46 :
47 : void FillAttribute( ::com::sun::star::xml::Attribute* pAttrib ) const;
48 : };
49 :
50 : typedef std::vector< UnknownAttribute > UnknownAttributeList;
51 :
52 : /// A native C++ interface to tokenisation
53 0 : class SAX_DLLPUBLIC FastTokenHandlerBase
54 : {
55 : public:
56 : virtual ~FastTokenHandlerBase();
57 : virtual sal_Int32 getTokenDirect( const char *pToken, sal_Int32 nLength ) const = 0;
58 : };
59 :
60 : /// avoid constantly allocating and freeing sequences.
61 0 : class SAX_DLLPUBLIC FastTokenLookup
62 : {
63 : static const int mnUtf8BufferSize = 128;
64 : ::css::uno::Sequence< sal_Int8 > maUtf8Buffer;
65 : public:
66 : FastTokenLookup();
67 : sal_Int32 getTokenFromChars(
68 : const ::css::uno::Reference< ::css::xml::sax::XFastTokenHandler > &mxTokenHandler,
69 : FastTokenHandlerBase *pTokenHandler,
70 : const char *pStr, size_t nLength = 0 );
71 : };
72 :
73 : class SAX_DLLPUBLIC FastAttributeList : public ::cppu::WeakImplHelper1< ::com::sun::star::xml::sax::XFastAttributeList >
74 : {
75 : public:
76 : FastAttributeList( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastTokenHandler >& xTokenHandler,
77 : FastTokenHandlerBase *pOptHandlerBase = NULL );
78 : virtual ~FastAttributeList();
79 :
80 : void clear();
81 : void add( sal_Int32 nToken, const sal_Char* pValue, size_t nValueLength = 0 );
82 : void add( sal_Int32 nToken, const OString& rValue );
83 : void addNS( sal_Int32 nNamespaceToken, sal_Int32 nToken, const OString& rValue );
84 : void addUnknown( const OUString& rNamespaceURL, const OString& rName, const sal_Char* pValue );
85 : void addUnknown( const OString& rName, const sal_Char* pValue );
86 :
87 : // performance sensitive shortcuts to avoid allocation ...
88 : bool getAsInteger( sal_Int32 nToken, sal_Int32 &rInt);
89 : bool getAsDouble( sal_Int32 nToken, double &rDouble);
90 : bool getAsChar( sal_Int32 nToken, const char*& rPos ) const;
91 :
92 : // XFastAttributeList
93 : virtual sal_Bool SAL_CALL hasAttribute( ::sal_Int32 Token ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
94 : virtual ::sal_Int32 SAL_CALL getValueToken( ::sal_Int32 Token ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
95 : virtual ::sal_Int32 SAL_CALL getOptionalValueToken( ::sal_Int32 Token, ::sal_Int32 Default ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
96 : virtual OUString SAL_CALL getValue( ::sal_Int32 Token ) throw (::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
97 : virtual OUString SAL_CALL getOptionalValue( ::sal_Int32 Token ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
98 : virtual ::com::sun::star::uno::Sequence< ::com::sun::star::xml::Attribute > SAL_CALL getUnknownAttributes( ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
99 : virtual ::com::sun::star::uno::Sequence< ::com::sun::star::xml::FastAttribute > SAL_CALL getFastAttributes() throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
100 :
101 : private:
102 : inline sal_Int32 AttributeValueLength(sal_Int32 i);
103 :
104 : private:
105 : sal_Char *mpChunk; ///< buffer to store all attribute values - null terminated strings
106 : sal_Int32 mnChunkLength; ///< size of allocated memory for mpChunk
107 : // maAttributeValues stores pointers, relative to mpChunk, for each attribute value string
108 : // length of the string is maAttributeValues[n+1] - maAttributeValues[n] - 1
109 : // maAttributeValues[0] == 0
110 : std::vector< sal_Int32 > maAttributeValues;
111 : std::vector< sal_Int32 > maAttributeTokens;
112 : UnknownAttributeList maUnknownAttributes;
113 : ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastTokenHandler > mxTokenHandler;
114 : FastTokenHandlerBase *mpTokenHandler;
115 :
116 : FastTokenLookup maTokenLookup;
117 : };
118 :
119 : }
120 :
121 : #endif
122 :
123 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|