1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: serialization_app_xml.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_forms.hxx"
34 #include "serialization.hxx"
35 #include "serialization_app_xml.hxx"
37 #include <unotools/processfactory.hxx>
38 #include <com/sun/star/xml/dom/XNode.hpp>
39 #include <com/sun/star/xml/dom/XDocument.hpp>
40 #include <com/sun/star/xml/dom/XNodeList.hpp>
41 #include <com/sun/star/xml/dom/NodeType.hpp>
42 #include <com/sun/star/lang/XUnoTunnel.hpp>
43 #include <com/sun/star/xml/xpath/XPathObjectType.hpp>
45 #include <libxml/tree.h>
47 CSerializationAppXML::CSerializationAppXML()
48 : m_aFactory(utl::getProcessServiceFactory())
49 , m_aPipe(CSS::uno::Reference
< CSS::io::XOutputStream
> (m_aFactory
->createInstance(
50 rtl::OUString::createFromAscii("com.sun.star.io.Pipe")), CSS::uno::UNO_QUERY
))
52 OSL_ENSURE(m_aPipe
.is(), "cannot create Pipe");
55 CSS::uno::Reference
< CSS::io::XInputStream
>
56 CSerializationAppXML::getInputStream()
58 // The pipes output is provided through it's
59 // XOutputStream interface aspect
60 return CSS::uno::Reference
< CSS::io::XInputStream
>(m_aPipe
, CSS::uno::UNO_QUERY
);
64 CSerializationAppXML::serialize_node(const CSS::uno::Reference
< CSS::xml::dom::XNode
>& rNode
)
66 CSS::uno::Reference
< CSS::xml::dom::XNode
> aNode
= rNode
;
67 if (aNode
->getNodeType() == CSS::xml::dom::NodeType_DOCUMENT_NODE
)
69 CSS::uno::Reference
< CSS::xml::dom::XDocument
> aDoc(rNode
, CSS::uno::UNO_QUERY_THROW
);
70 aNode
= CSS::uno::Reference
< CSS::xml::dom::XNode
>(aDoc
->getDocumentElement(), CSS::uno::UNO_QUERY_THROW
);
72 if (aNode
->getNodeType() != CSS::xml::dom::NodeType_ELEMENT_NODE
)
75 // clone the node to a new document and serialize that document
76 CSS::uno::Reference
< CSS::lang::XUnoTunnel
> aTunnel(aNode
, CSS::uno::UNO_QUERY
);
79 xmlNodePtr aNodePtr
= reinterpret_cast< xmlNodePtr
>( aTunnel
->getSomething(CSS::uno::Sequence
< sal_Int8
>()) );
80 xmlDocPtr aDocPtr
= xmlNewDoc((xmlChar
*)"1.0");
81 xmlNodePtr aDocNodePtr
= xmlDocCopyNode(aNodePtr
, aDocPtr
, 1);
82 if (aDocNodePtr
!= NULL
) {
83 xmlAddChild((xmlNodePtr
)aDocPtr
, aDocNodePtr
);
84 xmlChar
*buffer
= NULL
;
86 xmlDocDumpMemory(aDocPtr
, &buffer
, (int*)&size
);
88 // write the xml into the pipe through it's XOutputStream interface
89 m_aPipe
->writeBytes(CSS::uno::Sequence
< sal_Int8
>((sal_Int8
*)buffer
, size
));
94 // can't get tunnel to native backend
95 // logic for generic implementation could be implemented here...
96 OSL_ENSURE(sal_False
, "unkown dom implementation, cannot serialize");
103 CSerializationAppXML::serialize_nodeset()
105 CSS::uno::Reference< CSS::xml::dom::XNodeList > aNodeList = m_aXPathObject->getNodeList();
106 for (sal_Int32 i=0; i<aNodeList->getLength(); i++)
107 serialize_node(aNodeList->item(i));
108 m_aPipe->closeOutput();
113 CSerializationAppXML::serialize()
115 if (!m_aFragment
.is()) return;
117 CSS::uno::Reference
< CSS::xml::dom::XNode
> cur
= m_aFragment
->getFirstChild();
121 cur
= cur
->getNextSibling();
123 m_aPipe
->closeOutput();