summaryrefslogtreecommitdiff
path: root/xmlspec/XMLAttrs.cpp
blob: ec9f7bb10b96a708d535c5d7c0568d6be9011a44 (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
162
163
164
165
166
167
168
// standard C++ includes
#include <string>

// standard includes
#include <string.h>

// our includes
#include "XMLAttrs.h"
#include "XMLMisc.h"
#include "XMLSpec.h"

using namespace std;

XMLAttr::XMLAttr(const char* szName,
				const char* szValue)
	: XMLBase()
{
	if (szName)
		m_sName.assign(szName);
	if (szValue)
		m_sValue.assign(szValue);
}

XMLAttr::XMLAttr(const XMLAttr& rAttr)
	: XMLBase()
{
	m_sName.assign(rAttr.m_sName);
	m_sValue.assign(rAttr.m_sValue);
}

XMLAttr::~XMLAttr()
{
}

XMLAttr XMLAttr::operator=(XMLAttr attr)
{
	m_sName.assign(attr.m_sName);
	m_sValue.assign(attr.m_sValue);
}

XMLAttrs::XMLAttrs(const char** szAttrs)
	: XMLBase()
{
	for (int i = 0; szAttrs && szAttrs[i]; i += 2) {
		XMLAttr attr(szAttrs[i], szAttrs[i+1]);
		m_vAttrs.push_back(attr);
	}
}

XMLAttrs::XMLAttrs(const XMLAttrs& rAttrs)
	: XMLBase()
{
	m_vAttrs = rAttrs.m_vAttrs;
}

XMLAttrs::~XMLAttrs()
{
}

bool validateAttr(structValidAttrs& rAttr,
				  const char* szValue)
{
	switch (rAttr.m_nType) {
		case XATTRTYPE_STRING:
			return findStr(rAttr.m_szaMatches, szValue) != -1 ? true : false;
			break;
		case XATTRTYPE_INTEGER:
			return isInteger(szValue);
			break;
		case XATTRTYPE_BOOL:
			return isBool(szValue);
			break;
		case XATTRTYPE_DATE:
			return isDate(szValue);
			break;
		case XATTRTYPE_MAIL:
			return isEmail(szValue);
			break;
		case XATTRTYPE_NONE:
		default:
			return true;
			break;
	}
	return false;
}

static char* szaTypeDesc[] = { "string",
							   "integer",
							   "bool [Values: true|false]",
							   "date [Format: DDD MMM DD YYYY]",
							   "e-mail" };

bool XMLAttrs::validate(structValidAttrs* paValids,
						XMLBase* pError)
{
	// nothing found at present
	for (unsigned int i = 0; paValids[i].m_nValue != XATTR_END; i++)
		paValids[i].m_bFound = false;

	// test everything we have
	for (unsigned int i = 0; i < num(); i++) {
		bool bInvalid = true;
		for (unsigned int j = 0; paValids[j].m_nValue != XATTR_END; j++) {
			if (strcasecmp(paValids[j].m_szName, get(i).getName()) == 0) {
				paValids[j].m_bFound = true;
				if (!validateAttr(paValids[j], get(i).asString())) {
					char szTmp[1024];
					sprintf(szTmp, "Attribute value '%s' is not a valid %s.",
									get(i).asString(), szaTypeDesc[paValids[j].m_nType]);
					pError->setError(szTmp);
					return false;
				}
				else
					bInvalid = false;
				break;
			}
		}
		if (bInvalid) {
			char szTmp[1024];
			sprintf(szTmp, "Unknown attribute '%s'", get(i).getName());
			pError->setWarning(szTmp);
		}
	}

	// see if we have mandator tags that are not there
	for (unsigned int i = 0; paValids[i].m_nValue != XATTR_END; i++) {
		if (paValids[i].m_bMandatory && !paValids[i].m_bFound) {
			char szTmp[1024];
			sprintf(szTmp, "Mandatory attribute '%s' not found",
							paValids[i].m_szName);
			pError->setError(szTmp);
			return false;
		}
	}

	// if we got this far, everything is ok
	return true;
}

unsigned int getPos(const char* szName,
					XMLAttrs* pAttrs)
{
	if (szName) {
		for (unsigned int i = 0; i < pAttrs->num(); i++) {
			if (strcasecmp(szName, pAttrs->get(i).getName()) == 0)
				return i;
		}
	}
	return XATTR_END;
}

const char* XMLAttrs::asString(const char* szName)
{
	unsigned int nPos = getPos(szName, this);
	return (nPos == XATTR_END) ? NULL : get(nPos).asString();
}

unsigned int XMLAttrs::asInteger(const char* szName)
{
	unsigned int nPos = getPos(szName, this);
	return (nPos == XATTR_END) ? 0 : get(nPos).asInteger();
}

bool XMLAttrs::asBool(const char* szName)
{
	unsigned int nPos = getPos(szName, this);
	return (nPos == XATTR_END) ? true : get(nPos).asBool();
}