LibreOffice Module forms (master) 1
serialization_app_xml.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
22
23#include <com/sun/star/io/Pipe.hpp>
24#include <com/sun/star/xml/dom/DocumentBuilder.hpp>
25#include <com/sun/star/xml/dom/XNode.hpp>
26#include <com/sun/star/xml/dom/XDocument.hpp>
27#include <com/sun/star/xml/dom/NodeType.hpp>
28#include <com/sun/star/xml/sax/XSAXSerializable.hpp>
29#include <com/sun/star/xml/sax/Writer.hpp>
30#include <com/sun/star/beans/StringPair.hpp>
31#include <com/sun/star/xml/dom/XDocumentBuilder.hpp>
32
35
36using ::com::sun::star::uno::Reference;
37using ::com::sun::star::uno::Exception;
38using ::com::sun::star::uno::Sequence;
39using ::com::sun::star::uno::UNO_QUERY;
40using ::com::sun::star::uno::UNO_QUERY_THROW;
41using ::com::sun::star::uno::UNO_SET_THROW;
42using ::com::sun::star::xml::dom::DocumentBuilder;
43using ::com::sun::star::xml::dom::XNode;
44using ::com::sun::star::xml::dom::XDocument;
45using ::com::sun::star::xml::sax::XSAXSerializable;
46using ::com::sun::star::beans::StringPair;
47using ::com::sun::star::xml::dom::NodeType_DOCUMENT_NODE;
48using ::com::sun::star::xml::dom::NodeType_ELEMENT_NODE;
49using ::com::sun::star::xml::dom::XDocumentBuilder;
50using ::com::sun::star::xml::sax::Writer;
51using ::com::sun::star::xml::sax::XDocumentHandler;
52
54 : m_xBuffer(css::io::Pipe::create(comphelper::getProcessComponentContext()))
55{
56}
57
58Reference< css::io::XInputStream >
60{
61 // The pipes output is provided through it's
62 // XOutputStream interface aspect
63 return m_xBuffer;
64}
65
66void
67CSerializationAppXML::serialize_node(const Reference< XNode >& rNode)
68{
69 try
70 {
71 Reference< XSAXSerializable > xSerializer( rNode, UNO_QUERY );
72 if ( !xSerializer.is() )
73 {
74 // ensure we have a "real" node
75 Reference< XNode > xNode = rNode;
76 if ( xNode->getNodeType() == NodeType_DOCUMENT_NODE )
77 {
78 Reference< XDocument > const xDoc( xNode, UNO_QUERY_THROW );
79 xNode.set( xDoc->getDocumentElement(), UNO_QUERY_THROW );
80 }
81 ENSURE_OR_RETURN_VOID( xNode->getNodeType() == NodeType_ELEMENT_NODE,
82 "CSerializationAppXML::serialize_node: invalid node type!" );
83
84 // create a new document
85 Reference< XDocumentBuilder > const xDocBuilder = DocumentBuilder::create( comphelper::getProcessComponentContext() );
86 Reference< XDocument > const xDocument( xDocBuilder->newDocument(), UNO_SET_THROW );
87
88 // copy the to-be-serialized node
89 Reference< XNode > const xImportedNode( xDocument->importNode( xNode, true ), UNO_SET_THROW );
90 xDocument->appendChild( xImportedNode );
91
92 // ask the doc for the serializer
93 xSerializer.set( xDocument, UNO_QUERY );
94 }
95
96 ENSURE_OR_RETURN_VOID( xSerializer.is(),
97 "CSerializationAppXML::serialize_node: no serialization access to the node/document!" );
98
99 // create a SAXWriter to take the serialization events, and connect it to our pipe
100 Reference< css::xml::sax::XWriter > const xSaxWriter = Writer::create( comphelper::getProcessComponentContext() );
101 xSaxWriter->setOutputStream( Reference< css::io::XOutputStream >( m_xBuffer, UNO_QUERY_THROW) );
102
103 // do the serialization
104 xSerializer->serialize( Reference< XDocumentHandler >(xSaxWriter, UNO_QUERY_THROW), Sequence< StringPair >() );
105 }
106 catch( const Exception& )
107 {
108 DBG_UNHANDLED_EXCEPTION("forms.xforms");
109 }
110}
111
112void
114{
115 if (!m_aFragment.is()) return;
116
117 Reference< XNode > cur = m_aFragment->getFirstChild();
118 while (cur.is())
119 {
120 serialize_node(cur);
121 cur = cur->getNextSibling();
122 }
123 m_xBuffer->closeOutput();
124}
125
126/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void serialize() override
start the serialization process
void serialize_node(const css::uno::Reference< css::xml::dom::XNode > &aNode)
css::uno::Reference< css::io::XPipe > m_xBuffer
virtual css::uno::Reference< css::io::XInputStream > getInputStream() override
get the serialized bytes.
css::uno::Reference< css::xml::dom::XDocumentFragment > m_aFragment
#define DBG_UNHANDLED_EXCEPTION(...)
#define ENSURE_OR_RETURN_VOID(c, m)
@ Exception
Reference< XComponentContext > getProcessComponentContext()
css::uno::Reference< css::deployment::XPackageRegistry > create(css::uno::Reference< css::deployment::XPackageRegistry > const &xRootRegistry, OUString const &context, OUString const &cachePath, css::uno::Reference< css::uno::XComponentContext > const &xComponentContext)