xref: /aoo41x/main/sax/source/tools/fastattribs.cxx (revision f9b72d11)
1*f9b72d11SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*f9b72d11SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*f9b72d11SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*f9b72d11SAndrew Rist  * distributed with this work for additional information
6*f9b72d11SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*f9b72d11SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*f9b72d11SAndrew Rist  * "License"); you may not use this file except in compliance
9*f9b72d11SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*f9b72d11SAndrew Rist  *
11*f9b72d11SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*f9b72d11SAndrew Rist  *
13*f9b72d11SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*f9b72d11SAndrew Rist  * software distributed under the License is distributed on an
15*f9b72d11SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*f9b72d11SAndrew Rist  * KIND, either express or implied.  See the License for the
17*f9b72d11SAndrew Rist  * specific language governing permissions and limitations
18*f9b72d11SAndrew Rist  * under the License.
19*f9b72d11SAndrew Rist  *
20*f9b72d11SAndrew Rist  *************************************************************/
21*f9b72d11SAndrew Rist 
22*f9b72d11SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include <algorithm>
25cdf0e10cSrcweir #include <boost/bind.hpp>
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <sax/fastattribs.hxx>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir using ::rtl::OUString;
30cdf0e10cSrcweir using ::rtl::OString;
31cdf0e10cSrcweir using namespace ::com::sun::star::uno;
32cdf0e10cSrcweir using namespace ::com::sun::star::xml;
33cdf0e10cSrcweir using namespace ::com::sun::star::xml::sax;
34cdf0e10cSrcweir namespace sax_fastparser
35cdf0e10cSrcweir {
36cdf0e10cSrcweir 
UnknownAttribute(const OUString & rNamespaceURL,const OString & rName,const OString & rValue)37cdf0e10cSrcweir UnknownAttribute::UnknownAttribute( const OUString& rNamespaceURL, const OString& rName, const OString& rValue )
38cdf0e10cSrcweir 	: maNamespaceURL( rNamespaceURL ), maName( rName ), maValue( rValue )
39cdf0e10cSrcweir {
40cdf0e10cSrcweir }
41cdf0e10cSrcweir 
UnknownAttribute(const OString & rName,const OString & rValue)42cdf0e10cSrcweir UnknownAttribute::UnknownAttribute( const OString& rName, const OString& rValue )
43cdf0e10cSrcweir 	: maName( rName ), maValue( rValue )
44cdf0e10cSrcweir {
45cdf0e10cSrcweir }
46cdf0e10cSrcweir 
FillAttribute(Attribute * pAttrib) const47cdf0e10cSrcweir void UnknownAttribute::FillAttribute( Attribute* pAttrib ) const
48cdf0e10cSrcweir {
49cdf0e10cSrcweir 	if( pAttrib )
50cdf0e10cSrcweir 	{
51cdf0e10cSrcweir 		pAttrib->Name = OStringToOUString( maName, RTL_TEXTENCODING_UTF8 );
52cdf0e10cSrcweir 		pAttrib->NamespaceURL = maNamespaceURL;
53cdf0e10cSrcweir 		pAttrib->Value = OStringToOUString( maValue, RTL_TEXTENCODING_UTF8 );
54cdf0e10cSrcweir 	}
55cdf0e10cSrcweir }
56cdf0e10cSrcweir 
FastAttributeList(const::com::sun::star::uno::Reference<::com::sun::star::xml::sax::XFastTokenHandler> & xTokenHandler)57cdf0e10cSrcweir FastAttributeList::FastAttributeList( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastTokenHandler >& xTokenHandler )
58cdf0e10cSrcweir : mxTokenHandler( xTokenHandler )
59cdf0e10cSrcweir {
60cdf0e10cSrcweir 	maLastIter = maAttributes.end();
61cdf0e10cSrcweir }
62cdf0e10cSrcweir 
~FastAttributeList()63cdf0e10cSrcweir FastAttributeList::~FastAttributeList()
64cdf0e10cSrcweir {
65cdf0e10cSrcweir }
66cdf0e10cSrcweir 
clear()67cdf0e10cSrcweir void FastAttributeList::clear()
68cdf0e10cSrcweir {
69cdf0e10cSrcweir 	maAttributes.clear();
70cdf0e10cSrcweir 	maUnknownAttributes.clear();
71cdf0e10cSrcweir 	maLastIter = maAttributes.end();
72cdf0e10cSrcweir }
73cdf0e10cSrcweir 
add(sal_Int32 nToken,const OString & rValue)74cdf0e10cSrcweir void FastAttributeList::add( sal_Int32 nToken, const OString& rValue )
75cdf0e10cSrcweir {
76cdf0e10cSrcweir 	maAttributes[nToken] = rValue;
77cdf0e10cSrcweir }
78cdf0e10cSrcweir 
addUnknown(const OUString & rNamespaceURL,const OString & rName,const OString & rValue)79cdf0e10cSrcweir void FastAttributeList::addUnknown( const OUString& rNamespaceURL, const OString& rName, const OString& rValue )
80cdf0e10cSrcweir {
81cdf0e10cSrcweir 	maUnknownAttributes.push_back( UnknownAttribute( rNamespaceURL, rName, rValue ) );
82cdf0e10cSrcweir }
83cdf0e10cSrcweir 
addUnknown(const OString & rName,const OString & rValue)84cdf0e10cSrcweir void FastAttributeList::addUnknown( const OString& rName, const OString& rValue )
85cdf0e10cSrcweir {
86cdf0e10cSrcweir 	maUnknownAttributes.push_back( UnknownAttribute( rName, rValue ) );
87cdf0e10cSrcweir }
88cdf0e10cSrcweir 
89cdf0e10cSrcweir // XFastAttributeList
hasAttribute(::sal_Int32 Token)90cdf0e10cSrcweir sal_Bool FastAttributeList::hasAttribute( ::sal_Int32 Token ) throw (RuntimeException)
91cdf0e10cSrcweir {
92cdf0e10cSrcweir 	maLastIter = maAttributes.find( Token );
93cdf0e10cSrcweir 	return ( maLastIter != maAttributes.end() ) ? sal_True : sal_False;
94cdf0e10cSrcweir }
95cdf0e10cSrcweir 
getValueToken(::sal_Int32 Token)96cdf0e10cSrcweir sal_Int32 FastAttributeList::getValueToken( ::sal_Int32 Token ) throw (SAXException, RuntimeException)
97cdf0e10cSrcweir {
98cdf0e10cSrcweir 	if( ( maLastIter == maAttributes.end() ) || ( ( *maLastIter ).first != Token ) )
99cdf0e10cSrcweir 		maLastIter = maAttributes.find( Token );
100cdf0e10cSrcweir 
101cdf0e10cSrcweir 	if( maLastIter == maAttributes.end() )
102cdf0e10cSrcweir 		throw SAXException();
103cdf0e10cSrcweir 
104cdf0e10cSrcweir 	Sequence< sal_Int8 > aSeq( (sal_Int8*)(*maLastIter).second.getStr(), (*maLastIter).second.getLength() ) ;
105cdf0e10cSrcweir 	return mxTokenHandler->getTokenFromUTF8( aSeq );
106cdf0e10cSrcweir }
107cdf0e10cSrcweir 
getOptionalValueToken(::sal_Int32 Token,::sal_Int32 Default)108cdf0e10cSrcweir sal_Int32 FastAttributeList::getOptionalValueToken( ::sal_Int32 Token, ::sal_Int32 Default ) throw (RuntimeException)
109cdf0e10cSrcweir {
110cdf0e10cSrcweir 	if( ( maLastIter == maAttributes.end() ) || ( ( *maLastIter ).first != Token ) )
111cdf0e10cSrcweir 		maLastIter = maAttributes.find( Token );
112cdf0e10cSrcweir 
113cdf0e10cSrcweir 	if( maLastIter == maAttributes.end() )
114cdf0e10cSrcweir 		return Default;
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 	Sequence< sal_Int8 > aSeq( (sal_Int8*)(*maLastIter).second.getStr(), (*maLastIter).second.getLength() ) ;
117cdf0e10cSrcweir 	return mxTokenHandler->getTokenFromUTF8( aSeq );
118cdf0e10cSrcweir }
119cdf0e10cSrcweir 
getValue(::sal_Int32 Token)120cdf0e10cSrcweir OUString FastAttributeList::getValue( ::sal_Int32 Token ) throw (SAXException, RuntimeException)
121cdf0e10cSrcweir {
122cdf0e10cSrcweir 	if( ( maLastIter == maAttributes.end() ) || ( ( *maLastIter ).first != Token ) )
123cdf0e10cSrcweir 		maLastIter = maAttributes.find( Token );
124cdf0e10cSrcweir 
125cdf0e10cSrcweir 	if( maLastIter == maAttributes.end() )
126cdf0e10cSrcweir 		throw SAXException();
127cdf0e10cSrcweir 
128cdf0e10cSrcweir 	return OStringToOUString( (*maLastIter).second, RTL_TEXTENCODING_UTF8 );
129cdf0e10cSrcweir }
130cdf0e10cSrcweir 
getOptionalValue(::sal_Int32 Token)131cdf0e10cSrcweir OUString FastAttributeList::getOptionalValue( ::sal_Int32 Token ) throw (RuntimeException)
132cdf0e10cSrcweir {
133cdf0e10cSrcweir 	if( ( maLastIter == maAttributes.end() ) || ( ( *maLastIter ).first != Token ) )
134cdf0e10cSrcweir 		maLastIter = maAttributes.find( Token );
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 	OUString aRet;
137cdf0e10cSrcweir 	if( maLastIter != maAttributes.end() )
138cdf0e10cSrcweir 		aRet = OStringToOUString( (*maLastIter).second, RTL_TEXTENCODING_UTF8 );
139cdf0e10cSrcweir 
140cdf0e10cSrcweir 	return aRet;
141cdf0e10cSrcweir }
getUnknownAttributes()142cdf0e10cSrcweir Sequence< Attribute > FastAttributeList::getUnknownAttributes(  ) throw (RuntimeException)
143cdf0e10cSrcweir {
144cdf0e10cSrcweir 	Sequence< Attribute > aSeq( maUnknownAttributes.size() );
145cdf0e10cSrcweir 	Attribute* pAttr = aSeq.getArray();
146cdf0e10cSrcweir     for( UnknownAttributeList::iterator attrIter = maUnknownAttributes.begin(); attrIter != maUnknownAttributes.end(); attrIter++ )
147cdf0e10cSrcweir         (*attrIter).FillAttribute( pAttr++ );
148cdf0e10cSrcweir 	return aSeq;
149cdf0e10cSrcweir }
getFastAttributes()150cdf0e10cSrcweir Sequence< FastAttribute > FastAttributeList::getFastAttributes(  ) throw (RuntimeException)
151cdf0e10cSrcweir {
152cdf0e10cSrcweir 	Sequence< FastAttribute > aSeq( maAttributes.size() );
153cdf0e10cSrcweir 	FastAttribute* pAttr = aSeq.getArray();
154cdf0e10cSrcweir 	FastAttributeMap::iterator fastAttrIter = maAttributes.begin();
155cdf0e10cSrcweir 	for(; fastAttrIter != maAttributes.end(); fastAttrIter++ )
156cdf0e10cSrcweir 	{
157cdf0e10cSrcweir 		pAttr->Token = fastAttrIter->first;
158cdf0e10cSrcweir 		pAttr->Value = OStringToOUString( fastAttrIter->second, RTL_TEXTENCODING_UTF8 );
159cdf0e10cSrcweir 		pAttr++;
160cdf0e10cSrcweir 	}
161cdf0e10cSrcweir 	return aSeq;
162cdf0e10cSrcweir }
163cdf0e10cSrcweir 
164cdf0e10cSrcweir }
165