10a8c90248264a8b26970b4473770bcc3df8515fJosh Gao"""Different kinds of SAX Exceptions""" 20a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport sys 30a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoif sys.platform[:4] == "java": 40a8c90248264a8b26970b4473770bcc3df8515fJosh Gao from java.lang import Exception 50a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodel sys 60a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 70a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# ===== SAXEXCEPTION ===== 80a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 90a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass SAXException(Exception): 100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Encapsulate an XML error or warning. This class can contain 110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao basic error or warning information from either the XML parser or 120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao the application: you can subclass it to provide additional 130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao functionality, or to add localization. Note that although you will 140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao receive a SAXException as the argument to the handlers in the 150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ErrorHandler interface, you are not actually required to raise 160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao the exception; instead, you can simply read the information in 170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao it.""" 180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, msg, exception=None): 200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Creates an exception. The message is required, but the exception 210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao is optional.""" 220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._msg = msg 230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._exception = exception 240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Exception.__init__(self, msg) 250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def getMessage(self): 270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "Return a message for this exception." 280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._msg 290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def getException(self): 310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "Return the embedded exception, or None if there was none." 320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._exception 330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __str__(self): 350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "Create a string representation of the exception." 360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._msg 370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __getitem__(self, ix): 390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Avoids weird error messages if someone does exception[ix] by 400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao mistake, since Exception has __getitem__ defined.""" 410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise AttributeError("__getitem__") 420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# ===== SAXPARSEEXCEPTION ===== 450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 460a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass SAXParseException(SAXException): 470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Encapsulate an XML parse error or warning. 480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao This exception will include information for locating the error in 500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao the original XML document. Note that although the application will 510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao receive a SAXParseException as the argument to the handlers in the 520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ErrorHandler interface, the application is not actually required 530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao to raise the exception; instead, it can simply read the 540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao information in it and take a different action. 550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Since this exception is a subclass of SAXException, it inherits 570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao the ability to wrap another exception.""" 580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, msg, exception, locator): 600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "Creates the exception. The exception parameter is allowed to be None." 610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao SAXException.__init__(self, msg, exception) 620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._locator = locator 630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # We need to cache this stuff at construction time. 650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # If this exception is raised, the objects through which we must 660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # traverse to get this information may be deleted by the time 670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # it gets caught. 680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._systemId = self._locator.getSystemId() 690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._colnum = self._locator.getColumnNumber() 700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._linenum = self._locator.getLineNumber() 710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def getColumnNumber(self): 730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """The column number of the end of the text where the exception 740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao occurred.""" 750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._colnum 760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def getLineNumber(self): 780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "The line number of the end of the text where the exception occurred." 790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._linenum 800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def getPublicId(self): 820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "Get the public identifier of the entity where the exception occurred." 830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._locator.getPublicId() 840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def getSystemId(self): 860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "Get the system identifier of the entity where the exception occurred." 870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self._systemId 880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __str__(self): 900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "Create a string representation of the exception." 910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sysid = self.getSystemId() 920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if sysid is None: 930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sysid = "<unknown>" 940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao linenum = self.getLineNumber() 950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if linenum is None: 960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao linenum = "?" 970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao colnum = self.getColumnNumber() 980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if colnum is None: 990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao colnum = "?" 1000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return "%s:%s:%s: %s" % (sysid, linenum, colnum, self._msg) 1010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# ===== SAXNOTRECOGNIZEDEXCEPTION ===== 1040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1050a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass SAXNotRecognizedException(SAXException): 1060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Exception class for an unrecognized identifier. 1070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao An XMLReader will raise this exception when it is confronted with an 1090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao unrecognized feature or property. SAX applications and extensions may 1100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao use this class for similar purposes.""" 1110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# ===== SAXNOTSUPPORTEDEXCEPTION ===== 1140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1150a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass SAXNotSupportedException(SAXException): 1160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Exception class for an unsupported operation. 1170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao An XMLReader will raise this exception when a service it cannot 1190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao perform is requested (specifically setting a state or value). SAX 1200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao applications and extensions may use this class for similar 1210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao purposes.""" 1220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# ===== SAXNOTSUPPORTEDEXCEPTION ===== 1240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1250a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass SAXReaderNotAvailable(SAXNotSupportedException): 1260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Exception class for a missing driver. 1270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao An XMLReader module (driver) should raise this exception when it 1290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao is first imported, e.g. when a support module cannot be imported. 1300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao It also may be raised during parsing, e.g. if executing an external 1310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao program is not permitted.""" 132