PageRenderTime 26ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/forms/source/xforms/submission/serialization_app_xml.cxx

https://repo.or.cz/LibreOffice.git
C++ | 126 lines | 82 code | 17 blank | 27 comment | 6 complexity | d2155f0ede5bc8890e9c94f8675022a2 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause-No-Nuclear-License-2014, GPL-3.0, LGPL-2.1, LGPL-3.0, LGPL-2.0
  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. #include "serialization_app_xml.hxx"
  20. #include <com/sun/star/io/Pipe.hpp>
  21. #include <com/sun/star/xml/dom/DocumentBuilder.hpp>
  22. #include <com/sun/star/xml/dom/XNode.hpp>
  23. #include <com/sun/star/xml/dom/XDocument.hpp>
  24. #include <com/sun/star/xml/dom/NodeType.hpp>
  25. #include <com/sun/star/xml/sax/XSAXSerializable.hpp>
  26. #include <com/sun/star/xml/sax/Writer.hpp>
  27. #include <com/sun/star/beans/StringPair.hpp>
  28. #include <com/sun/star/xml/dom/XDocumentBuilder.hpp>
  29. #include <tools/diagnose_ex.h>
  30. #include <comphelper/processfactory.hxx>
  31. using ::com::sun::star::uno::Reference;
  32. using ::com::sun::star::uno::Exception;
  33. using ::com::sun::star::uno::Sequence;
  34. using ::com::sun::star::uno::UNO_QUERY;
  35. using ::com::sun::star::uno::UNO_QUERY_THROW;
  36. using ::com::sun::star::uno::UNO_SET_THROW;
  37. using ::com::sun::star::xml::dom::DocumentBuilder;
  38. using ::com::sun::star::xml::dom::XNode;
  39. using ::com::sun::star::xml::dom::XDocument;
  40. using ::com::sun::star::xml::sax::XSAXSerializable;
  41. using ::com::sun::star::beans::StringPair;
  42. using ::com::sun::star::xml::dom::NodeType_DOCUMENT_NODE;
  43. using ::com::sun::star::xml::dom::NodeType_ELEMENT_NODE;
  44. using ::com::sun::star::xml::dom::XDocumentBuilder;
  45. using ::com::sun::star::xml::sax::Writer;
  46. using ::com::sun::star::xml::sax::XDocumentHandler;
  47. CSerializationAppXML::CSerializationAppXML()
  48. : m_xBuffer(css::io::Pipe::create(comphelper::getProcessComponentContext()))
  49. {
  50. }
  51. Reference< css::io::XInputStream >
  52. CSerializationAppXML::getInputStream()
  53. {
  54. // The pipes output is provided through it's
  55. // XOutputStream interface aspect
  56. return m_xBuffer;
  57. }
  58. void
  59. CSerializationAppXML::serialize_node(const Reference< XNode >& rNode)
  60. {
  61. try
  62. {
  63. Reference< XSAXSerializable > xSerializer( rNode, UNO_QUERY );
  64. if ( !xSerializer.is() )
  65. {
  66. // ensure we have a "real" node
  67. Reference< XNode > xNode = rNode;
  68. if ( xNode->getNodeType() == NodeType_DOCUMENT_NODE )
  69. {
  70. Reference< XDocument > const xDoc( xNode, UNO_QUERY_THROW );
  71. xNode.set( xDoc->getDocumentElement(), UNO_QUERY_THROW );
  72. }
  73. ENSURE_OR_RETURN_VOID( xNode->getNodeType() == NodeType_ELEMENT_NODE,
  74. "CSerializationAppXML::serialize_node: invalid node type!" );
  75. // create a new document
  76. Reference< XDocumentBuilder > const xDocBuilder = DocumentBuilder::create( comphelper::getProcessComponentContext() );
  77. Reference< XDocument > const xDocument( xDocBuilder->newDocument(), UNO_SET_THROW );
  78. // copy the to-be-serialized node
  79. Reference< XNode > const xImportedNode( xDocument->importNode( xNode, true ), UNO_SET_THROW );
  80. xDocument->appendChild( xImportedNode );
  81. // ask the doc for the serializer
  82. xSerializer.set( xDocument, UNO_QUERY );
  83. }
  84. ENSURE_OR_RETURN_VOID( xSerializer.is(),
  85. "CSerializationAppXML::serialize_node: no serialization access to the node/document!" );
  86. // create a SAXWriter to take the serialization events, and connect it to our pipe
  87. Reference< css::xml::sax::XWriter > const xSaxWriter = Writer::create( comphelper::getProcessComponentContext() );
  88. xSaxWriter->setOutputStream( Reference< css::io::XOutputStream >( m_xBuffer, UNO_QUERY_THROW) );
  89. // do the serialization
  90. xSerializer->serialize( Reference< XDocumentHandler >(xSaxWriter, UNO_QUERY_THROW), Sequence< StringPair >() );
  91. }
  92. catch( const Exception& )
  93. {
  94. DBG_UNHANDLED_EXCEPTION("forms.xforms");
  95. }
  96. }
  97. void
  98. CSerializationAppXML::serialize()
  99. {
  100. if (!m_aFragment.is()) return;
  101. Reference< XNode > cur = m_aFragment->getFirstChild();
  102. while (cur.is())
  103. {
  104. serialize_node(cur);
  105. cur = cur->getNextSibling();
  106. }
  107. m_xBuffer->closeOutput();
  108. }
  109. /* vim:set shiftwidth=4 softtabstop=4 expandtab: */