How can I convert my 0.9 JSP files to 1.1?

Converting your JSP 0.9 to 1.1 is a straightforward
syntax translation for the majority of your files--you won't have to
change the semantics. I built a script, updatejsp, to take
care of translating 95% of my files; the others, I converted by hand
since it was easier/faster than making a perfect script.

Given the one-to-one nature of the translation, sed/awk (could've
used perl) are sufficient--full parser generators, such as ANTLR are overkill. On the other hand
sed/awk don't deal well with constructs that cross newline boundaries.
Oh well.

What does updatejsp not do? The script doesn't
deal with random whitespace (for example, it likes a space before the
0.9 import word) nor does it deal with tags broken across
lines. If you have large whitespace variation in your JSP files, I
would suggest fixing this script. If you have lots of tags broken
across lines, I'd write a real parser with ANTLR that sees a stream of
token objects not characters (making it not care about newlines and
weird whitespace).

Here is the code you'll need. The updatejsp script is
written in bash and you'll need awk for genimports.awk, which
is presumed to live in the same dir as the updatejsp script.
"Your mileage may vary." :)

updatejsp:

#!/bin/bash
#
# Convert bean, include, SCRIPT, and import syntax to JSP 1.1
# Handles modules and regular JSP files. Does not do a good
# job with the <BEAN...> tag. Assumes all on one list for example.
#
# Terence Parr, jGuru.com
#
# F is the file we're dealing with
F=$1
#
# compute location of genimport.awk; assume same dir as executable
# $0 is full pathname to this executable script
# I hack off the path (which assumes at least "./" as path) and
# then append "/genimport.awk" to get that script
#
UPDATEJSP=$0
GENIMPORT="`expr $UPDATEJSP : '(.*)/.*'`/genimport.awk"
# make sure a filename is specified
if !(test $1)
then
echo "Usage: updatejsp filename"
echo "Example: updatejsp view.jsp"
exit
fi
# make sure a filename is writable
if !(test -w $1)
then
echo "File $1 is not writable"
exit
fi
# make sure the file is indeed an old style file
if !(grep -s -q '%@ import' $F ||
grep -s -q '#INCLUDE' $F ||
grep -s -q '<SCRIPT' $F ||
grep -s -q '<BEAN' $F)
then
echo "Sorry, but $F doesn't look like an old-style JSP file to me"
echo "I couldn't find '<%@ import', '<BEAN...>', "<SCRIPT", or '#INCLUDE'"
exit
fi
# convert includes to new form and change "portal/" to "/portal/" and for jguru
sed 's/<!--#INCLUDE FILE="(.*)"-->/<%@ include file="1" %>/g' < $F
> /tmp/updatejsp.tmp
# convert BEAN to jsp:useBean and /BEAN to /jsp:useBean
# this does NOT work when <BEAN...> is on more than one line
# The next line is broken up for formatting purposes
# but the should be removed and the associated newline
# when you try to run the script
sed 's/<BEAN.*>/<jsp:useBean id="view" type="com.epicentric.portalbeans.PortalBeanView"
scope="request">/' < /tmp/updatejsp.tmp |
sed 's/</BEAN>/</jsp:useBean>/' > /tmp/updatejsp.tmp2
# print out the combined import as page directive
if ( grep -s -q '%@ import' $F )
then
awk -f $GENIMPORT < /tmp/updatejsp.tmp2 > /tmp/updatejsp.tmpF
fi
# convert SCRIPT to <%! block
sed 's/<SCRIPT.*"server">/<%!/' < /tmp/updatejsp.tmp2 |
sed 's/</SCRIPT>/%>/' > /tmp/updatejsp.tmp3
# print file minus old import lines
sed '/<%@ import=".*" %>/D' < /tmp/updatejsp.tmp3 >> /tmp/updatejsp.tmpF
# back up old file
mv $F $F.bak
# mv in the new file
mv /tmp/updatejsp.tmpF $F