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/Parser.hpp>
26 : #include <com/sun/star/xml/sax/Writer.hpp>
27 :
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 : 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;
63 :
64 : //==================================================================================================================
65 : //= StorageXMLOutputStream_Data
66 : //==================================================================================================================
67 0 : struct StorageXMLOutputStream_Data
68 : {
69 : Reference< XDocumentHandler > xHandler;
70 : ::std::stack< OUString > aElements;
71 : ::rtl::Reference< SvXMLAttributeList > xAttributes;
72 : };
73 :
74 : //==================================================================================================================
75 : //= StorageXMLOutputStream
76 : //==================================================================================================================
77 : //------------------------------------------------------------------------------------------------------------------
78 0 : 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 0 : ,m_pData( new StorageXMLOutputStream_Data )
83 : {
84 0 : const Reference< XWriter > xSaxWriter = Writer::create( i_rContext );
85 0 : xSaxWriter->setOutputStream( getOutputStream() );
86 :
87 0 : m_pData->xHandler.set( xSaxWriter, UNO_QUERY_THROW );
88 0 : m_pData->xHandler->startDocument();
89 :
90 0 : m_pData->xAttributes = new SvXMLAttributeList;
91 0 : }
92 :
93 : //------------------------------------------------------------------------------------------------------------------
94 0 : StorageXMLOutputStream::~StorageXMLOutputStream()
95 : {
96 0 : }
97 :
98 : //------------------------------------------------------------------------------------------------------------------
99 0 : void StorageXMLOutputStream::close()
100 : {
101 0 : ENSURE_OR_RETURN_VOID( m_pData->xHandler.is(), "illegal document handler" );
102 0 : m_pData->xHandler->endDocument();
103 : // do not call the base class, it would call closeOutput on the output stream, which is already done by
104 : // endDocument
105 : }
106 :
107 : //------------------------------------------------------------------------------------------------------------------
108 0 : void StorageXMLOutputStream::addAttribute( const OUString& i_rName, const OUString& i_rValue ) const
109 : {
110 0 : m_pData->xAttributes->AddAttribute( i_rName, i_rValue );
111 0 : }
112 :
113 : //------------------------------------------------------------------------------------------------------------------
114 0 : void StorageXMLOutputStream::startElement( const OUString& i_rElementName ) const
115 : {
116 0 : ENSURE_OR_RETURN_VOID( m_pData->xHandler.is(), "no document handler" );
117 :
118 0 : m_pData->xHandler->startElement( i_rElementName, m_pData->xAttributes.get() );
119 0 : m_pData->xAttributes = new SvXMLAttributeList;
120 0 : m_pData->aElements.push( i_rElementName );
121 : }
122 :
123 : //------------------------------------------------------------------------------------------------------------------
124 0 : void StorageXMLOutputStream::endElement() const
125 : {
126 0 : ENSURE_OR_RETURN_VOID( m_pData->xHandler.is(), "no document handler" );
127 0 : ENSURE_OR_RETURN_VOID( !m_pData->aElements.empty(), "no element on the stack" );
128 :
129 0 : const OUString sElementName( m_pData->aElements.top() );
130 0 : m_pData->xHandler->endElement( sElementName );
131 0 : m_pData->aElements.pop();
132 : }
133 :
134 : //------------------------------------------------------------------------------------------------------------------
135 0 : void StorageXMLOutputStream::ignorableWhitespace( const OUString& i_rWhitespace ) const
136 : {
137 0 : ENSURE_OR_RETURN_VOID( m_pData->xHandler.is(), "no document handler" );
138 :
139 0 : m_pData->xHandler->ignorableWhitespace( i_rWhitespace );
140 : }
141 :
142 : //------------------------------------------------------------------------------------------------------------------
143 0 : void StorageXMLOutputStream::characters( const OUString& i_rCharacters ) const
144 : {
145 0 : ENSURE_OR_RETURN_VOID( m_pData->xHandler.is(), "no document handler" );
146 :
147 0 : m_pData->xHandler->characters( i_rCharacters );
148 : }
149 :
150 : //==================================================================================================================
151 : //= StorageXMLInputStream_Data
152 : //==================================================================================================================
153 0 : struct StorageXMLInputStream_Data
154 : {
155 : Reference< XParser > xParser;
156 : };
157 :
158 : //==================================================================================================================
159 : //= StorageXMLInputStream
160 : //==================================================================================================================
161 : //------------------------------------------------------------------------------------------------------------------
162 0 : 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 0 : ,m_pData( new StorageXMLInputStream_Data )
167 : {
168 0 : m_pData->xParser.set( Parser::create(i_rContext) );
169 0 : }
170 :
171 : //------------------------------------------------------------------------------------------------------------------
172 0 : void StorageXMLInputStream::import( const Reference< XDocumentHandler >& i_rHandler )
173 : {
174 0 : ENSURE_OR_THROW( i_rHandler.is(), "illegal document handler (NULL)" );
175 :
176 0 : InputSource aInputSource;
177 0 : aInputSource.aInputStream = getInputStream();
178 :
179 0 : m_pData->xParser->setDocumentHandler( i_rHandler );
180 0 : m_pData->xParser->parseStream( aInputSource );
181 0 : }
182 :
183 : //------------------------------------------------------------------------------------------------------------------
184 0 : StorageXMLInputStream::~StorageXMLInputStream()
185 : {
186 0 : }
187 :
188 : //......................................................................................................................
189 : } // namespace dbaccess
190 : //......................................................................................................................
191 :
192 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|