2010-10-12 15:57:08 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-11-21 22:06:52 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following license notice:
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed
|
|
|
|
* with this work for additional information regarding copyright
|
|
|
|
* ownership. The ASF licenses this file to you under the Apache
|
|
|
|
* License, Version 2.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
|
|
|
*/
|
2004-11-16 09:58:50 +00:00
|
|
|
|
2006-09-16 23:07:16 +00:00
|
|
|
|
2004-11-16 09:58:50 +00:00
|
|
|
#include "serialization.hxx"
|
|
|
|
#include "serialization_app_xml.hxx"
|
|
|
|
|
2012-08-28 13:50:47 +02:00
|
|
|
#include <com/sun/star/io/Pipe.hpp>
|
2013-01-30 09:41:42 +02:00
|
|
|
#include <com/sun/star/xml/dom/DocumentBuilder.hpp>
|
2004-11-16 09:58:50 +00:00
|
|
|
#include <com/sun/star/xml/dom/XNode.hpp>
|
|
|
|
#include <com/sun/star/xml/dom/XDocument.hpp>
|
|
|
|
#include <com/sun/star/xml/dom/XNodeList.hpp>
|
|
|
|
#include <com/sun/star/xml/dom/NodeType.hpp>
|
|
|
|
#include <com/sun/star/lang/XUnoTunnel.hpp>
|
|
|
|
#include <com/sun/star/xml/xpath/XPathObjectType.hpp>
|
2011-03-24 12:27:31 +01:00
|
|
|
#include <com/sun/star/xml/sax/XSAXSerializable.hpp>
|
2013-01-30 09:41:42 +02:00
|
|
|
#include <com/sun/star/xml/sax/Writer.hpp>
|
2011-03-24 12:27:31 +01:00
|
|
|
#include <com/sun/star/beans/StringPair.hpp>
|
|
|
|
#include <com/sun/star/io/XActiveDataSource.hpp>
|
|
|
|
#include <com/sun/star/xml/dom/XDocumentBuilder.hpp>
|
2004-11-16 09:58:50 +00:00
|
|
|
|
2011-03-24 12:26:59 +01:00
|
|
|
#include <tools/diagnose_ex.h>
|
|
|
|
#include <comphelper/processfactory.hxx>
|
|
|
|
|
|
|
|
#include <limits>
|
2015-09-17 17:10:47 +01:00
|
|
|
#include <memory>
|
2011-03-24 12:26:59 +01:00
|
|
|
|
2011-03-24 12:27:31 +01:00
|
|
|
using ::com::sun::star::uno::Reference;
|
|
|
|
using ::com::sun::star::uno::Exception;
|
|
|
|
using ::com::sun::star::uno::Sequence;
|
|
|
|
using ::com::sun::star::uno::RuntimeException;
|
|
|
|
using ::com::sun::star::uno::UNO_QUERY;
|
|
|
|
using ::com::sun::star::uno::UNO_QUERY_THROW;
|
|
|
|
using ::com::sun::star::uno::UNO_SET_THROW;
|
2013-01-30 09:41:42 +02:00
|
|
|
using ::com::sun::star::xml::dom::DocumentBuilder;
|
2011-03-24 12:27:31 +01:00
|
|
|
using ::com::sun::star::xml::dom::XNode;
|
|
|
|
using ::com::sun::star::xml::dom::XDocument;
|
|
|
|
using ::com::sun::star::xml::sax::XSAXSerializable;
|
|
|
|
using ::com::sun::star::beans::StringPair;
|
|
|
|
using ::com::sun::star::io::XActiveDataSource;
|
|
|
|
using ::com::sun::star::xml::dom::NodeType_DOCUMENT_NODE;
|
|
|
|
using ::com::sun::star::xml::dom::NodeType_ELEMENT_NODE;
|
|
|
|
using ::com::sun::star::xml::dom::XDocumentBuilder;
|
2013-01-30 09:41:42 +02:00
|
|
|
using ::com::sun::star::xml::sax::Writer;
|
2011-03-24 12:27:31 +01:00
|
|
|
using ::com::sun::star::xml::sax::XDocumentHandler;
|
|
|
|
|
2004-11-16 09:58:50 +00:00
|
|
|
CSerializationAppXML::CSerializationAppXML()
|
2014-08-21 08:15:37 +02:00
|
|
|
: m_xBuffer(css::io::Pipe::create(comphelper::getProcessComponentContext()))
|
2011-03-24 12:27:31 +01:00
|
|
|
{
|
|
|
|
}
|
2004-11-16 09:58:50 +00:00
|
|
|
|
2014-08-21 08:15:37 +02:00
|
|
|
Reference< css::io::XInputStream >
|
2004-11-16 09:58:50 +00:00
|
|
|
CSerializationAppXML::getInputStream()
|
|
|
|
{
|
|
|
|
// The pipes output is provided through it's
|
|
|
|
// XOutputStream interface aspect
|
2014-08-21 08:15:37 +02:00
|
|
|
return Reference< css::io::XInputStream >(m_xBuffer, UNO_QUERY);
|
2004-11-16 09:58:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-03-24 12:27:31 +01:00
|
|
|
CSerializationAppXML::serialize_node(const Reference< XNode >& rNode)
|
2004-11-16 09:58:50 +00:00
|
|
|
{
|
2011-03-24 12:27:31 +01:00
|
|
|
try
|
2004-11-16 09:58:50 +00:00
|
|
|
{
|
2011-03-24 12:27:31 +01:00
|
|
|
Reference< XSAXSerializable > xSerializer( rNode, UNO_QUERY );
|
|
|
|
if ( !xSerializer.is() )
|
|
|
|
{
|
|
|
|
// ensure we have a "real" node
|
|
|
|
Reference< XNode > xNode = rNode;
|
|
|
|
if ( xNode->getNodeType() == NodeType_DOCUMENT_NODE )
|
|
|
|
{
|
|
|
|
Reference< XDocument > const xDoc( xNode, UNO_QUERY_THROW );
|
|
|
|
xNode.set( xDoc->getDocumentElement(), UNO_QUERY_THROW );
|
|
|
|
}
|
|
|
|
ENSURE_OR_RETURN_VOID( xNode->getNodeType() == NodeType_ELEMENT_NODE,
|
|
|
|
"CSerializationAppXML::serialize_node: invalid node type!" );
|
|
|
|
|
|
|
|
// create a new document
|
2013-01-30 09:41:42 +02:00
|
|
|
Reference< XDocumentBuilder > const xDocBuilder = DocumentBuilder::create( comphelper::getProcessComponentContext() );
|
2011-03-24 12:27:31 +01:00
|
|
|
Reference< XDocument > const xDocument( xDocBuilder->newDocument(), UNO_SET_THROW );
|
|
|
|
|
|
|
|
// copy the to-be-serialized node
|
|
|
|
Reference< XNode > const xImportedNode( xDocument->importNode( xNode, true ), UNO_SET_THROW );
|
|
|
|
xDocument->appendChild( xImportedNode );
|
|
|
|
|
|
|
|
// ask the doc for the serializer
|
|
|
|
xSerializer.set( xDocument, UNO_QUERY );
|
|
|
|
}
|
2011-03-24 12:26:59 +01:00
|
|
|
|
2011-03-24 12:27:31 +01:00
|
|
|
ENSURE_OR_RETURN_VOID( xSerializer.is(),
|
|
|
|
"CSerializationAppXML::serialize_node: no serialization access to the node/document!" );
|
2011-03-24 12:26:59 +01:00
|
|
|
|
2011-03-24 12:27:31 +01:00
|
|
|
// create a SAXWriter to take the serialization events, and connect it to our pipe
|
2014-08-21 08:15:37 +02:00
|
|
|
Reference< css::xml::sax::XWriter > const xSaxWriter = Writer::create( comphelper::getProcessComponentContext() );
|
|
|
|
xSaxWriter->setOutputStream( Reference< css::io::XOutputStream >( m_xBuffer, UNO_QUERY_THROW) );
|
2011-03-24 12:26:59 +01:00
|
|
|
|
2011-03-24 12:27:31 +01:00
|
|
|
// do the serialization
|
2013-01-30 09:41:42 +02:00
|
|
|
xSerializer->serialize( Reference< XDocumentHandler >(xSaxWriter, UNO_QUERY_THROW), Sequence< StringPair >() );
|
2011-03-24 12:27:31 +01:00
|
|
|
}
|
|
|
|
catch( const Exception& )
|
2004-11-16 09:58:50 +00:00
|
|
|
{
|
2011-03-24 12:27:31 +01:00
|
|
|
DBG_UNHANDLED_EXCEPTION();
|
2004-11-16 09:58:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CSerializationAppXML::serialize()
|
|
|
|
{
|
|
|
|
if (!m_aFragment.is()) return;
|
|
|
|
|
2011-03-24 12:27:31 +01:00
|
|
|
Reference< XNode > cur = m_aFragment->getFirstChild();
|
2004-11-16 09:58:50 +00:00
|
|
|
while (cur.is())
|
|
|
|
{
|
|
|
|
serialize_node(cur);
|
|
|
|
cur = cur->getNextSibling();
|
|
|
|
}
|
2011-03-24 12:27:31 +01:00
|
|
|
m_xBuffer->closeOutput();
|
2004-11-16 09:58:50 +00:00
|
|
|
}
|
2010-10-12 15:57:08 +02:00
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|