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 // MARKER(update_precomp.py): autogen include statement, do not remove 29 #include "precompiled_xmloff.hxx" 30 #include "xmlbasici.hxx" 31 #include <xmloff/attrlist.hxx> 32 #include <xmloff/nmspmap.hxx> 33 #include <xmloff/xmlimp.hxx> 34 35 using namespace ::com::sun::star; 36 using namespace ::com::sun::star::uno; 37 38 39 // ============================================================================= 40 // XMLBasicImportContext 41 // ============================================================================= 42 43 XMLBasicImportContext::XMLBasicImportContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const ::rtl::OUString& rLName, 44 const Reference< frame::XModel >& rxModel ) 45 :SvXMLImportContext( rImport, nPrfx, rLName ) 46 ,m_xModel( rxModel ) 47 { 48 Reference< lang::XMultiServiceFactory > xMSF = GetImport().getServiceFactory(); 49 if ( xMSF.is() ) 50 { 51 m_xHandler.set( xMSF->createInstance( 52 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.XMLOasisBasicImporter" ) ) ), 53 UNO_QUERY ); 54 } 55 56 if ( m_xHandler.is() ) 57 { 58 Reference< document::XImporter > xImporter( m_xHandler, UNO_QUERY ); 59 if ( xImporter.is() ) 60 { 61 Reference< lang::XComponent > xComp( m_xModel, UNO_QUERY ); 62 xImporter->setTargetDocument( xComp ); 63 } 64 } 65 } 66 67 // ----------------------------------------------------------------------------- 68 69 XMLBasicImportContext::~XMLBasicImportContext() 70 { 71 } 72 73 // ----------------------------------------------------------------------------- 74 75 SvXMLImportContext* XMLBasicImportContext::CreateChildContext( 76 sal_uInt16 nPrefix, const ::rtl::OUString& rLocalName, 77 const Reference< xml::sax::XAttributeList >& ) 78 { 79 SvXMLImportContext* pContext = 0; 80 81 if ( m_xHandler.is() ) 82 pContext = new XMLBasicImportChildContext( GetImport(), nPrefix, rLocalName, m_xHandler ); 83 84 if ( !pContext ) 85 pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName ); 86 87 return pContext; 88 } 89 90 // ----------------------------------------------------------------------------- 91 92 void XMLBasicImportContext::StartElement( 93 const Reference< xml::sax::XAttributeList >& rxAttrList ) 94 { 95 if ( m_xHandler.is() ) 96 { 97 m_xHandler->startDocument(); 98 99 // copy namespace declarations 100 SvXMLAttributeList* pAttrList = new SvXMLAttributeList( rxAttrList ); 101 Reference< xml::sax::XAttributeList > xAttrList( pAttrList ); 102 const SvXMLNamespaceMap& rNamespaceMap = GetImport().GetNamespaceMap(); 103 sal_uInt16 nPos = rNamespaceMap.GetFirstKey(); 104 while ( nPos != USHRT_MAX ) 105 { 106 ::rtl::OUString aAttrName( rNamespaceMap.GetAttrNameByKey( nPos ) ); 107 if ( xAttrList->getValueByName( aAttrName ).getLength() == 0 ) 108 pAttrList->AddAttribute( aAttrName, rNamespaceMap.GetNameByKey( nPos ) ); 109 nPos = rNamespaceMap.GetNextKey( nPos ); 110 } 111 112 m_xHandler->startElement( 113 GetImport().GetNamespaceMap().GetQNameByKey( GetPrefix(), GetLocalName() ), 114 xAttrList ); 115 } 116 } 117 118 // ----------------------------------------------------------------------------- 119 120 void XMLBasicImportContext::EndElement() 121 { 122 if ( m_xHandler.is() ) 123 { 124 m_xHandler->endElement( 125 GetImport().GetNamespaceMap().GetQNameByKey( GetPrefix(), GetLocalName() ) ); 126 m_xHandler->endDocument(); 127 } 128 } 129 130 // ----------------------------------------------------------------------------- 131 132 void XMLBasicImportContext::Characters( const ::rtl::OUString& rChars ) 133 { 134 if ( m_xHandler.is() ) 135 m_xHandler->characters( rChars ); 136 } 137 138 139 // ============================================================================= 140 // XMLBasicImportChildContext 141 // ============================================================================= 142 143 XMLBasicImportChildContext::XMLBasicImportChildContext( SvXMLImport& rImport, sal_uInt16 nPrfx, const ::rtl::OUString& rLName, 144 const Reference< xml::sax::XDocumentHandler >& rxHandler ) 145 :SvXMLImportContext( rImport, nPrfx, rLName ) 146 ,m_xHandler( rxHandler ) 147 { 148 } 149 150 // ----------------------------------------------------------------------------- 151 152 XMLBasicImportChildContext::~XMLBasicImportChildContext() 153 { 154 } 155 156 // ----------------------------------------------------------------------------- 157 158 SvXMLImportContext* XMLBasicImportChildContext::CreateChildContext( 159 sal_uInt16 nPrefix, const ::rtl::OUString& rLocalName, 160 const Reference< xml::sax::XAttributeList >& ) 161 { 162 return new XMLBasicImportChildContext( GetImport(), nPrefix, rLocalName, m_xHandler ); 163 } 164 165 // ----------------------------------------------------------------------------- 166 167 void XMLBasicImportChildContext::StartElement( 168 const Reference< xml::sax::XAttributeList >& xAttrList ) 169 { 170 if ( m_xHandler.is() ) 171 { 172 m_xHandler->startElement( 173 GetImport().GetNamespaceMap().GetQNameByKey( GetPrefix(), GetLocalName() ), 174 xAttrList ); 175 } 176 } 177 178 // ----------------------------------------------------------------------------- 179 180 void XMLBasicImportChildContext::EndElement() 181 { 182 if ( m_xHandler.is() ) 183 { 184 m_xHandler->endElement( 185 GetImport().GetNamespaceMap().GetQNameByKey( GetPrefix(), GetLocalName() ) ); 186 } 187 } 188 189 // ----------------------------------------------------------------------------- 190 191 void XMLBasicImportChildContext::Characters( const ::rtl::OUString& rChars ) 192 { 193 if ( m_xHandler.is() ) 194 m_xHandler->characters( rChars ); 195 } 196 197 // ----------------------------------------------------------------------------- 198