1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: fastattribs.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 #include <boost/bind.hpp>
34 #include <sax/fastattribs.hxx>
36 using ::rtl::OUString
;
38 using namespace ::com::sun::star::uno
;
39 using namespace ::com::sun::star::xml
;
40 using namespace ::com::sun::star::xml::sax
;
41 namespace sax_fastparser
44 UnknownAttribute::UnknownAttribute( const OUString
& rNamespaceURL
, const OString
& rName
, const OString
& rValue
)
45 : maNamespaceURL( rNamespaceURL
), maName( rName
), maValue( rValue
)
49 UnknownAttribute::UnknownAttribute( const OString
& rName
, const OString
& rValue
)
50 : maName( rName
), maValue( rValue
)
54 void UnknownAttribute::FillAttribute( Attribute
* pAttrib
) const
58 pAttrib
->Name
= OStringToOUString( maName
, RTL_TEXTENCODING_UTF8
);
59 pAttrib
->NamespaceURL
= maNamespaceURL
;
60 pAttrib
->Value
= OStringToOUString( maValue
, RTL_TEXTENCODING_UTF8
);
64 FastAttributeList::FastAttributeList( const ::com::sun::star::uno::Reference
< ::com::sun::star::xml::sax::XFastTokenHandler
>& xTokenHandler
)
65 : mxTokenHandler( xTokenHandler
)
67 maLastIter
= maAttributes
.end();
70 FastAttributeList::~FastAttributeList()
74 void FastAttributeList::clear()
77 maUnknownAttributes
.clear();
78 maLastIter
= maAttributes
.end();
81 void FastAttributeList::add( sal_Int32 nToken
, const OString
& rValue
)
83 maAttributes
[nToken
] = rValue
;
86 void FastAttributeList::addUnknown( const OUString
& rNamespaceURL
, const OString
& rName
, const OString
& rValue
)
88 maUnknownAttributes
.push_back( UnknownAttribute( rNamespaceURL
, rName
, rValue
) );
91 void FastAttributeList::addUnknown( const OString
& rName
, const OString
& rValue
)
93 maUnknownAttributes
.push_back( UnknownAttribute( rName
, rValue
) );
97 sal_Bool
FastAttributeList::hasAttribute( ::sal_Int32 Token
) throw (RuntimeException
)
99 maLastIter
= maAttributes
.find( Token
);
100 return ( maLastIter
!= maAttributes
.end() ) ? sal_True
: sal_False
;
103 sal_Int32
FastAttributeList::getValueToken( ::sal_Int32 Token
) throw (SAXException
, RuntimeException
)
105 if( ( maLastIter
== maAttributes
.end() ) || ( ( *maLastIter
).first
!= Token
) )
106 maLastIter
= maAttributes
.find( Token
);
108 if( maLastIter
== maAttributes
.end() )
109 throw SAXException();
111 Sequence
< sal_Int8
> aSeq( (sal_Int8
*)(*maLastIter
).second
.getStr(), (*maLastIter
).second
.getLength() ) ;
112 return mxTokenHandler
->getTokenFromUTF8( aSeq
);
115 sal_Int32
FastAttributeList::getOptionalValueToken( ::sal_Int32 Token
, ::sal_Int32 Default
) throw (RuntimeException
)
117 if( ( maLastIter
== maAttributes
.end() ) || ( ( *maLastIter
).first
!= Token
) )
118 maLastIter
= maAttributes
.find( Token
);
120 if( maLastIter
== maAttributes
.end() )
123 Sequence
< sal_Int8
> aSeq( (sal_Int8
*)(*maLastIter
).second
.getStr(), (*maLastIter
).second
.getLength() ) ;
124 return mxTokenHandler
->getTokenFromUTF8( aSeq
);
127 OUString
FastAttributeList::getValue( ::sal_Int32 Token
) throw (SAXException
, RuntimeException
)
129 if( ( maLastIter
== maAttributes
.end() ) || ( ( *maLastIter
).first
!= Token
) )
130 maLastIter
= maAttributes
.find( Token
);
132 if( maLastIter
== maAttributes
.end() )
133 throw SAXException();
135 return OStringToOUString( (*maLastIter
).second
, RTL_TEXTENCODING_UTF8
);
138 OUString
FastAttributeList::getOptionalValue( ::sal_Int32 Token
) throw (RuntimeException
)
140 if( ( maLastIter
== maAttributes
.end() ) || ( ( *maLastIter
).first
!= Token
) )
141 maLastIter
= maAttributes
.find( Token
);
144 if( maLastIter
!= maAttributes
.end() )
145 aRet
= OStringToOUString( (*maLastIter
).second
, RTL_TEXTENCODING_UTF8
);
149 Sequence
< Attribute
> FastAttributeList::getUnknownAttributes( ) throw (RuntimeException
)
151 Sequence
< Attribute
> aSeq( maUnknownAttributes
.size() );
152 Attribute
* pAttr
= aSeq
.getArray();
153 for( UnknownAttributeList::iterator attrIter
= maUnknownAttributes
.begin(); attrIter
!= maUnknownAttributes
.end(); attrIter
++ )
154 (*attrIter
).FillAttribute( pAttr
++ );
157 Sequence
< FastAttribute
> FastAttributeList::getFastAttributes( ) throw (RuntimeException
)
159 Sequence
< FastAttribute
> aSeq( maAttributes
.size() );
160 FastAttribute
* pAttr
= aSeq
.getArray();
161 FastAttributeMap::iterator fastAttrIter
= maAttributes
.begin();
162 for(; fastAttrIter
!= maAttributes
.end(); fastAttrIter
++ )
164 pAttr
->Token
= fastAttrIter
->first
;
165 pAttr
->Value
= OStringToOUString( fastAttrIter
->second
, RTL_TEXTENCODING_UTF8
);