1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "storagexmlstream.hxx"
23 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
24 #include <com/sun/star/io/XActiveDataSource.hpp>
25 #include <com/sun/star/xml/sax/Parser.hpp>
26 #include <com/sun/star/xml/sax/Writer.hpp>
28 #include <comphelper/componentcontext.hxx>
29 #include <cppuhelper/implbase1.hxx>
30 #include <rtl/ref.hxx>
31 #include <tools/diagnose_ex.h>
32 #include <xmloff/attrlist.hxx>
36 //......................................................................................................................
39 //......................................................................................................................
41 /** === begin UNO using === **/
42 using ::com::sun::star::uno::Reference
;
43 using ::com::sun::star::uno::XInterface
;
44 using ::com::sun::star::uno::UNO_QUERY
;
45 using ::com::sun::star::uno::UNO_QUERY_THROW
;
46 using ::com::sun::star::uno::UNO_SET_THROW
;
47 using ::com::sun::star::uno::Exception
;
48 using ::com::sun::star::uno::RuntimeException
;
49 using ::com::sun::star::uno::Any
;
50 using ::com::sun::star::uno::makeAny
;
51 using ::com::sun::star::uno::Sequence
;
52 using ::com::sun::star::uno::Type
;
53 using ::com::sun::star::embed::XStorage
;
54 using ::com::sun::star::xml::sax::XDocumentHandler
;
55 using ::com::sun::star::xml::sax::XAttributeList
;
56 using ::com::sun::star::xml::sax::XWriter
;
57 using ::com::sun::star::xml::sax::Writer
;
58 using ::com::sun::star::io::XStream
;
59 using ::com::sun::star::io::XOutputStream
;
60 using ::com::sun::star::io::XActiveDataSource
;
61 using ::com::sun::star::xml::sax::Parser
;
62 using ::com::sun::star::xml::sax::XParser
;
63 using ::com::sun::star::xml::sax::InputSource
;
64 /** === end UNO using === **/
66 //==================================================================================================================
67 //= StorageXMLOutputStream_Data
68 //==================================================================================================================
69 struct StorageXMLOutputStream_Data
71 Reference
< XDocumentHandler
> xHandler
;
72 ::std::stack
< ::rtl::OUString
> aElements
;
73 ::rtl::Reference
< SvXMLAttributeList
> xAttributes
;
76 //==================================================================================================================
77 //= StorageXMLOutputStream
78 //==================================================================================================================
79 //------------------------------------------------------------------------------------------------------------------
80 StorageXMLOutputStream::StorageXMLOutputStream( const ::comphelper::ComponentContext
& i_rContext
,
81 const Reference
< XStorage
>& i_rParentStorage
,
82 const ::rtl::OUString
& i_rStreamName
)
83 :StorageOutputStream( i_rContext
, i_rParentStorage
, i_rStreamName
)
84 ,m_pData( new StorageXMLOutputStream_Data
)
86 const Reference
< XWriter
> xSaxWriter
= Writer::create( i_rContext
.getUNOContext() );
87 xSaxWriter
->setOutputStream( getOutputStream() );
89 m_pData
->xHandler
.set( xSaxWriter
, UNO_QUERY_THROW
);
90 m_pData
->xHandler
->startDocument();
92 m_pData
->xAttributes
= new SvXMLAttributeList
;
95 //------------------------------------------------------------------------------------------------------------------
96 StorageXMLOutputStream::~StorageXMLOutputStream()
100 //------------------------------------------------------------------------------------------------------------------
101 void StorageXMLOutputStream::close()
103 ENSURE_OR_RETURN_VOID( m_pData
->xHandler
.is(), "illegal document handler" );
104 m_pData
->xHandler
->endDocument();
105 // do not call the base class, it would call closeOutput on the output stream, which is already done by
109 //------------------------------------------------------------------------------------------------------------------
110 void StorageXMLOutputStream::addAttribute( const ::rtl::OUString
& i_rName
, const ::rtl::OUString
& i_rValue
) const
112 m_pData
->xAttributes
->AddAttribute( i_rName
, i_rValue
);
115 //------------------------------------------------------------------------------------------------------------------
116 void StorageXMLOutputStream::startElement( const ::rtl::OUString
& i_rElementName
) const
118 ENSURE_OR_RETURN_VOID( m_pData
->xHandler
.is(), "no document handler" );
120 m_pData
->xHandler
->startElement( i_rElementName
, m_pData
->xAttributes
.get() );
121 m_pData
->xAttributes
= new SvXMLAttributeList
;
122 m_pData
->aElements
.push( i_rElementName
);
125 //------------------------------------------------------------------------------------------------------------------
126 void StorageXMLOutputStream::endElement() const
128 ENSURE_OR_RETURN_VOID( m_pData
->xHandler
.is(), "no document handler" );
129 ENSURE_OR_RETURN_VOID( !m_pData
->aElements
.empty(), "no element on the stack" );
131 const ::rtl::OUString
sElementName( m_pData
->aElements
.top() );
132 m_pData
->xHandler
->endElement( sElementName
);
133 m_pData
->aElements
.pop();
136 //------------------------------------------------------------------------------------------------------------------
137 void StorageXMLOutputStream::ignorableWhitespace( const ::rtl::OUString
& i_rWhitespace
) const
139 ENSURE_OR_RETURN_VOID( m_pData
->xHandler
.is(), "no document handler" );
141 m_pData
->xHandler
->ignorableWhitespace( i_rWhitespace
);
144 //------------------------------------------------------------------------------------------------------------------
145 void StorageXMLOutputStream::characters( const ::rtl::OUString
& i_rCharacters
) const
147 ENSURE_OR_RETURN_VOID( m_pData
->xHandler
.is(), "no document handler" );
149 m_pData
->xHandler
->characters( i_rCharacters
);
152 //==================================================================================================================
153 //= StorageXMLInputStream_Data
154 //==================================================================================================================
155 struct StorageXMLInputStream_Data
157 Reference
< XParser
> xParser
;
160 //==================================================================================================================
161 //= StorageXMLInputStream
162 //==================================================================================================================
163 //------------------------------------------------------------------------------------------------------------------
164 StorageXMLInputStream::StorageXMLInputStream( const ::comphelper::ComponentContext
& i_rContext
,
165 const Reference
< XStorage
>& i_rParentStorage
,
166 const ::rtl::OUString
& i_rStreamName
)
167 :StorageInputStream( i_rContext
, i_rParentStorage
, i_rStreamName
)
168 ,m_pData( new StorageXMLInputStream_Data
)
170 m_pData
->xParser
.set( Parser::create(i_rContext
.getUNOContext()) );
173 //------------------------------------------------------------------------------------------------------------------
174 void StorageXMLInputStream::import( const Reference
< XDocumentHandler
>& i_rHandler
)
176 ENSURE_OR_THROW( i_rHandler
.is(), "illegal document handler (NULL)" );
178 InputSource aInputSource
;
179 aInputSource
.aInputStream
= getInputStream();
181 m_pData
->xParser
->setDocumentHandler( i_rHandler
);
182 m_pData
->xParser
->parseStream( aInputSource
);
185 //------------------------------------------------------------------------------------------------------------------
186 StorageXMLInputStream::~StorageXMLInputStream()
190 //......................................................................................................................
191 } // namespace dbaccess
192 //......................................................................................................................
194 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */