Convert Disallowed Characters in Junos XML Protocol Perl Client Applications
Junos XML protocol Perl client applications that handle configuration
data usually accept and output the data either as Junos XML tag elements
or as formatted ASCII statements like those used in the Junos OS CLI.
As described in XML and Junos XML Management
Protocol Conventions Overview, certain characters cannot
appear in their regular form in an XML document. These characters
include the apostrophe ( ‘
), the
ampersand ( &
), the greater-than ( >
) and less-than ( <
) symbols, and the quotation mark ( "
). Because these characters might appear in formatted ASCII configuration
statements, the script must convert the characters to the corresponding
predefined entity references.
The Junos XML Protocol Perl distribution includes several
sample Perl scripts that perform various functions on devices running
Junos OS. The sample scripts include code to convert all disallowed
characters to predefined entity references. The load_configuration.pl sample script uses the get_escaped_text
subroutine to substitute predefined entity references for disallowed
characters (the get_configuration.pl script includes similar code). The script first defines the mappings
between the disallowed characters and predefined entity references,
and sets the variable $char_class
to a
regular expression that contains all of the entity references, as
follows:
my %escape_symbols = ( qq(") => '"', qq(>) => '>', qq(<) => '<', qq(') => ''', qq(&) => '&' ); my $char_class = join ("|", map { "($_)" } keys %escape_symbols);
The following code defines the get_escaped_text
subroutine for the load_configuration.pl script. A detailed discussion of the subsections in the routine
follows the complete code sample.
sub get_escaped_text { my $input_file = shift; my $input_string = ""; open(FH, $input_file) or return undef; while(<FH>) { my $line = $_; $line =~ s/<configuration-text>//g; $line =~ s/<\/configuration-text>//g; $line =~ s/($char_class)/$escape_symbols{$1}/ge; $input_string .= $line; } return "<configuration-text>$input_string</configuration-text>"; }
The first subsection of the preceding code sample reads in a file containing formatted ASCII configuration statements.
sub get_escaped_text { my $input_file = shift; my $input_string = ""; open(FH, $input_file) or return undef;
In the next subsection, the subroutine temporarily discards
the lines that contain the opening <get-configuration>
and closing </get-configuration>
tags,
then replaces the disallowed characters on each remaining line with
predefined entity references and appends the line to the $input_string
variable:
while(<FH>) { my $line = $_; $line =~ s/<configuration-text>//g; $line =~ s/<\/configuration-text>//g; $line =~ s/($char_class)/$escape_symbols{$1}/ge; $input_string .= $line; }
The subroutine concludes by replacing the opening <get-configuration>
and closing </get-configuration>
tags, and returning the converted set of statements:
return "<configuration-text>$input_string</configuration-text>"; }