midrange.com code scratchpad
Name:
SaxParsing
Scriptlanguage:
Plain Text
Tabwidth:
4
Date:
10/06/2011 09:13:23 pm
IP:
Logged
Description:
Sample code for SAX parsing, does not compile but should be useful for getting a feel for sax parsing
Code:
  1. //These are global in the program
  2.  
  3.       //Handler pointers
  4.      D warningHandler@...
  5.      D                 S               *   procptr
  6.      D                                     inz(%paddr('HANDLEWARNING'))
  7.      D errorHandler@...
  8.      D                 S               *   procptr
  9.      D                                     inz(%paddr('HANDLEERROR'))
  10.      D resetHandler@...
  11.      D                 S               *   procptr
  12.      D                                     inz(%paddr('HANDLERESET'))
  13.      D fatalHandler@...
  14.      D                 S               *   procptr
  15.      D                                     inz(%paddr('HANDLEFATAL'))     
  16.  
  17.  P*--------------------------------------------------
  18.      P* Procedure name: ValidateBuffer
  19.      P* Purpose:        Given a buffer and its length parses it in and reco...
  20.      P*                          rds any errors in Validation Error...
  21.      P*                          
  22.      P* Returns:        The return code of the operation
  23.      
  24.      P* Parameter:      buffer => the memory buffer to parse and validate
  25.      P* Parameter:      bufferLength => the number of bytes in the buffer
  26.      P* Parameter:      DoNamespaces => Whether or not to enforce namespaces
  27.      P* Parameter:      DoSchema => Allows enabling of processing any schem...
  28.      P*                          as encountered in the document
  29.      P* Parameter:      DoSchemaFullChecking => Enables full checking (some...
  30.      P*                           full checking items are RAM and CPU inten...
  31.      P*                          sive)
  32.      P* Parameter:      EncodingString => A string that states the buffers ...
  33.      P*                          encoding, if not supplied defaults to UTF-8
  34.      P* Parameter:      EncodingStringLength => THe number of bytes in the ...
  35.      P*                          encoding string, defaults to 5
  36.      P* Parameter:      EncodingStringCCSID => the ccsid of the encoding st...
  37.      P*                          ring field, defaults to 285
  38.      P*--------------------------------------------------
  39.      P ValidateBuffer...
  40.      P                 B                   Export
  41.      D ValidateBuffer...
  42.      D                 PI            10A
  43.      
  44.      D  buffer                         *   VALUE
  45.      D  bufferLength                 10U 0
  46.      D EncodingString                  *   VALUE
  47.      D                                     OPTIONS(*NOPASS)
  48.      D  EncodingStringLength...
  49.      D                               10U 0 VALUE
  50.      D                                     OPTIONS(*NOPASS)
  51.      D  EncodingStringCCSID...
  52.      D                               10U 0 VALUE
  53.      D                                     OPTIONS(*NOPASS)                
  54.      D* Local fields
  55.      D retField        S             10A   Inz('')
  56.      D saxParse@       s               *
  57.      D errorHandler@   s               *
  58.      D memBuffer@      s               *
  59.      DbufId            S              1a
  60.      Dbufid@           S               *   INZ(%addr(bufid))
  61.      D wrkEncStg       s               *
  62.      D wrkEncStgLen    s             10u 0
  63.      D wrkEncCCSID     s             10u 0
  64.      D parmsSupplied...
  65.      D                 s             10u 0
  66.      D false           s             10i 0 inz(0)
  67.      D defaultEnc      s              5a   inz('UTF-8')
  68.  
  69.      D wrkErrorInstance...
  70.      D                 S             10p 0     
  71.  
  72.  
  73.          saxParse@ = QxmlXMLReaderFactory_createXMLReader(); 
  74.   
  75.   
  76.         //Set parser options 
  77.         SetParserOptions(saxParse@:Session.DoNamespaces 
  78.         :Session.DoSchema:Session.DoFullSchema); 
  79.   
  80.   
  81.     errorHandler@ = QxmlErrorHandler_new(); 
  82.  
  83.       QxmlSAX2XMLReader_setErrorHandler
  84.          (SAXParse@:
  85.           errorHandler@);
  86.   
  87.       RegisterErrorEvents(errorHandler@);
  88.   
  89.       membuffer@ = CreateMumBufInputSource(buffer:bufferLength:
  90.                                     wrkEncStg:wrkEncCCSID:wrkEncStgLen); 
  91.  
  92.              QxmlSAX2XMLReader_parse_InputSource(SAXParse@:membuffer@);
  93.  
  94.  
  95.         //If this failed then send an error
  96.         if Qxml_DOMEXCDATA.Qxml_DOMRTNCOD <> 0;
  97.          //log rtn code in job log
  98.           
  99.  
  100.           retField = 'ERROR';
  101.         endif;
  102.  
  103.     ClearSAXResources(saxParse@:memBuffer@:errorHandler@); 
  104.  
  105.     //Decide on return code based on types of error
  106.         SELECT;
  107.         WHEN Session.SaxHasFatal;
  108.             retField = 'FATAL';
  109.         WHEN Session.SaxHasError;
  110.             retField = 'ERROR';
  111.         WHEN Session.SaxHasReset;
  112.               retField = 'RESET';
  113.         WHEN Session.SaxHasWarning;
  114.           retField = 'WARNING';
  115.         ENDSL;                   
  116.  
  117.       /END-FREE
  118.      P ValidateBuffer...
  119.      P                 E   
  120.   
  121.      P*-------------------------------------------------- 
  122.      P* Procedure name: SetParserOptions 
  123.      P* Purpose:        Sets the main options on a SAX Parser 
  124.      P*                 This method also looks up the Namespace group header 
  125.      P*                 and if it exists sets the schema locations as appropriate 
  126.      P* Returns: 
  127.      P* Parameter:      saxParser => The saxparser to set the options on 
  128.      P* Parameter:      DoNamespaces 
  129.      P* Parameter:      doSchema 
  130.      P* Parameter:      DoFullSchema 
  131.      P*-------------------------------------------------- 
  132.      P SetParserOptions... 
  133.      P                 B 
  134.      D SetParserOptions... 
  135.      D                 PI 
  136.      D  saxParser                      *   VALUE 
  137.      D  DoNamespaces                   N   VALUE 
  138.      D  doSchema                       N   VALUE 
  139.      D  DoFullSchema                   N   VALUE 
  140.   
  141.      D true            C                   1 
  142.      D ESY2rtnCode     s              7a 
  143.      D SchemaList      s           2048a 
  144.      D prop            s            100a   inz('http://apache.org/xml/propertie+ 
  145.      D                                     s/schema/external-schemaLocation') 
  146.      D SchemaListC     s           2049C 
  147.      D 
  148.      Dvalfeature       S             90A   INZ('http://xml.org/sax/features/va+ 
  149.      D                                          lidation') 
  150.      Dvalfeatdyn       S             90A   INZ('http://apache.org/xml/features/+ 
  151.      D                                          validation/dynamic') 
  152.      Dnamespfeat       S             90A   INZ('http://xml.org/sax/features/+ 
  153.      D                                          namespaces') 
  154.      Dschemafeat       S             90A   INZ('http://apache.org/xml/features/+ 
  155.      D                                          validation/schema') 
  156.      Dschfullchk       S             90A   INZ('http://apache.org/xml/features/+ 
  157.      D                                          validation/schema-full-+ 
  158.      D                                          checking') 
  159.   
  160.   
  161.   
  162.       /FREE 
  163.   
  164.   
  165.   
  166.         //Turn on validation 
  167.   
  168.         QxmlSAX2XMLReader_enableFeature(saxParser:%addr(valfeature) 
  169.                :285:%len(%trimr(valfeature))); 
  170.   
  171.         QxmlSAX2XMLReader_enableFeature(saxParser:%addr(valfeatdyn):285 
  172.                :%len(%trimr(valfeatdyn))); 
  173.   
  174.   
  175.         //Set namespaces 
  176.         if DoNamespaces; 
  177.   
  178.   
  179.         QxmlSAX2XMLReader_enableFeature(saxParser:%addr(namespfeat):285 
  180.                :%len(%trimr(namespfeat))); 
  181.   
  182.          
  183.          //Set do schema 
  184.         if doSchema; 
  185.         Qxml_DOMEXCDATA.Qxml_DOMRTNCOD = 0; 
  186.           QxmlSAX2XMLReader_enableFeature(saxParser:%addr(schemafeat):285 
  187.                :%len(%trimr(schemafeat))); 
  188.   
  189.         
  190.         //Set do full schema 
  191.         if DoFullSchema; 
  192.         Qxml_DOMEXCDATA.Qxml_DOMRTNCOD = 0; 
  193.           QxmlSAX2XMLReader_enableFeature(saxParser:%addr(schfullchk):285 
  194.                :%len(%trimr(schfullchk))); 
  195.   
  196.           
  197.         
  198.   
  199.            SchemaList = 'Set your schema list here'; 
  200.            // like 'NameSpaceURI file://root/blah/myschema.xsd AnotherNamespaceURI file://root/blahdeblah/mySchema2.xsd' 
  201.   
  202.          if SchemaList <> ''; 
  203.            //No point setting a blank schema list 
  204.             SchemaListC = %trimr(%ucs2(SchemaList)) + U'0000'; 
  205.             QxmlSAX2XMLReader_setProperty(saxParser:%addr(prop):285 
  206.             :%len(%trimr(prop)): 
  207.             %addr(SchemaListC)); 
  208.          endif; 
  209.   
  210.         endif; 
  211.   
  212.         return; 
  213.   
  214.       /END-FREE 
  215.      P SetParserOptions... 
  216.      P                 E   
  217.  
  218.  P*--------------------------------------------------
  219.      P* Procedure name: RegisterErrorEvents
  220.      P* Purpose:        Registers the relevant error events on the error ha...
  221.      P*                          ndler
  222.      P* Returns:
  223.      P* Parameter:      ErrorHandler => The error handler to register the e...
  224.      P*                          vents on
  225.      P*--------------------------------------------------
  226.      P RegisterErrorEvents...
  227.      P                 B
  228.      D RegisterErrorEvents...
  229.      D                 PI
  230.      D  ErrorHandler                   *   VALUE
  231.  
  232.  
  233.       /FREE
  234.  
  235.        //register warnings
  236.  
  237.        QxmlErrorHandler_setCallback
  238.          (ErrorHandler:
  239.           Qxml_WARNINGHNDLR:
  240.           warningHandler@);
  241.  
  242.        
  243.  
  244.        //register error
  245.        Qxml_DOMEXCDATA.Qxml_DOMRTNCOD = 0;
  246.        QxmlErrorHandler_setCallback
  247.          (ErrorHandler:
  248.           Qxml_ERRORHNDLR:
  249.           errorHandler@);
  250.  
  251.         
  252.  
  253.        //register fatal
  254.        Qxml_DOMEXCDATA.Qxml_DOMRTNCOD = 0;
  255.        QxmlErrorHandler_setCallback
  256.          (ErrorHandler:
  257.           Qxml_FATALERRORHNDLR:
  258.           fatalHandler@);
  259.  
  260.         
  261.  
  262.         //register reset
  263.         // QxmlErrorHandler_setCallback
  264.         //   (ErrorHandler:
  265.         //    Qxml_RESETERRORHNDLR:
  266.          //   resetHandler@);
  267.  
  268.       /END-FREE
  269.      P RegisterErrorEvents...
  270.      P                 E   
  271.  
  272.      P*--------------------------------------------------
  273.      P* Procedure name: ClearSAXResources
  274.      P* Purpose:        Clears up after usage of the SAX parser, deletes th...
  275.      P*                          e parser, membuf input, error handler and ...
  276.      P*                          closes down the two synon functions if the...
  277.      P*                          y were called
  278.      P* Returns:
  279.      P* Parameter:      Parser => The parser that was used
  280.      P* Parameter:      MemorySource => The memeory source that was used
  281.      P* Parameter:      ErrorHandler => the error handler that was used
  282.      P*--------------------------------------------------
  283.      P ClearSAXResources...
  284.      P                 B
  285.      D ClearSAXResources...
  286.      D                 PI
  287.      D  Parser                         *   VALUE
  288.      D  MemorySource                   *   VALUE
  289.      D  ErrorHandler                   *   VALUE
  290.  
  291.      D Function        s              1a   inz('N')
  292.      D Closedown       s              1a   inz('Y')
  293.      
  294.       /FREE
  295.  
  296.        //Delete the memory source
  297.        Qxml_DOMEXCDATA.Qxml_DOMRTNCOD = 0;
  298.        QxmlMemBufInputSource_delete(MemorySource);
  299.        
  300.  
  301.        //Delete the error handler
  302.        Qxml_DOMEXCDATA.Qxml_DOMRTNCOD = 0;
  303.        QxmlErrorHandler_delete(ErrorHandler);
  304.        
  305.  
  306.        //Delete the parser
  307.        Qxml_DOMEXCDATA.Qxml_DOMRTNCOD = 0;
  308.        QxmlSAX2XMLReader_delete(Parser);
  309.        
  310.  
  311.       
  312.       /END-FREE
  313.      P ClearSAXResources...
  314.      P                 E  
  315.  
  316.      P*--------------------------------------------------
  317.      P* Procedure name: HandleWarning
  318.      P* Purpose:        Handles any warnings that are thrown by the SAX parser
  319.      P* Returns:        Nothing
  320.      P* Parameter:      SaxParserException => THe exception that is thrown ...
  321.      P*                          by the parser
  322.      P*--------------------------------------------------
  323.      P HandleWarning...
  324.      P                 B
  325.      D HandleWarning...
  326.      D                 PI
  327.      D  SaxParserException...
  328.      D                                 *   VALUE
  329.  
  330.       /FREE
  331.  
  332.        //Only if not ignoring warnings
  333.        if not Session.SaxIgnoreWarning  ;
  334.  
  335.          //Track that warning has been
  336.          Session.SaxHasWarning = *on;
  337.  
  338.          
  339.  
  340.        endif;
  341.  
  342.       /END-FREE
  343.      P HandleWarning...
  344.      P                 E        
  345.  
  346.      P*--------------------------------------------------
  347.      P* Procedure name: HandleError
  348.      P* Purpose:        Handles parser errors
  349.      P* Returns:
  350.      P* Parameter:      SaxParserException => THe exception that was thrown
  351.      P*--------------------------------------------------
  352.      P HandleError...
  353.      P                 B
  354.      D HandleError...
  355.      D                 PI
  356.      D  SaxParserException...
  357.      D                                 *   VALUE
  358.  
  359.  
  360.       /FREE
  361.  
  362.         //Only if not ignoring errors
  363.        if not Session.SaxIgnoreErrors;
  364.  
  365.         //Track the fact we have had an error
  366.         Session.SaxHasError = *on;
  367.  
  368.         if Session.DumpValidation;
  369.          //Report a warning diagnostic with the current surrogate
  370.          ReportDiagnostic('E':SaxParserException);
  371.         endif;
  372.  
  373.        endif;
  374.  
  375.       /END-FREE
  376.      P HandleError...
  377.      P                 E                           
  378.  
  379.      P*--------------------------------------------------
  380.      P* Procedure name: HandleFatal
  381.      P* Purpose:        Handles fatal parser exceptions
  382.      P* Returns:
  383.      P* Parameter:      SaxParserException => The sax exception
  384.      P*--------------------------------------------------
  385.      P HandleFatal...
  386.      P                 B
  387.      D HandleFatal...
  388.      D                 PI
  389.      D  SaxParserException...
  390.      D                                 *   VALUE
  391.  
  392.  
  393.       /FREE
  394.  
  395.         //Only if not ignoring fatals
  396.        if not Session.SaxIgnoreFatal;
  397.  
  398.          //Track the fact that we have had a fatal
  399.          Session.SaxHasFatal = *on;
  400.  
  401.          if Session.DumpValidation;
  402.          //Report a warning diagnostic with the current surrogate
  403.          ReportDiagnostic('F':SaxParserException);
  404.          endif;
  405.  
  406.        endif;
  407.  
  408.       /END-FREE
  409.      P HandleFatal...
  410.      P                 E  
  411.  
  412.      P*--------------------------------------------------
  413.      P* Procedure name: ReportDiagnostic
  414.      P* Purpose:        Reports a diagnostic given a type and a sax parser ...
  415.      P*                          exception
  416.      P* Returns:
  417.      P* Parameter:      Type
  418.      P* Parameter:      SaxParserException => the exception that was thrown
  419.      P*--------------------------------------------------
  420.      P ReportDiagnostic...
  421.      P                 B
  422.      D ReportDiagnostic...
  423.      D                 PI
  424.      D  Type                          1A   VALUE
  425.      D  SaxParserException...
  426.      D                                 *   VALUE
  427.      D Line            S             10I 0
  428.      D Col             S             10I 0
  429.      D Msg@            S               *
  430.      Dtransmsg         S            250A
  431.      DTRANSMSG@        S               *   INZ(%ADDR(transmsg))
  432.      DErrprov          S             10I 0 INZ(200)
  433.      DErravail         S             10I 0 INZ(0)
  434.      D LinePacked      s              5P 0
  435.      D ColPacked       s              5P 0
  436.      D wrkErrIns       s             10P 0
  437.      D Function        s              1a   INZ('P')
  438.      D Closedown       s              1a   INZ('N')
  439.      D rtnCode         s              7a
  440.  
  441.       /FREE
  442.  
  443.        //Get the line number
  444.        LINE=QxmlSAXParseException_getLineNumber
  445.          (SaxParserException);
  446.        //Get the column number
  447.        COL =QxmlSAXParseException_getColumn...
  448.           Number(SaxParserException);
  449.  
  450.        //Get the exception message
  451.          MSG@= QxmlSAXException_getMessage
  452.          (SaxParserException);
  453.          //Convert it to our CCSID
  454.          QxmlTranscode(MSG@:
  455.           Qxml_UNICODE:
  456.           TRANSMSG@:
  457.           %ADDR(ErrPROV):
  458.           %ADDR(ErrAVAIL):
  459.           285);
  460.  
  461.          //Convert line and col to packed
  462.          LinePacked = %dec(Line);
  463.          ColPacked = %dec(Col);
  464.  
  465.          //whatever you feel you need to do here with the info above
  466.  
  467.          return;
  468.  
  469.  
  470.       /END-FREE
  471.      P ReportDiagnostic...
  472.      P                 E   
© 2004-2019 by midrange.com generated in 0.008s valid xhtml & css