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>
28 #include <rtl/ustrbuf.hxx>
29 #include <rtl/strbuf.hxx>
30 #include <comphelper/processfactory.hxx>
31 #include <comphelper/string.hxx>
35 #include "serialization_urlencoded.hxx"
37 using namespace css::uno
;
38 using namespace css::io
;
39 using namespace css::xml::xpath
;
40 using namespace css::xml::dom
;
42 CSerializationURLEncoded::CSerializationURLEncoded()
43 : m_aPipe(Pipe::create(comphelper::getProcessComponentContext()))
50 reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
52 mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
53 unreserved = alphanum | mark
55 bool CSerializationURLEncoded::is_unreserved(sal_Char c
)
57 if (comphelper::string::isalnumAscii(c
))
73 void CSerializationURLEncoded::encode_and_append(const OUString
& aString
, OStringBuffer
& aBuffer
)
75 OString utf8String
= OUStringToOString(aString
, RTL_TEXTENCODING_UTF8
);
76 const sal_uInt8
*pString
= reinterpret_cast< const sal_uInt8
* >( utf8String
.getStr() );
77 sal_Char tmpChar
[4]; tmpChar
[3] = 0;
83 if ( is_unreserved(*pString
) ) {
84 aBuffer
.append(*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
, 3, "%%%X", *pString
% 0x100);
94 aBuffer
.append(tmpChar
);
97 snprintf(tmpChar
, 3, "%%%X", *pString
% 0x100);
98 aBuffer
.append(tmpChar
);
99 while (*pString
>= 0x80) {
102 snprintf(tmpChar
, 3, "%%%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
= Reference
< XText
>(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
165 Reference
< XOutputStream
> out(m_aPipe
, UNO_QUERY
);
167 css::uno::Reference
< css::xml::dom::XNode
> cur
= m_aFragment
->getFirstChild();
171 cur
= cur
->getNextSibling();
173 m_aPipe
->closeOutput();
176 Reference
< XInputStream
> CSerializationURLEncoded::getInputStream()
178 return Reference
< XInputStream
>(m_aPipe
, UNO_QUERY
);
182 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */