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