Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
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/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
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 .
18 : : */
19 : :
20 : :
21 : : #include "storagexmlstream.hxx"
22 : :
23 : : #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
24 : : #include <com/sun/star/io/XActiveDataSource.hpp>
25 : : #include <com/sun/star/xml/sax/XParser.hpp>
26 : :
27 : : #include <comphelper/componentcontext.hxx>
28 : : #include <cppuhelper/implbase1.hxx>
29 : : #include <rtl/ref.hxx>
30 : : #include <tools/diagnose_ex.h>
31 : : #include <xmloff/attrlist.hxx>
32 : :
33 : : #include <stack>
34 : :
35 : : //......................................................................................................................
36 : : namespace dbaccess
37 : : {
38 : : //......................................................................................................................
39 : :
40 : : /** === begin UNO using === **/
41 : : using ::com::sun::star::uno::Reference;
42 : : using ::com::sun::star::uno::XInterface;
43 : : using ::com::sun::star::uno::UNO_QUERY;
44 : : using ::com::sun::star::uno::UNO_QUERY_THROW;
45 : : using ::com::sun::star::uno::UNO_SET_THROW;
46 : : using ::com::sun::star::uno::Exception;
47 : : using ::com::sun::star::uno::RuntimeException;
48 : : using ::com::sun::star::uno::Any;
49 : : using ::com::sun::star::uno::makeAny;
50 : : using ::com::sun::star::uno::Sequence;
51 : : using ::com::sun::star::uno::Type;
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::io::XStream;
56 : : using ::com::sun::star::io::XOutputStream;
57 : : using ::com::sun::star::io::XActiveDataSource;
58 : : using ::com::sun::star::xml::sax::XParser;
59 : : using ::com::sun::star::xml::sax::InputSource;
60 : : /** === end UNO using === **/
61 : :
62 : : //==================================================================================================================
63 : : //= StorageXMLOutputStream_Data
64 : : //==================================================================================================================
65 [ # # ][ # # ]: 0 : struct StorageXMLOutputStream_Data
66 : : {
67 : : Reference< XDocumentHandler > xHandler;
68 : : ::std::stack< ::rtl::OUString > aElements;
69 : : ::rtl::Reference< SvXMLAttributeList > xAttributes;
70 : : };
71 : :
72 : : //==================================================================================================================
73 : : //= StorageXMLOutputStream
74 : : //==================================================================================================================
75 : : //------------------------------------------------------------------------------------------------------------------
76 : 0 : StorageXMLOutputStream::StorageXMLOutputStream( const ::comphelper::ComponentContext& i_rContext,
77 : : const Reference< XStorage >& i_rParentStorage,
78 : : const ::rtl::OUString& i_rStreamName )
79 : : :StorageOutputStream( i_rContext, i_rParentStorage, i_rStreamName )
80 [ # # ][ # # ]: 0 : ,m_pData( new StorageXMLOutputStream_Data )
81 : : {
82 [ # # ][ # # ]: 0 : const Reference< XActiveDataSource > xSaxWriter( i_rContext.createComponent( "com.sun.star.xml.sax.Writer" ), UNO_QUERY_THROW );
83 [ # # ][ # # ]: 0 : xSaxWriter->setOutputStream( getOutputStream() );
84 : :
85 [ # # ]: 0 : m_pData->xHandler.set( xSaxWriter, UNO_QUERY_THROW );
86 [ # # ][ # # ]: 0 : m_pData->xHandler->startDocument();
87 : :
88 [ # # ][ # # ]: 0 : m_pData->xAttributes = new SvXMLAttributeList;
89 : 0 : }
90 : :
91 : : //------------------------------------------------------------------------------------------------------------------
92 : 0 : StorageXMLOutputStream::~StorageXMLOutputStream()
93 : : {
94 [ # # ]: 0 : }
95 : :
96 : : //------------------------------------------------------------------------------------------------------------------
97 : 0 : void StorageXMLOutputStream::close()
98 : : {
99 [ # # ]: 0 : ENSURE_OR_RETURN_VOID( m_pData->xHandler.is(), "illegal document handler" );
100 : 0 : m_pData->xHandler->endDocument();
101 : : // do not call the base class, it would call closeOutput on the output stream, which is already done by
102 : : // endDocument
103 : : }
104 : :
105 : : //------------------------------------------------------------------------------------------------------------------
106 : 0 : void StorageXMLOutputStream::addAttribute( const ::rtl::OUString& i_rName, const ::rtl::OUString& i_rValue ) const
107 : : {
108 : 0 : m_pData->xAttributes->AddAttribute( i_rName, i_rValue );
109 : 0 : }
110 : :
111 : : //------------------------------------------------------------------------------------------------------------------
112 : 0 : void StorageXMLOutputStream::startElement( const ::rtl::OUString& i_rElementName ) const
113 : : {
114 [ # # ]: 0 : ENSURE_OR_RETURN_VOID( m_pData->xHandler.is(), "no document handler" );
115 : :
116 [ # # ][ # # ]: 0 : m_pData->xHandler->startElement( i_rElementName, m_pData->xAttributes.get() );
117 [ # # ]: 0 : m_pData->xAttributes = new SvXMLAttributeList;
118 : 0 : m_pData->aElements.push( i_rElementName );
119 : : }
120 : :
121 : : //------------------------------------------------------------------------------------------------------------------
122 : 0 : void StorageXMLOutputStream::endElement() const
123 : : {
124 [ # # ]: 0 : ENSURE_OR_RETURN_VOID( m_pData->xHandler.is(), "no document handler" );
125 [ # # ]: 0 : ENSURE_OR_RETURN_VOID( !m_pData->aElements.empty(), "no element on the stack" );
126 : :
127 [ # # ]: 0 : const ::rtl::OUString sElementName( m_pData->aElements.top() );
128 [ # # ][ # # ]: 0 : m_pData->xHandler->endElement( sElementName );
129 [ # # ]: 0 : m_pData->aElements.pop();
130 : : }
131 : :
132 : : //------------------------------------------------------------------------------------------------------------------
133 : 0 : void StorageXMLOutputStream::ignorableWhitespace( const ::rtl::OUString& i_rWhitespace ) const
134 : : {
135 [ # # ]: 0 : ENSURE_OR_RETURN_VOID( m_pData->xHandler.is(), "no document handler" );
136 : :
137 : 0 : m_pData->xHandler->ignorableWhitespace( i_rWhitespace );
138 : : }
139 : :
140 : : //------------------------------------------------------------------------------------------------------------------
141 : 0 : void StorageXMLOutputStream::characters( const ::rtl::OUString& i_rCharacters ) const
142 : : {
143 [ # # ]: 0 : ENSURE_OR_RETURN_VOID( m_pData->xHandler.is(), "no document handler" );
144 : :
145 : 0 : m_pData->xHandler->characters( i_rCharacters );
146 : : }
147 : :
148 : : //==================================================================================================================
149 : : //= StorageXMLInputStream_Data
150 : : //==================================================================================================================
151 : 0 : struct StorageXMLInputStream_Data
152 : : {
153 : : Reference< XParser > xParser;
154 : : };
155 : :
156 : : //==================================================================================================================
157 : : //= StorageXMLInputStream
158 : : //==================================================================================================================
159 : : //------------------------------------------------------------------------------------------------------------------
160 : 0 : StorageXMLInputStream::StorageXMLInputStream( const ::comphelper::ComponentContext& i_rContext,
161 : : const Reference< XStorage >& i_rParentStorage,
162 : : const ::rtl::OUString& i_rStreamName )
163 : : :StorageInputStream( i_rContext, i_rParentStorage, i_rStreamName )
164 [ # # ][ # # ]: 0 : ,m_pData( new StorageXMLInputStream_Data )
165 : : {
166 [ # # ][ # # ]: 0 : m_pData->xParser.set( i_rContext.createComponent( "com.sun.star.xml.sax.Parser" ), UNO_QUERY_THROW );
167 : 0 : }
168 : :
169 : : //------------------------------------------------------------------------------------------------------------------
170 : 0 : void StorageXMLInputStream::import( const Reference< XDocumentHandler >& i_rHandler )
171 : : {
172 [ # # ][ # # ]: 0 : ENSURE_OR_THROW( i_rHandler.is(), "illegal document handler (NULL)" );
[ # # ][ # # ]
173 : :
174 [ # # ]: 0 : InputSource aInputSource;
175 [ # # ]: 0 : aInputSource.aInputStream = getInputStream();
176 : :
177 [ # # ][ # # ]: 0 : m_pData->xParser->setDocumentHandler( i_rHandler );
178 [ # # ][ # # ]: 0 : m_pData->xParser->parseStream( aInputSource );
[ # # ]
179 : 0 : }
180 : :
181 : : //------------------------------------------------------------------------------------------------------------------
182 : 0 : StorageXMLInputStream::~StorageXMLInputStream()
183 : : {
184 [ # # ]: 0 : }
185 : :
186 : : //......................................................................................................................
187 : : } // namespace dbaccess
188 : : //......................................................................................................................
189 : :
190 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|