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 .
20 #include "storagexmlstream.hxx"
22 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
23 #include <com/sun/star/io/XActiveDataSource.hpp>
24 #include <com/sun/star/xml/sax/Parser.hpp>
25 #include <com/sun/star/xml/sax/Writer.hpp>
27 #include <cppuhelper/implbase1.hxx>
28 #include <rtl/ref.hxx>
29 #include <tools/diagnose_ex.h>
30 #include <xmloff/attrlist.hxx>
37 using ::com::sun::star::uno::Reference
;
38 using ::com::sun::star::uno::XInterface
;
39 using ::com::sun::star::uno::UNO_QUERY
;
40 using ::com::sun::star::uno::UNO_QUERY_THROW
;
41 using ::com::sun::star::uno::UNO_SET_THROW
;
42 using ::com::sun::star::uno::Exception
;
43 using ::com::sun::star::uno::RuntimeException
;
44 using ::com::sun::star::uno::Any
;
45 using ::com::sun::star::uno::makeAny
;
46 using ::com::sun::star::uno::Sequence
;
47 using ::com::sun::star::uno::Type
;
48 using ::com::sun::star::uno::XComponentContext
;
49 using ::com::sun::star::embed::XStorage
;
50 using ::com::sun::star::xml::sax::XDocumentHandler
;
51 using ::com::sun::star::xml::sax::XAttributeList
;
52 using ::com::sun::star::xml::sax::XWriter
;
53 using ::com::sun::star::xml::sax::Writer
;
54 using ::com::sun::star::io::XStream
;
55 using ::com::sun::star::io::XOutputStream
;
56 using ::com::sun::star::io::XActiveDataSource
;
57 using ::com::sun::star::xml::sax::Parser
;
58 using ::com::sun::star::xml::sax::XParser
;
59 using ::com::sun::star::xml::sax::InputSource
;
61 // StorageXMLOutputStream_Data
62 struct StorageXMLOutputStream_Data
64 Reference
< XDocumentHandler
> xHandler
;
65 ::std::stack
< OUString
> aElements
;
66 ::rtl::Reference
< SvXMLAttributeList
> xAttributes
;
69 // StorageXMLOutputStream
70 StorageXMLOutputStream::StorageXMLOutputStream( const Reference
<XComponentContext
>& i_rContext
,
71 const Reference
< XStorage
>& i_rParentStorage
,
72 const OUString
& i_rStreamName
)
73 :StorageOutputStream( i_rContext
, i_rParentStorage
, i_rStreamName
)
74 ,m_pData( new StorageXMLOutputStream_Data
)
76 const Reference
< XWriter
> xSaxWriter
= Writer::create( i_rContext
);
77 xSaxWriter
->setOutputStream( getOutputStream() );
79 m_pData
->xHandler
.set( xSaxWriter
, UNO_QUERY_THROW
);
80 m_pData
->xHandler
->startDocument();
82 m_pData
->xAttributes
= new SvXMLAttributeList
;
85 StorageXMLOutputStream::~StorageXMLOutputStream()
89 void StorageXMLOutputStream::close()
91 ENSURE_OR_RETURN_VOID( m_pData
->xHandler
.is(), "illegal document handler" );
92 m_pData
->xHandler
->endDocument();
93 // do not call the base class, it would call closeOutput on the output stream, which is already done by
97 void StorageXMLOutputStream::addAttribute( const OUString
& i_rName
, const OUString
& i_rValue
) const
99 m_pData
->xAttributes
->AddAttribute( i_rName
, i_rValue
);
102 void StorageXMLOutputStream::startElement( const OUString
& i_rElementName
) const
104 ENSURE_OR_RETURN_VOID( m_pData
->xHandler
.is(), "no document handler" );
106 m_pData
->xHandler
->startElement( i_rElementName
, m_pData
->xAttributes
.get() );
107 m_pData
->xAttributes
= new SvXMLAttributeList
;
108 m_pData
->aElements
.push( i_rElementName
);
111 void StorageXMLOutputStream::endElement() const
113 ENSURE_OR_RETURN_VOID( m_pData
->xHandler
.is(), "no document handler" );
114 ENSURE_OR_RETURN_VOID( !m_pData
->aElements
.empty(), "no element on the stack" );
116 const OUString
sElementName( m_pData
->aElements
.top() );
117 m_pData
->xHandler
->endElement( sElementName
);
118 m_pData
->aElements
.pop();
121 void StorageXMLOutputStream::ignorableWhitespace( const OUString
& i_rWhitespace
) const
123 ENSURE_OR_RETURN_VOID( m_pData
->xHandler
.is(), "no document handler" );
125 m_pData
->xHandler
->ignorableWhitespace( i_rWhitespace
);
128 void StorageXMLOutputStream::characters( const OUString
& i_rCharacters
) const
130 ENSURE_OR_RETURN_VOID( m_pData
->xHandler
.is(), "no document handler" );
132 m_pData
->xHandler
->characters( i_rCharacters
);
135 // StorageXMLInputStream_Data
136 struct StorageXMLInputStream_Data
138 Reference
< XParser
> xParser
;
141 // StorageXMLInputStream
142 StorageXMLInputStream::StorageXMLInputStream( const Reference
<XComponentContext
>& i_rContext
,
143 const Reference
< XStorage
>& i_rParentStorage
,
144 const OUString
& i_rStreamName
)
145 :StorageInputStream( i_rContext
, i_rParentStorage
, i_rStreamName
)
146 ,m_pData( new StorageXMLInputStream_Data
)
148 m_pData
->xParser
.set( Parser::create(i_rContext
) );
151 void StorageXMLInputStream::import( const Reference
< XDocumentHandler
>& i_rHandler
)
153 ENSURE_OR_THROW( i_rHandler
.is(), "illegal document handler (NULL)" );
155 InputSource aInputSource
;
156 aInputSource
.aInputStream
= getInputStream();
158 m_pData
->xParser
->setDocumentHandler( i_rHandler
);
159 m_pData
->xParser
->parseStream( aInputSource
);
162 StorageXMLInputStream::~StorageXMLInputStream()
166 } // namespace dbaccess
168 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */