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