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