Consultor Eletrônico



Kbase P157147: 4GL/ABL: Sample code to demonstrate error handling by the SAX parser
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   5/25/2010
Status: Verified

GOAL:

4GL/ABL: Sample code to demonstrate error handling by the SAX parser

GOAL:

How does the callback procedure for the error event with DTD validation look like?

GOAL:

How is the error event captured by the error call back procedure during the parsing of an XML document with DTD validation?


FACT(s) (Environment):

All Supported Operating Systems
OpenEdge 10.x
OpenEdge Category: Language (4GL/ABL)

FIX:

The following are two procedures SAXParseWithDTDValidation.p and SAXCallbacksPhone.p and two documents CustomersWithDTD.xml and CustomersWithDTD.dtd to demonstrate how the error event is captured by the error call back procedure during the parsing of an XML document with DTD validation.

To run the sample, put all the four files in your OpenEdge 10.x directory and execute the SAXParseWithDTDValidation.p from a proenv session using the command:

prowin32 -p SAXParseWithDTDValidation.p.

/* 1. SAXParseWithDTDValidation.p */
DEFINE VARIABLE hParser AS HANDLE NO-UNDO.
DEFINE VARIABLE hCallbacks AS HANDLE NO-UNDO.
CREATE SAX-READER hParser.
RUN "SAXCallbacks.p" PERSISTENT SET hCallbacks.
hParser:HANDLER = hCallbacks.
hParser:SET-INPUT-SOURCE("FILE", "CustomersWithDTD.xml").
hParser:SCHEMA-PATH="CustomersWithDTD.dtd".
hParser:VALIDATION-ENABLED=TRUE.
hParser:SAX-PARSE( ) NO-ERROR.
IF ERROR-STATUS:ERROR THEN DO:
IF ERROR-STATUS:NUM-MESSAGES > 0 THEN
/* unable to begin the parse */
MESSAGE ERROR-STATUS:GET-MESSAGE(1)
VIEW-AS ALERT-BOX INFO BUTTONS OK.
ELSE
/* error detected in a callback */
MESSAGE RETURN-VALUE VIEW-AS ALERT-BOX.
END.
DELETE OBJECT hParser.
DELETE PROCEDURE hCallbacks.

/* 2. SAXCallbacks.p */

DEFINE VARIABLE currentContent AS CHARACTER.
DEFINE VARIABLE currentElementName AS CHARACTER.
DEFINE VARIABLE currentPhone AS CHARACTER.
DEFINE VARIABLE currentTimeZone AS CHARACTER.
DEFINE VARIABLE currentEOD AS CHARACTER.
DEFINE VARIABLE currentCustomer AS CHARACTER.
/* Invoked when the XML parser detects the start of an XML document. */
PROCEDURE StartDocument:
currentCustomer = "".
currentTimeZone = "".
currentEOD = "".
currentPhone = "".
currentContent = "".
currentElementName = "".
END PROCEDURE.
/* Invoked when the XML parser detects the beginning of an element. */
PROCEDURE StartElement:
DEFINE INPUT PARAMETER namespaceURI AS CHARACTER.
DEFINE INPUT PARAMETER localName AS CHARACTER.
DEFINE INPUT PARAMETER qname AS CHARACTER.
DEFINE INPUT PARAMETER attributes AS HANDLE.
IF qName = "Phone" THEN DO:
currentTimeZone = attributes:GET-VALUE-BY-QNAME("BusinessDayEndTime").
currentEOD = attributes:GET-VALUE-BY-QNAME("TimeZone").
END.
currentElementName = qname.
END PROCEDURE.
/* Invoked when the XML parser detects character data. */
PROCEDURE Characters:
DEFINE INPUT PARAMETER charData AS MEMPTR.
DEFINE INPUT PARAMETER numChars AS INTEGER.
currentContent = currentContent + GET-STRING(charData, 1, GET-SIZE(charData)).
END PROCEDURE.
/* Invoked when the XML parser detects the end of an element. */
PROCEDURE EndElement:
DEFINE INPUT PARAMETER name.spaceURI AS CHARACTER.
DEFINE INPUT PARAMETER localName AS CHARACTER.
DEFINE INPUT PARAMETER qName AS CHARACTER.
IF qname = "Name" THEN currentCustomer = currentContent.
IF qname = "Phone" THEN currentPhone = currentContent.
IF qname = "Customer" THEN DO:
MESSAGE currentCustomer "~t" currentPhone "~t" currentTimeZone "~t" currentEOD "~n"
VIEW-AS ALERT-BOX INFO BUTTONS OK.
ASSIGN
currentCustomer = ""
currentTimeZone = ""
currentEOD = ""
currentPhone = "".
END.
ASSIGN
currentElementName = ""
currentContent = "".
END PROCEDURE.
/* Invoked when the XML parser detects the end of an XML document. */
PROCEDURE EndDocument:
MESSAGE "Done!" "~n"
VIEW-AS ALERT-BOX INFO BUTTONS OK.
END PROCEDURE.
/* Invoked to report a warning. */
PROCEDURE Warning:
DEFINE INPUT PARAMETER ErrMessage AS CHARACTER NO-UNDO.
MESSAGE "The following WARNING was generated:~n" + ErrMessage
VIEW-AS ALERT-BOX INFO BUTTONS OK.
END PROCEDURE.

