1*63bba73cSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*63bba73cSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*63bba73cSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*63bba73cSAndrew Rist * distributed with this work for additional information 6*63bba73cSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*63bba73cSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*63bba73cSAndrew Rist * "License"); you may not use this file except in compliance 9*63bba73cSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*63bba73cSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*63bba73cSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*63bba73cSAndrew Rist * software distributed under the License is distributed on an 15*63bba73cSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*63bba73cSAndrew Rist * KIND, either express or implied. See the License for the 17*63bba73cSAndrew Rist * specific language governing permissions and limitations 18*63bba73cSAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*63bba73cSAndrew Rist *************************************************************/ 21*63bba73cSAndrew Rist 22*63bba73cSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_xmloff.hxx" 26cdf0e10cSrcweir #include "TokenContext.hxx" 27cdf0e10cSrcweir #include <xmloff/xmltkmap.hxx> 28cdf0e10cSrcweir #include <xmloff/xmlimp.hxx> 29cdf0e10cSrcweir #include <xmloff/nmspmap.hxx> 30cdf0e10cSrcweir #include "xmloff/xmlerror.hxx" 31cdf0e10cSrcweir 32cdf0e10cSrcweir #include <tools/debug.hxx> 33cdf0e10cSrcweir 34cdf0e10cSrcweir using rtl::OUString; 35cdf0e10cSrcweir using com::sun::star::uno::Reference; 36cdf0e10cSrcweir using com::sun::star::xml::sax::XAttributeList; 37cdf0e10cSrcweir 38cdf0e10cSrcweir 39cdf0e10cSrcweir struct SvXMLTokenMapEntry aEmptyMap[1] = 40cdf0e10cSrcweir { 41cdf0e10cSrcweir XML_TOKEN_MAP_END 42cdf0e10cSrcweir }; 43cdf0e10cSrcweir 44cdf0e10cSrcweir 45cdf0e10cSrcweir TokenContext::TokenContext( SvXMLImport& rImport, 46cdf0e10cSrcweir sal_uInt16 nPrefix, 47cdf0e10cSrcweir const OUString& rLocalName, 48cdf0e10cSrcweir const SvXMLTokenMapEntry* pAttributes, 49cdf0e10cSrcweir const SvXMLTokenMapEntry* pChildren ) 50cdf0e10cSrcweir : SvXMLImportContext( rImport, nPrefix, rLocalName ), 51cdf0e10cSrcweir mpAttributes( pAttributes ), 52cdf0e10cSrcweir mpChildren( pChildren ) 53cdf0e10cSrcweir { 54cdf0e10cSrcweir } 55cdf0e10cSrcweir 56cdf0e10cSrcweir TokenContext::~TokenContext() 57cdf0e10cSrcweir { 58cdf0e10cSrcweir } 59cdf0e10cSrcweir 60cdf0e10cSrcweir void TokenContext::StartElement( 61cdf0e10cSrcweir const Reference<XAttributeList>& xAttributeList ) 62cdf0e10cSrcweir { 63cdf0e10cSrcweir // iterate over attributes 64cdf0e10cSrcweir // - if in map: call HandleAttribute 65cdf0e10cSrcweir // - xmlns:... : ignore 66cdf0e10cSrcweir // - other: warning 67cdf0e10cSrcweir DBG_ASSERT( mpAttributes != NULL, "no token map for attributes" ); 68cdf0e10cSrcweir SvXMLTokenMap aMap( mpAttributes ); 69cdf0e10cSrcweir 70cdf0e10cSrcweir sal_Int16 nCount = xAttributeList->getLength(); 71cdf0e10cSrcweir for( sal_Int16 i = 0; i < nCount; i++ ) 72cdf0e10cSrcweir { 73cdf0e10cSrcweir // get key/local-name pair from namespace map 74cdf0e10cSrcweir OUString sLocalName; 75cdf0e10cSrcweir sal_uInt16 nPrefix = GetImport().GetNamespaceMap(). 76cdf0e10cSrcweir GetKeyByAttrName( xAttributeList->getNameByIndex(i), &sLocalName ); 77cdf0e10cSrcweir 78cdf0e10cSrcweir // get token from token map 79cdf0e10cSrcweir sal_uInt16 nToken = aMap.Get( nPrefix, sLocalName ); 80cdf0e10cSrcweir 81cdf0e10cSrcweir // and the value... 82cdf0e10cSrcweir const OUString& rValue = xAttributeList->getValueByIndex(i); 83cdf0e10cSrcweir 84cdf0e10cSrcweir if( nToken != XML_TOK_UNKNOWN ) 85cdf0e10cSrcweir { 86cdf0e10cSrcweir HandleAttribute( nToken, rValue ); 87cdf0e10cSrcweir } 88cdf0e10cSrcweir else if( nPrefix != XML_NAMESPACE_XMLNS ) 89cdf0e10cSrcweir { 90cdf0e10cSrcweir // error handling, for all attribute that are not 91cdf0e10cSrcweir // namespace declarations 92cdf0e10cSrcweir GetImport().SetError( XMLERROR_UNKNOWN_ATTRIBUTE, 93cdf0e10cSrcweir sLocalName, rValue); 94cdf0e10cSrcweir } 95cdf0e10cSrcweir } 96cdf0e10cSrcweir } 97cdf0e10cSrcweir 98cdf0e10cSrcweir SvXMLImportContext* TokenContext::CreateChildContext( 99cdf0e10cSrcweir sal_uInt16 nPrefix, 100cdf0e10cSrcweir const OUString& rLocalName, 101cdf0e10cSrcweir const Reference<XAttributeList>& xAttrList ) 102cdf0e10cSrcweir { 103cdf0e10cSrcweir // call HandleChild for elements in token map. Ignore other content. 104cdf0e10cSrcweir 105cdf0e10cSrcweir SvXMLImportContext* pContext = NULL; 106cdf0e10cSrcweir 107cdf0e10cSrcweir DBG_ASSERT( mpChildren != NULL, "no token map for child elements" ); 108cdf0e10cSrcweir SvXMLTokenMap aMap( mpChildren ); 109cdf0e10cSrcweir sal_uInt16 nToken = aMap.Get( nPrefix, rLocalName ); 110cdf0e10cSrcweir if( nToken != XML_TOK_UNKNOWN ) 111cdf0e10cSrcweir { 112cdf0e10cSrcweir // call handle child, and pass down arguments 113cdf0e10cSrcweir pContext = HandleChild( nToken, nPrefix, rLocalName, xAttrList ); 114cdf0e10cSrcweir } 115cdf0e10cSrcweir 116cdf0e10cSrcweir // error handling: create default context and generate warning 117cdf0e10cSrcweir if( pContext == NULL ) 118cdf0e10cSrcweir { 119cdf0e10cSrcweir GetImport().SetError( XMLERROR_UNKNOWN_ELEMENT, rLocalName ); 120cdf0e10cSrcweir pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName ); 121cdf0e10cSrcweir } 122cdf0e10cSrcweir return pContext; 123cdf0e10cSrcweir } 124cdf0e10cSrcweir 125cdf0e10cSrcweir bool lcl_IsWhiteSpace( sal_Unicode c ) 126cdf0e10cSrcweir { 127cdf0e10cSrcweir return c == sal_Unicode( ' ' ) 128cdf0e10cSrcweir || c == sal_Unicode( 0x09 ) 129cdf0e10cSrcweir || c == sal_Unicode( 0x0A ) 130cdf0e10cSrcweir || c == sal_Unicode( 0x0D ); 131cdf0e10cSrcweir } 132cdf0e10cSrcweir 133cdf0e10cSrcweir void TokenContext::Characters( const ::rtl::OUString& rCharacters ) 134cdf0e10cSrcweir { 135cdf0e10cSrcweir // get iterators for string data 136cdf0e10cSrcweir const sal_Unicode* pBegin = rCharacters.getStr(); 137cdf0e10cSrcweir const sal_Unicode* pEnd = &( pBegin[ rCharacters.getLength() ] ); 138cdf0e10cSrcweir 139cdf0e10cSrcweir // raise error if non-whitespace character is found 140cdf0e10cSrcweir if( ::std::find_if( pBegin, pEnd, ::std::not1(::std::ptr_fun(lcl_IsWhiteSpace)) ) != pEnd ) 141cdf0e10cSrcweir GetImport().SetError( XMLERROR_UNKNOWN_CHARACTERS, rCharacters ); 142cdf0e10cSrcweir } 143