xref: /haiku/src/tests/add-ons/print/ppd/model/StatementWrapper.cpp (revision e81a954787e50e56a7f06f72705b7859b6ab06d1)
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 "StatementWrapper.h"
10 
11 static const char* kOpenUIStatement = "OpenUI";
12 static const char* kCloseUIStatement = "CloseUI";
13 
14 static const char* kOpenGroupStatement = "OpenGroup";
15 static const char* kCloseGroupStatement = "CloseGroup";
16 static const char* kOpenSubGroupStatement = "OpenSubGroup";
17 static const char* kCloseSubGroupStatement = "CloseSubGroup";
18 
19 // JCL
20 static const char* kJCL = "JCL";
21 static const char* kJCLOpenUIStatement = "JCLOpenUI";
22 static const char* kJCLCloseUIStatement = "JCLCloseUI";
23 
24 
25 StatementWrapper::StatementWrapper(Statement* statement)
26 	: fStatement(statement)
27 {
28 	// nothing to do
29 }
30 
31 GroupStatement::GroupStatement(Statement* statement)
32 	: StatementWrapper(statement)
33 {
34 	// nothing to do
35 }
36 
37 bool GroupStatement::IsUIGroup()
38 {
39 	return strcmp(GetKeyword(), kOpenUIStatement) == 0;
40 }
41 
42 bool GroupStatement::IsGroup()
43 {
44 	return strcmp(GetKeyword(), kOpenGroupStatement) == 0;
45 }
46 
47 bool GroupStatement::IsSubGroup()
48 {
49 	return strcmp(GetKeyword(), kOpenSubGroupStatement) == 0;
50 }
51 
52 bool GroupStatement::IsJCL()
53 {
54 	return strstr(GetKeyword(), kJCL) == GetKeyword();
55 }
56 
57 bool GroupStatement::IsOpenGroup()
58 {
59 	const char* keyword = GetKeyword();
60 
61 	return strcmp(keyword, kOpenUIStatement) == 0 ||
62 		strcmp(keyword, kOpenGroupStatement) == 0 ||
63 		strcmp(keyword, kOpenSubGroupStatement) == 0 ||
64 		strcmp(keyword, kJCLOpenUIStatement) == 0;
65 }
66 
67 bool GroupStatement::IsCloseGroup()
68 {
69 	const char* keyword = GetKeyword();
70 
71 	return strcmp(keyword, kCloseUIStatement) == 0 ||
72 		strcmp(keyword, kCloseGroupStatement) == 0 ||
73 		strcmp(keyword, kCloseSubGroupStatement) == 0 ||
74 		strcmp(keyword, kJCLCloseUIStatement) == 0;
75 }
76 
77 Value* GroupStatement::GetValue()
78 {
79 	if (strcmp(GetKeyword(), kOpenUIStatement) == 0 ||
80 		strcmp(GetKeyword(), kJCLOpenUIStatement) == 0) {
81 		return GetStatement()->GetOption();
82 	} else {
83 		return GetStatement()->GetValue();
84 	}
85 }
86 
87 const char* GroupStatement::GetGroupName()
88 {
89 	Value* value = GetValue();
90 	if (value == NULL) return NULL;
91 	BString* string = value->GetValue();
92 	if (string == NULL) return NULL;
93 	const char* name = string->String();
94 	if (name != NULL && *name == '*') {
95 		// skip '*'
96 		name ++;
97 	}
98 	return name;
99 }
100 
101 const char* GroupStatement::GetGroupTranslation()
102 {
103 	Value* value = GetValue();
104 	if (value == NULL) return NULL;
105 	BString* string = value->GetTranslation();
106 	if (string == NULL) return NULL;
107 	return string->String();
108 }
109 
110 GroupStatement::Type GroupStatement::GetType()
111 {
112 	const char* type = GetStatement()->GetValueString();
113 	if (type == NULL) return kUnknown;
114 
115 	if (strstr(type, "PickOne") != NULL) return kPickOne;
116 	if (strstr(type, "PickMany") != NULL) return kPickMany;
117 	if (strstr(type, "Boolean") != NULL) return kBoolean;
118 
119 	return kUnknown;
120 }
121