/* Invoked to report an error encountered by the parser while parsing the XML document. */
PROCEDURE Error:
DEFINE INPUT PARAMETER ErrMessage AS CHARACTER NO-UNDO.
MESSAGE "The following NONFATAL ERROR was generated:~n" + ErrMessage
VIEW-AS ALERT-BOX INFO BUTTONS OK.
END PROCEDURE.
/* Invoked to report a fatal error. */
PROCEDURE FatalError:
DEFINE INPUT PARAMETER ErrMessage AS CHARACTER NO-UNDO.
RETURN ERROR "The following FATAL ERROR was generated:~n" + ErrMessage.
END PROCEDURE.
/* 3. CustomersWithDTD.xml */

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Customers SYSTEM "CustomersWithDTD.dtd">
<Customers>
<Customer>
<Country>USA</Country>
<Name>Everything Underwater</Name>
<AddressInfo>
<Address>440 Ocean Avenue</Address>
<Address2>
</Address2>
<City>Boston</City>
<State>MA</State>
<PostalCode>02114</PostalCode>
</AddressInfo>
<Phone BusinessDayEndTime="1600">(617) 221-0550</Phone>
<SalesRep>Charles Urchin</SalesRep>
<FinancialInfo Currency="USD">
<CreditLimit>4000</CreditLimit>
<Balance>319.50</Balance>
<Terms>30 days</Terms>
<Discount>15</Discount>
</FinancialInfo>
<Comments></Comments>
<Fax>(617) 221-4958</Fax>
<EmailAddress>underthesea@earthlink.net</EmailAddress>
</Customer>
<Customer>
<CustNum>1001</CustNum>
<Country>United Kingdom</Country>
<Name>Glider's Paradise</Name>
<AddressInfo>
<Address>91 Cliff Lane</Address>
. <Address2>Leyton</Address2>
<City>Aberdeen</City>
<State>Granouab</State>
<PostalCode>AB9 2YY</PostalCode>
</AddressInfo>
<Phone TimeZone="GMT" BusinessDayEndTime="1750">0224 495 947</Phone>
<SalesRep>Amelia Airhardt</SalesRep>
<FinancialInfo Currency="Euro">
<CreditLimit>6000</CreditLimit>
<Balance>2300</Balance>
<Terms>60 days</Terms>
<Discount>20</Discount>
</FinancialInfo>
<Comments></Comments>
<Fax>0224 495 334</Fax>
<EmailAddress>away_we_go@gliders.uk</EmailAddress>
</Customer>
</Customers>
/* 4. CustomersWithDTD.dtd */

<?xml encoding="UTF-8"?>
<!ELEMENT Customers (Customer)+>
<!ATTLIST Customers
xmlns CDATA #FIXED ''>
<!ELEMENT Customer (CustNum,Country,Name,AddressInfo,Phone,SalesRep,
FinancialInfo,Comments,Fax,EmailAddress)>
<!ATTLIST Customer
xmlns CDATA #FIXED ''>
<!ELEMENT CustNum (#PCDATA)>
<!ATTLIST CustNum
xmlns CDATA #FIXED ''>
<!ELEMENT Country (#PCDATA)>
<!ATTLIST Country
xmlns CDATA #FIXED ''>
<!ELEMENT Name (#PCDATA)>
<!ATTLIST Name
xmlns CDATA #FIXED ''>
<!ELEMENT AddressInfo (Address,Address2,City,State,PostalCode)>
<!ATTLIST AddressInfo
xmlns CDATA #FIXED ''>
<!ELEMENT Phone (#PCDATA)>
<!ATTLIST Phone
xmlns CDATA #FIXED ''
BusinessDayEndTime CDATA #REQUIRED
TimeZone CDATA 'EST'>
<!ELEMENT SalesRep (#PCDATA)>
<!ATTLIST SalesRep
xmlns CDATA #FIXED ''>
<!ELEMENT FinancialInfo (CreditLimit,Balance,Terms,Discount)>
<!ATTLIST FinancialInfo
xmlns CDATA #FIXED ''
Currency CDATA #REQUIRED>
<!ELEMENT Comments EMPTY>
<!ATTLIST Comments
xmlns CDATA #FIXED ''>
<!ELEMENT Fax (#PCDATA)>
<!ATTLIST Fax
xmlns CDATA #FIXED ''>
<!ELEMENT EmailAddress (#PCDATA)>
<!ATTLIST EmailAddress
xmlns CDATA #FIXED ''>
<!ELEMENT Address (#PCDATA)>
<!ATTLIST Address
xmlns CDATA #FIXED ''>
<!ELEMENT Address2 (#PCDATA)>
<!ATTLIST Address2
xmlns CDATA #FIXED ''>
<!ELEMENT City (#PCDATA)>
<!ATTLIST City
xmlns CDATA #FIXED ''>
<!ELEMENT State (#PCDATA)>
<!ATTLIST State
xmlns CDATA #FIXED ''>
<!ELEMENT PostalCode (#PCDATA)>
<!ATTLIST PostalCode
xmlns CDATA #FIXED ''>RONG>
<!ELEMENT CreditLimit (#PCDATA)>
<!ATTLIST CreditLimit
xmlns CDATA #FIXED ''>
<!ELEMENT Balance (#PCDATA)>
<!ATTLIST Balance
xmlns CDATA #FIXED ''>
<!ELEMENT Terms (#PCDATA)>
<!ATTLIST Terms
xmlns CDATA #FIXED ''>
<!ELEMENT Discount (#PCDATA)>
<!ATTLIST Discount
xmlns CDATA #FIXED ''>.