For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2004 > parsing a file









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author parsing a file
Stephen Finley

2004-04-06, 11:33 am

Hi all

hope someone can help

We have several jsp files that are not formatted correctly. I have a
script that will escape a quote if it is not all ready escaped in a
valid statement, but can not get it to work if the quote is already
escaped inside a quoted statement.

Any idea what to do with already escaped double quotes? For example,
what if you have the following:

<raf:custom_tag attribute="<%="This string has an escaped \" in it"%>"/>

it should get changed to the following?

<raf:custom_tag attribute="<%=\"This string has an escaped \\" in
it\"%>"/>



Here is the code I am using



#! /usr/bin/perl
use strict;
use warnings;

use Getopt::Long;

my $quiet = 0;
my $edit = 0;

my $usage = "jspQuotes.pl [-edit] [-quiet] file(s)\n";
die $usage unless
GetOptions( quiet => \$quiet,
edit => \$edit );
die $usage unless @ARGV;


foreach my $filename (@ARGV) {
my $temp = "$filename~";
my $modified = 0;

open INPUT, $filename or die "Couldn't read $filename: $!\n";

if ($edit) {
open OUTPUT, ">$temp" or die "Couldn't write $temp: $!\n";
select OUTPUT;
warn "Editing $filename\n" unless ($quiet);
}

my $injsp = 0;
my $intag = 0;
my $inlibtag = 0;

while (<INPUT> ) {
my $numbad = 0;
my $doublePrevVal = "";
my $prevVal = "";
# \x22 is " without confusing emacs "
my @a = split /(<%|%>|<[^\s]|[^\s]>|:|[^\\]\x22)/;
#print("Start:\n");
foreach (@a) {
#print("\n#\n#$_\n");
#print("prevVal: $prevVal, doublePrevVal: $doublePrevVal\n");
#print("it: $intag, ij: $injsp, ilt: $inlibtag\n");
if (/:/ and $doublePrevVal =~ /</ ) { $inlibtag = 1; }
if (/[^\\]\x22/ and $injsp and $intag and $inlibtag) {
warn "Unescaped JSP quote at $filename:$.\n" unless
($quiet or $numbad);
$numbad++;
s/(.)\x22/$1\\\x22/; # insert the missing \
$modified = 1;
}
if (/^\x22/ and $prevVal !~ /\\$/ and $injsp and $intag and
$inlibtag) {
warn "Unescaped JSP quote at $filename:$.\n" unless
($quiet or $numbad);
$numbad++;
s/^\x22/\\\x22/; # insert the missing \
$modified = 1;
}
if (/<%/) { $injsp = 1; }
elsif (/%>/) { $injsp = 0; }
elsif (/</ and !$injsp) { $intag = 1; }
elsif (/>/ and !$injsp) { $intag = 0; $inlibtag = 0; }
else { };
$doublePrevVal = $prevVal;
$prevVal = $_;

print; # always print array element
}
}
close INPUT;
if ($edit) {
close OUTPUT;
if ($modified) {
rename($temp, $filename);
} else {
unlink($temp);
}
}
}



Wc -Sx- Jones

2004-04-06, 3:35 pm

Stephen Finley wrote:

> <raf:custom_tag attribute="<%="This string has an escaped \" in it"%>"/>
>
> it should get changed to the following?
>
> <raf:custom_tag attribute="<%=\"This string has an escaped \\" in
> it\"%>"/>


You are saying this is wrong:
<raf:custom_tag attribute="<%="This string has
an escaped \" in it"%>"/>

(It has
And this is right?
<raf:custom_tag attribute="<%=\"This string has
an escaped \\" in it\"%>"/>

This \\ escapes only the slash in \\" not the " so the result is
unbalanced " " in the output -- is that what you want?

I think you need to test ahead for \" and skip changing it - maybe this:

s^(?!\s\\"\s)(\s\"\s)^\\$1^;


I'm ...
-Sx-
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com