LibreOffice Module dbaccess (master) 1
storagexmlstream.cxx
Go to the documentation of this file.
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/embed/ElementModes.hpp>
23#include <com/sun/star/xml/sax/XDocumentHandler.hpp>
24#include <com/sun/star/xml/sax/Parser.hpp>
25#include <com/sun/star/xml/sax/Writer.hpp>
26
27#include <rtl/ref.hxx>
29
30namespace dbaccess
31{
32
33 using ::com::sun::star::uno::Reference;
34 using ::com::sun::star::uno::UNO_QUERY_THROW;
35 using ::com::sun::star::uno::XComponentContext;
36 using ::com::sun::star::embed::XStorage;
37 using ::com::sun::star::xml::sax::XDocumentHandler;
38 using ::com::sun::star::xml::sax::XWriter;
39 using ::com::sun::star::xml::sax::Writer;
40 using ::com::sun::star::xml::sax::Parser;
41 using ::com::sun::star::xml::sax::InputSource;
42
43 // StorageXMLOutputStream
44 StorageXMLOutputStream::StorageXMLOutputStream( const Reference<XComponentContext>& i_rContext,
45 const Reference< XStorage >& i_rParentStorage,
46 const OUString& i_rStreamName )
47 :StorageOutputStream( i_rParentStorage, i_rStreamName )
48 {
49 const Reference< XWriter > xSaxWriter = Writer::create( i_rContext );
50 xSaxWriter->setOutputStream( getOutputStream() );
51
52 mxHandler.set( xSaxWriter, UNO_QUERY_THROW );
53 mxHandler->startDocument();
54
55 mxAttributes = new comphelper::AttributeList;
56 }
57
58 StorageXMLOutputStream::~StorageXMLOutputStream()
59 {
60 }
61
62 void StorageXMLOutputStream::close()
63 {
64 ENSURE_OR_RETURN_VOID( mxHandler.is(), "illegal document handler" );
65 mxHandler->endDocument();
66 // do not call the base class, it would call closeOutput on the output stream, which is already done by
67 // endDocument
68 }
69
70 void StorageXMLOutputStream::addAttribute( const OUString& i_rName, const OUString& i_rValue ) const
71 {
72 mxAttributes->AddAttribute( i_rName, i_rValue );
73 }
74
75 void StorageXMLOutputStream::startElement( const OUString& i_rElementName )
76 {
77 ENSURE_OR_RETURN_VOID( mxHandler.is(), "no document handler" );
78
79 mxHandler->startElement( i_rElementName, mxAttributes );
80 mxAttributes = new comphelper::AttributeList;
81 maElements.push( i_rElementName );
82 }
83
84 void StorageXMLOutputStream::endElement()
85 {
86 ENSURE_OR_RETURN_VOID( mxHandler.is(), "no document handler" );
87 ENSURE_OR_RETURN_VOID( !maElements.empty(), "no element on the stack" );
88
89 const OUString sElementName( maElements.top() );
90 mxHandler->endElement( sElementName );
91 maElements.pop();
92 }
93
94 void StorageXMLOutputStream::ignorableWhitespace( const OUString& i_rWhitespace ) const
95 {
96 ENSURE_OR_RETURN_VOID( mxHandler.is(), "no document handler" );
97
98 mxHandler->ignorableWhitespace( i_rWhitespace );
99 }
100
101 void StorageXMLOutputStream::characters( const OUString& i_rCharacters ) const
102 {
103 ENSURE_OR_RETURN_VOID( mxHandler.is(), "no document handler" );
104
105 mxHandler->characters( i_rCharacters );
106 }
107
108 // StorageXMLInputStream
109 StorageXMLInputStream::StorageXMLInputStream( const Reference<XComponentContext>& i_rContext,
110 const Reference< XStorage >& i_rParentStorage,
111 const OUString& i_rStreamName )
112 {
113 ENSURE_OR_THROW( i_rParentStorage.is(), "illegal stream" );
114
115 const Reference< css::io::XStream > xStream(
116 i_rParentStorage->openStreamElement( i_rStreamName, css::embed::ElementModes::READ ), css::uno::UNO_SET_THROW );
117 m_xInputStream.set( xStream->getInputStream(), css::uno::UNO_SET_THROW );
118
119 m_xParser.set( Parser::create(i_rContext) );
120 }
121
122 void StorageXMLInputStream::import( const Reference< XDocumentHandler >& i_rHandler )
123 {
124 ENSURE_OR_THROW( i_rHandler.is(), "illegal document handler (NULL)" );
125
126 InputSource aInputSource;
127 aInputSource.aInputStream = m_xInputStream;
128
129 m_xParser->setDocumentHandler( i_rHandler );
130 m_xParser->parseStream( aInputSource );
131 }
132
133 StorageXMLInputStream::~StorageXMLInputStream()
134 {
135 }
136
137} // namespace dbaccess
138
139/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XInputStream > xStream
StorageXMLOutputStream(const css::uno::Reference< css::uno::XComponentContext > &i_rContext, const css::uno::Reference< css::embed::XStorage > &i_rParentStorage, const OUString &i_rStreamName)
#define ENSURE_OR_THROW(c, m)
#define ENSURE_OR_RETURN_VOID(c, m)
rtl::Reference< ParserThread > m_xParser
bool getOutputStream(ProgramOptions const &options, OString const &extension, std::ostream **ppOutputStream, OString &targetSourceFileName, OString &tmpSourceFileName)
rtl::Reference< FragmentHandler > mxHandler