aboutsummaryrefslogtreecommitdiff
path: root/src/tafgroup.cpp
blob: 42f4610436d88f4427257232b879af0044991a0d (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
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
/*
 * Copyright (C) 2007-2010 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#include "bu/tafgroup.h"
#include "bu/tafproperty.h"
#include "bu/tafcomment.h"

Bu::TafGroup::TafGroup( const TafGroup &rSrc ) :
	TafNode( typeGroup ),
	sName( rSrc.sName )
{
	for( NodeList::const_iterator i = rSrc.lChildren.begin(); i; i++ )
	{
		switch( (*i)->getType() )
		{
			case typeGroup:
				addChild( new TafGroup( *dynamic_cast<const TafGroup *>(*i) ) );
				break;

			case typeProperty:
				addChild( new TafProperty( *dynamic_cast<const TafProperty *>(*i) ) );
				break;

			case typeComment:
				addChild( new TafComment( *dynamic_cast<const TafComment *>(*i) ) );
				break;
		}
	}
}

Bu::TafGroup::TafGroup( const Bu::String &sName ) :
	TafNode( typeGroup ),
	sName( sName )
{
}

Bu::TafGroup::~TafGroup()
{
	for( NodeList::iterator i = lChildren.begin(); i != lChildren.end(); i++ )
	{
		delete (*i);
	}
}

const Bu::String &Bu::TafGroup::getName() const
{
	return sName;
}

void Bu::TafGroup::setName( const Bu::String &sName )
{
	this->sName = sName;
}

Bu::TafNode *Bu::TafGroup::addChild( Bu::TafNode *pNode )
{
	switch( pNode->getType() )
	{
		case typeGroup:
			addChild( (Bu::TafGroup *)pNode );
			break;

		case typeProperty:
			addChild( (Bu::TafProperty *)pNode );
			break;

		case typeComment:
			addChild( (Bu::TafComment *)pNode );
			break;
	}

	return pNode;
}

Bu::TafGroup *Bu::TafGroup::addChild( TafGroup *pNode )
{
	TafGroup *pGroup = (TafGroup *)pNode;
	if( !hChildren.has( pGroup->getName() ) )
		hChildren.insert( pGroup->getName(), GroupList() );
	hChildren.get( pGroup->getName() ).append( pGroup );
	lChildren.append( pNode );
	return pNode;
}

Bu::TafProperty *Bu::TafGroup::addChild( TafProperty *pNode )
{
	TafProperty *pProperty = (TafProperty *)pNode;
	if( !hProp.has( pProperty->getName() ) )
		hProp.insert( pProperty->getName(), PropList() );
	hProp.get( pProperty->getName() ).append( pProperty->getValue() );
	lChildren.append( pNode );
	return pNode;
}

Bu::TafComment *Bu::TafGroup::addChild( TafComment *pNode )
{
	lChildren.append( pNode );
	return pNode;
}

Bu::TafGroup *Bu::TafGroup::addGroup( const Bu::String &sName )
{
	return addChild( new TafGroup( sName ) );
}

Bu::TafProperty *Bu::TafGroup::addProperty(
	const Bu::String &sName, const Bu::String &sValue )
{
	return addChild( new TafProperty( sName, sValue ) );
}

bool Bu::TafGroup::hasChild( const Bu::String &sName ) const
{
	return hChildren.has( sName );
}

const Bu::TafGroup::GroupList &Bu::TafGroup::getChildren( const Bu::String &sName ) const
{
	try {
		return hChildren.get( sName );
	} catch( Bu::HashException &e )
	{
		throw Bu::TafException("No children of group \"%s\" match \"%s\".",
			this->sName.getStr(), sName.getStr() );
	}
}

const Bu::TafGroup::NodeList &Bu::TafGroup::getChildren() const
{
	return lChildren;
}

const Bu::TafGroup *Bu::TafGroup::getChild( const Bu::String &sName ) const
{
	try {
		return hChildren.get( sName ).first();
	} catch( Bu::HashException &e )
	{
		throw Bu::TafException("No children of group \"%s\" match \"%s\".",
			this->sName.getStr(), sName.getStr() );
	}
}

bool Bu::TafGroup::hasProperty( const Bu::String &sName ) const
{
	return hProp.has( sName );
}

const Bu::TafGroup::PropList &Bu::TafGroup::getProperties( const Bu::String &sName ) const
{
	try {
		return hProp.get( sName );
	} catch( Bu::HashException &e )
	{
		throw Bu::TafException("No properties of group \"%s\" match \"%s\".",
			this->sName.getStr(), sName.getStr() );
	}
}

const Bu::String &Bu::TafGroup::getProperty( const Bu::String &sName ) const
{
	try {
		return hProp.get( sName ).first();
	} catch( Bu::HashException &e )
	{
		throw Bu::TafException("No properties of group \"%s\" match \"%s\".",
			this->sName.getStr(), sName.getStr() );
	}	
}

const Bu::String &Bu::TafGroup::getProperty( const Bu::String &sName,
	const Bu::String &sDef ) const
{
	try
	{
		return hProp.get( sName ).first();
	}
	catch( Bu::HashException &e )
	{
		return sDef;
	}
}

const Bu::TafGroup *Bu::TafGroup::getChildByPath(
		const Bu::String &sPath ) const
{
	return getChildByPath( sPath.split('/') );
}

const Bu::TafGroup *Bu::TafGroup::getChildByPath( Bu::StrList lPath ) const
{
	const Bu::TafGroup *cur = this;

	for( Bu::StrList::const_iterator i = lPath.begin(); i; i++ )
	{
		cur = cur->getChild( *i );
	}

	return cur;
}

const Bu::String &Bu::TafGroup::getByPath( const Bu::String &sPath ) const
{
	return getByPath( sPath.split('/') );
}

const Bu::String &Bu::TafGroup::getByPath( Bu::StrList lPath ) const
{
	const Bu::TafGroup *cur = this;

	for( Bu::StrList::const_iterator i = lPath.begin(); i; i++ )
	{
		if( !(i+1) )
			break;
		cur = cur->getChild( *i );
	}

	return cur->getProperty( lPath.last() );
}