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 <cppuhelper/implbase1.hxx>
29 #include <rtl/ref.hxx>
30 #include <tools/diagnose_ex.h>
31 #include <xmloff/attrlist.hxx>
35 //......................................................................................................................
38 //......................................................................................................................
40 using ::com::sun::star::uno::Reference
;
41 using ::com::sun::star::uno::XInterface
;
42 using ::com::sun::star::uno::UNO_QUERY
;
43 using ::com::sun::star::uno::UNO_QUERY_THROW
;
44 using ::com::sun::star::uno::UNO_SET_THROW
;
45 using ::com::sun::star::uno::Exception
;
46 using ::com::sun::star::uno::RuntimeException
;
47 using ::com::sun::star::uno::Any
;
48 using ::com::sun::star::uno::makeAny
;
49 using ::com::sun::star::uno::Sequence
;
50 using ::com::sun::star::uno::Type
;
51 using ::com::sun::star::uno::XComponentContext
;
52 using ::com::sun::star::embed::XStorage
;
53 using ::com::sun::star::xml::sax::XDocumentHandler
;
54 using ::com::sun::star::xml::sax::XAttributeList
;
55 using ::com::sun::star::xml::sax::XWriter
;
56 using ::com::sun::star::xml::sax::Writer
;
57 using ::com::sun::star::io::XStream
;
58 using ::com::sun::star::io::XOutputStream
;
59 using ::com::sun::star::io::XActiveDataSource
;
60 using ::com::sun::star::xml::sax::Parser
;
61 using ::com::sun::star::xml::sax::XParser
;
62 using ::com::sun::star::xml::sax::InputSource
;
64 //==================================================================================================================
65 //= StorageXMLOutputStream_Data
66 //==================================================================================================================
67 struct StorageXMLOutputStream_Data
69 Reference
< XDocumentHandler
> xHandler
;
70 ::std::stack
< OUString
> aElements
;
71 ::rtl::Reference
< SvXMLAttributeList
> xAttributes
;
74 //==================================================================================================================
75 //= StorageXMLOutputStream
76 //==================================================================================================================
77 //------------------------------------------------------------------------------------------------------------------
78 StorageXMLOutputStream::StorageXMLOutputStream( const Reference
<XComponentContext
>& i_rContext
,
79 const Reference
< XStorage
>& i_rParentStorage
,
80 const OUString
& i_rStreamName
)
81 :StorageOutputStream( i_rContext
, i_rParentStorage
, i_rStreamName
)
82 ,m_pData( new StorageXMLOutputStream_Data
)
84 const Reference
< XWriter
> xSaxWriter
= Writer::create( i_rContext
);
85 xSaxWriter
->setOutputStream( getOutputStream() );
87 m_pData
->xHandler
.set( xSaxWriter
, UNO_QUERY_THROW
);
88 m_pData
->xHandler
->startDocument();
90 m_pData
->xAttributes
= new SvXMLAttributeList
;
93 //------------------------------------------------------------------------------------------------------------------
94 StorageXMLOutputStream::~StorageXMLOutputStream()
98 //------------------------------------------------------------------------------------------------------------------
99 void StorageXMLOutputStream::close()
101 ENSURE_OR_RETURN_VOID( m_pData
->xHandler
.is(), "illegal document handler" );
102 m_pData
->xHandler
->endDocument();
103 // do not call the base class, it would call closeOutput on the output stream, which is already done by
107 //------------------------------------------------------------------------------------------------------------------
108 void StorageXMLOutputStream::addAttribute( const OUString
& i_rName
, const OUString
& i_rValue
) const
110 m_pData
->xAttributes
->AddAttribute( i_rName
, i_rValue
);
113 //------------------------------------------------------------------------------------------------------------------
114 void StorageXMLOutputStream::startElement( const OUString
& i_rElementName
) const
116 ENSURE_OR_RETURN_VOID( m_pData
->xHandler
.is(), "no document handler" );
118 m_pData
->xHandler
->startElement( i_rElementName
, m_pData
->xAttributes
.get() );
119 m_pData
->xAttributes
= new SvXMLAttributeList
;
120 m_pData
->aElements
.push( i_rElementName
);
123 //------------------------------------------------------------------------------------------------------------------
124 void StorageXMLOutputStream::endElement() const
126 ENSURE_OR_RETURN_VOID( m_pData
->xHandler
.is(), "no document handler" );
127 ENSURE_OR_RETURN_VOID( !m_pData
->aElements
.empty(), "no element on the stack" );
129 const OUString
sElementName( m_pData
->aElements
.top() );
130 m_pData
->xHandler
->endElement( sElementName
);
131 m_pData
->aElements
.pop();
134 //------------------------------------------------------------------------------------------------------------------
135 void StorageXMLOutputStream::ignorableWhitespace( const OUString
& i_rWhitespace
) const
137 ENSURE_OR_RETURN_VOID( m_pData
->xHandler
.is(), "no document handler" );
139 m_pData
->xHandler
->ignorableWhitespace( i_rWhitespace
);
142 //------------------------------------------------------------------------------------------------------------------
143 void StorageXMLOutputStream::characters( const OUString
& i_rCharacters
) const
145 ENSURE_OR_RETURN_VOID( m_pData
->xHandler
.is(), "no document handler" );
147 m_pData
->xHandler
->characters( i_rCharacters
);
150 //==================================================================================================================
151 //= StorageXMLInputStream_Data
152 //==================================================================================================================
153 struct StorageXMLInputStream_Data
155 Reference
< XParser
> xParser
;
158 //==================================================================================================================
159 //= StorageXMLInputStream
160 //==================================================================================================================
161 //------------------------------------------------------------------------------------------------------------------
162 StorageXMLInputStream::StorageXMLInputStream( const Reference
<XComponentContext
>& i_rContext
,
163 const Reference
< XStorage
>& i_rParentStorage
,
164 const OUString
& i_rStreamName
)
165 :StorageInputStream( i_rContext
, i_rParentStorage
, i_rStreamName
)
166 ,m_pData( new StorageXMLInputStream_Data
)
168 m_pData
->xParser
.set( Parser::create(i_rContext
) );
171 //------------------------------------------------------------------------------------------------------------------
172 void StorageXMLInputStream::import( const Reference
< XDocumentHandler
>& i_rHandler
)
174 ENSURE_OR_THROW( i_rHandler
.is(), "illegal document handler (NULL)" );
176 InputSource aInputSource
;
177 aInputSource
.aInputStream
= getInputStream();
179 m_pData
->xParser
->setDocumentHandler( i_rHandler
);
180 m_pData
->xParser
->parseStream( aInputSource
);
183 //------------------------------------------------------------------------------------------------------------------
184 StorageXMLInputStream::~StorageXMLInputStream()
188 //......................................................................................................................
189 } // namespace dbaccess
190 //......................................................................................................................
192 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */