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);
 }
    }
}


Reply via email to