summaryrefslogtreecommitdiffstats
path: root/reader/src/external/ProgramCollection.cpp
blob: 9599d9007aa2d129fdd60a3a79c39b5d2d361fad (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 * Copyright (C) 2004-2012 Geometer Plus <[email protected]>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#include <algorithm>

#include <ZLibrary.h>
#include <ZLFile.h>
#include <ZLXMLReader.h>
#include <ZLResource.h>

#include "ProgramCollection.h"
#include "../options/FBCategoryKey.h"

class ProgramCollectionBuilder : public ZLXMLReader {

public:
	ProgramCollectionBuilder(ProgramCollectionMap &collectionMap);
	~ProgramCollectionBuilder();

private:
	void startElementHandler(const char *tag, const char **attributes);	
	void endElementHandler(const char *tag);	

private:
	ProgramCollectionMap &myCollectionMap;
	shared_ptr<ProgramCollection> myCurrentCollection;
	shared_ptr<Program> myCurrentProgram;
};

static const std::string SECTION = "section";
static const std::string PROGRAM = "program";
static const std::string ACTION = "action";
static const std::string OPTION = "option";

ProgramCollectionBuilder::ProgramCollectionBuilder(ProgramCollectionMap &collectionMap) : myCollectionMap(collectionMap) {
}

ProgramCollectionBuilder::~ProgramCollectionBuilder() {
}

void ProgramCollectionBuilder::startElementHandler(const char *tag, const char **attributes) {
	if (SECTION == tag) {
		const char *name = attributeValue(attributes, "name");
		if (name != 0) {
			myCurrentCollection = myCollectionMap.myMap[name];
			if (myCurrentCollection.isNull()) {
				myCurrentCollection = new ProgramCollection(name);
				myCollectionMap.myMap[name] = myCurrentCollection;
			}
		}
	} else if (!myCurrentCollection.isNull() && (PROGRAM == tag)) {
		const char *name = attributeValue(attributes, "name");
		const char *protocol = attributeValue(attributes, "protocol");
		const char *testFile = attributeValue(attributes, "testFile");
		if ((name != 0) && (protocol != 0)) {
			shared_ptr<ZLMessageOutputChannel> channel =
				ZLCommunicationManager::Instance().createMessageOutputChannel(protocol, (testFile != 0) ? testFile : "");
			if (!channel.isNull()) {
				std::string sName = name;
				if (!sName.empty()) {
					if (sName[0] == '%') {
						sName = ZLResource::resource("external")[sName.substr(1)].value();
					}
					myCurrentProgram = new Program(sName, channel);
					myCurrentCollection->myNames.push_back(sName);
					myCurrentCollection->myPrograms[sName] = myCurrentProgram;
				}
			}
		}
	} else if (!myCurrentProgram.isNull() && (ACTION == tag)) {
		const char *name = attributeValue(attributes, "name");
		if (name != 0) {
			static const std::string NAME = "name";
			ZLCommunicationManager::Data &data = myCurrentProgram->myCommandData[name];
			for (const char **it = attributes; (*it != 0) && (*(it + 1) != 0); it += 2) {
				if (NAME != *it) {
					data[*it] = *(it + 1);
				}
			}
		}
	} else if (!myCurrentProgram.isNull() && (OPTION == tag)) {
		const char *name = attributeValue(attributes, "name");
		if (name != 0) {
			const char *defaultValue = attributeValue(attributes, "defaultValue");
			const std::string sName = name;
			const std::string sDefaultValue = (defaultValue != 0) ? defaultValue : std::string();
			myCurrentProgram->myOptions.push_back(Program::OptionDescription(sName, sDefaultValue));
			myCurrentProgram->myDefaultValues[sName] = sDefaultValue;
		}
	}
}

void ProgramCollectionBuilder::endElementHandler(const char *tag) {
	if (SECTION == tag) {
		if (!myCurrentCollection.isNull()) {
			const std::vector<std::string> &names = myCurrentCollection->names();
			ZLStringOption &nameOption = myCurrentCollection->CurrentNameOption;
			if (!names.empty() && (std::find(names.begin(), names.end(), nameOption.value()) == names.end())) {
				nameOption.setValue(names.front());
			}
		}
		myCurrentCollection = 0;
		myCurrentProgram = 0;
	} else if (PROGRAM == tag) {
		myCurrentProgram = 0;
	}
}

ProgramCollectionMap::ProgramCollectionMap() {
	ProgramCollectionBuilder builder(*this);
	builder.readDocument(ZLFile(ZLibrary::DefaultFilesPathPrefix() + "external.xml"));
}

shared_ptr<ProgramCollection> ProgramCollectionMap::collection(const std::string &name) const {
	std::map<std::string,shared_ptr<ProgramCollection> >::const_iterator it = myMap.find(name);
	return (it != myMap.end()) ? it->second : 0;
}

ProgramCollection::ProgramCollection(const std::string &name) :
	EnableCollectionOption(ZLCategoryKey::CONFIG, name, "Enabled", true),
	CurrentNameOption(ZLCategoryKey::CONFIG, name, "Name", "") {
}

const std::vector<std::string> &ProgramCollection::names() const {
	return myNames;
}

shared_ptr<Program> ProgramCollection::program(const std::string &name) const {
	std::map<std::string,shared_ptr<Program> >::const_iterator it = myPrograms.find(name);
	return (it != myPrograms.end()) ? it->second : 0;
}

shared_ptr<Program> ProgramCollection::currentProgram() const {
	if (!EnableCollectionOption.value()) {
		return 0;
	}
	return program(CurrentNameOption.value());
}

Program::Program(const std::string &name, shared_ptr<ZLMessageOutputChannel> channel) : myName(name), myChannel(channel) {
}

void Program::run(const std::string &command, const std::string &parameter) const {
	if (!myChannel.isNull()) {
		std::map<std::string,ZLCommunicationManager::Data>::const_iterator it = myCommandData.find(command);
		if (it != myCommandData.end()) {
			ZLCommunicationManager::Data data = it->second;
			for (ZLCommunicationManager::Data::iterator jt = data.begin(); jt != data.end(); ++jt) {
				if (!jt->second.empty() && jt->second[0] == '%') {
					const std::string optionName = jt->second.substr(1);
					std::map<std::string,std::string>::const_iterator st = myDefaultValues.find(optionName);
					jt->second = ZLStringOption(
						FBCategoryKey::EXTERNAL,
						myName,
						optionName,
						(st != myDefaultValues.end()) ? st->second : "").value();
				}
			}
			shared_ptr<ZLMessageSender> sender = myChannel->createSender(data);
			if (!sender.isNull()) {
				sender->sendStringMessage(parameter);
			}
		}
	}
}

const std::vector<Program::OptionDescription> &Program::options() const {
	return myOptions;
}

Program::OptionDescription::OptionDescription(const std::string &name, const std::string &defaultValue) : OptionName(name), DefaultValue(defaultValue) {
}