When the application loads, the
initApp()
method
is called for Flex applications or the timeline (non-function) code
is executed for Flash Professional applications. This code defines
a sample XML packet to be verified by the Validator class. The following
code is run:
employeeXML =
<employee id="12345">
<firstName>John</firstName>
<lastName>Doe</lastName>
<costCenter>12345</costCenter>
<costCenter>67890</costCenter>
</employee>;
}
The XML packet is later displayed in a TextArea component instance
on the Stage. This step allows you to modify the XML packet before
attempting to revalidate it.
When the user clicks the Validate button, the
validateData()
method
is called. This method validates the employee XML packet using the
validateEmployeeXML()
method
in the Validator class. The following code shows the
validateData()
method:
function validateData():void
{
try
{
var tempXML:XML = XML(xmlText.text);
Validator.validateEmployeeXML(tempXML);
status.text = "The XML was successfully validated.";
}
catch (error:FatalError)
{
showFatalError(error);
}
catch (error:WarningError)
{
showWarningError(error);
}
catch (error:Error)
{
showGenericError(error);
}
}
First, a temporary XML object is created using the contents of
the TextArea component instance
xmlText
. Next,
the
validateEmployeeXML()
method in the custom
Validator class (com.example.programmingas3/errors/Validator.as)
is called and passes the temporary XML object as a parameter. If
the XML packet is valid, the
status
Label component
instance displays a success message and the application exits. If
the
validateEmployeeXML()
method throws a custom
error (that is, a FatalError, WarningError, or a generic Error occurs),
the appropriate
catch
statement executes and calls
either the
showFatalError()
,
showWarningError()
,
or
showGenericError()
methods. Each of these methods
displays an appropriate message in a text area named
statusTex
t
to notify the user of the specific error that occurred. Each method
also updates the
status
Label component instance
with a specific message.
If a fatal error occurs during an attempt to validate the employee
XML packet, the error message is displayed in the
statusText
text
area, and the
xmlText
TextArea component instance
and
validateBtn
Button component instance are disabled,
as the following code shows:
function showFatalError(error:FatalError):void
{
var message:String = error.message + "\n\n";
var title:String = error.getTitle();
statusText.text = message + " " + title + "\n\nThis application has ended.";
this.xmlText.enabled = false;
this.validateBtn.enabled = false;
hideButtons();
}
If a warning error instead of a fatal error occurs, the error
message is displayed in the
statusText
TextArea
instance, but the
xmlText
TextField and Button component
instances aren’t disabled. The
showWarningError()
method displays
the custom error message in the
statusText
text
area. The message also asks the user to decide if they want to proceed
with validating the XML or cancel the script. The following excerpt
shows the
showWarningError()
method:
function showWarningError(error:WarningError):void
{
var message:String = error.message + "\n\n" + "Do you want to exit this application?";
showButtons();
var title:String = error.getTitle();
statusText.text = message;
}
When the user clicks either the Yes or No button, the
closeHandler()
method
is called. The following excerpt shows the
closeHandler()
method:
function closeHandler(event:CloseEvent):void
{
switch (event.detail)
{
case yesButton:
showFatalError(new FatalError(9999));
break;
case noButton:
statusText.text = "";
hideButtons();
break;
}
}
If the user chooses to cancel the script by clicking Yes, a FatalError
is thrown, causing the application to terminate.