bool XmlSettingsEntry::remove( QStringList &xpath )
{
	QStringList::ConstIterator itXPath;
	AbstractNode *node = NULL;
	QString lastElem(xpath.last()); /* last element from xpath */
	xpath.removeLast();

	XmlSettingsEntry entry( find(xpath, itXPath, node) );

	if( itXPath != xpath.end() ) {
		return false;
	}

	JQ_ASSERT(node);

	if( node->type() == AbstractNode::Map ) {
		MapNode *mapNode = dynamic_cast<MapNode*>(node);
		MapNode::iterator it = mapNode->find( lastElem );
		if( it == mapNode->end() )
			return false;
		mapNode->erase( it );
		m_master->m_modified = true;
		return true;
	}


	if( node->type() == AbstractNode::Vector ) {
		JQ_ASSERT( lastElem.startsWith("[") );
		JQ_ASSERT( lastElem.endsWith("]") && lastElem.size() > 2 );

		bool ok;
		int index = lastElem.mid(1, lastElem.size()-2).toUInt(&ok);
		JQ_ASSERT(ok == true);
		VectorNode *vectorNode = dynamic_cast<VectorNode*>(node);
		vectorNode->erase( vectorNode->begin() + index );
		m_master->m_modified = true;
		return true;
	}

	return false;
}
XmlSettingsEntry XmlSettingsEntry::makeBranch( const QStringList &xpath )
{
	QStringList::ConstIterator itXPath;
	AbstractNode *node = NULL;
	XmlSettingsEntry entry( find(xpath, itXPath, node) );

	if( itXPath != xpath.end() ) {
		JQ_ASSERT(node);
		m_master->clearErrors();
		AbstractNode::Type nodeType = node->type();

		// Error
		if( nodeType == AbstractNode::Scalar )
			return XmlSettingsEntry( entry.m_master );

		QStringList::ConstIterator itXPathEnd = xpath.end();

		for(; itXPath != itXPathEnd; itXPath++ ) {

			QString elem(*itXPath);

			switch( nodeType ) {

			case AbstractNode::Map:
				{
					MapNode *mapNode = (MapNode*)node;
					if( itXPath + 1 == itXPathEnd ) { // Last element, must be a scalar
						(*mapNode)[elem] = (node = new ScalarNode());
					} else {
						(*mapNode)[elem] = (node = new VectorNode());
					}
				}
				nodeType = AbstractNode::Vector;
				break;

			case AbstractNode::Vector:
				if( elem.startsWith("[") ) {
					JQ_ASSERT( elem.endsWith("]") && elem.size() > 2 );

					VectorNode *vectorNode = (VectorNode*)node;
					bool ok;
					int index = elem.mid(1, elem.size()-2).toUInt(&ok);
					JQ_ASSERT(ok == true);

					if( index >= vectorNode->size() ) {
						vectorNode->resize(index+1);
					}

					int sz = vectorNode->size();
					for( int i = 0; i < sz; i++ ) {
						if( !(*vectorNode)[i] )
							(*vectorNode)[i] = (node = new MapNode() );
					}

					if( itXPath + 1 == itXPathEnd ) { // Last element, must be a scalar
						MapNode *mapNode = dynamic_cast<MapNode*>((*vectorNode)[index]);
						(*mapNode)[__TEXT_PSEUDO_ELEMENT] = (node = new ScalarNode());
					}
				} else {
					return XmlSettingsEntry(m_master);
				}
				nodeType = AbstractNode::Map;
				break;

			default: //AbstractNode::Scalar:
				return XmlSettingsEntry(m_master);
			}
		}
	}

	return XmlSettingsEntry(m_master, node);
}