1*24acc546SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*24acc546SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*24acc546SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*24acc546SAndrew Rist * distributed with this work for additional information
6*24acc546SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*24acc546SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*24acc546SAndrew Rist * "License"); you may not use this file except in compliance
9*24acc546SAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
11*24acc546SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
13*24acc546SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*24acc546SAndrew Rist * software distributed under the License is distributed on an
15*24acc546SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*24acc546SAndrew Rist * KIND, either express or implied. See the License for the
17*24acc546SAndrew Rist * specific language governing permissions and limitations
18*24acc546SAndrew Rist * under the License.
19cdf0e10cSrcweir *
20*24acc546SAndrew Rist *************************************************************/
21*24acc546SAndrew Rist
22*24acc546SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_forms.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include "serialization.hxx"
28cdf0e10cSrcweir #include "serialization_app_xml.hxx"
29cdf0e10cSrcweir
30cdf0e10cSrcweir /** === begin UNO includes === **/
31cdf0e10cSrcweir #include <com/sun/star/xml/dom/XNode.hpp>
32cdf0e10cSrcweir #include <com/sun/star/xml/dom/XDocument.hpp>
33cdf0e10cSrcweir #include <com/sun/star/xml/dom/XNodeList.hpp>
34cdf0e10cSrcweir #include <com/sun/star/xml/dom/NodeType.hpp>
35cdf0e10cSrcweir #include <com/sun/star/lang/XUnoTunnel.hpp>
36cdf0e10cSrcweir #include <com/sun/star/xml/xpath/XPathObjectType.hpp>
37cdf0e10cSrcweir #include <com/sun/star/xml/sax/XSAXSerializable.hpp>
38cdf0e10cSrcweir #include <com/sun/star/beans/StringPair.hpp>
39cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSource.hpp>
40cdf0e10cSrcweir #include <com/sun/star/xml/dom/XDocumentBuilder.hpp>
41cdf0e10cSrcweir /** === end UNO includes === **/
42cdf0e10cSrcweir
43cdf0e10cSrcweir #include <tools/diagnose_ex.h>
44cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
45cdf0e10cSrcweir
46cdf0e10cSrcweir #include <boost/scoped_ptr.hpp>
47cdf0e10cSrcweir #include <limits>
48cdf0e10cSrcweir
49cdf0e10cSrcweir /** === begin UNO using === **/
50cdf0e10cSrcweir using ::com::sun::star::uno::Reference;
51cdf0e10cSrcweir using ::com::sun::star::uno::Exception;
52cdf0e10cSrcweir using ::com::sun::star::uno::Sequence;
53cdf0e10cSrcweir using ::com::sun::star::uno::RuntimeException;
54cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY;
55cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY_THROW;
56cdf0e10cSrcweir using ::com::sun::star::uno::UNO_SET_THROW;
57cdf0e10cSrcweir using ::com::sun::star::xml::dom::XNode;
58cdf0e10cSrcweir using ::com::sun::star::xml::dom::XDocument;
59cdf0e10cSrcweir using ::com::sun::star::xml::sax::XSAXSerializable;
60cdf0e10cSrcweir using ::com::sun::star::beans::StringPair;
61cdf0e10cSrcweir using ::com::sun::star::io::XActiveDataSource;
62cdf0e10cSrcweir using ::com::sun::star::xml::dom::NodeType_DOCUMENT_NODE;
63cdf0e10cSrcweir using ::com::sun::star::xml::dom::NodeType_ELEMENT_NODE;
64cdf0e10cSrcweir using ::com::sun::star::xml::dom::XDocumentBuilder;
65cdf0e10cSrcweir using ::com::sun::star::xml::sax::XDocumentHandler;
66cdf0e10cSrcweir /** === end UNO using === **/
67cdf0e10cSrcweir
CSerializationAppXML()68cdf0e10cSrcweir CSerializationAppXML::CSerializationAppXML()
69cdf0e10cSrcweir :m_aContext( ::comphelper::getProcessServiceFactory() )
70cdf0e10cSrcweir {
71cdf0e10cSrcweir m_aContext.createComponent( "com.sun.star.io.Pipe", m_xBuffer );
72cdf0e10cSrcweir }
73cdf0e10cSrcweir
74cdf0e10cSrcweir Reference< CSS::io::XInputStream >
getInputStream()75cdf0e10cSrcweir CSerializationAppXML::getInputStream()
76cdf0e10cSrcweir {
77cdf0e10cSrcweir // The pipes output is provided through it's
78cdf0e10cSrcweir // XOutputStream interface aspect
79cdf0e10cSrcweir return Reference< CSS::io::XInputStream >(m_xBuffer, UNO_QUERY);
80cdf0e10cSrcweir }
81cdf0e10cSrcweir
82cdf0e10cSrcweir void
serialize_node(const Reference<XNode> & rNode)83cdf0e10cSrcweir CSerializationAppXML::serialize_node(const Reference< XNode >& rNode)
84cdf0e10cSrcweir {
85cdf0e10cSrcweir try
86cdf0e10cSrcweir {
87cdf0e10cSrcweir Reference< XSAXSerializable > xSerializer( rNode, UNO_QUERY );
88cdf0e10cSrcweir if ( !xSerializer.is() )
89cdf0e10cSrcweir {
90cdf0e10cSrcweir // ensure we have a "real" node
91cdf0e10cSrcweir Reference< XNode > xNode = rNode;
92cdf0e10cSrcweir if ( xNode->getNodeType() == NodeType_DOCUMENT_NODE )
93cdf0e10cSrcweir {
94cdf0e10cSrcweir Reference< XDocument > const xDoc( xNode, UNO_QUERY_THROW );
95cdf0e10cSrcweir xNode.set( xDoc->getDocumentElement(), UNO_QUERY_THROW );
96cdf0e10cSrcweir }
97cdf0e10cSrcweir ENSURE_OR_RETURN_VOID( xNode->getNodeType() == NodeType_ELEMENT_NODE,
98cdf0e10cSrcweir "CSerializationAppXML::serialize_node: invalid node type!" );
99cdf0e10cSrcweir
100cdf0e10cSrcweir // create a new document
101cdf0e10cSrcweir Reference< XDocumentBuilder > const xDocBuilder(
102cdf0e10cSrcweir m_aContext.createComponent( "com.sun.star.xml.dom.DocumentBuilder" ), UNO_QUERY_THROW );
103cdf0e10cSrcweir Reference< XDocument > const xDocument( xDocBuilder->newDocument(), UNO_SET_THROW );
104cdf0e10cSrcweir
105cdf0e10cSrcweir // copy the to-be-serialized node
106cdf0e10cSrcweir Reference< XNode > const xImportedNode( xDocument->importNode( xNode, true ), UNO_SET_THROW );
107cdf0e10cSrcweir xDocument->appendChild( xImportedNode );
108cdf0e10cSrcweir
109cdf0e10cSrcweir // ask the doc for the serializer
110cdf0e10cSrcweir xSerializer.set( xDocument, UNO_QUERY );
111cdf0e10cSrcweir }
112cdf0e10cSrcweir
113cdf0e10cSrcweir ENSURE_OR_RETURN_VOID( xSerializer.is(),
114cdf0e10cSrcweir "CSerializationAppXML::serialize_node: no serialization access to the node/document!" );
115cdf0e10cSrcweir
116cdf0e10cSrcweir // create a SAXWriter to take the serialization events, and connect it to our pipe
117cdf0e10cSrcweir Reference< XDocumentHandler > const xSaxWriter(
118cdf0e10cSrcweir m_aContext.createComponent( "com.sun.star.xml.sax.Writer" ), UNO_QUERY_THROW );
119cdf0e10cSrcweir Reference< XActiveDataSource > const xDataSource( xSaxWriter, UNO_QUERY_THROW );
120cdf0e10cSrcweir xDataSource->setOutputStream( m_xBuffer );
121cdf0e10cSrcweir
122cdf0e10cSrcweir // do the serialization
123cdf0e10cSrcweir xSerializer->serialize( xSaxWriter, Sequence< StringPair >() );
124cdf0e10cSrcweir }
125cdf0e10cSrcweir catch( const Exception& )
126cdf0e10cSrcweir {
127cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION();
128cdf0e10cSrcweir }
129cdf0e10cSrcweir }
130cdf0e10cSrcweir
131cdf0e10cSrcweir void
serialize()132cdf0e10cSrcweir CSerializationAppXML::serialize()
133cdf0e10cSrcweir {
134cdf0e10cSrcweir if (!m_aFragment.is()) return;
135cdf0e10cSrcweir
136cdf0e10cSrcweir Reference< XNode > cur = m_aFragment->getFirstChild();
137cdf0e10cSrcweir while (cur.is())
138cdf0e10cSrcweir {
139cdf0e10cSrcweir serialize_node(cur);
140cdf0e10cSrcweir cur = cur->getNextSibling();
141cdf0e10cSrcweir }
142cdf0e10cSrcweir m_xBuffer->closeOutput();
143cdf0e10cSrcweir }
144