summaryrefslogtreecommitdiff
path: root/orkbasecxx/serializers/UrlSerializer.cpp
blob: 5718e6cdc5eff98e0605a1ff585c046b004ff58e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * Oreka -- A media capture and retrieval platform
 * 
 * Copyright (C) 2005, orecx LLC
 *
 * http://www.orecx.com
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License.
 * Please refer to http://www.gnu.org/copyleft/gpl.html
 *
 */

#include "ace/OS_NS_ctype.h"
#include "Utils.h"
#include "UrlSerializer.h"


void UrlSerializer::AddString(const char* key, CStdString& value)
{
	CStdString pair;
	CStdString escapedValue;
	EscapeUrl(value, escapedValue);
	pair.Format("%s=%s&", key, (PCSTR)escapedValue);
	m_output += pair;
}

CStdString UrlSerializer::Serialize()
{
	m_deSerialize = false;		// Set Serialize mode
	m_object->Define(this);
	return m_output;
}

typedef enum {UrlStartState, UrlKeyState, UrlValueState, UrlErrorState} UrlState;

void UrlSerializer::DeSerialize(CStdString& input)
{
	// read string and extract values into map
	UrlState state = UrlStartState;
	CStdString key;
	CStdString value;
	CStdString errorDescription;

	input.Trim();
	input.ToLower();

	for(int i=0; i<input.length() && state!= UrlErrorState; i++)
	{
		TCHAR character = input[i];

		switch(state)
		{
		case UrlStartState:
			if(character == '&')
			{
				;	// ignore ampersands
			}
			else if ( ACE_OS::ace_isalnum(character) )
			{
				state = UrlKeyState;
				key = character;
			}
			else
			{
				state = UrlErrorState;
				errorDescription = "Cannot find key start, keys must be alphanum";
			}
			break;
		case UrlKeyState:
			if( ACE_OS::ace_isalnum(character) )
			{
				key += character;
			}
			else if (character == '=')
			{
				state = UrlValueState;
				value.Empty();
			}
			else
			{
				state = UrlErrorState;
				errorDescription = "Invalid key character, keys must be alphanum";
			}
			break;
		case UrlValueState:
			if( character == '=')
			{
				state = UrlErrorState;
				errorDescription = "Value followed by = sign, value should always be followed by space sign";				
			}
			else if (character == '&')
			{
				state = UrlStartState;
			}
			else
			{
				value += character;
			}
			break;
		default:
			state = UrlErrorState;
			errorDescription = "Non-existing state";
		}	// switch(state)

		if ( (state == UrlStartState) || (i == (input.length()-1)) )
		{
			if (!key.IsEmpty())
			{
				// Url unescape
				CStdString unescapedValue;
				UnEscapeUrl(value, unescapedValue);

				// Add pair to key-value map
				m_map.insert(std::make_pair(key, unescapedValue));
				key.Empty();
				value.Empty();
			}
		}
	}	// for(int i=0; i<input.length() && state!= UrlErrorState; i++)

	m_deSerialize = true;		// Set DeSerialize mode
	m_object->Define(this);
	m_object->Validate();
}

// Escape the space, colon and percent characters for serializing to Key-Value-Pair text
void UrlSerializer::EscapeUrl(CStdString& in, CStdString& out)
{
	//####### fixme, need unescaping
	out = in;
}

// Unescape the space, colon and percent characters for serializing to Key-Value-Pair text
void UrlSerializer::UnEscapeUrl(CStdString& in, CStdString& out)
{
	// ###### fixme, need escaping
	out = in;
}

CStdString UrlSerializer::FindClass(CStdString& input)
{
	CStdString result;
	int equalsPostion = input.Find('=');
	if (equalsPostion != -1)
	{
		int ampersandPostion = input.Find('&');
		if (ampersandPostion > equalsPostion)
		{
			// there is at least one parameter
			result = input.Mid(equalsPostion+1, ampersandPostion-equalsPostion-1);
		}
		else
		{
			// there is no parameter
			result = input.Mid(equalsPostion+1, input.GetLength() - equalsPostion - 1);
		}
	}
	result.ToLower();
	return result;
}