# This file is part of snavtofamix (Source Navigator to FAMIX).
#
# snavtofamix 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.
#
# snavtofamix 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 snavtofamix; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# Copyright 2007   University of Antwerp
# Author(s): Bart Du Bois <bart.dubois@ua.ac.be>


from dictionaries.NamespaceDictionary import NamespaceDictionary
from NamespaceEntity import NamespaceEntity
from ClassEntity import ClassEntity
from common.SourceLocation import SourceLocation

##
# Given a file name and line number, calculates the list of
# parent NamespaceEntities.
##
class NamespaceContainmentChecker(object):
	def __init__(self, namespaceDict):
		self.namespaceDict = namespaceDict

	def getNamespaceDictionary(self):
		return self.namespaceDict

	##
	# Retrieve a list of NamespaceEntities surrounding
	# the given line number in the given file.
	#
	# The NamespaceEntities are sorted on their starting line number.
	##
	def getSurroundingNamespaceList(self, fileName, lineNr):
		parentNamespaceList = []

		location = SourceLocation(fileName, lineNr, lineNr)

		if self.namespaceDict.fileInDictionary(fileName):
			for reference in self.namespaceDict.getReferences(fileName):
				if reference.surroundsLocation(location):
					parentNamespaceList.append(reference)

		return parentNamespaceList

	##
	# Return a list of names of namespaces surrounding the given line number
	# in the given file.
	##
	def getSurroundingNamespaceNames(self, fileName, lineNr):
		names = []

		for reference in self.getSurroundingNamespaceList(fileName, lineNr):
			names.append(reference.getName())

		return names

	##
	# Returns the qualified name of the first parent namespace containing the given line.
	# E.g., Foo::Bar::Baz.
	##
	def getSurroundingNamespaceName(self, sourceLocation):
		name = ""
		reference = self.getNamespaceDictionary().getDeepestSurroundingNamespace(sourceLocation)

		if reference != None:
			name = reference.getName()

		return name
