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 : #include "storagexmlstream.hxx"
21 :
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>
26 :
27 : #include <cppuhelper/implbase1.hxx>
28 : #include <rtl/ref.hxx>
29 : #include <tools/diagnose_ex.h>
30 : #include <xmloff/attrlist.hxx>
31 :
32 : #include <stack>
33 :
34 : namespace dbaccess
35 : {
36 :
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;
60 :
61 : // StorageXMLOutputStream_Data
62 0 : struct StorageXMLOutputStream_Data
63 : {
64 : Reference< XDocumentHandler > xHandler;
65 : ::std::stack< OUString > aElements;
66 : ::rtl::Reference< SvXMLAttributeList > xAttributes;
67 : };
68 :
69 : // StorageXMLOutputStream
70 0 : 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 0 : ,m_pData( new StorageXMLOutputStream_Data )
75 : {
76 0 : const Reference< XWriter > xSaxWriter = Writer::create( i_rContext );
77 0 : xSaxWriter->setOutputStream( getOutputStream() );
78 :
79 0 : m_pData->xHandler.set( xSaxWriter, UNO_QUERY_THROW );
80 0 : m_pData->xHandler->startDocument();
81 :
82 0 : m_pData->xAttributes = new SvXMLAttributeList;
83 0 : }
84 :
85 0 : StorageXMLOutputStream::~StorageXMLOutputStream()
86 : {
87 0 : }
88 :
89 0 : void StorageXMLOutputStream::close()
90 : {
91 0 : ENSURE_OR_RETURN_VOID( m_pData->xHandler.is(), "illegal document handler" );
92 0 : m_pData->xHandler->endDocument();
93 : // do not call the base class, it would call closeOutput on the output stream, which is already done by
94 : // endDocument
95 : }
96 :
97 0 : void StorageXMLOutputStream::addAttribute( const OUString& i_rName, const OUString& i_rValue ) const
98 : {
99 0 : m_pData->xAttributes->AddAttribute( i_rName, i_rValue );
100 0 : }
101 :
102 0 : void StorageXMLOutputStream::startElement( const OUString& i_rElementName ) const
103 : {
104 0 : ENSURE_OR_RETURN_VOID( m_pData->xHandler.is(), "no document handler" );
105 :
106 0 : m_pData->xHandler->startElement( i_rElementName, m_pData->xAttributes.get() );
107 0 : m_pData->xAttributes = new SvXMLAttributeList;
108 0 : m_pData->aElements.push( i_rElementName );
109 : }
110 :
111 0 : void StorageXMLOutputStream::endElement() const
112 : {
113 0 : ENSURE_OR_RETURN_VOID( m_pData->xHandler.is(), "no document handler" );
114 0 : ENSURE_OR_RETURN_VOID( !m_pData->aElements.empty(), "no element on the stack" );
115 :
116 0 : const OUString sElementName( m_pData->aElements.top() );
117 0 : m_pData->xHandler->endElement( sElementName );
118 0 : m_pData->aElements.pop();
119 : }
120 :
121 0 : void StorageXMLOutputStream::ignorableWhitespace( const OUString& i_rWhitespace ) const
122 : {
123 0 : ENSURE_OR_RETURN_VOID( m_pData->xHandler.is(), "no document handler" );
124 :
125 0 : m_pData->xHandler->ignorableWhitespace( i_rWhitespace );
126 : }
127 :
128 0 : void StorageXMLOutputStream::characters( const OUString& i_rCharacters ) const
129 : {
130 0 : ENSURE_OR_RETURN_VOID( m_pData->xHandler.is(), "no document handler" );
131 :
132 0 : m_pData->xHandler->characters( i_rCharacters );
133 : }
134 :
135 : // StorageXMLInputStream_Data
136 0 : struct StorageXMLInputStream_Data
137 : {
138 : Reference< XParser > xParser;
139 : };
140 :
141 : // StorageXMLInputStream
142 0 : 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 0 : ,m_pData( new StorageXMLInputStream_Data )
147 : {
148 0 : m_pData->xParser.set( Parser::create(i_rContext) );
149 0 : }
150 :
151 0 : void StorageXMLInputStream::import( const Reference< XDocumentHandler >& i_rHandler )
152 : {
153 0 : ENSURE_OR_THROW( i_rHandler.is(), "illegal document handler (NULL)" );
154 :
155 0 : InputSource aInputSource;
156 0 : aInputSource.aInputStream = getInputStream();
157 :
158 0 : m_pData->xParser->setDocumentHandler( i_rHandler );
159 0 : m_pData->xParser->parseStream( aInputSource );
160 0 : }
161 :
162 0 : StorageXMLInputStream::~StorageXMLInputStream()
163 : {
164 0 : }
165 :
166 : } // namespace dbaccess
167 :
168 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|