1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <com/sun/star/io/Pipe.hpp>
22 #include <com/sun/star/xml/xpath/XPathObjectType.hpp>
23 #include <com/sun/star/xml/dom/XNode.hpp>
24 #include <com/sun/star/xml/dom/XText.hpp>
25 #include <com/sun/star/xml/dom/XNodeList.hpp>
26 #include <com/sun/star/xml/dom/NodeType.hpp>
27 #include <rtl/character.hxx>
28 #include <rtl/ustrbuf.hxx>
29 #include <rtl/strbuf.hxx>
30 #include <comphelper/processfactory.hxx>
34 #include "serialization_urlencoded.hxx"
36 using namespace css::uno
;
37 using namespace css::io
;
38 using namespace css::xml::xpath
;
39 using namespace css::xml::dom
;
41 CSerializationURLEncoded::CSerializationURLEncoded()
42 : m_aPipe(Pipe::create(comphelper::getProcessComponentContext()))
49 reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
51 mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
52 unreserved = alphanum | mark
54 bool CSerializationURLEncoded::is_unreserved(char c
)
56 if (rtl::isAsciiAlphanumeric(static_cast<unsigned char>(c
)))
72 void CSerializationURLEncoded::encode_and_append(
73 std::u16string_view aString
, OStringBuffer
& aBuffer
)
75 OString utf8String
= OUStringToOString(aString
, RTL_TEXTENCODING_UTF8
);
76 const sal_uInt8
*pString
= reinterpret_cast< const sal_uInt8
* >( utf8String
.getStr() );
83 if ( is_unreserved(*pString
) ) {
84 aBuffer
.append(char(*pString
));
85 } else if (*pString
== 0x20) {
87 } else if (*pString
== 0x0d && *(pString
+1) == 0x0a) {
88 aBuffer
.append("%0D%0A");
90 } else if (*pString
== 0x0a) {
91 aBuffer
.append("%0D%0A");
93 snprintf(tmpChar
, 4, "%%%X", *pString
% 0x100);
94 aBuffer
.append(tmpChar
);
97 snprintf(tmpChar
, 4, "%%%X", *pString
% 0x100);
98 aBuffer
.append(tmpChar
);
99 while (*pString
>= 0x80) {
102 snprintf(tmpChar
, 4, "%%%X", *pString
% 0x100);
103 aBuffer
.append(tmpChar
);
110 void CSerializationURLEncoded::serialize_node(const Reference
< XNode
>& aNode
)
112 // serialize recursive
113 // every element node E that has a text child T will be serialized in document order
114 // <E1>T1<E2>T2</E2></E1><E3>T3</E3> -> E1=T2&E2=T2&E3=T3 (En := local name)
117 Reference
< XNodeList
> aChildList
= aNode
->getChildNodes();
118 Reference
< XNode
> aChild
;
119 // is this an element node?
120 if (aNode
->getNodeType() == NodeType_ELEMENT_NODE
)
122 OUString aName
= aNode
->getNodeName();
123 // find any text children
124 OUStringBuffer aValue
;
125 Reference
< XText
> aText
;
126 for(sal_Int32 i
=0; i
< aChildList
->getLength(); i
++)
128 aChild
= aChildList
->item(i
);
129 if (aChild
->getNodeType() == NodeType_TEXT_NODE
)
131 aText
.set(aChild
, UNO_QUERY
);
132 aValue
.append(aText
->getData());
137 if (!aValue
.isEmpty())
139 OUString aUnencValue
= aValue
.makeStringAndClear();
140 OStringBuffer aEncodedBuffer
;
141 encode_and_append(aName
, aEncodedBuffer
);
142 aEncodedBuffer
.append("=");
143 encode_and_append(aUnencValue
, aEncodedBuffer
);
144 aEncodedBuffer
.append("&");
145 sal_Int8
const *pData
= reinterpret_cast<sal_Int8
const *>(aEncodedBuffer
.getStr());
146 Sequence
< sal_Int8
> sData(pData
, aEncodedBuffer
.getLength());
147 m_aPipe
->writeBytes(sData
);
151 // element children...
152 for(sal_Int32 i
=0; i
< aChildList
->getLength(); i
++)
154 aChild
= aChildList
->item(i
);
155 // if this is an element node, it might be a candidate for serialization
156 if (aChild
.is() && aChild
->getNodeType() == NodeType_ELEMENT_NODE
)
157 serialize_node(aChild
);
161 void CSerializationURLEncoded::serialize()
164 // output stream to the pipe buffer
166 css::uno::Reference
< css::xml::dom::XNode
> cur
= m_aFragment
->getFirstChild();
170 cur
= cur
->getNextSibling();
172 m_aPipe
->closeOutput();
175 Reference
< XInputStream
> CSerializationURLEncoded::getInputStream()
181 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */