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 : #include <algorithm>
21 :
22 : #include <sax/fastattribs.hxx>
23 :
24 : using ::rtl::OUString;
25 : using ::rtl::OString;
26 : using namespace ::com::sun::star::uno;
27 : using namespace ::com::sun::star::xml;
28 : using namespace ::com::sun::star::xml::sax;
29 : namespace sax_fastparser
30 : {
31 :
32 358 : UnknownAttribute::UnknownAttribute( const OUString& rNamespaceURL, const OString& rName, const OString& rValue )
33 358 : : maNamespaceURL( rNamespaceURL ), maName( rName ), maValue( rValue )
34 : {
35 358 : }
36 :
37 0 : UnknownAttribute::UnknownAttribute( const OString& rName, const OString& rValue )
38 0 : : maName( rName ), maValue( rValue )
39 : {
40 0 : }
41 :
42 0 : void UnknownAttribute::FillAttribute( Attribute* pAttrib ) const
43 : {
44 0 : if( pAttrib )
45 : {
46 0 : pAttrib->Name = OStringToOUString( maName, RTL_TEXTENCODING_UTF8 );
47 0 : pAttrib->NamespaceURL = maNamespaceURL;
48 0 : pAttrib->Value = OStringToOUString( maValue, RTL_TEXTENCODING_UTF8 );
49 : }
50 0 : }
51 :
52 6481 : FastAttributeList::FastAttributeList( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastTokenHandler >& xTokenHandler )
53 6481 : : mxTokenHandler( xTokenHandler )
54 : {
55 6481 : maLastIter = maAttributes.end();
56 6481 : }
57 :
58 12962 : FastAttributeList::~FastAttributeList()
59 : {
60 12962 : }
61 :
62 41774 : void FastAttributeList::clear()
63 : {
64 41774 : maAttributes.clear();
65 41774 : maUnknownAttributes.clear();
66 41774 : maLastIter = maAttributes.end();
67 41774 : }
68 :
69 55039 : void FastAttributeList::add( sal_Int32 nToken, const OString& rValue )
70 : {
71 55039 : maAttributes[nToken] = rValue;
72 55039 : }
73 :
74 358 : void FastAttributeList::addUnknown( const OUString& rNamespaceURL, const OString& rName, const OString& rValue )
75 : {
76 358 : maUnknownAttributes.push_back( UnknownAttribute( rNamespaceURL, rName, rValue ) );
77 358 : }
78 :
79 0 : void FastAttributeList::addUnknown( const OString& rName, const OString& rValue )
80 : {
81 0 : maUnknownAttributes.push_back( UnknownAttribute( rName, rValue ) );
82 0 : }
83 :
84 : // XFastAttributeList
85 52827 : sal_Bool FastAttributeList::hasAttribute( ::sal_Int32 Token ) throw (RuntimeException)
86 : {
87 52827 : maLastIter = maAttributes.find( Token );
88 52827 : return ( maLastIter != maAttributes.end() ) ? sal_True : sal_False;
89 : }
90 :
91 0 : sal_Int32 FastAttributeList::getValueToken( ::sal_Int32 Token ) throw (SAXException, RuntimeException)
92 : {
93 0 : if( ( maLastIter == maAttributes.end() ) || ( ( *maLastIter ).first != Token ) )
94 0 : maLastIter = maAttributes.find( Token );
95 :
96 0 : if( maLastIter == maAttributes.end() )
97 0 : throw SAXException();
98 :
99 0 : Sequence< sal_Int8 > aSeq( (sal_Int8*)(*maLastIter).second.getStr(), (*maLastIter).second.getLength() ) ;
100 0 : return mxTokenHandler->getTokenFromUTF8( aSeq );
101 : }
102 :
103 10914 : sal_Int32 FastAttributeList::getOptionalValueToken( ::sal_Int32 Token, ::sal_Int32 Default ) throw (RuntimeException)
104 : {
105 10914 : if( ( maLastIter == maAttributes.end() ) || ( ( *maLastIter ).first != Token ) )
106 10461 : maLastIter = maAttributes.find( Token );
107 :
108 10914 : if( maLastIter == maAttributes.end() )
109 7275 : return Default;
110 :
111 3639 : Sequence< sal_Int8 > aSeq( (sal_Int8*)(*maLastIter).second.getStr(), (*maLastIter).second.getLength() ) ;
112 3639 : return mxTokenHandler->getTokenFromUTF8( aSeq );
113 : }
114 :
115 32578 : OUString FastAttributeList::getValue( ::sal_Int32 Token ) throw (SAXException, RuntimeException)
116 : {
117 32578 : if( ( maLastIter == maAttributes.end() ) || ( ( *maLastIter ).first != Token ) )
118 5862 : maLastIter = maAttributes.find( Token );
119 :
120 32578 : if( maLastIter == maAttributes.end() )
121 20 : throw SAXException();
122 :
123 32558 : return OStringToOUString( (*maLastIter).second, RTL_TEXTENCODING_UTF8 );
124 : }
125 :
126 15806 : OUString FastAttributeList::getOptionalValue( ::sal_Int32 Token ) throw (RuntimeException)
127 : {
128 15806 : if( ( maLastIter == maAttributes.end() ) || ( ( *maLastIter ).first != Token ) )
129 14417 : maLastIter = maAttributes.find( Token );
130 :
131 15806 : OUString aRet;
132 15806 : if( maLastIter != maAttributes.end() )
133 9426 : aRet = OStringToOUString( (*maLastIter).second, RTL_TEXTENCODING_UTF8 );
134 :
135 15806 : return aRet;
136 : }
137 5473 : Sequence< Attribute > FastAttributeList::getUnknownAttributes( ) throw (RuntimeException)
138 : {
139 5473 : Sequence< Attribute > aSeq( maUnknownAttributes.size() );
140 5473 : Attribute* pAttr = aSeq.getArray();
141 5473 : for( UnknownAttributeList::iterator attrIter = maUnknownAttributes.begin(); attrIter != maUnknownAttributes.end(); ++attrIter )
142 0 : (*attrIter).FillAttribute( pAttr++ );
143 5473 : return aSeq;
144 : }
145 7826 : Sequence< FastAttribute > FastAttributeList::getFastAttributes( ) throw (RuntimeException)
146 : {
147 7826 : Sequence< FastAttribute > aSeq( maAttributes.size() );
148 7826 : FastAttribute* pAttr = aSeq.getArray();
149 7826 : FastAttributeMap::iterator fastAttrIter = maAttributes.begin();
150 14706 : for(; fastAttrIter != maAttributes.end(); ++fastAttrIter )
151 : {
152 6880 : pAttr->Token = fastAttrIter->first;
153 6880 : pAttr->Value = OStringToOUString( fastAttrIter->second, RTL_TEXTENCODING_UTF8 );
154 6880 : pAttr++;
155 : }
156 7826 : return aSeq;
157 : }
158 :
159 : }
160 :
161 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|