1/*
2 * Copyright 2008, Haiku.
3 * Distributed under the terms of the MIT license.
4 *
5 * Authors:
6 *		Michael Pfeiffer <laplace@users.sourceforge.net>
7 */
8
9#include "Statement.h"
10
11#include <stdio.h>
12
13
14Statement::Statement()
15	: fType(kUnknown)
16	, fKeyword(NULL)
17	, fOption(NULL)
18	, fValue(NULL)
19	, fChildren(NULL)
20{
21}
22
23Statement::~Statement()
24{
25	delete fKeyword;
26	delete fOption;
27	delete fValue;
28	delete fChildren;
29}
30
31void Statement::SetType(Type type)
32{
33	fType = type;
34}
35
36Statement::Type Statement::GetType()
37{
38	return fType;
39}
40
41void Statement::SetKeyword(BString* keyword)
42{
43	fKeyword = keyword;
44}
45
46BString* Statement::GetKeyword()
47{
48	return fKeyword;
49}
50
51void Statement::SetOption(Value* option)
52{
53	fOption = option;
54}
55
56Value* Statement::GetOption()
57{
58	return fOption;
59}
60
61
62void Statement::SetValue(Value* value)
63{
64	fValue = value;
65}
66
67Value* Statement::GetValue()
68{
69	return fValue;
70}
71
72StatementList* Statement::GetChildren()
73{
74	return fChildren;
75}
76
77void Statement::AddChild(Statement* statement)
78{
79	if (fChildren == NULL) {
80		fChildren = new StatementList(true);
81	}
82	fChildren->Add(statement);
83}
84
85const char* Statement::GetKeywordString()
86{
87	if (fKeyword != NULL) {
88		return fKeyword->String();
89	}
90	return NULL;
91}
92
93const char* Statement::GetOptionString()
94{
95	Value* option = GetOption();
96	if (option != NULL) {
97		return option->GetValueString();
98	}
99	return NULL;
100}
101
102const char* Statement::GetTranslationString()
103{
104	Value* option = GetOption();
105	if (option != NULL) {
106		return option->GetTranslationString();
107	}
108	return NULL;
109}
110
111const char* Statement::GetValueString()
112{
113	Value* value = GetValue();
114	if (value != NULL) {
115		return value->GetValueString();
116	}
117	return NULL;
118}
119
120const char* Statement::GetValueTranslationString()
121{
122	Value* value = GetValue();
123	if (value != NULL) {
124		return value->GetTranslationString();
125	}
126	return NULL;
127}
128
129
130const char* Statement::ElementForType() {
131	switch (fType) {
132		case kDefault: return "Default";
133			break;
134		case kParam:   return "Param";
135			break;
136		case kQuery:   return "Query";
137			break;
138		case kValue:   return "Value";
139			break;
140		case kUnknown: return "Unknown";
141			break;
142	}
143	return NULL;
144}
145
146void Statement::Print()
147{
148	bool hasValue = fValue != NULL;
149	bool hasOption = fOption != NULL;
150
151	printf("<%s", ElementForType());
152
153	if (fKeyword != NULL) {
154		printf(" keyword=\"%s\"", fKeyword->String());
155	}
156
157	if (hasValue || hasOption) {
158		printf(">\n");
159	} else {
160		printf("/>\n");
161	}
162
163
164	if (hasOption) {
165		printf("\t<option>\n");
166		fOption->Print();
167		printf("\t</option>\n");
168	}
169
170
171	if (hasValue) {
172		printf("\t<value>\n");
173		fValue->Print();
174		printf("\t</value>\n");
175	}
176
177	if (GetChildren() != NULL) {
178		printf("\t<children>\n");
179		GetChildren()->Print();
180		printf("\t</children>\n");
181	}
182
183	if (hasValue || hasOption) {
184		printf("</%s>\n\n", ElementForType());
185	}
186}
187
188