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 "serialization.hxx"
22 : #include "serialization_app_xml.hxx"
23 :
24 : /** === begin UNO includes === **/
25 : #include <com/sun/star/io/Pipe.hpp>
26 : #include <com/sun/star/xml/dom/XNode.hpp>
27 : #include <com/sun/star/xml/dom/XDocument.hpp>
28 : #include <com/sun/star/xml/dom/XNodeList.hpp>
29 : #include <com/sun/star/xml/dom/NodeType.hpp>
30 : #include <com/sun/star/lang/XUnoTunnel.hpp>
31 : #include <com/sun/star/xml/xpath/XPathObjectType.hpp>
32 : #include <com/sun/star/xml/sax/XSAXSerializable.hpp>
33 : #include <com/sun/star/beans/StringPair.hpp>
34 : #include <com/sun/star/io/XActiveDataSource.hpp>
35 : #include <com/sun/star/xml/dom/XDocumentBuilder.hpp>
36 : /** === end UNO includes === **/
37 :
38 : #include <tools/diagnose_ex.h>
39 : #include <comphelper/processfactory.hxx>
40 :
41 : #include <boost/scoped_ptr.hpp>
42 : #include <limits>
43 :
44 : /** === begin UNO using === **/
45 : using ::com::sun::star::uno::Reference;
46 : using ::com::sun::star::uno::Exception;
47 : using ::com::sun::star::uno::Sequence;
48 : using ::com::sun::star::uno::RuntimeException;
49 : using ::com::sun::star::uno::UNO_QUERY;
50 : using ::com::sun::star::uno::UNO_QUERY_THROW;
51 : using ::com::sun::star::uno::UNO_SET_THROW;
52 : using ::com::sun::star::xml::dom::XNode;
53 : using ::com::sun::star::xml::dom::XDocument;
54 : using ::com::sun::star::xml::sax::XSAXSerializable;
55 : using ::com::sun::star::beans::StringPair;
56 : using ::com::sun::star::io::XActiveDataSource;
57 : using ::com::sun::star::xml::dom::NodeType_DOCUMENT_NODE;
58 : using ::com::sun::star::xml::dom::NodeType_ELEMENT_NODE;
59 : using ::com::sun::star::xml::dom::XDocumentBuilder;
60 : using ::com::sun::star::xml::sax::XDocumentHandler;
61 : /** === end UNO using === **/
62 :
63 0 : CSerializationAppXML::CSerializationAppXML()
64 : : m_xFactory(comphelper::getProcessServiceFactory())
65 0 : , m_xBuffer(CSS::io::Pipe::create(comphelper::getProcessComponentContext()))
66 : {
67 0 : }
68 :
69 : Reference< CSS::io::XInputStream >
70 0 : CSerializationAppXML::getInputStream()
71 : {
72 : // The pipes output is provided through it's
73 : // XOutputStream interface aspect
74 0 : return Reference< CSS::io::XInputStream >(m_xBuffer, UNO_QUERY);
75 : }
76 :
77 : void
78 0 : CSerializationAppXML::serialize_node(const Reference< XNode >& rNode)
79 : {
80 : try
81 : {
82 0 : Reference< XSAXSerializable > xSerializer( rNode, UNO_QUERY );
83 0 : if ( !xSerializer.is() )
84 : {
85 : // ensure we have a "real" node
86 0 : Reference< XNode > xNode = rNode;
87 0 : if ( xNode->getNodeType() == NodeType_DOCUMENT_NODE )
88 : {
89 0 : Reference< XDocument > const xDoc( xNode, UNO_QUERY_THROW );
90 0 : xNode.set( xDoc->getDocumentElement(), UNO_QUERY_THROW );
91 : }
92 0 : ENSURE_OR_RETURN_VOID( xNode->getNodeType() == NodeType_ELEMENT_NODE,
93 : "CSerializationAppXML::serialize_node: invalid node type!" );
94 :
95 : // create a new document
96 : Reference< XDocumentBuilder > const xDocBuilder(
97 0 : m_xFactory->createInstance( "com.sun.star.xml.dom.DocumentBuilder" ), UNO_QUERY_THROW );
98 0 : Reference< XDocument > const xDocument( xDocBuilder->newDocument(), UNO_SET_THROW );
99 :
100 : // copy the to-be-serialized node
101 0 : Reference< XNode > const xImportedNode( xDocument->importNode( xNode, true ), UNO_SET_THROW );
102 0 : xDocument->appendChild( xImportedNode );
103 :
104 : // ask the doc for the serializer
105 0 : xSerializer.set( xDocument, UNO_QUERY );
106 : }
107 :
108 0 : ENSURE_OR_RETURN_VOID( xSerializer.is(),
109 : "CSerializationAppXML::serialize_node: no serialization access to the node/document!" );
110 :
111 : // create a SAXWriter to take the serialization events, and connect it to our pipe
112 : Reference< XDocumentHandler > const xSaxWriter(
113 0 : m_xFactory->createInstance( "com.sun.star.xml.sax.Writer" ), UNO_QUERY_THROW );
114 0 : Reference< XActiveDataSource > const xDataSource( xSaxWriter, UNO_QUERY_THROW );
115 0 : xDataSource->setOutputStream( Reference< CSS::io::XOutputStream >( m_xBuffer, UNO_QUERY_THROW) );
116 :
117 : // do the serialization
118 0 : xSerializer->serialize( xSaxWriter, Sequence< StringPair >() );
119 : }
120 0 : catch( const Exception& )
121 : {
122 : DBG_UNHANDLED_EXCEPTION();
123 : }
124 : }
125 :
126 : void
127 0 : CSerializationAppXML::serialize()
128 : {
129 0 : if (!m_aFragment.is()) return;
130 :
131 0 : Reference< XNode > cur = m_aFragment->getFirstChild();
132 0 : while (cur.is())
133 : {
134 0 : serialize_node(cur);
135 0 : cur = cur->getNextSibling();
136 : }
137 0 : m_xBuffer->closeOutput();
138 : }
139 :
140 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|