import copy, StringIO, re

try:
	import logging
except:
	import InfoLogging as logging

import dtdcode

# HTML Class uses this to suppress end-tag output, XHTML class uses these to write singletons.
TAG_OPTIONAL=1
TAG_REQUIRED=0
TAG_FORBIDDEN=2

MLT_SPACE = re.compile ('  +')
NBSP_REF = "&nbsp;"

class TagNotAllowedException (Exception):
	def __init__ (self, tag, stack):
		stackMsg = []
		self.tag = tag
		for oldtag, atts in stack:
			stackMsg.append ("<%s%s>" % (oldtag, atts))
		stackMsg = " ".join (stackMsg)
		self.msg = "Tag %s not allowed here: %s" % (tag, stackMsg)
	
	def getTag (self):
		return self.tag
		
	def __str__ (self):
		return self.msg
		
class BadCloseTagException (Exception):
	def __init__ (self, tag, stack, expected=None):
		stackMsg = []
		self.tag = tag
		for oldtag, atts in stack:
			stackMsg.append ("<%s%s>" % (oldtag, atts))
		stackMsg = " ".join (stackMsg)
		if (expected is None):
			self.msg = "Close tag %s has no corresponding open tag.  (Elements currently open are: %s)" % (tag, stackMsg)
		else:
			self.msg = "Received close tag %s when expecting %s.  (Elements currently open are: %s)" % (tag, expected, stackMsg)
		
	def getTag (self):
		return self.tag
		
	def __str__ (self):