1 /************************************************************************* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 4 * Copyright 2009 by Sun Microsystems, Inc. 5 * 6 * OpenOffice.org - a multi-platform office productivity suite 7 * 8 * This file is part of OpenOffice.org. 9 * 10 * OpenOffice.org is free software: you can redistribute it and/or modify 11 * it under the terms of the GNU Lesser General Public License version 3 12 * only, as published by the Free Software Foundation. 13 * 14 * OpenOffice.org is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU Lesser General Public License version 3 for more details 18 * (a copy is included in the LICENSE file that accompanied this code). 19 * 20 * You should have received a copy of the GNU Lesser General Public License 21 * version 3 along with OpenOffice.org. If not, see 22 * <http://www.openoffice.org/license.html> 23 * for a copy of the LGPLv3 License. 24 ************************************************************************/ 25 26 #include "precompiled_dbaccess.hxx" 27 28 #include "storagexmlstream.hxx" 29 30 /** === begin UNO includes === **/ 31 #include <com/sun/star/xml/sax/XDocumentHandler.hpp> 32 #include <com/sun/star/io/XActiveDataSource.hpp> 33 #include <com/sun/star/xml/sax/XParser.hpp> 34 /** === end UNO includes === **/ 35 36 #include <comphelper/componentcontext.hxx> 37 #include <cppuhelper/implbase1.hxx> 38 #include <rtl/ref.hxx> 39 #include <tools/diagnose_ex.h> 40 #include <xmloff/attrlist.hxx> 41 42 #include <stack> 43 44 //...................................................................................................................... 45 namespace dbaccess 46 { 47 //...................................................................................................................... 48 49 /** === begin UNO using === **/ 50 using ::com::sun::star::uno::Reference; 51 using ::com::sun::star::uno::XInterface; 52 using ::com::sun::star::uno::UNO_QUERY; 53 using ::com::sun::star::uno::UNO_QUERY_THROW; 54 using ::com::sun::star::uno::UNO_SET_THROW; 55 using ::com::sun::star::uno::Exception; 56 using ::com::sun::star::uno::RuntimeException; 57 using ::com::sun::star::uno::Any; 58 using ::com::sun::star::uno::makeAny; 59 using ::com::sun::star::uno::Sequence; 60 using ::com::sun::star::uno::Type; 61 using ::com::sun::star::embed::XStorage; 62 using ::com::sun::star::xml::sax::XDocumentHandler; 63 using ::com::sun::star::xml::sax::XAttributeList; 64 using ::com::sun::star::io::XStream; 65 using ::com::sun::star::io::XOutputStream; 66 using ::com::sun::star::io::XActiveDataSource; 67 using ::com::sun::star::xml::sax::XParser; 68 using ::com::sun::star::xml::sax::InputSource; 69 /** === end UNO using === **/ 70 71 //================================================================================================================== 72 //= StorageXMLOutputStream_Data 73 //================================================================================================================== 74 struct StorageXMLOutputStream_Data 75 { 76 Reference< XDocumentHandler > xHandler; 77 ::std::stack< ::rtl::OUString > aElements; 78 ::rtl::Reference< SvXMLAttributeList > xAttributes; 79 }; 80 81 //================================================================================================================== 82 //= StorageXMLOutputStream 83 //================================================================================================================== 84 //------------------------------------------------------------------------------------------------------------------ 85 StorageXMLOutputStream::StorageXMLOutputStream( const ::comphelper::ComponentContext& i_rContext, 86 const Reference< XStorage >& i_rParentStorage, 87 const ::rtl::OUString& i_rStreamName ) 88 :StorageOutputStream( i_rContext, i_rParentStorage, i_rStreamName ) 89 ,m_pData( new StorageXMLOutputStream_Data ) 90 { 91 const Reference< XActiveDataSource > xSaxWriter( i_rContext.createComponent( "com.sun.star.xml.sax.Writer" ), UNO_QUERY_THROW ); 92 xSaxWriter->setOutputStream( getOutputStream() ); 93 94 m_pData->xHandler.set( xSaxWriter, UNO_QUERY_THROW ); 95 m_pData->xHandler->startDocument(); 96 97 m_pData->xAttributes = new SvXMLAttributeList; 98 } 99 100 //------------------------------------------------------------------------------------------------------------------ 101 StorageXMLOutputStream::~StorageXMLOutputStream() 102 { 103 } 104 105 //------------------------------------------------------------------------------------------------------------------ 106 void StorageXMLOutputStream::close() 107 { 108 ENSURE_OR_RETURN_VOID( m_pData->xHandler.is(), "illegal document handler" ); 109 m_pData->xHandler->endDocument(); 110 // do not call the base class, it would call closeOutput on the output stream, which is already done by 111 // endDocument 112 } 113 114 //------------------------------------------------------------------------------------------------------------------ 115 void StorageXMLOutputStream::addAttribute( const ::rtl::OUString& i_rName, const ::rtl::OUString& i_rValue ) const 116 { 117 m_pData->xAttributes->AddAttribute( i_rName, i_rValue ); 118 } 119 120 //------------------------------------------------------------------------------------------------------------------ 121 void StorageXMLOutputStream::startElement( const ::rtl::OUString& i_rElementName ) const 122 { 123 ENSURE_OR_RETURN_VOID( m_pData->xHandler.is(), "no document handler" ); 124 125 m_pData->xHandler->startElement( i_rElementName, m_pData->xAttributes.get() ); 126 m_pData->xAttributes = new SvXMLAttributeList; 127 m_pData->aElements.push( i_rElementName ); 128 } 129 130 //------------------------------------------------------------------------------------------------------------------ 131 void StorageXMLOutputStream::endElement() const 132 { 133 ENSURE_OR_RETURN_VOID( m_pData->xHandler.is(), "no document handler" ); 134 ENSURE_OR_RETURN_VOID( !m_pData->aElements.empty(), "no element on the stack" ); 135 136 const ::rtl::OUString sElementName( m_pData->aElements.top() ); 137 m_pData->xHandler->endElement( sElementName ); 138 m_pData->aElements.pop(); 139 } 140 141 //------------------------------------------------------------------------------------------------------------------ 142 void StorageXMLOutputStream::ignorableWhitespace( const ::rtl::OUString& i_rWhitespace ) const 143 { 144 ENSURE_OR_RETURN_VOID( m_pData->xHandler.is(), "no document handler" ); 145 146 m_pData->xHandler->ignorableWhitespace( i_rWhitespace ); 147 } 148 149 //------------------------------------------------------------------------------------------------------------------ 150 void StorageXMLOutputStream::characters( const ::rtl::OUString& i_rCharacters ) const 151 { 152 ENSURE_OR_RETURN_VOID( m_pData->xHandler.is(), "no document handler" ); 153 154 m_pData->xHandler->characters( i_rCharacters ); 155 } 156 157 //================================================================================================================== 158 //= StorageXMLInputStream_Data 159 //================================================================================================================== 160 struct StorageXMLInputStream_Data 161 { 162 Reference< XParser > xParser; 163 }; 164 165 //================================================================================================================== 166 //= StorageXMLInputStream 167 //================================================================================================================== 168 //------------------------------------------------------------------------------------------------------------------ 169 StorageXMLInputStream::StorageXMLInputStream( const ::comphelper::ComponentContext& i_rContext, 170 const Reference< XStorage >& i_rParentStorage, 171 const ::rtl::OUString& i_rStreamName ) 172 :StorageInputStream( i_rContext, i_rParentStorage, i_rStreamName ) 173 ,m_pData( new StorageXMLInputStream_Data ) 174 { 175 m_pData->xParser.set( i_rContext.createComponent( "com.sun.star.xml.sax.Parser" ), UNO_QUERY_THROW ); 176 } 177 178 //------------------------------------------------------------------------------------------------------------------ 179 void StorageXMLInputStream::import( const Reference< XDocumentHandler >& i_rHandler ) 180 { 181 ENSURE_OR_THROW( i_rHandler.is(), "illegal document handler (NULL)" ); 182 183 InputSource aInputSource; 184 aInputSource.aInputStream = getInputStream(); 185 186 m_pData->xParser->setDocumentHandler( i_rHandler ); 187 m_pData->xParser->parseStream( aInputSource ); 188 } 189 190 //------------------------------------------------------------------------------------------------------------------ 191 StorageXMLInputStream::~StorageXMLInputStream() 192 { 193 } 194 195 //...................................................................................................................... 196 } // namespace dbaccess 197 //...................................................................................................................... 198