FileDocumentation.cpp 7.64 KB
Newer Older
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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
////////////////////////////////////////////////////////////////////////////////
/// @brief reads documentation entries from a file
///
/// @file
///
/// Copyright (C) 2006-2010 Jedox AG
///
/// This program is free software; you can redistribute it and/or modify it
/// under the terms of the GNU General Public License (Version 2) as published
/// by the Free Software Foundation at http://www.gnu.org/copyleft/gpl.html.
///
/// 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., 59 Temple
/// Place, Suite 330, Boston, MA 02111-1307 USA
///
/// You may obtain a copy of the License at
///
/// <a href="http://www.jedox.com/license_palo_suite.txt">
///   http://www.jedox.com/license_palo_suite.txt
/// </a>
///
/// If you are developing and distributing open source applications under the
/// GPL License, then you are free to use Palo under the GPL License.  For OEMs,
/// ISVs, and VARs who distribute Palo with their products, and do not license
/// and distribute their source code under the GPL, Jedox provides a flexible
/// OEM Commercial License.
///
/// Portions of the code developed by triagens GmbH, Koeln on behalf of Jedox
/// AG. Intellectual property rights for these portions has triagens GmbH,
/// Koeln, or othervise Jedox AG, Freiburg. Exclusive worldwide exploitation
/// right (commercial copyright) has Jedox AG, Freiburg.
///
/// @author Frank Celler, triagens GmbH, Cologne, Germany
/// @author Achim Brandt, triagens GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////

#include "PaloDocumentation/FileDocumentation.h"

#include "Logger/Logger.h"

#include <iostream>
#include <fstream>

#include "Collections/StringUtils.h"

namespace palo {
FileDocumentation::FileDocumentation(const string& filename) :
	lastParamId(-1), lastResultId(-1), lastExampleId(-1)
{
	string keyword;
	bool foundKeyword = false;
	string value = "";
	string line;
	int lineNumber = 0;

	ifstream file(filename.c_str());

	if (!file) {
		Logger::warning << "could not open documentation file '" << filename << "'" << endl;
		return;
	}

	while (!file.eof()) {
		getline(file, line);

		if (line.empty()) {
			value.append("\n");
		} else {

			// check for keyword (starting with @)
			if (line[0] == '@') {
				if (foundKeyword) {
					processDocumentationEntry(keyword, value);
					value = "";
				}

				foundKeyword = true;

				// split line into keyword and value
				size_t end = line.find(" ");

				if (end == string::npos) {
					keyword = line;
				} else {
					keyword = line.substr(0, end);
					value = line.substr(end);
				}
			} else {
				value.append("\n");
				value.append(line);
			}
		}

		lineNumber++;
	}

	if (foundKeyword) {
		processDocumentationEntry(keyword, value);
	}
}

bool FileDocumentation::hasDocumentationEntry(const string& name)
{
	map<string, vector<string> >::const_iterator i = docEntries.find(name);

	if (i != docEntries.end()) {
		return true;
	} else {
		return false;
	}
}

const vector<string>& FileDocumentation::getDocumentationEntries(const string& name)
{
	static const vector<string> error;

	map<string, vector<string> >::const_iterator i = docEntries.find(name);

	if (i != docEntries.end()) {
		return i->second;
	} else {
		return error;
	}
}

const string& FileDocumentation::getDocumentationEntry(const string& name, size_t index)
{
	static const string error = "";
	const vector<string>& v = getDocumentationEntries(name);

	if (index < v.size()) {
		return v[index];
	} else {
		return error;
	}
}

void FileDocumentation::processDocumentationEntry(const string& name, const string& value)
{
	if (name == "@param") {
		lastParamId = addDocumentationEntry(name, value);

		// create empty values for some other parameter attributes
		addDocumentationEntry("@param_type", "");
		addDocumentationEntry("@param_description", "");
	} else if (name == "@result") {
		lastResultId = addDocumentationEntry(name, value);

		// create empty values for some other parameter attributes
		addDocumentationEntry("@result_type", "");
		addDocumentationEntry("@result_description", "");
	} else if (name == "@example") {
		lastExampleId = addDocumentationEntry(name, value);

		// create empty values for some other parameter attributes
		addDocumentationEntry("@example_description", "");
	} else if (name == "@server") {
		lastServerId = addDocumentationEntry(name, value);
		addDocumentationEntry("@server_description", "");
	} else if (name == "@database") {
		lastDatabaseId = addDocumentationEntry(name, value);
		addDocumentationEntry("@database_description", "");
	} else if (name == "@dimension") {
		lastDimensionId = addDocumentationEntry(name, value);
		addDocumentationEntry("@dimension_description", "");
	} else if (name == "@element") {
		lastElementId = addDocumentationEntry(name, value);
		addDocumentationEntry("@element_description", "");
	} else if (name == "@cube") {
		lastCubeId = addDocumentationEntry(name, value);
		addDocumentationEntry("@cube_description", "");
	} else if (name == "@cell") {
		lastCellId = addDocumentationEntry(name, value);
		addDocumentationEntry("@cell_description", "");
	} else if (name == "@param_type") {
		if (lastParamId != -1) {
			replaceDocumentationEntry(name, value, lastParamId);
		}
	} else if (name == "@param_description") {
		if (lastParamId != -1) {
			replaceDocumentationEntry(name, value, lastParamId);
		}
	} else if (name == "@result_type") {
		if (lastResultId != -1) {
			replaceDocumentationEntry(name, value, lastResultId);
		}
	} else if (name == "@result_description") {
		if (lastResultId != -1) {
			replaceDocumentationEntry(name, value, lastResultId);
		}
	} else if (name == "@example_description") {
		if (lastExampleId != -1) {
			replaceDocumentationEntry(name, value, lastExampleId);
		}
	} else if (name == "@server_description") {
		if (lastServerId != -1) {
			replaceDocumentationEntry(name, value, lastServerId);
		}
	} else if (name == "@database_description") {
		if (lastDatabaseId != -1) {
			replaceDocumentationEntry(name, value, lastDatabaseId);
		}
	} else if (name == "@dimension_description") {
		if (lastDimensionId != -1) {
			replaceDocumentationEntry(name, value, lastDimensionId);
		}
	} else if (name == "@element_description") {
		if (lastElementId != -1) {
			replaceDocumentationEntry(name, value, lastElementId);
		}
	} else if (name == "@cube_description") {
		if (lastCubeId != -1) {
			replaceDocumentationEntry(name, value, lastCubeId);
		}
	} else if (name == "@cell_description") {
		if (lastCellId != -1) {
			replaceDocumentationEntry(name, value, lastCellId);
		}
	} else {
		addDocumentationEntry(name, value);
	}
}

ssize_t FileDocumentation::addDocumentationEntry(const string& name, const string& value)
{
	map<string, vector<string> >::iterator i = docEntries.find(name);

	if (i != docEntries.end()) {

		// append value
		i->second.push_back(StringUtils::trim(value));

		return (ssize_t)(i->second.size() - 1);
	} else {
		docEntries[name].push_back(StringUtils::trim(value));
		return 0;
	}
}

void FileDocumentation::replaceDocumentationEntry(const string& name, const string& value, size_t position)
{
	map<string, vector<string> >::iterator i = docEntries.find(name);

	if (i != docEntries.end()) {
		if (position < i->second.size()) {
			vector<string>::iterator vi = i->second.begin() + position;

			i->second.insert(vi, StringUtils::trim(value));
			vi = i->second.begin() + position + 1;
			i->second.erase(vi);
		}
	}
}
}