xref: /aoo41x/main/sax/source/tools/fastattribs.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #include <algorithm>
29*cdf0e10cSrcweir #include <boost/bind.hpp>
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <sax/fastattribs.hxx>
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir using ::rtl::OUString;
34*cdf0e10cSrcweir using ::rtl::OString;
35*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
36*cdf0e10cSrcweir using namespace ::com::sun::star::xml;
37*cdf0e10cSrcweir using namespace ::com::sun::star::xml::sax;
38*cdf0e10cSrcweir namespace sax_fastparser
39*cdf0e10cSrcweir {
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir UnknownAttribute::UnknownAttribute( const OUString& rNamespaceURL, const OString& rName, const OString& rValue )
42*cdf0e10cSrcweir 	: maNamespaceURL( rNamespaceURL ), maName( rName ), maValue( rValue )
43*cdf0e10cSrcweir {
44*cdf0e10cSrcweir }
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir UnknownAttribute::UnknownAttribute( const OString& rName, const OString& rValue )
47*cdf0e10cSrcweir 	: maName( rName ), maValue( rValue )
48*cdf0e10cSrcweir {
49*cdf0e10cSrcweir }
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir void UnknownAttribute::FillAttribute( Attribute* pAttrib ) const
52*cdf0e10cSrcweir {
53*cdf0e10cSrcweir 	if( pAttrib )
54*cdf0e10cSrcweir 	{
55*cdf0e10cSrcweir 		pAttrib->Name = OStringToOUString( maName, RTL_TEXTENCODING_UTF8 );
56*cdf0e10cSrcweir 		pAttrib->NamespaceURL = maNamespaceURL;
57*cdf0e10cSrcweir 		pAttrib->Value = OStringToOUString( maValue, RTL_TEXTENCODING_UTF8 );
58*cdf0e10cSrcweir 	}
59*cdf0e10cSrcweir }
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir FastAttributeList::FastAttributeList( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XFastTokenHandler >& xTokenHandler )
62*cdf0e10cSrcweir : mxTokenHandler( xTokenHandler )
63*cdf0e10cSrcweir {
64*cdf0e10cSrcweir 	maLastIter = maAttributes.end();
65*cdf0e10cSrcweir }
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir FastAttributeList::~FastAttributeList()
68*cdf0e10cSrcweir {
69*cdf0e10cSrcweir }
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir void FastAttributeList::clear()
72*cdf0e10cSrcweir {
73*cdf0e10cSrcweir 	maAttributes.clear();
74*cdf0e10cSrcweir 	maUnknownAttributes.clear();
75*cdf0e10cSrcweir 	maLastIter = maAttributes.end();
76*cdf0e10cSrcweir }
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir void FastAttributeList::add( sal_Int32 nToken, const OString& rValue )
79*cdf0e10cSrcweir {
80*cdf0e10cSrcweir 	maAttributes[nToken] = rValue;
81*cdf0e10cSrcweir }
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir void FastAttributeList::addUnknown( const OUString& rNamespaceURL, const OString& rName, const OString& rValue )
84*cdf0e10cSrcweir {
85*cdf0e10cSrcweir 	maUnknownAttributes.push_back( UnknownAttribute( rNamespaceURL, rName, rValue ) );
86*cdf0e10cSrcweir }
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir void FastAttributeList::addUnknown( const OString& rName, const OString& rValue )
89*cdf0e10cSrcweir {
90*cdf0e10cSrcweir 	maUnknownAttributes.push_back( UnknownAttribute( rName, rValue ) );
91*cdf0e10cSrcweir }
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir // XFastAttributeList
94*cdf0e10cSrcweir sal_Bool FastAttributeList::hasAttribute( ::sal_Int32 Token ) throw (RuntimeException)
95*cdf0e10cSrcweir {
96*cdf0e10cSrcweir 	maLastIter = maAttributes.find( Token );
97*cdf0e10cSrcweir 	return ( maLastIter != maAttributes.end() ) ? sal_True : sal_False;
98*cdf0e10cSrcweir }
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir sal_Int32 FastAttributeList::getValueToken( ::sal_Int32 Token ) throw (SAXException, RuntimeException)
101*cdf0e10cSrcweir {
102*cdf0e10cSrcweir 	if( ( maLastIter == maAttributes.end() ) || ( ( *maLastIter ).first != Token ) )
103*cdf0e10cSrcweir 		maLastIter = maAttributes.find( Token );
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir 	if( maLastIter == maAttributes.end() )
106*cdf0e10cSrcweir 		throw SAXException();
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir 	Sequence< sal_Int8 > aSeq( (sal_Int8*)(*maLastIter).second.getStr(), (*maLastIter).second.getLength() ) ;
109*cdf0e10cSrcweir 	return mxTokenHandler->getTokenFromUTF8( aSeq );
110*cdf0e10cSrcweir }
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir sal_Int32 FastAttributeList::getOptionalValueToken( ::sal_Int32 Token, ::sal_Int32 Default ) throw (RuntimeException)
113*cdf0e10cSrcweir {
114*cdf0e10cSrcweir 	if( ( maLastIter == maAttributes.end() ) || ( ( *maLastIter ).first != Token ) )
115*cdf0e10cSrcweir 		maLastIter = maAttributes.find( Token );
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir 	if( maLastIter == maAttributes.end() )
118*cdf0e10cSrcweir 		return Default;
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir 	Sequence< sal_Int8 > aSeq( (sal_Int8*)(*maLastIter).second.getStr(), (*maLastIter).second.getLength() ) ;
121*cdf0e10cSrcweir 	return mxTokenHandler->getTokenFromUTF8( aSeq );
122*cdf0e10cSrcweir }
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir OUString FastAttributeList::getValue( ::sal_Int32 Token ) throw (SAXException, RuntimeException)
125*cdf0e10cSrcweir {
126*cdf0e10cSrcweir 	if( ( maLastIter == maAttributes.end() ) || ( ( *maLastIter ).first != Token ) )
127*cdf0e10cSrcweir 		maLastIter = maAttributes.find( Token );
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir 	if( maLastIter == maAttributes.end() )
130*cdf0e10cSrcweir 		throw SAXException();
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir 	return OStringToOUString( (*maLastIter).second, RTL_TEXTENCODING_UTF8 );
133*cdf0e10cSrcweir }
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir OUString FastAttributeList::getOptionalValue( ::sal_Int32 Token ) throw (RuntimeException)
136*cdf0e10cSrcweir {
137*cdf0e10cSrcweir 	if( ( maLastIter == maAttributes.end() ) || ( ( *maLastIter ).first != Token ) )
138*cdf0e10cSrcweir 		maLastIter = maAttributes.find( Token );
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir 	OUString aRet;
141*cdf0e10cSrcweir 	if( maLastIter != maAttributes.end() )
142*cdf0e10cSrcweir 		aRet = OStringToOUString( (*maLastIter).second, RTL_TEXTENCODING_UTF8 );
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir 	return aRet;
145*cdf0e10cSrcweir }
146*cdf0e10cSrcweir Sequence< Attribute > FastAttributeList::getUnknownAttributes(  ) throw (RuntimeException)
147*cdf0e10cSrcweir {
148*cdf0e10cSrcweir 	Sequence< Attribute > aSeq( maUnknownAttributes.size() );
149*cdf0e10cSrcweir 	Attribute* pAttr = aSeq.getArray();
150*cdf0e10cSrcweir     for( UnknownAttributeList::iterator attrIter = maUnknownAttributes.begin(); attrIter != maUnknownAttributes.end(); attrIter++ )
151*cdf0e10cSrcweir         (*attrIter).FillAttribute( pAttr++ );
152*cdf0e10cSrcweir 	return aSeq;
153*cdf0e10cSrcweir }
154*cdf0e10cSrcweir Sequence< FastAttribute > FastAttributeList::getFastAttributes(  ) throw (RuntimeException)
155*cdf0e10cSrcweir {
156*cdf0e10cSrcweir 	Sequence< FastAttribute > aSeq( maAttributes.size() );
157*cdf0e10cSrcweir 	FastAttribute* pAttr = aSeq.getArray();
158*cdf0e10cSrcweir 	FastAttributeMap::iterator fastAttrIter = maAttributes.begin();
159*cdf0e10cSrcweir 	for(; fastAttrIter != maAttributes.end(); fastAttrIter++ )
160*cdf0e10cSrcweir 	{
161*cdf0e10cSrcweir 		pAttr->Token = fastAttrIter->first;
162*cdf0e10cSrcweir 		pAttr->Value = OStringToOUString( fastAttrIter->second, RTL_TEXTENCODING_UTF8 );
163*cdf0e10cSrcweir 		pAttr++;
164*cdf0e10cSrcweir 	}
165*cdf0e10cSrcweir 	return aSeq;
166*cdf0e10cSrcweir }
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir }
169