[Contents] [Prev] [Next] [Index] [Report an Error]

Reading In and Parsing the Configuration Data

In the following code sample, the load_configuration.pl script then reads in and parses a file that contains JUNOS XML configuration tag elements or formatted ASCII statements. The name of the file was previously obtained from the command line and assigned to the $xmlfile variable. A detailed discussion of the functional subsections follows the complete code sample.

     print "Loading configuration from $xmlfile ...\n";
     if (! -f $xmlfile) {
         print "ERROR: Cannot load configuration in $xmlfile\n";
         graceful_shutdown($jnx, $xmlfile, STATE_LOCKED, REPORT_FAILURE);
     }
    
     my $parser = new XML::DOM::Parser;
     ...
    
     my $doc;
     if ($opt{t}) {
         my $xmlstring = get_escaped_text($xmlfile);
         $doc = $parser->parsestring($xmlstring) if $xmlstring;
    
     } else {
         $doc = $parser->parsefile($xmlfile);
     }
    
     unless ( ref $doc ) {
         print "ERROR: Cannot parse $xmlfile, check to make sure the XML data is well-formed\n";
         graceful_shutdown($jnx, $xmlfile, STATE_LOCKED, REPORT_FAILURE);
     }

The first subsection of the preceding code sample verifies the existence of the file containing configuration data. The name of the file was previously obtained from the command line and assigned to the $xmlfile variable. If the file does not exist, the script invokes the graceful_shutdown subroutine:

     print "Loading configuration from $xmlfile ...\n";
     if (! -f $xmlfile) {
         print "ERROR: Cannot load configuration in $xmlfile\n";
         graceful_shutdown($jnx, $xmlfile, STATE_LOCKED, REPORT_FAILURE);
     }

If the -t command-line option was included when the load_configuration.pl script was invoked, the file referenced by the $xmlfile variable should contain formatted ASCII configuration statements like those returned by the CLI configuration-mode show command. The script invokes the get_escaped_text subroutine described in Converting Disallowed Characters, assigning the result to the $xmlstring variable. The script invokes the parsestring function to transform the data in the file into the proper format for loading into the configuration hierarchy, and assigns the result to the $doc variable. The parsestring function is defined in the XML::DOM::Parser module, and the first line in the following sample code instantiates the module as an object, setting the $parser variable to refer to it:

     my $parser = new XML::DOM::Parser;
     ...
     my $doc;
     if ($opt{t}) {
         my $xmlstring = get_escaped_text($xmlfile);
         $doc = $parser->parsestring($xmlstring) if $xmlstring;

If the file contains JUNOS XML configuration tag elements instead, the script invokes the parsefile function (also defined in the XML::DOM::Parser module) on the file:

     } else {
         $doc = $parser->parsefile($xmlfile);
     }

If the parser cannot transform the file, the script invokes the graceful_shutdown subroutine described in Handling Error Conditions:

     unless ( ref $doc ) {
         print "ERROR: Cannot parse $xmlfile, check to make sure the XML data is well-formed\n";
         graceful_shutdown($jnx, $xmlfile, STATE_LOCKED, REPORT_FAILURE);
     }

[Contents] [Prev] [Next] [Index] [Report an Error]