LibreOffice Module forms (master) 1
serialization_urlencoded.cxx
Go to the documentation of this file.
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
20
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>
31
32#include <stdio.h>
33
35
36using namespace css::uno;
37using namespace css::io;
38using namespace css::xml::xpath;
39using namespace css::xml::dom;
40
43{
44}
45
46
47/*
48 rfc2396
49 reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
50 "$" | ","
51 mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
52 unreserved = alphanum | mark
53*/
55{
56 if (rtl::isAsciiAlphanumeric(static_cast<unsigned char>(c)))
57 return true;
58 switch (c) {
59 case '-':
60 case '_':
61 case '.':
62 case '!':
63 case '~':
64 case '*':
65 case '\'':
66 case '(':
67 case ')':
68 return true;
69 }
70 return false;
71}
73 std::u16string_view aString, OStringBuffer& aBuffer)
74{
75 OString utf8String = OUStringToOString(aString, RTL_TEXTENCODING_UTF8);
76 const sal_uInt8 *pString = reinterpret_cast< const sal_uInt8 * >( utf8String.getStr() );
77 char tmpChar[4];
78
79 while( *pString != 0)
80 {
81 if( *pString < 0x80 )
82 {
83 if ( is_unreserved(*pString) ) {
84 aBuffer.append(char(*pString));
85 } else if (*pString == 0x20) {
86 aBuffer.append('+');
87 } else if (*pString == 0x0d && *(pString+1) == 0x0a) {
88 aBuffer.append("%0D%0A");
89 pString++;
90 } else if (*pString == 0x0a) {
91 aBuffer.append("%0D%0A");
92 } else {
93 snprintf(tmpChar, 4, "%%%X", *pString % 0x100);
94 aBuffer.append(tmpChar);
95 }
96 } else {
97 snprintf(tmpChar, 4, "%%%X", *pString % 0x100);
98 aBuffer.append(tmpChar);
99 while (*pString >= 0x80) {
100 // continuation...
101 pString++;
102 snprintf(tmpChar, 4, "%%%X", *pString % 0x100);
103 aBuffer.append(tmpChar);
104 }
105 }
106 pString++;
107 }
108}
109
110void CSerializationURLEncoded::serialize_node(const Reference< XNode >& aNode)
111{
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)
115
116 // this node
117 Reference< XNodeList > aChildList = aNode->getChildNodes();
118 Reference< XNode > aChild;
119 // is this an element node?
120 if (aNode->getNodeType() == NodeType_ELEMENT_NODE)
121 {
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++)
127 {
128 aChild = aChildList->item(i);
129 if (aChild->getNodeType() == NodeType_TEXT_NODE)
130 {
131 aText.set(aChild, UNO_QUERY);
132 aValue.append(aText->getData());
133 }
134 }
135
136 // found anything?
137 if (!aValue.isEmpty())
138 {
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);
148 }
149 }
150
151 // element children...
152 for(sal_Int32 i=0; i < aChildList->getLength(); i++)
153 {
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);
158 }
159}
160
162{
163
164 // output stream to the pipe buffer
165
166 css::uno::Reference< css::xml::dom::XNode > cur = m_aFragment->getFirstChild();
167 while (cur.is())
168 {
169 serialize_node(cur);
170 cur = cur->getNextSibling();
171 }
172 m_aPipe->closeOutput();
173}
174
176{
177 return m_aPipe;
178}
179
180
181/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void serialize() override
start the serialization process
void serialize_node(const css::uno::Reference< css::xml::dom::XNode > &aNode)
static void encode_and_append(std::u16string_view aString, OStringBuffer &aBuffer)
css::uno::Reference< css::io::XPipe > m_aPipe
virtual css::uno::Reference< css::io::XInputStream > getInputStream() override
get the serialized bytes.
css::uno::Reference< css::xml::dom::XDocumentFragment > m_aFragment
OUString aName
std::unique_ptr< sal_Int32[]> pData
Reference< XComponentContext > getProcessComponentContext()
css::uno::Reference< css::deployment::XPackageRegistry > create(css::uno::Reference< css::deployment::XPackageRegistry > const &xRootRegistry, OUString const &context, OUString const &cachePath, css::uno::Reference< css::uno::XComponentContext > const &xComponentContext)
int i
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
unsigned char sal_uInt8
signed char sal_Int8
std::unique_ptr< char[]> aBuffer